Skip to content

Commit

Permalink
Wrap check
Browse files Browse the repository at this point in the history
  • Loading branch information
HeleneBlt committed Feb 7, 2025
1 parent 96751f9 commit a3c8921
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 200 deletions.
60 changes: 35 additions & 25 deletions R/BIOMOD_ProjectionWrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
##'
##'
##' @param bm.wrap a \code{\link{BIOMOD.wrap.out}} object returned by the
##' \code{\link{BIOMOD_Modeling}} function
##' \code{\link{BIOMOD_Wrap}} function
##' @param proj.name a \code{character} corresponding to the name (ID) of the projection set
##' (\emph{a new folder will be created within the simulation folder with this name})
##' @param new.env A \code{matrix}, \code{data.frame} or
Expand Down Expand Up @@ -126,7 +126,7 @@
##' unlink('GuloGulo', recursive = TRUE)
##' }
##'
##' @importFrom biomod2 BIOMOD_Projection BIOMOD_EnsembleForecasting
##' @importFrom biomod2 BIOMOD_Projection BIOMOD_EnsembleForecasting get_evaluations get_formal_data
##' @export
##'
##'
Expand Down Expand Up @@ -171,33 +171,43 @@ BIOMOD_ProjectionWrap <- function(bm.wrap,


# output <- capture.output(
proj_single <- BIOMOD_Projection(bm.wrap@single.models,
proj.name = proj.name,
new.env = new.env,
new.env.xy = new.env.xy,
models.chosen = models.chosen.single,
metric.binary = metric.binary,
metric.filter = metric.filter,
compress = compress,
build.clamping.mask = build.clamping.mask,
nb.cpu = nb.cpu,
digits = digits)
proj_single <- BIOMOD_Projection(bm.wrap@single.models,
proj.name = proj.name,
new.env = new.env,
new.env.xy = new.env.xy,
models.chosen = models.chosen.single,
metric.binary = metric.binary,
metric.filter = metric.filter,
compress = compress,
build.clamping.mask = build.clamping.mask,
nb.cpu = nb.cpu,
digits = digits,
seed.val = seed.val,
omit.na = omit.na,
on_0_1000 = on_0_1000,
do.stack = do.stack,
keep.in.memory = keep.in.memory,
output.format = output.format,
overwrite = overwrite)
# )

cat("\n\t Projection of ensemble models")

# output <- capture.output(
proj_ens <- BIOMOD_EnsembleForecasting(bm.wrap@ensemble.models,
proj.name = proj.name,
new.env = new.env,
new.env.xy = new.env.xy,
models.chosen = models.chosen.ens,
metric.binary = metric.binary,
metric.filter = metric.filter,
compress = compress,
build.clamping.mask = build.clamping.mask,
nb.cpu = nb.cpu,
digits = digits)
proj_ens <- BIOMOD_EnsembleForecasting(bm.wrap@ensemble.models,
proj.name = proj.name,
new.env = new.env,
new.env.xy = new.env.xy,
models.chosen = models.chosen.ens,
metric.binary = metric.binary,
metric.filter = metric.filter,
compress = compress,
nb.cpu = nb.cpu,
digits = digits,
on_0_1000 = on_0_1000,
do.stack = do.stack,
keep.in.memory = keep.in.memory,
output.format = output.format)
# )


Expand Down Expand Up @@ -300,7 +310,7 @@ BIOMOD_ProjectionWrap <- function(bm.wrap,
metric.binary <- NULL
metric.filter <- NULL
} else {
models.evaluation <- get_evaluations(bm.wrap@single.models)
models.evaluation <- biomod2::get_evaluations(bm.wrap@single.models)
if (is.null(models.evaluation)) {
warning("Binary and/or Filtered transformations of projection not ran because of models evaluation information missing")
} else {
Expand Down
195 changes: 23 additions & 172 deletions R/BIOMOD_Wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
##'
##' @return
##'
##' A \code{\link{MS.models.out}} object acting as a proxi for the created \code{BIOMOD.models.out}.
##' A \code{\link{BIOMOD.wrap.out}} object containing a \code{BIOMOD.formated.data}, \code{BIOMOD.models.out},
##' and a \code{BIOMOD.ensemble.models.out}.
##'
##' @examples
##' library(terra)
Expand Down Expand Up @@ -289,132 +290,19 @@ BIOMOD_Wrap <- function(dir.name = ".",
do.progress = FALSE)
# )

## laisser les messages ?!?! Rajouter quelques messages ?
# save sur un fichier texte et donner le lien
## Possibility to create a new slot with a link for a text file with the ouput
## And supress the output in this case.

cat("\n\t > Creating wrap object") #?
wrap <- new("BIOMOD.wrap.out",
formated.data = formated.data,
single.models = single.models,
ensemble.models = em.models,
output = "link")
.bm_cat("Done") #to remove ?
ensemble.models = em.models)
.bm_cat("Done")

return(wrap)
}

# #======================================================================================================
#
# ## BIOMOD_Wrap for ms
#
# setMethod("BIOMOD_Wrap", signature(ms.project.name = "character"), function(ms.project.name,
# dir.name = ".",
# modeling.id = as.character(format(Sys.time(), "%s")),
# data.type = "binary",
# resp.name,
# resp.var,
# resp.xy = NULL,
# expl.var,
# eval.resp.var = NULL,
# eval.resp.xy = NULL,
# eval.expl.var = NULL,
# filter.raster = FALSE,
# params.PA,
# models,
# models.pa = NULL,
# metric.eval = c("KAPPA", "TSS", "ROC"),
# weights = NULL,
# prevalence = NULL,
# scale.models = FALSE,
# var.import = 0,
# params.CV,
# params.OPT,
# em.algo,
# params.EM,
# seed.val = NULL,
# nb.cpu = 1){
#
# ## 0. Check arguments ---------------------------------------------------------------------------
# args <- .BIOMOD_Wrap.check.args(dir.name = dir.name,
# modeling.id = modeling.id,
# data.type = data.type,
# resp.name = resp.name,
# resp.var = resp.var,
# resp.xy = resp.xy,
# expl.var = expl.var,
# eval.resp.var = eval.resp.var,
# eval.resp.xy = eval.resp.xy,
# eval.expl.var = eval.expl.var,
# filter.raster = filter.raster,
# params.PA = params.PA,
# models = models,
# models.pa = models.pa,
# metric.eval = metric.eval,
# weights = weights,
# prevalence = prevalence,
# var.import = var.import,
# params.CV = params.CV,
# params.OPT = params.OPT,
# em.algo = em.algo,
# params.EM = params.EM,
# seed.val = seed.val,
# nb.cpu = nb.cpu
# )
# for (argi in names(args)) { assign(x = argi, value = args[[argi]]) }
# rm(args)
#
# formated.data <- MS_FormatingData(ms.project.name = ms.project.name,
# dir.name = dir.name,
# resp.name = resp.name,
# resp.var = resp.var,
# expl.var = expl.var,
# data.type = data.type,
# resp.xy = resp.xy,
# eval.resp.var = eval.resp.var,
# eval.expl.var = eval.expl.var,
# eval.resp.xy = eval.resp.xy,
# params = params.PA,
# #single.formated.data = NULL,
# #ms.formated.data = NULL,
# filter.raster = filter.raster,
# seed.val = NULL)
#
#
# single.models <- MS_Modeling(formated.data,
# modeling.id = modeling.id,
# models = models,
# params.CV = params.CV,
# params.OPT = params.OPT,
# weights = weights,
# prevalence = prevalence,
# metric.eval = metric.eval,
# var.import = var.import,
# scale.models = FALSE,
# nb.cpu = nb.cpu,
# seed.val = NULL)
#
# ### petit gros problème
# params.EM <- .inversion_species_params(params.EM)
# em.models <- MS_EnsembleModeling(single.models,
# models.chosen = params.EM$models.chosen,
# em.by = params.EM$em.by,
# em.algo = em.algo,
# metric.select = params.EM$metric.select,
# metric.select.thresh = params.EM$metric.select.thresh,
# metric.select.table = params.EM$metric.select.table,
# metric.select.dataset = params.EM$metric.select.dataset,
# metric.eval = metric.eval,
# var.import = var.import,
# EMci.alpha = params.EM$EMci.alpha,
# EMwmean.decay = params.EM$EMwmean.decay,
# nb.cpu = nb.cpu,
# seed.val = NULL)
#
# return(list("formated.data" = formated.data,
# "single.models" = single.models,
# "ensemble.models" = em.models))
# })


# ---------------------------------------------------------------------------- #

Expand Down Expand Up @@ -450,16 +338,11 @@ BIOMOD_Wrap <- function(dir.name = ".",
if (!is.character(modeling.id) || length(modeling.id) > 1) { stop("modeling.id must be a 'character' of length 1") }

## 2. Check params.PA
if (length(resp.name) == 1){
params.PA <- check.params.PA(params.PA)
} else {
if (missing(params.PA)){
params.PA <- list()
}
for (sp in resp.name){
params.PA[[sp]] <- check.params.PA(params.PA[[sp]])
}
if (missing(params.PA)){
params.PA <- list()
}
params.PA <- check.params.PA(params.PA)


## 3. Check modeling parameters

Expand Down Expand Up @@ -525,40 +408,24 @@ BIOMOD_Wrap <- function(dir.name = ".",


## 4. Check params.CV
if (length(resp.name) == 1){
params.CV <- check.params.CV(params.CV)
} else {
if (missing(params.CV)){
params.CV <- list()
}
for (sp in resp.name){
params.CV[[sp]] <- check.params.CV(params.CV[[sp]])
}
if (missing(params.CV)){
params.CV <- list()
}
params.CV <- check.params.CV(params.CV)


## 5. Check params.OPT
if (length(resp.name) == 1){
params.OPT <- check.params.OPT(params.OPT)
} else {
if (missing(params.OPT)){
params.OPT <- list()
}
for (sp in resp.name){
params.OPT[[sp]] <- check.params.OPT(params.OPT[[sp]])
}
if (missing(params.OPT)){
params.OPT <- list()
}
params.OPT <- check.params.OPT(params.OPT)


## 6. Check params.EM
if (length(resp.name) == 1){
params.EM <- check.params.EM(params.EM)
} else {
if (missing(params.EM)){
params.EM <- list()
}
for (sp in resp.name){
params.EM[[sp]] <- check.params.EM(params.EM[[sp]])
}
}
if (missing(params.EM)){
params.EM <- list()
}
params.EM <- check.params.EM(params.EM)


## 7. Check general parameters
Expand Down Expand Up @@ -720,20 +587,4 @@ check.params.EM <- function(params.EM){
}


.inversion_species_params <- function(liste){
names_liste <- names(liste)
names_arguments <- names(unlist(liste))
for (n in names_liste){
names_arguments <- sub(paste0(n,"."), "",names_arguments)
}
names_arguments <- unique(names_arguments)
new <- list()
for (a in names_arguments){
list_a <- list()
for (l in names_liste){
list_a[[l]] <- liste[[l]][[a]]
}
new[[a]] <- list_a
}
return(new)
}

4 changes: 1 addition & 3 deletions R/biomod3_class5_Wrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
##' @slot formated.data a \code{BIOMOD.formated.data} object
##' @slot single.models a \code{BIOMOD.models.out} object
##' @slot ensemble.models a \code{BIOMOD.ensemble.models.out} object
##' @slot output a \code{character} link to the output file
##'
##' @param object a \code{\link{BIOMOD.wrap.out}} object
##'
Expand All @@ -29,8 +28,7 @@
setClass("BIOMOD.wrap.out",
representation(formated.data = "BIOMOD.formated.data",
single.models = "BIOMOD.models.out",
ensemble.models = "BIOMOD.ensemble.models.out",
output = "character"),
ensemble.models = "BIOMOD.ensemble.models.out"),
validity = function(object){ return(TRUE) })


Expand Down
29 changes: 29 additions & 0 deletions R/bm_SpeciesParameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,35 @@ bm_SpeciesParameters <- function(resp.name,

.fun_testIfIn(TRUE, "em.by", em.by, em.by.avail)

metric.select <- unique(metric.select)
avail.eval.meth.list <- c('TSS', 'KAPPA', 'ACCURACY', 'BIAS', 'POD', 'FAR', 'POFD'
, 'SR', 'CSI', 'ETS', 'HK', 'HSS', 'OR', 'ORSS', 'ROC'
, 'BOYCE', 'MPA',
"Accuracy", "Recall", "Precision", "F1",
'RMSE','MSE',"MAE","Rsquared","Rsquared_aj","Max_error")

.fun_testIfIn(TRUE, "metric.select", metric.select, avail.eval.meth.list)

if (!is.null(metric.select.dataset)) {
metric.select.dataset.available <- c("calibration", "validation", "evaluation")
.fun_testIfIn(TRUE, "metric.select.dataset", metric.select.dataset, metric.select.dataset.available)
}

.fun_testIfPosNum(TRUE, "EMci.alpha", EMci.alpha)
if (EMci.alpha <= 0 | EMci.alpha >= 0.5) {
stop("EMci.alpha must be a numeric between 0 and 0.5")
}

if ((!is.numeric(EMwmean.decay) &&
!is.character(EMwmean.decay) &&
!is.function(EMwmean.decay)) ||
(is.numeric(EMwmean.decay) && EMwmean.decay < 0) ||
(is.character(EMwmean.decay) && EMwmean.decay != 'proportional')) {
stop("'EMwmean.decay' should be either 'proportional', a numeric value > 0 or a function")
}



params.EM <- list("models.chosen" = models.chosen, "em.by" = em.by,
"metric.select" = metric.select, "metric.select.thresh" = metric.select.thresh,
"metric.select.table" = metric.select.table, "metric.select.dataset" = metric.select.dataset,
Expand Down

0 comments on commit a3c8921

Please sign in to comment.