biomodhub / biomod2

BIOMOD is a computer platform for ensemble forecasting of species distributions, enabling the treatment of a range of methodological uncertainties in models and the examination of species-environment relationships.
85 stars 22 forks source link

Error in BIOMOD_DataFormating - [Error processing species argument is not interpretable as logical] #515

Closed zali37 closed 1 week ago

zali37 commented 2 weeks ago

Please make sure to close the issue once you consider it as solved Please use screenshots only when you cannot copy-paste the object, e.g. for figures or maps

Error and context I am trying to model two species with true absences. I have the binary vector in my data, but I do not know if I am doing it right. The error says "Error processing species argument is not interpretable as logical"

Code used to get the error diplos_wrapper <- function(sp) { cat("\n> species : ", sp) diplos_data <- diplos[diplos$sp == sp, ] diplos_format <- BIOMOD_FormatingData( resp.name = sp, resp.var = diplos_data$presence, # I am using presence=1 and absence=0 resp.xy = diplos_data[, c("lon", "lat")], expl.var = stk_current, filter.raster = TRUE ) registerDoParallel() diplos_modelo <- BIOMOD_Modeling( bm.format = diplos_format, modeling.id = "prueba", models = c("SRE", "RF", "GAM", "ANN"), CV.strategy = "random", CV.nb.rep = 5, CV.perc = 0.8, var.import = 3, metric.eval = c("TSS", "BIAS") ) diplos_ensemble_models <- BIOMOD_EnsembleModeling( bm.mod = diplos_modelo, models.chosen = "all", em.by = "all", em.algo = c("EMmean", "EMca", "EMcv"), metric.select = c("TSS", "BIAS"), metric.select.thresh = c(0.7, 0.7), metric.select.dataset = "calibration", metric.eval = c("TSS", "BIAS"), var.import = 0 ) proj_scen <- c("stk_current", "stk_future_ssp370_2060", "stk_future_ssp370_2100", "stk_future_ssp585_2060", "stk_future_ssp585_2100")

for (scen in proj_scen) { cat("\n> projections of", scen) sp_proj <- BIOMOD_Projection( bm.mod = diplos_modelo, proj.name = scen, new.env = get(scen), models.chosen = "all", metric.binary = "TSS", build.clamping.mask = FALSE, do.stack = FALSE, output.format = ".img" ) sp_ens_proj <- BIOMOD_EnsembleForecasting( bm.em = diplos_ensemble_models, bm.proj = sp_proj, proj.name = "CurrentENM", models.chosen = 'all', metric.binary = 'TSS', metric.filter = 'all', do.stack = FALSE, output.format = ".img" ) } return(paste0(sp, " modeling completed!"))

if (require(snowfall)) { sfInit(parallel = TRUE, cpus = 5) cat("Cluster initialized with 5 CPUs\n") sfExport("stk_current") sfExport("spp_to_model") sfExport("diplos") sfLibrary(biomod2) all_species_bm <- lapply(spp_to_model, diplos_wrapper)
sfStop() } else { for (sp in spp_to_model) { diplos_wrapper(sp) } }

str(diplos) 'data.frame': 195 obs. of 4 variables: $ sp : chr "bibronii_ss" "bibronii_ss" "bibronii_ss" "bibronii_ss" ... $ lat : num -46.2 -46.3 -46.3 -46.4 -46.5 ... $ lon : num -71.6 -71.9 -68.6 -68.1 -71.9 ... $ presence: num 1 1 1 1 1 1 1 1 1 1 ...

table(diplos$sp) bibronii_ss darwinii_ss 151 44

stk_current class : RasterStack dimensions : 335, 291, 97485, 4 (nrow, ncol, ncell, nlayers) resolution : 0.04166667, 0.04166667 (x, y) extent : -75.70833, -63.58333, -55.95833, -42 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs names : clip_bio2, clip_bio5, clip_bio6, clip_bio15 min values : 3.751515, 0.800000, -14.472000, 6.956398 max values : 14.04067, 29.30000, 3.70000, 67.16410

stk_future_ssp370_2060 class : RasterStack dimensions : 335, 291, 97485, 4 (nrow, ncol, ncell, nlayers) resolution : 0.04166667, 0.04166666 (x, y) extent : -75.70833, -63.58333, -55.95833, -42 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs names : clip_bio2, clip_bio5, clip_bio6, clip_bio15 min values : 4.30, 3.98, -10.84, 8.10 max values : 14.44, 30.26, 4.84, 68.50

stk_future_ssp370_2100 class : RasterStack dimensions : 337, 293, 98741, 4 (nrow, ncol, ncell, nlayers) resolution : 0.04166666, 0.04166668 (x, y) extent : -75.75, -63.54167, -56, -41.95833 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs names : clip_bio2, clip_bio5, clip_bio6, clip_bio15 min values : 3.76, 4.22, -11.00, 6.56 max values : 14.82, 33.86, 6.40, 71.84

stk_future_ssp585_2060 class : RasterStack dimensions : 337, 293, 98741, 4 (nrow, ncol, ncell, nlayers) resolution : 0.04166666, 0.04166668 (x, y) extent : -75.75, -63.54167, -56, -41.95833 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs names : clip_bio2, clip_bio5, clip_bio6, clip_bio15 min values : 3.74, 3.12, -12.38, 7.96 max values : 14.54, 32.12, 5.28, 70.08

stk_future_ssp585_2100 class : RasterStack dimensions : 337, 293, 98741, 4 (nrow, ncol, ncell, nlayers) resolution : 0.04166666, 0.04166668 (x, y) extent : -75.75, -63.54167, -56, -41.95833 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs names : clip_bio2, clip_bio5, clip_bio6, clip_bio15 min values : 3.78, 4.86, -10.48, 6.80 max values : 15.04, 34.94, 6.94, 73.16

Environment Information sessionInfo() R version 4.3.3 (2024-02-29 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: America/Buenos_Aires tzcode source: internal

attached base packages: [1] parallel splines stats graphics grDevices utils datasets [8] methods base

other attached packages: [1] snowfall_1.84-6.3 snow_0.4-4 biomod2_4.2-5-2
[4] xgboost_1.7.8.1 randomForest_4.7-1.2 maxnet_0.1.4
[7] earth_5.3.3 plotmo_3.6.4 plotrix_3.8-4
[10] Formula_1.2-5 gbm_2.2.2 lattice_0.22-5
[13] doParallel_1.0.17 iterators_1.0.14 ggtext_0.1.2
[16] tidyterra_0.6.1 mgcv_1.9-1 nlme_3.1-166
[19] gam_1.22-5 foreach_1.5.2 raster_3.6-26
[22] sp_2.1-4 mda_0.5-4 class_7.3-22
[25] rpart_4.1.23 nnet_7.3-19 terra_1.7-78

loaded via a namespace (and not attached): [1] gtable_0.3.5 ggplot2_3.5.1 vctrs_0.6.5
[4] tools_4.3.3 generics_0.1.3 proxy_0.4-27
[7] tibble_3.2.1 PresenceAbsence_1.1.11 fansi_1.0.6
[10] pkgconfig_2.0.3 KernSmooth_2.23-22 Matrix_1.6-1.1
[13] data.table_1.15.4 lifecycle_1.0.4 compiler_4.3.3
[16] stringr_1.5.1 munsell_0.5.1 codetools_0.2-19
[19] pillar_1.9.0 tidyr_1.3.1 classInt_0.4-10
[22] abind_1.4-5 tidyselect_1.2.1 stringi_1.8.4
[25] sf_1.0-16 dplyr_1.1.4 reshape2_1.4.4
[28] purrr_1.0.2 grid_4.3.3 colorspace_2.1-1
[31] cli_3.6.2 magrittr_2.0.3 survival_3.6-4
[34] utf8_1.2.4 e1071_1.7-13 scales_1.3.0
[37] rlang_1.1.3 gridtext_0.1.5 Rcpp_1.0.12
[40] DBI_1.2.2 glue_1.7.0 xml2_1.3.6
[43] pROC_1.18.5 rstudioapi_0.16.0 reshape_0.8.9
[46] jsonlite_1.8.8 R6_2.5.1 plyr_1.8.9
[49] units_0.8-5

Additional information If you have any additional information or context you can add it here.

HeleneBlt commented 1 week ago

Hello there!

It's strange indeed: your code seems totally fine! Could you share the output of traceback() after the error or unwrap your modelling so we can localise the error ?

Thanks a lot! Hélène

zali37 commented 1 week ago

Hello! thank you for answering me so fast I did not know how to unwrap the modeling so I tried to do everything again with one species.

bibronii <- read.csv(file = "SOLObibronii.csv",header = T,sep = ",",dec = ".") bibronii$presence <- as.numeric(bibronii$presence)

bibronii_format <- BIOMOD_FormatingData( resp.name = "bibronii", resp.var = bibronii$presence, resp.xy = bibronii[, c("lon", "lat")], expl.var = stk_current, filter.raster = TRUE )

registerDoParallel() bibronii_modelo <- BIOMOD_Modeling( bm.format = bibronii_format, modeling.id = "solo_bibronii", models = c("SRE", "RF", "GAM", "ANN"), CV.strategy = "random", CV.nb.rep = 5, CV.perc = 0.8, var.import = 3, metric.eval = c("TSS", "BIAS") )

bibronii_ensemble_models <- BIOMOD_EnsembleModeling( bm.mod = bibronii_modelo, models.chosen = "all", em.by = "all", em.algo = c("EMmean", "EMca", "EMcv"), metric.select = c("TSS", "BIAS"), metric.select.thresh = c(0.7, 0.7), metric.select.dataset = "calibration", metric.eval = c("TSS", "BIAS"), var.import = 0 )

proj_scen <- c("stk_current", "stk_future_ssp370_2060", "stk_future_ssp370_2100", "stk_future_ssp585_2060", "stk_future_ssp585_2100")

for (scen in proj_scen) { cat("\n> projections of", scen)

bibronii_proj <- BIOMOD_Projection( bm.mod = bibronii_modelo, proj.name = scen, new.env = get(scen), models.chosen = "all", metric.binary = "TSS", build.clamping.mask = FALSE, do.stack = FALSE, output.format = ".img" )

bibronii_ens_proj <- BIOMOD_EnsembleForecasting( bm.em = bibronii_ensemble_models, bm.proj = bibronii_proj, proj.name = "CurrentENMbibronii", models.chosen = 'all', metric.binary = 'TSS', metric.filter = 'all', do.stack = FALSE, output.format = ".img" ) }

Something is wrong with the ensemble model projection for the future. I got this error: Error in { : task 1 failed - "NULL value passed as symbol address"

And I got this with traceback() traceback() 4: stop(simpleError(msg, call = expr)) 3: e$fun(obj, substitute(ex), parent.frame(), e$data) 2: foreach(em.name = models.chosen) %dopar% { cat("\n\t> Projecting", em.name, "...") if (do.stack) { filename <- NULL } else { filename <- file.path(indiv_proj_dir, paste0(em.name, output.format)) } mod <- get(BIOMOD_LoadModels(bm.out = bm.em, full.name = em.name)) ef.tmp <- predict(mod, newdata = formal_pred, on_0_1000 = on_0_1000, data_as_formal_predictions = TRUE, filename = filename, mod.name = em.name, na.rm = na.rm) if (do.stack) { if (proj_is_raster) { return(wrap(ef.tmp)) } else { return(ef.tmp) } ... 1: BIOMOD_EnsembleForecasting(bm.em = bibronii_ensemble_models, bm.proj = bibronii_proj, proj.name = "CurrentENMbibronii", models.chosen = "all", metric.binary = "TSS", metric.filter = "all", do.stack = FALSE, output.format = ".img")

HeleneBlt commented 1 week ago

Hi !

The problem comes just for registerDoParallel() which interferes with the parallelisation of biomod2. It depends on what you want to do but here, I suggest you remove it.

Don't hesitate if it doesn't work for you 🙂 Hélène

zali37 commented 1 week ago

Hello. Just so you know, it has already worked. Thank you so much!