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

Fix blockCV::spatialBlock() functions #93

Merged
merged 2 commits into from
Mar 10, 2021
Merged

Fix blockCV::spatialBlock() functions #93

merged 2 commits into from
Mar 10, 2021

Conversation

pat-s
Copy link
Member

@pat-s pat-s commented Mar 10, 2021

fixes #92

The linked issue also revealed that some other {blockCV} function settings were not reproducible with {ml3spatiotempcv}.
This should now be fixed and protected by a test.

In addition this PR fixes an issue with the recently added support for {sf} objects: coordinates names from sf objects are now retrieved dynamically.
Before some functions would have errored if the coordinate names were not named "x" and "y".

library(mlr3spatiotempcv)
library(mlr3)
library(blockCV)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1

set.seed(123)
x <- runif(5000, -80.5, -75)
y <- runif(5000, 39.7, 42)

data <- data.frame( # spp="test",
  label = factor(round(runif(length(x), 0, 1))),
  x = x,
  y = y
)

testSF <- st_as_sf(data[, c("x", "y", "label")],
  coords = c("x", "y"),
  crs = "EPSG: 4326"
)

# mlr3spatiotempcv
testTask <- TaskClassifST$new(
  id = "test",
  backend = testSF,
  target = "label",
  positive = "1"
)

blockSamp <- rsmp("spcv_block",
  # folds = 1,
  range = 50000,
  selection = "checkerboard"
)
blockSamp$instantiate(testTask)

# blockCV
testBlock <- suppressMessages(suppressWarnings(spatialBlock(
  speciesData = testSF,
  theRange = 50000,
  selection = "checkerboard",
  showBlocks = FALSE,
  verbose = FALSE
)))

print(all.equal(blockSamp$instance$fold, testBlock$foldID))
#> [1] TRUE

