@@ -111,32 +111,55 @@ getMaxentModel <- function(allTrainData, nCores, isTuningOn) {
111111 {
112112 usableCores <- max(1 , min(nCores , parallel :: detectCores() - 1 ))
113113 useParallel <- usableCores > 1
114- ENMevaluate(
115- occ = presenceTrainData ,
116- bg.coords = absenceTrainData ,
117- tune.args = tuneArgs ,
118- progbar = FALSE ,
119- partitions = " randomkfold" ,
120- parallel = useParallel ,
121- numCores = usableCores ,
122- quiet = TRUE ,
123- algorithm = ' maxent.jar'
124- )
114+
115+ # Try new ENMeval 2.0+ API first
116+ tryCatch({
117+ ENMeval :: ENMevaluate(
118+ occs = presenceTrainData ,
119+ bg = absenceTrainData ,
120+ tune.args = tuneArgs ,
121+ partitions = " randomkfold" ,
122+ algorithm = ' maxent.jar' ,
123+ parallel = useParallel ,
124+ numCores = usableCores ,
125+ quiet = TRUE
126+ )
127+ }, error = function (e1 ) {
128+ # If that fails, try old ENMeval < 2.0 API
129+ if (grepl(" unused argument|unexpected.*argument|object.*not found" , conditionMessage(e1 ))) {
130+ rsyncrosim :: updateRunLog(type = " warning" , message = " ENMevaluate failed, update ENMeval package to version 2.0" )
131+ stop(e1 )
132+ } else {
133+ stop(e1 )
134+ }
135+ })
125136 },
126137 error = function (e ) {
127138 warning(
128- " Parallel Maxent failed due to memory issue. Retrying in serial mode..."
129- )
130- ENMevaluate(
131- occ = presenceTrainData ,
132- bg.coords = absenceTrainData ,
133- tune.args = tuneArgs ,
134- progbar = FALSE ,
135- partitions = " randomkfold" ,
136- parallel = FALSE ,
137- quiet = TRUE ,
138- algorithm = ' maxent.jar'
139+ " Parallel Maxent failed. Retrying in serial mode...\n " ,
140+ " Original error: " , conditionMessage(e )
139141 )
142+
143+ # Retry in serial mode with new API
144+ tryCatch({
145+ ENMeval :: ENMevaluate(
146+ occs = presenceTrainData ,
147+ bg = absenceTrainData ,
148+ tune.args = tuneArgs ,
149+ partitions = " randomkfold" ,
150+ algorithm = ' maxent.jar' ,
151+ parallel = FALSE ,
152+ quiet = TRUE
153+ )
154+ }, error = function (e2 ) {
155+ # If that fails, try old API in serial
156+ if (grepl(" unused argument|unexpected.*argument|object.*not found" , conditionMessage(e2 ))) {
157+ rsyncrosim :: updateRunLog(type = " warning" , message = " ENMevaluate failed, update ENMeval package to version 2.0" )
158+ stop(e2 )
159+ } else {
160+ stop(e2 )
161+ }
162+ })
140163 }
141164 )
142165
@@ -174,7 +197,7 @@ getMaxentModel <- function(allTrainData, nCores, isTuningOn) {
174197
175198 # ENMeval/MaxEnt logistic output is a numeric vector of P(presence)
176199 p1 <- tryCatch(
177- as.numeric(predict(mxModel , newdata , type = " logistic" )),
200+ as.numeric(dismo :: predict(mxModel , newdata , type = " logistic" )),
178201 error = function (e ) {
179202 # some MaxEnt builds require 'dismo::predict' signature
180203 as.numeric(dismo :: predict(mxModel , newdata , args = " logistic" ))
@@ -249,7 +272,7 @@ predictMaxent <- function(raster, model, filename = "", memfrac = 0.5) {
249272
250273 # MaxEnt logistic prediction
251274 p1 <- tryCatch(
252- as.numeric(predict(m $ model , data , type = " logistic" )),
275+ as.numeric(dismo :: predict(m $ model , data , type = " logistic" )),
253276 error = function (e ) {
254277 # some MaxEnt builds require 'dismo::predict' signature
255278 as.numeric(dismo :: predict(m $ model , data , args = " logistic" ))
0 commit comments