Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rasterLayer argument in blockCV::spatialBlock() #94

Merged
merged 4 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Suggests:
mlr3tuning,
patchwork,
plotly,
raster,
rmarkdown,
rpart,
sf,
Expand Down
13 changes: 10 additions & 3 deletions R/ResamplingRepeatedSpCVBlock.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' # Instantiate Resampling
#' rrcv = rsmp("repeated_spcv_block",
#' folds = 3, repeats = 2,
#' range = c(5000, 10000))
#' range = c(5000L, 10000L))
#' rrcv$instantiate(task)
#'
#' # Individual sets:
Expand Down Expand Up @@ -44,10 +44,17 @@ ResamplingRepeatedSpCVBlock = R6Class("ResamplingRepeatedSpCVBlock",
ParamInt$new("repeats", lower = 1, default = 1L, tags = "required"),
ParamInt$new("rows", lower = 1L),
ParamInt$new("cols", lower = 1L),
ParamUty$new("range"),
ParamUty$new("range",
custom_check = function(x) checkmate::assert_integer(x)),
ParamFct$new("selection", levels = c(
"random", "systematic",
"checkerboard"), default = "random")
"checkerboard"), default = "random"),
ParamUty$new("rasterLayer",
default = NULL,
custom_check = function(x) {
checkmate::check_class(x, "RasterLayer",
null.ok = TRUE)
})
))

ps$values = list(folds = 10L, repeats = 1L)
Expand Down
13 changes: 10 additions & 3 deletions R/ResamplingSpCVBlock.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' task = tsk("ecuador")
#'
#' # Instantiate Resampling
#' rcv = rsmp("spcv_block", range = 1000)
#' rcv = rsmp("spcv_block", range = 1000L)
#' rcv$instantiate(task)
#'
#' # Individual sets:
Expand All @@ -36,10 +36,13 @@ ResamplingSpCVBlock = R6Class("ResamplingSpCVBlock",
ParamInt$new("folds", lower = 1L, default = 10L, tags = "required"),
ParamInt$new("rows", lower = 1L),
ParamInt$new("cols", lower = 1L),
ParamInt$new("range", lower = 1L),
ParamInt$new("range"),
ParamFct$new("selection", levels = c(
"random", "systematic",
"checkerboard"), default = "random")
"checkerboard"), default = "random"),
ParamUty$new("rasterLayer",
default = NULL,
custom_check = function(x) checkmate::check_class(x, "RasterLayer", null.ok = TRUE))
))
ps$values = list(folds = 10L)
super$initialize(
Expand Down Expand Up @@ -79,6 +82,9 @@ ResamplingSpCVBlock = R6Class("ResamplingSpCVBlock",
if (is.null(self$param_set$values$selection)) {
self$param_set$values$selection = self$param_set$default[["selection"]]
}
if (is.null(self$param_set$values$rasterLayer)) {
self$param_set$values$rasterLayer = self$param_set$default[["rasterLayer"]]
}

# Check for valid combinations of rows, cols and folds
if (!is.null(self$param_set$values$row) &&
Expand Down Expand Up @@ -128,6 +134,7 @@ ResamplingSpCVBlock = R6Class("ResamplingSpCVBlock",
rows = self$param_set$values$rows,
cols = self$param_set$values$cols,
k = self$param_set$values$folds,
rasterLayer = self$param_set$values$rasterLayer,
selection = self$param_set$values$selection,
showBlocks = FALSE,
progress = FALSE,
Expand Down
2 changes: 1 addition & 1 deletion R/autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#' library(mlr3)
#' library(mlr3spatiotempcv)
#' task = tsk("ecuador")
#' resampling = rsmp("spcv_block", range = 1000)
#' resampling = rsmp("spcv_block", range = 1000L)
#' resampling$instantiate(task)
#'
#' ## Visualize all partitions
Expand Down
29 changes: 21 additions & 8 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
],
"issueTracker": "https://github.com/mlr-org/mlr3spatiotempcv/issues",
"license": "https://spdx.org/licenses/LGPL-3.0",
"version": "0.1.1.9000",
"version": "0.2.0.9001",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
"url": "https://r-project.org"
},
"runtimePlatform": "R version 4.0.3 Patched (2020-12-21 r79675)",
"runtimePlatform": "R version 4.0.4 Patched (2021-02-17 r80031)",
"author": [
{
"@type": "Person",
Expand Down Expand Up @@ -223,6 +223,18 @@
},
"sameAs": "https://CRAN.R-project.org/package=plotly"
},
{
"@type": "SoftwareApplication",
"identifier": "raster",
"name": "raster",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=raster"
},
{
"@type": "SoftwareApplication",
"identifier": "rmarkdown",
Expand Down Expand Up @@ -369,7 +381,7 @@
"@type": "SoftwareApplication",
"identifier": "mlr3misc",
"name": "mlr3misc",
"version": ">= 0.1.7",
"version": ">= 0.7.0",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -409,17 +421,18 @@
}
],
"releaseNotes": "https://github.com/mlr-org/mlr3spatiotempcv/blob/master/NEWS.md",
"readme": "https://github.com/mlr-org/mlr3spatiotempcv/blob/master/README.md",
"fileSize": "11481.001KB",
"contIntegration": "https://codecov.io/github/mlr-org/mlr3spatiotempcv?branch=master",
"developmentStatus": "https://www.tidyverse.org/lifecycle/#maturing",
"readme": "https://github.com/mlr-org/mlr3spatiotempcv/blob/main/README.md",
"fileSize": "13087.485KB",
"contIntegration": "https://codecov.io/github/mlr-org/mlr3spatiotempcv?branch=main",
"developmentStatus": "https://lifecycle.r-lib.org/articles/stages.html",
"keywords": [
"mlr3",
"resampling",
"spatial",
"cross-validation",
"temporal",
"resampling-methods"
"resampling-methods",
"r"
],
"provider": {
"@id": "https://cran.r-project.org",
Expand Down
2 changes: 1 addition & 1 deletion man/ResamplingRepeatedSpCVBlock.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/ResamplingSpCVBlock.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/autoplot.ResamplingSpCVBlock.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ test_make_blockCV_test_df = function() {

data_sf = sf::st_as_sf(data,
coords = c("x", "y"),
crs = "EPSG: 4326")
crs = "EPSG:4326")

return(data_sf)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-1-autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ test_that("plot() works for 'repeated_spcv_block'", {
set.seed(42)

plots = prepare_autoplot("repeated_spcv_block",
folds = 4, range = c(2, 4), repeats = 2)
folds = 4, range = c(2L, 4L), repeats = 2)

# autoplot() is used instead of plot() to prevent side-effect plotting
p1 = autoplot(plots$rsp, plots$task, crs = 4326)
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-ResamplingRepeatedSpCVBlock.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
test_that("folds can be printed", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2, 4))
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2L, 4L))
rsp$instantiate(task)

