Skip to content

Commit

Permalink
Merge pull request #337 from biomodhub/devel_current
Browse files Browse the repository at this point in the history
Bugfix predict RF / MAXENT
  • Loading branch information
MayaGueguen committed Sep 28, 2023
2 parents c96ed83 + 1dc2770 commit 192e6a4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion R/BIOMOD_EnsembleModeling.R
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ BIOMOD_EnsembleModeling <- function(bm.mod,
# subsetting environment and coord
env_to_predict <- get_formal_data(bm.mod)@data.env.var[index_to_predict, , drop = FALSE]
coord_to_predict <- get_formal_data(bm.mod)@coord[index_to_predict,]

# prediction on the other PA datasets
new_prediction <-
get_predictions(
Expand Down
4 changes: 2 additions & 2 deletions R/BIOMOD_Projection.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ BIOMOD_Projection <- function(bm.mod,
}
}

## 4. MAKING PROJECTIONS ------------------------------------------------------------------------

## 4. MAKING PROJECTIONS ------------------------------------------------------------------------

if (nb.cpu > 1) {
if (.getOS() != "windows") {
Expand Down Expand Up @@ -326,6 +326,7 @@ BIOMOD_Projection <- function(bm.mod,
return(filename)
}
}

## Putting predictions into the right format
if (do.stack) {
if (proj_is_raster) {
Expand Down Expand Up @@ -466,7 +467,6 @@ BIOMOD_Projection <- function(bm.mod,
}
}


### save binary/filtered file link into proj_out ----------------------------
if (!is.null(metric.binary)) {
proj_out@proj.out@link <- c(proj_out@proj.out@link, saved.files.binary)
Expand Down
3 changes: 3 additions & 0 deletions R/biomod2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
mess <- paste(pkgname, RFver, "loaded.\n")
mess <- paste(mess, "/!\\ New set up for modeling options. We apologize for the trouble ^[*.*]^")
packageStartupMessage(mess)

toLoad <- unique(ModelsTable$package[-which(ModelsTable$package %in% c("MAXENT", "biomod2"))])
for (pkg in toLoad) eval(parse(text = paste0("require(", pkg, ")")))
}
}

Expand Down
32 changes: 16 additions & 16 deletions R/biomod2_classes_4.R
Original file line number Diff line number Diff line change
Expand Up @@ -791,20 +791,20 @@ setMethod('predict2', signature(object = 'MAXENT_biomod2_model', newdata = "Spat
}

# checking maxent.jar is present
path_to_maxent.jar <- file.path(object@model_options@args.default$path_to_maxent.jar, "maxent.jar")
path_to_maxent.jar <- file.path(object@model_options$path_to_maxent.jar, "maxent.jar")
if (!file.exists(path_to_maxent.jar)) {
path_to_maxent.jar <- file.path(getwd(), "maxent.jar")
}


maxent.command <-
paste0("java ",
ifelse(is.null(object@model_options@args.default$memory_allocated), "",
paste0("-mx", object@model_options@args.default$memory_allocated, "m")),
ifelse(is.null(object@model_options@args.default$initial_heap_size), "",
paste0(" -Xms", object@model_options@args.default$initial_heap_size)),
ifelse(is.null(object@model_options@args.default$max_heap_size), "",
paste0(" -Xmx", object@model_options@args.default$max_heap_size)),
ifelse(is.null(object@model_options$memory_allocated), "",
paste0("-mx", object@model_options$memory_allocated, "m")),
ifelse(is.null(object@model_options$initial_heap_size), "",
paste0(" -Xms", object@model_options$initial_heap_size)),
ifelse(is.null(object@model_options$max_heap_size), "",
paste0(" -Xmx", object@model_options$max_heap_size)),
" -cp ", "\"", path_to_maxent.jar, "\"",
" density.Project ",
"\"", list.files(path = object@model_output_dir, pattern = ".lambdas$", full.names = TRUE), "\" ",
Expand Down Expand Up @@ -891,20 +891,20 @@ setMethod('predict2', signature(object = 'MAXENT_biomod2_model', newdata = "data
write.table(Pred_swd, file = m_predictFile, quote = FALSE, row.names = FALSE, col.names = TRUE, sep = ",")

# checking maxent.jar is present
path_to_maxent.jar <- file.path(object@model_options@args.default$path_to_maxent.jar, "maxent.jar")
path_to_maxent.jar <- file.path(object@model_options$path_to_maxent.jar, "maxent.jar")
if (!file.exists(path_to_maxent.jar)) {
path_to_maxent.jar <- file.path(getwd(), "maxent.jar")
}

# cat("\n\t\tRunning Maxent...")
maxent.command <-
paste0("java ",
ifelse(is.null(object@model_options@args.default$memory_allocated), "",
paste0("-mx", object@model_options@args.default$memory_allocated, "m")),
ifelse(is.null(object@model_options@args.default$initial_heap_size), "",
paste0(" -Xms", object@model_options@args.default$initial_heap_size)),
ifelse(is.null(object@model_options@args.default$max_heap_size), "",
paste0(" -Xmx", object@model_options@args.default$max_heap_size)),
ifelse(is.null(object@model_options$memory_allocated), "",
paste0("-mx", object@model_options$memory_allocated, "m")),
ifelse(is.null(object@model_options$initial_heap_size), "",
paste0(" -Xms", object@model_options$initial_heap_size)),
ifelse(is.null(object@model_options$max_heap_size), "",
paste0(" -Xmx", object@model_options$max_heap_size)),
" -cp ", "\"", path_to_maxent.jar, "\"",
" density.Project ",
"\"", list.files(path = object@model_output_dir, pattern = ".lambdas$", full.names = TRUE), "\" ",
Expand Down Expand Up @@ -1053,7 +1053,7 @@ setClass('RF_biomod2_model',

setMethod('predict2', signature(object = 'RF_biomod2_model', newdata = "SpatRaster"),
function(object, newdata, ...) {
if (!is.null(object@model_options@args.default$type) && object@model_options@args.default$type == "classification") {
if (!is.null(object@model_options$type) && object@model_options$type == "classification") {
predfun <- function(object, newdata, mod.name){
# new predict command used with terra
subset(predict(newdata, model = get_formal_model(object),
Expand Down Expand Up @@ -1083,7 +1083,7 @@ setMethod('predict2', signature(object = 'RF_biomod2_model', newdata = "SpatRast
##' @rdname predict2.bm
setMethod('predict2', signature(object = 'RF_biomod2_model', newdata = "data.frame"),
function(object, newdata, ...) {
if (!is.null(object@model_options@args.default$type) && object@model_options@args.default$type == "classification") {
if (!is.null(object@model_options$type) && object@model_options$type == "classification") {
predfun <- function(object, newdata, not_na_rows) {
as.numeric(predict(get_formal_model(object), as.data.frame(newdata[not_na_rows, , drop = FALSE]), type = 'prob')[, '1'])
}
Expand Down
Binary file modified docs/articles/pictures/SCHEMA_BIOMOD2_WORKFLOW_functions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/pictures/SCHEMA_BIOMOD2_WORKFLOW_functions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 192e6a4

Please sign in to comment.