wpeterman / ResistanceGA

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

Error in SS_optim: Raster Outputs - Error in { : task 5 failed - "0 (non-NA) cases" #25

Closed cmu002 closed 2 years ago

cmu002 commented 2 years ago

G'day, I've been attempting to run some soil geochemistry rasters through the SS_optim function, however, I keep getting an error output of Error in { : task 5 failed - "0 (non-NA) cases". I've sourced these datasets externally, however, they have the exact same extent/dimension/resolution as all previous datasets that have worked. This leads me to believe that there is something wrong with the actual raster, however, I can't seem to rectify the issue. Has anyone else come across this issue before in their .asc files?

Any advice would be greatly appreciated!

Data can be found here.

The full code used:

# Load Package
library(ResistanceGA) 
library(rgdal)
library(corpcor)
library(pryr)

## Create Directory for Examples ##
if("ResistanceGA_Examples"%in%dir("D:/ResistanceGA/Data/Model_Outputs/100m")==FALSE)
  dir.create(file.path("D:/ResistanceGA/Data/Model_Outputs/", "100m"))

## Create Directory Object for .asc Files and results
write.dir <- "D:/ResistanceGA/Data/Model_Outputs/FINAL"

## Genetic data Directory ##
gen.dir <- "D:/ResistanceGA/Data/Genetic_Data"

## Set working directory ##
setwd("D:/ResistanceGA/Data/100m")

## Sample locations
samples <- read.table(file.path(gen.dir,"EGK_SampleLoc.txt")), sep="")
sp.dat <- SpatialPoints(samples[,c(1,2)], proj4string = CRS("+proj=longlat +datum=WGS84"))
sp.dat.reproj <- spTransform(sp.dat, CRS("+proj=utm +zone=56 +south"))

## Input Genetic Matrix ##
gen_dist <- read.table(file.path(gen.dir, "EGK_Fst.txt')), sep="", header = FALSE)

## Bring in Geochemistry Rasters ##
carbon <- raster('carbon.asc')
phos <-raster('phosphorus.asc')
nit <-raster('nitrogen.asc')

## Create the Raster Stack ##
r.stack <- stack(carbon, phos, nit)

## Change Projection Of Datasets ##
crs(sp.dat.reproj)<- CRS("+proj=utm +zone=56 +south +datum=WGS84")
crs(r.stack)<- CRS("+proj=utm +zone=56 +south +datum=WGS84")

## Genetic Distance Inputs ##
gdist.inputs <- gdist.prep(length(sp.dat.reproj),
                           samples = sp.dat.reproj,
                           response = lower(as.matrix(gen_dist)),
                           method = 'commuteDistance')

GA.inputs <- GA.prep(ASCII.dir = r.stack,
                     Results.dir = write.dir,
                     parallel = 14)

SS_RESULTS.gdist <- SS_optim(gdist.inputs = gdist.inputs,
                             GA.inputs = GA.inputs)
wpeterman commented 2 years ago

This is an issue with how ResistanceGA is using gdistance. I haven't had time to figure out a solution. Your data works fine when optimizing with Circuitscape in Julia. Download the latest version of ResistanceGA devtools::install_github("ResistanceGA", build_vignettes = T)

There's a help document for installing and setting Julia and Circuitscape. The code below, following your code above, worked for me. Note that this was a short run to test the code.

# Use Julia ---------------------------------------------------------------

jl_home <- "C:/Users/peterman.73/AppData/Local/Programs/Julia-1.7.2/bin/"

jl_inputs <- jl.prep(n.Pops = length(sp.dat.reproj),
                     CS_Point.File = sp.dat.reproj,
                     response = lower(as.matrix(gen_dist)),
                     JULIA_HOME = jl_home)

GA.inputs <- GA.prep(ASCII.dir = r.stack,
                     Results.dir = write.dir ,
                     maxiter = 1,
                     pop.size = 5
                     # parallel = 14
)

SS_RESULTS.jl <- SS_optim(jl.inputs = jl_inputs,
                          GA.inputs = GA.inputs)
cmu002 commented 2 years ago

@wpeterman Thanks so much for the timely response! I've gotten it to work so far with Julia, so I'm going to close the issue. However, after digging around I noticed that this error is a common occurrence in the lme4 package when dealing with raster datasets. Rather strange how it worked on all of my other datasets, but occurred only on rasters I got from a third party...