rspatial / dismo

R dismo package
GNU General Public License v3.0
22 stars 11 forks source link

error when making BIOCLIM predictions with new dismo version #14

Closed jamiemkass closed 2 years ago

jamiemkass commented 3 years ago

I get this error when making predictions with BIOCLIM models using the new version.

.percRank not available for .Call() for package "dismo"

The older version 1.1-4 does not have this problem.

Thanks.

jamiemkass commented 3 years ago

Hello, wondering if there was an easy fix for this. As bioclim.predict() is tied up with Rcpp, making the fix on my end is quite difficult. I wonder if a simple change to the NAMESPACE file would be enough to remedy this.

Thanks for your help.

rhijmans commented 3 years ago

Sorry for the slow response. But I cannot reproduce this on version 3.6 or version 4 on different platforms. Are you on an old version of R with old packages?
Can you start a new session with only library(dismo) and then run the example in ?bioclim. Can you include your sessionInfo()?

jamiemkass commented 3 years ago

Thanks for the response. I did some investigation, and what you recommended works just fine. However, if I do library(ecospat) and then run dismo::predict(logo, bc), I get the .percRank error. Any idea what's going on? Is there some dependency of ecospat that is responsible here? My package loads ecospat internally for some functionality, and that is what was producing my error.

Also, to confirm, this error does not surface with dismo versions pre 1.3-3.

Thanks very much for your help.

jamiemkass commented 3 years ago

Here's my sessionInfo(), before and after loading ecospat.

Before:

R version 4.0.3 (2020-10-10) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Big Sur 10.16

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages: [1] dismo_1.3-3 raster_3.4-5 sp_1.4-5 ENMeval_2.0.0 magrittr_2.0.1

loaded via a namespace (and not attached): [1] compiler_4.0.3 rgdal_1.5-23 tools_4.0.3 Rcpp_1.0.6 codetools_0.2-18 grid_4.0.3 iterators_1.0.13 foreach_1.5.1
[9] lattice_0.20-41

After:

R version 4.0.3 (2020-10-10) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Big Sur 10.16

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages: [1] ecospat_3.2 gbm_2.1.8 ape_5.4-1 ade4_1.7-16 dismo_1.3-3 raster_3.4-5 sp_1.4-5 ENMeval_2.0.0 magrittr_2.0.1

loaded via a namespace (and not attached): [1] backports_1.2.1 Hmisc_4.5-0 rasterVis_0.50.2 plyr_1.8.6 splines_4.0.3 ggplot2_3.3.3
[7] TH.data_1.0-10 digest_0.6.27 foreach_1.5.1 htmltools_0.5.1.1 earth_5.3.0 fansi_0.4.2
[13] checkmate_2.0.0 cluster_2.1.1 doParallel_1.0.16 ks_1.12.0 recipes_0.1.15 gower_0.2.2
[19] matrixStats_0.58.0 sandwich_3.0-0 CircStats_0.2-6 prettyunits_1.1.1 jpeg_0.1-8.1 colorspace_2.0-0
[25] xfun_0.22 dplyr_1.0.5 rgdal_1.5-23 crayon_1.4.1 hexbin_1.28.2 survival_3.2-10
[31] zoo_1.8-9 iterators_1.0.13 glue_1.4.2 PresenceAbsence_1.1.9 gtable_0.3.0 ipred_0.9-11
[37] MatrixModels_0.5-0 rms_6.2-0 abind_1.4-5 SparseM_1.81 scales_1.1.1 mvtnorm_1.1-1
[43] DBI_1.1.1 Rcpp_1.0.6 plotrix_3.8-1 viridisLite_0.3.0 progress_1.2.2 htmlTable_2.1.0
[49] mclust_5.4.7 foreign_0.8-81 proxy_0.4-25 Formula_1.2-4 stats4_4.0.3 lava_1.6.9
[55] prodlim_2019.11.13 ecodist_2.0.7 htmlwidgets_1.5.3 RColorBrewer_1.1-2 nabor_0.5.0 ellipsis_0.3.1
[61] pkgconfig_2.0.3 reshape_0.8.8 nnet_7.3-15 deldir_0.2-10 utf8_1.2.1 caret_6.0-86
[67] tidyselect_1.1.0 rlang_0.4.10 reshape2_1.4.4 munsell_0.5.0 TeachingDemos_2.12 tools_4.0.3
[73] generics_0.1.0 adehabitatHR_0.4.19 stringr_1.4.0 maxnet_0.1.2 ModelMetrics_1.2.2.2 knitr_1.31
[79] purrr_0.3.4 randomForest_4.6-14 nlme_3.1-152 quantreg_5.85 biomod2_3.4.6 compiler_4.0.3
[85] rstudioapi_0.13 png_0.1-7 e1071_1.7-6 tibble_3.1.0 stringi_1.5.3 plotmo_3.6.0
[91] lattice_0.20-41 poibin_1.5 Matrix_1.3-2 classInt_0.4-3 permute_0.9-5 vegan_2.5-7
[97] vctrs_0.3.7 pillar_1.5.1 lifecycle_1.0.0 data.table_1.14.0 maptools_1.1-1 conquer_1.0.2
[103] R6_2.5.0 latticeExtra_0.6-29 KernSmooth_2.23-18 gridExtra_2.3 codetools_0.2-18 polspline_1.1.19
[109] boot_1.3-27 MASS_7.3-53.1 gtools_3.8.2 assertthat_0.2.1 withr_2.4.1 multcomp_1.4-16
[115] mgcv_1.8-34 adehabitatMA_0.3.14 parallel_4.0.3 hms_1.0.0 terra_1.1-4 grid_4.0.3
[121] rpart_4.1-15 timeDate_3043.102 adehabitatLT_0.3.25 tidyr_1.1.3 class_7.3-18 mda_0.5-2
[127] pROC_1.17.0.1 snowfall_1.84-6.1 lubridate_1.7.10 base64enc_0.1-3

