Skip to content

Commit

Permalink
back compatibility for get_predictions with argument model.as.col
Browse files Browse the repository at this point in the history
export function .transform_model.as.col for ecospat
  • Loading branch information
rpatin committed Jan 16, 2023
1 parent 014d1c8 commit 954e33b
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 46 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(.transform.outputs.array)
export(.transform.outputs.list)
export(.transform_model.as.col)
export(BIOMOD_ConvertOldRun)
export(BIOMOD_CrossValidation)
export(BIOMOD_EnsembleForecasting)
Expand Down
27 changes: 6 additions & 21 deletions R/BIOMOD_RangeSize.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@
##' metric.binary = 'TSS')
##'
##' # Load current and future binary projections
##' CurrentProj <- get_predictions(myBiomodProj, metric.binary = "TSS")
##' FutureProj <- get_predictions(myBiomodProjectionFuture, metric.binary = "TSS")
##'
##' CurrentProj <- get_predictions(myBiomodProj,
##' metric.binary = "TSS",
##' model.as.col = TRUE)
##' FutureProj <- get_predictions(myBiomodProjectionFuture,
##' metric.binary = "TSS",
##' model.as.col = TRUE)
##' # Compute differences
##' myBiomodRangeSize <- BIOMOD_RangeSize(proj.current = CurrentProj, proj.future = FutureProj)
##'
Expand Down Expand Up @@ -301,17 +304,6 @@ setMethod('BIOMOD_RangeSize', signature(proj.current = 'SpatRaster', proj.future

.BIOMOD_RangeSize.check.args <- function(proj.current, proj.future) {

## Transformation of data.frame so that each column is a model
if (inherits(proj.current, "data.frame")) {
if (all(c("full.name","points","pred") %in% colnames(proj.current)) &&
all(c("full.name","points","pred") %in% colnames(proj.future))) {
proj.current <- .transform_df(proj.current)
proj.future <- .transform_df(proj.future)
} else {
stop("'proj.current' and 'proj.future' must have all columns 'full.name' (model name), 'points' (index of prediction) and 'pred' (binary prediction).")
}
}

## dimensions checking ------------------------
if (inherits(proj.current, "data.frame")) {
dim_current <- nrow(proj.current)
Expand Down Expand Up @@ -392,10 +384,3 @@ setMethod('BIOMOD_RangeSize', signature(proj.current = 'SpatRaster', proj.future
}
}

# transform data.frame from long to wide for BIOMOD_RangeSize
##' @importFrom stats reshape
.transform_df <- function(df){
df <- reshape(df[,c("full.name","points","pred")], idvar = "points", timevar = "full.name", direction = "wide")[,-1, drop = FALSE]
colnames(df) <- substring(colnames(df), 6)
df
}
27 changes: 24 additions & 3 deletions R/biomod2_classes_3.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
##' \code{TSS}, \code{KAPPA}, \code{ACCURACY}, \code{BIAS}, \code{POD}, \code{FAR},
##' \code{POFD}, \code{SR}, \code{CSI}, \code{ETS}, \code{HK}, \code{HSS}, \code{OR}, \code{ORSS}
##'
##' @param model.as.col (\emph{optional, default} \code{FALSE}) \cr
##' A \code{boolean} given to \code{\link{get_predictions}}. If \code{TRUE}
##' prediction are returned as a wide \code{data.frame} with each column containing
##' predictions for a single model and corresponding to the old output given by
##' \pkg{biomod2} in version < 4.2-2. If \code{FALSE} predictions are returned
##' as a long \code{data.frame} with many additional informations readily
##' available.
##'
##'
##' @return
##'
Expand Down Expand Up @@ -468,7 +476,8 @@ setMethod("get_formal_data", "BIOMOD.models.out",

setMethod("get_predictions", "BIOMOD.models.out",
function(obj, evaluation = FALSE
, full.name = NULL, PA = NULL, run = NULL, algo = NULL)
, full.name = NULL, PA = NULL, run = NULL, algo = NULL,
model.as.col = FALSE)
{
if (evaluation && (!obj@has.evaluation.data)) {
warning("! Calibration data returned because no evaluation data available")
Expand All @@ -486,6 +495,9 @@ setMethod("get_predictions", "BIOMOD.models.out",
keep_lines <- .filter_outputs.df(out, subset.list = list(full.name = full.name, PA = PA
, run = run, algo = algo))
out <- out[keep_lines, ]
if (model.as.col) {
out <- .transform_model.as.col(out)
}
return(out)
}
)
Expand Down Expand Up @@ -971,7 +983,8 @@ setMethod("get_predictions", "BIOMOD.projection.out",
function(obj, metric.binary = NULL, metric.filter = NULL
, full.name = NULL, PA = NULL, run = NULL, algo = NULL
, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, ...) {
, merged.by.PA = NULL, filtered.by = NULL,
model.as.col = FALSE, ...) {

# extract layers from [email protected]@link concerned by metric.filter
# or metric.binary
Expand Down Expand Up @@ -1009,7 +1022,11 @@ setMethod("get_predictions", "BIOMOD.projection.out",
, run = run, algo = algo))
}
out <- out[keep_lines, ]
if (model.as.col) {
out <- .transform_model.as.col(out)
}
}

return(out)
}
)
Expand Down Expand Up @@ -1256,7 +1273,8 @@ setMethod("get_kept_models", "BIOMOD.ensemble.models.out", function(obj) { retur

setMethod("get_predictions", "BIOMOD.ensemble.models.out",
function(obj, evaluation = FALSE, full.name = NULL, merged.by.algo = NULL, merged.by.run = NULL
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL)
, merged.by.PA = NULL, filtered.by = NULL, algo = NULL,
model.as.col = FALSE)
{
# check evaluation data availability
if (evaluation && (!get_formal_data(obj)@has.evaluation.data)) {
Expand All @@ -1279,6 +1297,9 @@ setMethod("get_predictions", "BIOMOD.ensemble.models.out",
, filtered.by = filtered.by
, algo = algo))
out <- out[keep_lines, ]
if (model.as.col) {
out <- .transform_model.as.col(out)
}
return(out)
}
)
Expand Down
29 changes: 28 additions & 1 deletion R/biomod2_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ get_var_range <- function(data)
}
proj
}
## TRANSFORM models outputs getting specific slot -------------------------------------------------
## TRANSFORM models outputs getting specific slot --------------------
## used in BIOMOD_Modeling.R, BIOMOD_EnsembleModeling.R

.transform_outputs_list = function(obj.type, obj.out, out = 'evaluation')
Expand Down Expand Up @@ -430,6 +430,33 @@ get_var_range <- function(data)
return(output)
}

## transform predictions data.frame from long to wide with models as columns ----------------
## used in BIOMOD_RangeSize.R
##' @name .transform_model.as.col
##' @author Remi Patin
##'
##' @title Transform predictions data.frame from long to wide with models as columns
##'
##' @description This function is used internally in \code{\link{get_predictions}}
##' to ensure back-compatibility with former output of \code{\link{get_predictions}}
##' (i.e. for \pkg{biomod2} version < 4.2-2). It transform a long \code{data.frame}
##' into a wide \code{data.frame} with each column corresponding to a single model.
##'
##' \emph{Note that the function is intended for internal use but have been
##' made available for compatibility with \pkg{ecospat}}
##'
##' @param df a long \code{data.frame}, generally a standard output of
##' \code{\link{get_predictions}}
##'
##' @return a wide \code{data.frame}
##' @importFrom stats reshape
##' @export
##' @keywords internal
.transform_model.as.col <- function(df){
df <- reshape(df[,c("full.name","points","pred")], idvar = "points", timevar = "full.name", direction = "wide")[,-1, drop = FALSE]
colnames(df) <- substring(colnames(df), 6)
df
}

## EXTRACT model names according to specific infos ------------------------------------------------
## used in biomod2_classes_3.R, BIOMOD_LoadModels, BIOMOD_Projection, BIOMOD_EnsembleModeling, bm_PlotRangeSize
Expand Down
8 changes: 6 additions & 2 deletions R/bm_PlotRangeSize.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@
##' metric.binary = 'TSS')
##'
##' # Load current and future binary projections
##' CurrentProj <- get_predictions(myBiomodProj, metric.binary = "TSS")
##' FutureProj <- get_predictions(myBiomodProjectionFuture, metric.binary = "TSS")
##' CurrentProj <- get_predictions(myBiomodProj,
##' metric.binary = "TSS",
##' model.as.col = TRUE)
##' FutureProj <- get_predictions(myBiomodProjectionFuture,
##' metric.binary = "TSS",
##' model.as.col = TRUE)
##'
##' # Compute differences
##' myBiomodRangeSize <- BIOMOD_RangeSize(proj.current = CurrentProj, proj.future = FutureProj)
Expand Down
14 changes: 11 additions & 3 deletions docs/articles/news.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions docs/reference/BIOMOD_RangeSize.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions docs/reference/bm_PlotRangeSize.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 954e33b

Please sign in to comment.