wpeterman / ResistanceGA

Optimize resistance surfaces using Genetic Algorithms
36 stars 15 forks source link

SS_optim: Error in { : task 2 failed - "0 (non-NA) cases" #29

Closed MaxHirschfeld closed 1 year ago

MaxHirschfeld commented 1 year ago

Hi Bill,

I am trying to run single surface optimization on a continuous raster but keep getting the error: Error in { : task 2 failed - "0 (non-NA) cases".

I am attaching the single continuous raster surface (continuous_bathymetry.asc). This is a bathymetry of the Galapagos Islands with values inverted to be positive and with the land surface assigned the highest number to keep only positive numbers and to indicate the highest resistance for an aquatic organism. continuous_bathymetry.asc.zip

Any help would be very appreciated. Thank you

`

Data prep

I created a directory as per vignette

write.dir <- "/..."

Create sampling locations and convert them to a SpatialPoints data frame

loc <- data.frame( x = c(-91.439824, -91.388629, -91.359097, -91.556453, -90.491406, -89.608276, -89.703003), y = c(-0.269343, -0.431598, -0.296499, -0.054629, -1.27581, -0.886111, -1.348597)) loc2 <- SpatialPoints(loc, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

Load pairwise genetic distances (these were extracted from a matrix that represents the same order as the sampling locations)

gendist <- as.vector(c(0.000, 0.000, 0.003, 0.015, 0.014, 0.030, 0.000, 0.003, 0.015, 0.015, 0.030, 0.002, 0.014, 0.013, 0.030, 0.016, 0.017, 0.031, 0.021, 0.037, 0.029))

Prepare GA.inputs

GA.inputs <- GA.prep(ASCII.dir=write.dir, # raster objects to optimized. Results.dir= write.dir, # using the directory we previously made select.trans = list("M"), seed = 555, parallel = 4)

Prepare gdist.inputs

gdist.inputs <- gdist.prep(n.Pops = length(loc2), # The number of populations that are being assessed samples = loc2, # provide a SpatialPoints object response = gendist, # Vector of pairwise genetic distances (lower half of pairwise matrix) method = 'commuteDistance')

Run single surface optimization

SS_RESULTS.gdist <- SS_optim(gdist.inputs = gdist.inputs, GA.inputs = GA.inputs)

This results in the Error in { : task 2 failed - "0 (non-NA) cases".

sessionInfo()

R version 4.2.2 (2022-10-31) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Monterey 12.6.2

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2/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] ResistanceGA_4.2-8 viridis_0.6.2 viridisLite_0.4.1 raster_3.6-14
[5] sp_1.6-0

loaded via a namespace (and not attached): [1] jsonlite_1.8.4 splines_4.2.2 foreach_1.5.2
[4] shiny_1.7.4 stats4_4.2.2 spatstat.geom_3.0-5
[7] pillar_1.8.1 lattice_0.20-45 glue_1.6.2
[10] digest_0.6.31 promises_1.2.0.1 polyclip_1.10-4
[13] minqa_1.2.5 colorspace_2.1-0 MuMIn_1.47.1
[16] htmltools_0.5.4 httpuv_1.6.8 Matrix_1.5-3
[19] plyr_1.8.8 spatstat.sparse_3.0-0 JuliaCall_0.17.5
[22] pkgconfig_2.0.3 s2_1.1.2 xtable_1.8-4
[25] spatstat.core_2.4-4.010 scales_1.2.1 tensor_1.5
[28] XR_0.7.2 terra_1.6-53 later_1.3.0
[31] spatstat.utils_3.0-1 lme4_1.1-31 proxy_0.4-27
[34] tibble_3.1.8 mgcv_1.8-41 generics_0.1.3
[37] ggplot2_3.4.0 ellipsis_0.3.2 spatstat.random_3.0-1
[40] XRJulia_0.9.0 cli_3.6.0 magrittr_2.0.3
[43] crayon_1.5.2 mime_0.12 deldir_1.0-6
[46] fansi_1.0.4 doParallel_1.0.17 nlme_3.1-161
[49] MASS_7.3-58.2 class_7.3-21 tools_4.2.2
[52] lifecycle_1.0.3 munsell_0.5.0 e1071_1.7-12
[55] gdistance_1.6 akima_0.6-3.4 compiler_4.2.2
[58] rlang_1.0.6 units_0.8-1 classInt_0.4-8
[61] grid_4.2.2 nloptr_2.0.3 iterators_1.0.14
[64] rstudioapi_0.14 goftest_1.2-3 igraph_1.3.5
[67] miniUI_0.1.1.1 wk_0.7.1 boot_1.3-28.1
[70] GA_3.2.3 gtable_0.3.1 codetools_0.2-18
[73] abind_1.4-5 DBI_1.1.3 R6_2.5.1
[76] gridExtra_2.3 rgdal_1.6-4 knitr_1.41
[79] dplyr_1.0.10 fastmap_1.1.0 utf8_1.2.2
[82] ggExtra_0.10.0 spdep_1.2-7 KernSmooth_2.23-20
[85] spatstat.data_3.0-0 parallel_4.2.2 Rcpp_1.0.10
[88] sf_1.0-9 vctrs_0.5.2 rpart_4.1.19
[91] spData_2.2.1 tidyselect_1.2.0 xfun_0.36 `

wpeterman commented 1 year ago

There is an issue (unresolved) when optimizing using gdistance. I've mostly stopped supporting this function as optimization with Circuitscape in Julia is faster and more robust. I would encourage you to follow the Vignette/Help documents to install Julia and Circuitscape. Everything worked fine with your data when I checked it with this method.

MaxHirschfeld commented 1 year ago

Thank you very much for your quick response, Bill.

I am using Mac. Can ResistanceGA execute CIRCUITSCAPE on MacOS? As per the vignette it states it can't. If it doesn't work, unfortunately, I am not able to use ResistanceGA at the moment.

I would appreciate your help or any suggestions.

Thank you very much and all the best,

Max

Maximilian Hirschfeld Ph.D. Tropical Marine Ecology

@.*** http://www.enphocus.net

On January 31, 2023 at 10:09:52 pm +10:00, Bill Peterman @.***> wrote:

There is an issue (unresolved) when optimizing using gdistance. I've mostly stopped supporting this function as optimization with Circuitscape in Julia is faster and more robust. I would encourage you to follow the Vignette/Help documents to install Julia and Circuitscape. Everything worked fine with your data when I checked it with this method.

— Reply to this email directly, view it on GitHub https://github.com/wpeterman/ResistanceGA/issues/29#issuecomment-1410238610, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANHVVVPPHKJOV655N2BKHI3WVD6JBANCNFSM6AAAAAAUMCNLQY. You are receiving this because you authored the thread.Message ID: @.***>

wpeterman commented 1 year ago

Yes, but it using Circuitscape v5 run in Julia. There is documentation provided with the latest ResistanceGA version for installing and checking Julia and Circuitscape. devtools::install_github('wpeterman/ResistanceGA', build_vignettes = TRUE)

MaxHirschfeld commented 1 year ago

Thank you very much, Bill.

I'll wok with that.

All the best,

Max

Maximilian Hirschfeld Ph.D. Tropical Marine Ecology

@.*** http://www.enphocus.net

On February 1, 2023 at 9:55:41 pm +10:00, Bill Peterman @.***> wrote:

Yes, but it using Circuitscape v5 run in Julia. There is documentation provided with the latest ResistanceGA version for installing and checking Julia and Circuitscape. devtools::install_github('wpeterman/ResistanceGA', build_vignettes = TRUE)

— Reply to this email directly, view it on GitHub https://github.com/wpeterman/ResistanceGA/issues/29#issuecomment-1411935946, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANHVVVIRHFJYBT54LCXCSRLWVJFL3ANCNFSM6AAAAAAUMCNLQY. You are receiving this because you authored the thread.Message ID: @.***>

MaxHirschfeld commented 1 year ago

Dear Bill,

I have installed and tested Circuitscape and Julia on my Mac as per your recommendations. Now I am trying to run single surface optimization using the latest ResistanceGA (4.2-8) using the same example data I have provided in the Issue #29 but keep getting errors. I have tried to find a solution but have not been able to resolve it and would be grateful for any suggestions.

When I run

jl.inputs <- jl.prep(n.Pops = length(loc),

  • response = gendist, CS_Point.File = loc,
  • cholmod = TRUE,
  • silent = FALSE,
  • Julia_link = 'JuliaCall',+ JULIA_HOME = JULIA_HOME)

Everything seems to be working fine:

[1] "Test: Run Circuitscape from Julia"

[ Info: 2023-02-16 12:24:06 : Precision used: None

[ Info: 2023-02-16 12:24:07 : Reading maps

[ Info: 2023-02-16 12:24:09 : Resistance/Conductance map has 2500 nodes

[ Info: 2023-02-16 12:24:12 : Solver used: CHOLMOD

┌ Warning: 2023-02-16 12:24:14 : CHOLMOD only works with double precision. Converting single precision matrix to double └ @ Circuitscape ~/.julia/packages/Circuitscape/HNOrX/src/logging.jl:25

[ Info: 2023-02-16 12:24:14 : Graph has 2500 nodes, 5 focal points and 1 connected components

[ Info: 2023-02-16 12:24:14 : Total number of pair solves = 10

[ Info: 2023-02-16 12:24:14 : Triggering resistance calculation shortcut

[ Info: 2023-02-16 12:24:14 : Total number of pair solves has been reduced to 4

[ Info: 2023-02-16 12:24:14 : Time taken to construct cholesky factor = 0.06898055

[ Info: 2023-02-16 12:24:14 : Time taken to construct local nodemap = 7.8662e-5 seconds

[ Info: 2023-02-16 12:24:14 : Solving points 1 to 4

[ Info: 2023-02-16 12:24:16 : Time taken to complete job = 9.185529825

Test Passed

However, when running

jl.optim <- SS_optim(jl.inputs = jl.inputs, GA.inputs = GA.inputs)

using either the example you provide in the "Julia_guide" PDF or my example data I get the following issue:

[ Info: 2023-02-16 12:28:01 : Precision used: None

signal (6): Abort trap: 6 in expression starting at none:0 [ Info: 2023-02-16 12:28:01 : Precision used: None

signal (6): Abort trap: 6 in expression starting at none:0

signal (11): Segmentation fault: 11 in expression starting at none:0

signal (11): Segmentation fault: 11 in expression starting at none:0 [ Info: 2023-02-16 12:28:01 : Precision used: None

signal (6): Abort trap: 6 in expression starting at none:0

signal (11): Segmentation fault: 11 in expression starting at none:0 [ Info: 2023-02-16 12:28:03 : Precision used: None

signal (6): Abort trap: 6 in expression starting at none:0

signal (11): Segmentation fault: 11in expression starting at none:0

I have tried to find a solution online as it seems to have to do with Julia and/or Circuitscape but have not been able to resolve the issue.

Thank you very much in advance for any help or suggestions where to look for solutions.

All the best,

Max

Maximilian Hirschfeld Ph.D. Tropical Marine Ecology

@.*** http://www.enphocus.net

On February 1, 2023 at 9:55:41 pm +10:00, Bill Peterman @.***> wrote:

Yes, but it using Circuitscape v5 run in Julia. There is documentation provided with the latest ResistanceGA version for installing and checking Julia and Circuitscape. devtools::install_github('wpeterman/ResistanceGA', build_vignettes = TRUE)

— Reply to this email directly, view it on GitHub https://github.com/wpeterman/ResistanceGA/issues/29#issuecomment-1411935946, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANHVVVIRHFJYBT54LCXCSRLWVJFL3ANCNFSM6AAAAAAUMCNLQY. You are receiving this because you authored the thread.Message ID: @.***>

wpeterman commented 1 year ago

This code works for me on a Windows machine. The main difference is that I'm importing the point and raster layers as R objects, which then get formatted/exported by ResistanceGA for use with Circuitscape.

library(ResistanceGA)

write.dir <- paste0(getwd(), "/results/")

loc <- data.frame( x = c(-91.439824, -91.388629, -91.359097, -91.556453, -90.491406, -89.608276, -89.703003),
                   y = c(-0.269343, -0.431598, -0.296499, -0.054629, -1.27581, -0.886111, -1.348597))
loc2 <- SpatialPoints(loc, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

# Load pairwise genetic distances (these were extracted from a matrix that represents the same order as the sampling locations)

gendist <- as.vector(c(0.000, 0.000, 0.003, 0.015, 0.014,
                       0.030, 0.000, 0.003, 0.015, 0.015,
                       0.030, 0.002, 0.014, 0.013, 0.030,
                       0.016, 0.017, 0.031, 0.021, 0.037, 0.029))

rast <- raster("asc/continous_bathymetry.asc")

plot(rast); plot(loc2, add=T)

# Prepare GA.inputs

GA.inputs <- GA.prep(ASCII.dir=paste0(getwd(),"/asc/"), # raster objects to optimized.
                     Results.dir= write.dir, # using the directory we previously made
                     select.trans = list("M"),
                     seed = 555,
                     # parallel = 4,
                     pop.size = 5,
                     maxiter = 1)

# Julia -------------------------------------------------------------------
jl_home <- "C:/Users/peterman.73/AppData/Local/Programs/Julia-1.8.5/bin/"
jl_inputs <- jl.prep(n.Pops = length(loc2),
                     response = gendist,
                     CS_Point.File = loc2,
                     JULIA_HOME = jl_home)

SS_RESULTS.jl <- SS_optim(jl.inputs = jl_inputs, 
                          GA.inputs = GA.inputs)
MaxHirschfeld commented 1 year ago

Hi Bill,

First of all thank you for your last response. I have been able to run my landscape surface layers through optimization with ResistanceGA, using Julia to run Circuitscape in the background. Creating current maps using Run_CS.jl also works well.

I would like to ask you one more question that may have a very straight forward solution for you, but I have not been able to solve it.

I am working with an aquatic species and therefore land surface is a complete barrier to movement. However, when using original values of the digital elevation model (ranging from large ocean depth up to over 1000m elevation) in the optimization, areas above sea level are assigned low resistance values. When I use the surface raster with NA's for areas above sealevel SS_optim command fails to generate the results. Circuitscape can use .asc raster files that have nodata coded as -9999. However, this doesn't work for optimizing in R using ResistanceGA.

Do you have any recommendation or solutions on how to include barriers to movement, areas that should not me included in the optimization at all?

I would appreciate any suggestions you may have.

Thank you and all the best,

Max

Maximilian Hirschfeld Ph.D. Tropical Marine Ecology

@.*** http://www.enphocus.net

On February 16, 2023 at 10:47:26 pm +10:00, Bill Peterman @.***> wrote:

This code works for me on a Windows machine. The main difference is that I'm importing the point and raster layers as R objects, which then get formatted/exported by ResistanceGA for use with Circuitscape. library(ResistanceGA)

write.dir <- paste0(getwd(), "/results/")

loc <- data.frame( x = c(-91.439824, -91.388629, -91.359097, -91.556453, -90.491406, -89.608276, -89.703003), y = c(-0.269343, -0.431598, -0.296499, -0.054629, -1.27581, -0.886111, -1.348597)) loc2 <- SpatialPoints(loc, proj4string=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

Load pairwise genetic distances (these were extracted from a matrix that represents the same order as the sampling locations)

gendist <- as.vector(c(0.000, 0.000, 0.003, 0.015, 0.014, 0.030, 0.000, 0.003, 0.015, 0.015, 0.030, 0.002, 0.014, 0.013, 0.030, 0.016, 0.017, 0.031, 0.021, 0.037, 0.029))

rast <- raster("asc/continous_bathymetry.asc")

plot(rast); plot(loc2, add=T)

Prepare GA.inputs

GA.inputs <- GA.prep(ASCII.dir=paste0(getwd(),"/asc/"), # raster objects to optimized. Results.dir= write.dir, # using the directory we previously made select.trans = list("M"), seed = 555,

parallel = 4,

pop.size = 5, maxiter = 1)

Julia -------------------------------------------------------------------

jl_home <- "C:/Users/peterman.73/AppData/Local/Programs/Julia-1.8.5/bin/" jl_inputs <- jl.prep(n.Pops = length(loc2), response = gendist, CS_Point.File = loc2, JULIA_HOME = jl_home)

SS_RESULTS.jl <- SS_optim(jl.inputs = jl_inputs, GA.inputs = GA.inputs)

— Reply to this email directly, view it on GitHub https://github.com/wpeterman/ResistanceGA/issues/29#issuecomment-1433035841, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANHVVVOYDZ7WU64OAESTSL3WXYOV5ANCNFSM6AAAAAAUMCNLQY. You are receiving this because you authored the thread.Message ID: @.***>

wpeterman commented 1 year ago

HI Max-- Not sure what you're experiencing. Setting values to NA within a raster layer and then using this layer with ResistanceGA and Circuitscape works for me.