jamiemkass commented 3 years ago

Just checking in about this. Are you able to identify the issue here? I also posted an issue to ecospat but currently no reply as of yet.

Thanks so much.

rhijmans commented 3 years ago

I cannot reproduce this. The below works as expected for me:

library(ecospat)
library(dismo)
logo <- stack(system.file("external/rlogo.grd", package="raster"))
pts <- matrix(c(48.243420, 48.243420, 47.985820, 52.880230, 49.531423, 46.182616, 54.168232, 
  69.624263, 83.792291, 85.337894, 74.261072, 83.792291, 95.126713, 84.565092, 66.275456, 41.803408,
  25.832176, 3.936132, 18.876962, 17.331359,7.048974, 13.648543, 26.093446, 28.544714, 39.104026, 
  44.572240, 51.171810, 56.262906, 46.269272, 38.161230, 30.618865, 21.945145, 34.390047, 59.656971,
  69.839163, 73.233228, 63.239594, 45.892154, 43.252326, 28.356155) , ncol=2)
bc <- bioclim(logo, pts)
p <- predict(logo, bc)

Can you provide a reproducible example where this (or something like this) does not work?

jamiemkass commented 3 years ago

Thanks for checking. I am baffled, because I just updated R, reinstalled both packages to be sure, and tried this again. Here's my exact output, with it failing after the ecospat load, and succeeding without it.

Fail:

R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ecospat)
Loading required package: ade4
Loading required package: ape
Loading required package: gbm
Loaded gbm 2.1.8
Loading required package: sp
Registered S3 methods overwritten by 'adehabitatMA':
  method                       from
  print.SpatialPixelsDataFrame sp  
  print.SpatialPixels          sp  
> library(dismo)
Loading required package: raster

Attaching package: ‘raster’

The following objects are masked from ‘package:ape’:

    rotate, zoom

> logo <- stack(system.file("external/rlogo.grd", package="raster"))
> pts <- matrix(c(48.243420, 48.243420, 47.985820, 52.880230, 49.531423, 46.182616, 54.168232, 
+                 69.624263, 83.792291, 85.337894, 74.261072, 83.792291, 95.126713, 84.565092, 66.275456, 41.803408,
+                 25.832176, 3.936132, 18.876962, 17.331359,7.048974, 13.648543, 26.093446, 28.544714, 39.104026, 
+                 44.572240, 51.171810, 56.262906, 46.269272, 38.161230, 30.618865, 21.945145, 34.390047, 59.656971,
+                 69.839163, 73.233228, 63.239594, 45.892154, 43.252326, 28.356155) , ncol=2)
> bc <- bioclim(logo, pts)
> p <- predict(logo, bc)
Error in .Call("percRank", as.double(pres), as.integer(dim(pres)), as.double(vals),  : 
  "percRank" not available for .Call() for package "dismo"
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] dismo_1.3-3  raster_3.4-5 ecospat_3.2  sp_1.4-5     gbm_2.1.8    ape_5.4-1    ade4_1.7-16 