Created on 2021-03-10 by the reprex package (v1.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                                      
#>  version  R version 4.0.4 Patched (2021-02-17 r80031)
#>  os       macOS Big Sur 10.16                        
#>  system   x86_64, darwin17.0                         
#>  ui       X11                                        
#>  language (EN)                                       
#>  collate  en_US.UTF-8                                
#>  ctype    en_US.UTF-8                                
#>  tz       Europe/Zurich                              
#>  date     2021-03-10                                 
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package          * version    date       lib source                       
#>  assertthat         0.2.1      2019-03-21 [1] standard (@0.2.1)            
#>  backports          1.2.1      2020-12-09 [1] RSPM (R 4.0.4)               
#>  blockCV          * 2.1.1      2020-02-23 [1] CRAN (R 4.0.4)               
#>  checkmate          2.0.0      2020-02-06 [1] RSPM (R 4.0.4)               
#>  class              7.3-18     2021-01-24 [2] CRAN (R 4.0.4)               
#>  classInt           0.4-3      2020-04-07 [1] CRAN (R 4.0.4)               
#>  cli                2.3.1      2021-02-23 [1] CRAN (R 4.0.4)               
#>  codetools          0.2-18     2020-11-04 [2] CRAN (R 4.0.4)               
#>  colorspace         2.0-0      2020-11-11 [1] CRAN (R 4.0.4)               
#>  crayon             1.4.1      2021-02-08 [1] standard (@1.4.1)            
#>  data.table         1.14.0     2021-02-21 [1] standard (@1.14.0)           
#>  DBI                1.1.1      2021-01-15 [1] CRAN (R 4.0.4)               
#>  digest             0.6.27     2020-10-24 [1] standard (@0.6.27)           
#>  dplyr              1.0.5      2021-03-05 [1] CRAN (R 4.0.4)               
#>  e1071              1.7-4      2020-10-14 [1] CRAN (R 4.0.4)               
#>  ellipsis           0.3.1      2020-05-15 [1] standard (@0.3.1)            
#>  evaluate           0.14       2019-05-28 [1] standard (@0.14)             
#>  fansi              0.4.2      2021-01-15 [1] standard (@0.4.2)            
#>  fs                 1.5.0      2020-07-31 [1] standard (@1.5.0)            
#>  generics           0.1.0      2020-10-31 [1] standard (@0.1.0)            
#>  ggplot2            3.3.3      2020-12-30 [1] CRAN (R 4.0.4)               
#>  glue               1.4.2      2020-08-27 [1] standard (@1.4.2)            
#>  gtable             0.3.0      2019-03-25 [1] CRAN (R 4.0.4)               
#>  highr              0.8        2019-03-20 [1] standard (@0.8)              
#>  hms                1.0.0      2021-01-13 [1] standard (@1.0.0)            
#>  htmltools          0.5.1.1    2021-01-22 [1] standard (@0.5.1.1)          
#>  KernSmooth         2.23-18    2020-10-29 [2] CRAN (R 4.0.4)               
#>  knitr              1.31       2021-01-27 [1] standard (@1.31)             
#>  lattice            0.20-41    2020-04-02 [2] CRAN (R 4.0.4)               
#>  lgr                0.4.2      2021-01-10 [1] CRAN (R 4.0.4)               
#>  lifecycle          1.0.0      2021-02-15 [1] standard (@1.0.0)            
#>  magrittr           2.0.1      2020-11-17 [1] standard (@2.0.1)            
#>  mlr3             * 0.11.0     2021-03-05 [1] CRAN (R 4.0.4)               
#>  mlr3misc           0.7.0      2021-01-05 [1] CRAN (R 4.0.4)               
#>  mlr3spatiotempcv * 0.2.0.9000 2021-03-10 [1] local                        
#>  munsell            0.5.0      2018-06-12 [1] CRAN (R 4.0.4)               
#>  palmerpenguins     0.1.0      2020-07-23 [1] CRAN (R 4.0.4)               
#>  paradox            0.7.1      2021-03-07 [1] CRAN (R 4.0.4)               
#>  parallelly         1.23.0     2021-01-04 [1] CRAN (R 4.0.4)               
#>  pillar             1.5.0      2021-02-22 [1] standard (@1.5.0)            
#>  pkgconfig          2.0.3      2019-09-22 [1] standard (@2.0.3)            
#>  prettyunits        1.1.1      2020-01-24 [1] standard (@1.1.1)            
#>  progress           1.2.2      2019-05-16 [1] CRAN (R 4.0.4)               
#>  purrr              0.3.4      2020-04-17 [1] standard (@0.3.4)            
#>  R.cache            0.14.0     2019-12-06 [1] RSPM (R 4.0.4)               
#>  R.methodsS3        1.8.1      2020-08-26 [1] RSPM (R 4.0.4)               
#>  R.oo               1.24.0     2020-08-26 [1] RSPM (R 4.0.4)               
#>  R.utils            2.10.1     2020-08-26 [1] RSPM (R 4.0.4)               
#>  R6                 2.5.0      2020-10-28 [1] standard (@2.5.0)            
#>  raster             3.4-5      2020-11-14 [1] CRAN (R 4.0.4)               
#>  Rcpp               1.0.6      2021-01-15 [1] standard (@1.0.6)            
#>  rematch2           2.1.2      2020-05-01 [1] standard (@2.1.2)            
#>  reprex             1.0.0      2021-01-27 [1] standard (@1.0.0)            
#>  rgdal              1.5-23     2021-02-03 [1] CRAN (R 4.0.4)               
#>  rlang              0.4.10     2020-12-30 [1] standard (@0.4.10)           
#>  rmarkdown          2.7        2021-02-19 [1] CRAN (R 4.0.4)               
#>  scales             1.1.1      2020-05-11 [1] CRAN (R 4.0.4)               
#>  sessioninfo        1.1.1      2018-11-05 [1] standard (@1.1.1)            
#>  sf               * 0.9-7      2021-01-06 [1] CRAN (R 4.0.4)               
#>  sp                 1.4-5      2021-01-10 [1] CRAN (R 4.0.4)               
#>  stringi            1.5.3      2020-09-09 [1] standard (@1.5.3)            
#>  stringr            1.4.0      2019-02-10 [1] standard (@1.4.0)            
#>  styler             1.3.2.9000 2021-03-04 [1] Github (pat-s/styler@51d5200)
#>  tibble             3.1.0      2021-02-25 [1] standard (@3.1.0)            
#>  tidyselect         1.1.0      2020-05-11 [1] CRAN (R 4.0.4)               
#>  units              0.7-0      2021-02-25 [1] CRAN (R 4.0.4)               
#>  utf8               1.1.4      2018-05-24 [1] standard (@1.1.4)            
#>  uuid               0.1-4      2020-02-26 [1] CRAN (R 4.0.4)               
#>  vctrs              0.3.6      2020-12-17 [1] standard (@0.3.6)            
#>  withr              2.4.1      2021-01-26 [1] standard (@2.4.1)            
#>  xfun               0.21       2021-02-10 [1] standard (@0.21)             
#>  yaml               2.2.1      2020-02-01 [1] standard (@2.2.1)            
#> 
#> [1] /Users/pjs/Library/R/4.0/library
#> [2] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

- get coordinates names from sf objects dynamically
@pat-s pat-s merged commit d3e7fe9 into main Mar 10, 2021
@pat-s pat-s deleted the fix-blockCV branch March 10, 2021 16:54
pat-s added a commit that referenced this pull request Jun 17, 2022
pat-s added a commit that referenced this pull request Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checkerboard pattern with spcv_block?
1 participant