expect_equal(rsp$folds(1:6), c(1, 2, 1, 2, 1, 2))
})

test_that("reps can be printed", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2, 4))
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2L, 4L))
rsp$instantiate(task)

expect_equal(rsp$repeats(1:9), c(1, 1, 1, 2, 2, 2, 3, 3, 3))
})

test_that("resampling iterations equals folds * repeats", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2, 4))
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2L, 4L))
rsp$instantiate(task)

expect_equal(rsp$iters, 6)
Expand All @@ -32,7 +32,7 @@ test_that("error when neither cols & rows | range is specified", {

test_that("error when length(range) != length(repeats)", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", repeats = 2, range = 5)
rsp = rsmp("repeated_spcv_block", repeats = 2, range = 5L)
expect_error(
rsp$instantiate(task),
".*to be the same length as 'range'."
Expand All @@ -41,13 +41,13 @@ test_that("error when length(range) != length(repeats)", {

test_that("no error when length(range) == repeats", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2, 4))
rsp = rsmp("repeated_spcv_block", folds = 3, repeats = 2, range = c(2L, 4L))
expect_silent(rsp$instantiate(task))
})

test_that("error when number of desired folds is larger than number possible blocks", {
task = test_make_twoclass_task()
rsp = rsmp("repeated_spcv_block", folds = 10, repeats = 2, range = c(2, 4))
rsp = rsmp("repeated_spcv_block", folds = 10, repeats = 2, range = c(2L, 4L))
expect_error(rsp$instantiate(task))
})

Expand Down
41 changes: 36 additions & 5 deletions tests/testthat/test-ResamplingSpCVBlock.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
test_that("resampling iterations equals folds", {
task = test_make_twoclass_task()
rsp = rsmp("spcv_block", folds = 2, range = 2)
rsp = rsmp("spcv_block", folds = 2, range = 2L)
rsp$instantiate(task)

expect_equal(rsp$iters, 2)
})

test_that("error when number of desired folds is larger than number possible blocks", {
task = test_make_twoclass_task()
rsp = rsmp("spcv_block", folds = 10, range = 4)
rsp = rsmp("spcv_block", folds = 10, range = 4L)

expect_error(rsp$instantiate(task))
})
Expand All @@ -33,7 +33,7 @@ test_that("error when only one of rows or cols is set", {
})

test_that("Error when length(range) >= 2", {
expect_error(rsmp("spcv_block", range = c(500, 1000)))
expect_error(rsmp("spcv_block", range = c(500L, 1000L)))
})


Expand All @@ -44,7 +44,7 @@ test_that("mlr3spatiotempcv indices are the same as blockCV indices: selection =
task = test_make_blockCV_test_task()

rsmp <- rsmp("spcv_block",
range = 50000,
range = 50000L,
selection = "checkerboard")
rsmp$instantiate(task)

Expand All @@ -53,7 +53,7 @@ test_that("mlr3spatiotempcv indices are the same as blockCV indices: selection =
capture.output(testBlock <- suppressMessages(
suppressWarnings(blockCV::spatialBlock(
speciesData = testSF,
theRange = 50000,
theRange = 50000L,
selection = "checkerboard",
showBlocks = FALSE)
)))
Expand Down Expand Up @@ -87,3 +87,34 @@ test_that("mlr3spatiotempcv indices are the same as blockCV indices: cols and ro

expect_equal(rsmp$instance$fold, testBlock$foldID)
})

test_that("mlr3spatiotempcv indices are the same as blockCV indices: rasterLayer", {
set.seed(42)

task = test_make_blockCV_test_task()
testSF = test_make_blockCV_test_df()

r <- raster::raster(raster::extent(testSF), crs = "EPSG:4326")
r[] <- 10

rsmp <- rsmp("spcv_block",
range = 50000L,
selection = "checkerboard",
rasterLayer = r)
rsmp$instantiate(task)


# blockCV
capture.output(testBlock <- suppressMessages(
blockCV::spatialBlock(
speciesData = testSF,
theRange = 50000L,
selection = "checkerboard",
rasterLayer = r,
showBlocks = FALSE,
verbose = FALSE,
progress = FALSE)
))

expect_equal(rsmp$instance$fold, testBlock$foldID)
})