loaded via a namespace (and not attached):
  [1] backports_1.2.1       Hmisc_4.5-0           rasterVis_0.50.1      plyr_1.8.6            splines_4.0.5        
  [6] ggplot2_3.3.3         TH.data_1.0-10        digest_0.6.27         foreach_1.5.1         htmltools_0.5.1.1    
 [11] earth_5.3.0           fansi_0.4.2           magrittr_2.0.1        checkmate_2.0.0       cluster_2.1.1        
 [16] doParallel_1.0.16     ks_1.12.0             recipes_0.1.15        gower_0.2.2           matrixStats_0.58.0   
 [21] sandwich_3.0-0        CircStats_0.2-6       prettyunits_1.1.1     jpeg_0.1-8.1          colorspace_2.0-0     
 [26] rgdal_1.5-23          xfun_0.22             dplyr_1.0.5           crayon_1.4.1          hexbin_1.28.2        
 [31] survival_3.2-10       zoo_1.8-9             iterators_1.0.13      glue_1.4.2            PresenceAbsence_1.1.9
 [36] gtable_0.3.0          ipred_0.9-11          MatrixModels_0.5-0    rms_6.1-1             abind_1.4-5          
 [41] SparseM_1.81          scales_1.1.1          mvtnorm_1.1-1         Rcpp_1.0.6            plotrix_3.8-1        
 [46] viridisLite_0.3.0     progress_1.2.2        htmlTable_2.1.0       mclust_5.4.7          foreign_0.8-81       
 [51] proxy_0.4-25          Formula_1.2-4         stats4_4.0.5          lava_1.6.9            prodlim_2019.11.13   
 [56] ecodist_2.0.7         htmlwidgets_1.5.3     RColorBrewer_1.1-2    nabor_0.5.0           ellipsis_0.3.1       
 [61] pkgconfig_2.0.3       reshape_0.8.8         nnet_7.3-15           deldir_0.2-10         utf8_1.2.1           
 [66] caret_6.0-86          tidyselect_1.1.0      rlang_0.4.10          reshape2_1.4.4        munsell_0.5.0        
 [71] TeachingDemos_2.12    tools_4.0.5           generics_0.1.0        adehabitatHR_0.4.19   stringr_1.4.0        
 [76] maxnet_0.1.2          ModelMetrics_1.2.2.2  knitr_1.31            purrr_0.3.4           randomForest_4.6-14  
 [81] nlme_3.1-152          quantreg_5.85         biomod2_3.4.6         compiler_4.0.5        rstudioapi_0.13      
 [86] png_0.1-7             ENMeval_1.9.0         e1071_1.7-5           tibble_3.1.0          stringi_1.5.3        
 [91] plotmo_3.6.0          lattice_0.20-41       poibin_1.5            Matrix_1.3-2          classInt_0.4-3       
 [96] vegan_2.5-7           permute_0.9-5         vctrs_0.3.7           pillar_1.5.1          lifecycle_1.0.0      
[101] data.table_1.14.0     maptools_1.1-1        conquer_1.0.2         R6_2.5.0              latticeExtra_0.6-29  
[106] KernSmooth_2.23-18    gridExtra_2.3         codetools_0.2-18      polspline_1.1.19      boot_1.3-27          
[111] MASS_7.3-53.1         gtools_3.8.2          withr_2.4.1           multcomp_1.4-16       adehabitatMA_0.3.14  
[116] mgcv_1.8-34           parallel_4.0.5        hms_1.0.0             terra_1.1-4           grid_4.0.5           
[121] rpart_4.1-15          timeDate_3043.102     adehabitatLT_0.3.25   tidyr_1.1.3           class_7.3-18         
[126] mda_0.5-2             pROC_1.17.0.1         snowfall_1.84-6.1     lubridate_1.7.10      base64enc_0.1-3      
[131] tinytex_0.31  

Success:

R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 
> library(dismo)
Loading required package: raster
Loading required package: sp
> logo <- stack(system.file("external/rlogo.grd", package="raster"))
> pts <- matrix(c(48.243420, 48.243420, 47.985820, 52.880230, 49.531423, 46.182616, 54.168232, 
+                 69.624263, 83.792291, 85.337894, 74.261072, 83.792291, 95.126713, 84.565092, 66.275456, 41.803408,
+                 25.832176, 3.936132, 18.876962, 17.331359,7.048974, 13.648543, 26.093446, 28.544714, 39.104026, 
+                 44.572240, 51.171810, 56.262906, 46.269272, 38.161230, 30.618865, 21.945145, 34.390047, 59.656971,
+                 69.839163, 73.233228, 63.239594, 45.892154, 43.252326, 28.356155) , ncol=2)
> bc <- bioclim(logo, pts)
> p <- predict(logo, bc)
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] dismo_1.3-3  raster_3.4-5 sp_1.4-5    

loaded via a namespace (and not attached):
[1] compiler_4.0.5   rgdal_1.5-23     tools_4.0.5      Rcpp_1.0.6       tinytex_0.31     codetools_0.2-18
[7] grid_4.0.5       xfun_0.22        lattice_0.20-41 
rhijmans commented 3 years ago

To more specific, I cannot reproduce this on Big Sur 11.2.3, nor on windows or linux.

Somehow, for some reason, when you use predict it appears to use dismo R code from a previous version --- but it cannot find the C code from that previous version... This is too weird.

Can you show traceback() just after the error occurs?

Also, you can probably use this workaround

p <- predict(logo, bc, useC=FALSE)

jamiemkass commented 3 years ago

Thanks so much for checking on this. I am also on Big Sur 11.2.3. Your useC = FALSE suggestion worked for me -- can I use this for now to avoid the error? For reference, here is my traceback():

traceback() 6: .local(object, ...) 5: predict(model, object, filename = filename, ext = ext, progress = progress, format = format, overwrite = overwrite, ...) 4: predict(model, object, filename = filename, ext = ext, progress = progress, format = format, overwrite = overwrite, ...) 3: .local(object, ...) 2: predict(logo, bc) 1: predict(logo, bc)

rhijmans commented 3 years ago

By using useC=F you use pure R code, so it is perhaps a bit slower, but otherwise the results should be the same.

jamiemkass commented 3 years ago

Excellent. I'll use that for now. Thanks very much again.