When training a spatial classification task with sample weights and then predicting with predict_spatial() or terra::predict(), I get an error related to col_roles not recognizing the X/Y coordinate columns.
library(mlr3spatial)
library(dplyr)
library(sf)
library(terra, exclude = "resample")
data = sf::read_sf(system.file("extdata",
"leipzig_points.gpkg", package = "mlr3spatial"),
stringsAsFactors = TRUE)
weights.table = 1/table(data$land_cover)
weights.vect = as.numeric(weights.table[as.character(data$land_cover)])
data.w = data |> dplyr::mutate(weights = weights.vect, .after=ndvi)
task = mlr3spatial::as_task_classif_st(data.w, target = "land_cover",coords_as_features = FALSE)
task$set_col_roles("weights", roles = "weights_learner")
lrn = lrn("classif.ranger")
lrn$train(task)
stack = terra::rast(system.file("extdata", "leipzig_raster.tif",
package = "mlr3spatial"))
mlr3spatial::predict_spatial(stack, lrn)
terra::predict(stack, lrn)
Error:
> Error in .__Task__col_roles(self = self, private = private, super = super, :
> Assertion on 'elements of col_roles' failed: Must be a subset of {'b02','b03','b04','b06','b07','b08','b11','land_cover','ndvi'}, but has additional elements {'X','Y'}.
This error can be traced back to mlr3, inside the predict_newdata(). When task$col_roles (line: 509) is updated, X and Y are not preserved causing the assertion in Task$col_roles <- (line: 1106) to fail
sessioninfo()
> R version 4.5.1 (2025-06-13 ucrt)
> Platform: x86_64-w64-mingw32/x64
> Running under: Windows 11 x64 (build 26100)
>
> Matrix products: default
> LAPACK version 3.12.1
>
> locale:
> [1] LC_COLLATE=Italian_Italy.utf8 LC_CTYPE=Italian_Italy.utf8 LC_MONETARY=Italian_Italy.utf8 LC_NUMERIC=C
> [5] LC_TIME=Italian_Italy.utf8
>
> time zone: Europe/Rome
> tzcode source: internal
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] mlr3learners_0.12.0 terra_1.8-60 sf_1.0-21 dplyr_1.1.4 mlr3spatial_0.6.0 mlr3_1.1.0
>
> loaded via a namespace (and not attached):
> [1] Matrix_1.7-3 ranger_0.17.0 compiler_4.5.1 crayon_1.5.3 tidyselect_1.2.1
> [6] Rcpp_1.1.0 parallel_4.5.1 globals_0.18.0 uuid_1.2-1 lattice_0.22-7
> [11] R6_2.6.1 generics_0.1.4 classInt_0.4-11 palmerpenguins_0.1.1 backports_1.5.0
> [16] checkmate_2.3.2 future_1.67.0 tibble_3.3.0 units_0.8-7 paradox_1.0.1
> [21] DBI_1.2.3 pillar_1.11.0 rlang_1.1.6 lgr_0.5.0 mlr3misc_0.18.0
> [26] cli_3.6.5 withr_3.0.2 magrittr_2.0.3 class_7.3-23 digest_0.6.37
> [31] grid_4.5.1 rstudioapi_0.17.1 lifecycle_1.0.4 vctrs_0.6.5 KernSmooth_2.23-26
> [36] proxy_0.4-27 glue_1.8.0 data.table_1.17.8 listenv_0.9.1 codetools_0.2-20
> [41] parallelly_1.45.1 e1071_1.7-16 tools_4.5.1 pkgconfig_2.0.3
When training a spatial classification task with sample weights and then predicting with
predict_spatial()orterra::predict(), I get an error related tocol_rolesnot recognizing the X/Y coordinate columns.Error:
This error can be traced back to mlr3, inside the
predict_newdata(). Whentask$col_roles(line: 509) is updated, X and Y are not preserved causing the assertion inTask$col_roles <-(line: 1106) to fail