Skip to content

Commit a319964

Browse files
committed
update 3.1.7; adding sp dependency
1 parent 18c0ad1 commit a319964

File tree

10 files changed

+942
-886
lines changed

10 files changed

+942
-886
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: blockCV
22
Type: Package
33
Title: Spatial and Environmental Blocking for K-Fold and LOO Cross-Validation
4-
Version: 3.1-6
5-
Date: 2025-06-23
4+
Version: 3.1-7
5+
Date: 2025-08-01
66
Authors@R: c(person("Roozbeh", "Valavi", role = c("aut", "cre"),
77
email = "valavi.r@gmail.com", comment = c(ORCID = "0000-0003-2495-5277")),
88
person("Jane", "Elith", role = "aut",
@@ -24,6 +24,7 @@ Depends:
2424
R (>= 3.5.0)
2525
Imports:
2626
sf (>= 1.0),
27+
sp,
2728
Rcpp (>= 1.0.2)
2829
Suggests:
2930
terra (>= 1.6-41),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# version 3.1.7
2+
* Temporarily added `sp` package dependency to avoid CRAN check issues related to using `sf::as_Spatial` function [#55].
3+
14
# version 3.1.6
25
* Resolved unclear error messages; issue [#52](https://github.com/rvalavi/blockCV/issues/52) by A. Márcia Barbosa
36
* Resolved ggplot testing failure; issue [#54](https://github.com/rvalavi/blockCV/issues/54) by Teun van den Brand

R/cv_similarity.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ cv_similarity <- function(
7272
if(!class(cv) %in% c("cv_spatial", "cv_cluster", "cv_buffer", "cv_nndm")){
7373
stop("'cv' must be a blockCV cv_* object.")
7474
}
75+
# if (!any(inherits(cv, c("cv_spatial", "cv_cluster", "cv_buffer", "cv_nndm")))) {
76+
# stop("'cv' must be a blockCV cv_* object.")
77+
# }
7578

7679
# The iteration must be a natural number
7780
tryCatch(
@@ -119,14 +122,16 @@ cv_similarity <- function(
119122
}
120123
fold_names <- paste("Fold", num_plot, sep = "")
121124
# reshape for plotting
122-
mes_reshp <- stats::reshape(df,
123-
direction = "long",
124-
idvar = "id",
125-
varying = fold_names,
126-
times = fold_names,
127-
v.names = "value",
128-
timevar = "folds"
125+
mes_reshp <- stats::reshape(
126+
df,
127+
direction = "long",
128+
idvar = "id",
129+
varying = fold_names,
130+
times = fold_names,
131+
v.names = "value",
132+
timevar = "folds"
129133
)
134+
130135
# remove NAs
131136
mes_reshp <- mes_reshp[stats::complete.cases(mes_reshp), ]
132137
if(.is_loo(cv)) mes_reshp$folds <- as.numeric(substr(mes_reshp$folds, 5, 25))

R/cv_spatial_autocor.R

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ cv_spatial_autocor <- function(
143143
}
144144
}
145145

146-
# to be used for filtering blocks
146+
# to be used for filtering blocks only
147147
samp_point <- terra::spatSample(
148148
r[[1]],
149149
size = 5e4,
@@ -178,10 +178,10 @@ cv_spatial_autocor <- function(
178178

179179
# make a dataframe from variograms data
180180
vario_data <- data.frame(layers = seq_len(nlayer), range = 1, sill = 1)
181-
for(v in seq_along(vario_list)){
181+
for (v in seq_along(vario_list)){
182182
vario_data$layers[v] <- if(missing(x)) names(r)[v] else column[v]
183-
vario_data$range[v] <- vario_list[[v]]$var_model[2,3]
184-
vario_data$sill[v] <- vario_list[[v]]$var_model[2,2]
183+
vario_data$range[v] <- vario_list[[v]]$var_model[2, 3]
184+
vario_data$sill[v] <- vario_list[[v]]$var_model[2, 2]
185185
}
186186

187187
# order them for plotting
@@ -191,7 +191,7 @@ cv_spatial_autocor <- function(
191191

192192
size <- the_range <- stats::median(vario_data$range)
193193

194-
if(sf::st_is_longlat(x_obj)){
194+
if (sf::st_is_longlat(x_obj)){
195195
vario_data$range <- vario_data$range * 1000
196196
the_range <- the_range * 1000
197197
size <- the_range / deg_to_metre
@@ -268,6 +268,67 @@ summary.cv_spatial_autocor <- function(object, ...){
268268
}
269269

270270

271+
# auto-fit variogram models
272+
.fit_variogram <- function(
273+
i,
274+
xx = NULL,
275+
rr = NULL,
276+
column = NULL,
277+
num_sample = 1e4,
278+
progress = FALSE,
279+
pb = NULL
280+
){
281+
if(is.null(xx)){
282+
points <- sf::st_as_sf(
283+
terra::spatSample(
284+
x = stats::setNames(rr[[i]], "target"),
285+
size = num_sample,
286+
method = "random",
287+
as.points = TRUE,
288+
na.rm = TRUE
289+
)
290+
)
291+
} else{
292+
points <- xx[column[i]] # [i] in case there are more column
293+
names(points) <- c("target", "geometry")
294+
}
295+
# NOTE: apparently the gstat package returns different units for range with sp and sf objects!
296+
# So, need to keep this a SpatialPointDataFrame for now and have to fake using it!!!
297+
if (FALSE) {
298+
sp::SpatialPoints
299+
}
300+
fit_vario <- automap::autofitVariogram(
301+
formula = target ~ 1,
302+
input_data = .as_sp(points)
303+
)
304+
if(progress) utils::setTxtProgressBar(pb, i)
305+
306+
return(fit_vario)
307+
}
308+
309+
310+
# Annoying step to import sp so there'll be no CRAN errors
311+
# Sp is only require because automap produces different output with latlong sf objects
312+
# Don't use sf::as_Spatial because this somehow depends on sp and requires sp dependency anyway
313+
.as_sp <- function(x) {
314+
out <- if (sf::st_is_longlat(x)) {
315+
coords <- sf::st_coordinates(x)
316+
attrs <- sf::st_drop_geometry(x)
317+
crs_info <- sf::st_crs(x)$proj4string
318+
319+
sp::SpatialPointsDataFrame(
320+
coords = coords,
321+
data = attrs,
322+
proj4string = sp::CRS(crs_info)
323+
)
324+
} else {
325+
x
326+
}
327+
328+
return(out)
329+
}
330+
331+
271332

272333
# make a bar plot for cv_spatial_autocor
273334
.make_bar_plot <- function(vario_data, the_range, ptnum){
@@ -306,34 +367,3 @@ summary.cv_spatial_autocor <- function(object, ...){
306367
return(p)
307368
}
308369

309-
310-
# auto-fit variogram models
311-
.fit_variogram <- function(
312-
i,
313-
xx = NULL,
314-
rr = NULL,
315-
column = NULL,
316-
num_sample = 1e4,
317-
progress = FALSE,
318-
pb = NULL
319-
){
320-
if(is.null(xx)){
321-
points <- terra::spatSample(
322-
x = rr[[i]],
323-
size = num_sample,
324-
method = "random",
325-
as.points = TRUE,
326-
na.rm = TRUE
327-
)
328-
points <- sf::as_Spatial(sf::st_as_sf(points))
329-
names(points) <- "target"
330-
} else{
331-
points <- xx[column[i]] # [i] in case there are more column
332-
points <- sf::as_Spatial(points)
333-
names(points) <- "target"
334-
}
335-
fit_vario <- automap::autofitVariogram(target~1, points)
336-
if(progress) utils::setTxtProgressBar(pb, i)
337-
338-
return(fit_vario)
339-
}

0 commit comments

Comments
 (0)