Skip to content

Commit 86ac1d3

Browse files
committed
Small bug fix for manually supplied thresholds to scenarios
1 parent f95ad19 commit 86ac1d3

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Minor improvements and bug fixes
88
* IIASA internal functionalities such as preparation of GLOBIOM data have been transferred to [BNRTools](https://github.com/iiasa/BNRTools)
9+
* Small bug fixed related to manual provision of scenario thresholds.
910

1011
# ibis.iSDM 0.1.5
1112

R/class-distributionmodel.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ DistributionModel <- R6::R6Class(
271271
colNA = NA, col = ibis_colours[['sdm_colour']]
272272
)
273273
} else {
274-
message(
275-
paste0('No model predictions found.')
274+
cli::cli_alert_warning(
275+
paste0('No model predictions found in object.')
276276
)
277277
}
278278
},
@@ -309,7 +309,7 @@ DistributionModel <- R6::R6Class(
309309
colNA = NA, col = col
310310
)
311311
} else {
312-
message("No computed threshold was found!")
312+
cli::cli_alert_warning("No computed threshold was found!")
313313
invisible(self)
314314
}
315315
},
@@ -381,6 +381,7 @@ DistributionModel <- R6::R6Class(
381381
assertthat::assert_that(is.character(what))
382382
# Get model
383383
obj <- self$get_data(x)
384+
cli::cli_inform('Calculating partial dependence plots...')
384385
if( self$get_name() == 'GDB-Model'){
385386
# How many effects
386387
n <- length( stats::coef( obj ))
@@ -412,7 +413,6 @@ DistributionModel <- R6::R6Class(
412413
ggplot2::ggplot() +
413414
inlabru::gg(obj$summary.fixed, bar = TRUE)
414415
} else if( self$get_name() == 'BART-Model'){
415-
message('Calculating partial dependence plots')
416416
self$partial(obj, x.var = what, ...)
417417
} else if( self$get_name() == 'BREG-Model'){
418418
if(what == "fixed") what <- "coefficients"

R/project.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ methods::setMethod(
140140
}
141141
}
142142

143-
144143
new_crs <- new_preds$get_projection()
145144
if(is.na(new_crs)) if(getOption('ibis.setupmessages', default = TRUE)) myLog('[Scenario]','yellow','Missing projection of future predictors.')
146145

@@ -254,12 +253,22 @@ methods::setMethod(
254253
# Get constraints, threshold values and other parameters
255254
scenario_threshold <- mod$get_threshold()
256255
if(!is.Waiver(scenario_threshold)){
257-
# Not get the baseline raster
258-
thresh_reference <- grep('threshold',fit$show_rasters(),value = T)[1] # Use the first one always
259-
assertthat::assert_that(!is.na(thresh_reference))
260-
baseline_threshold <- mod$get_model()$get_data(thresh_reference)
261-
256+
# If scenario threshold is numeric and not a raster, create a baseline
257+
if(is.numeric(scenario_threshold) && !is.Raster(scenario_threshold)){
258+
baseline_threshold <- try({
259+
# Get prediction and threshold
260+
threshold(fit$get_data(),method = 'fixed', value = scenario_threshold)
261+
},silent = TRUE)
262+
if(inherits(baseline_threshold, "try-error")) cli::cli_alert_danger("Set thresholds require a prediction first!")
263+
} else {
264+
# Assume an existing threshold exists
265+
thresh_reference <- grep('threshold',fit$show_rasters(),value = T)[1] # Use the first one always
266+
assertthat::assert_that(!is.na(thresh_reference))
267+
baseline_threshold <- fit$get_data(thresh_reference)
268+
}
269+
# Correct CRS just in case
262270
if(is.na(terra::crs(baseline_threshold))) terra::crs(baseline_threshold) <- terra::crs( background )
271+
263272
# Furthermore apply new limits also to existing predictions (again)
264273
if(!is.null( mod$get_limits() )){
265274
# Get Limit and settings from model
@@ -286,7 +295,6 @@ methods::setMethod(
286295
baseline_threshold <- terra::extend(baseline_threshold, template)
287296
baseline_threshold <- terra::crop(baseline_threshold, template)
288297
}
289-
290298
} else {
291299
baseline_threshold <- new_waiver()
292300
}

R/threshold.R

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ methods::setMethod(
297297
# Specify by type:
298298
if(method == "fixed"){
299299
# Fixed threshold. Confirm to be set
300-
assertthat::assert_that(is.numeric(value), msg = 'Fixed value is missing!')
300+
assertthat::assert_that(is.numeric(value), msg = 'For method Fixed, a constant value needs to be supplied!')
301301
tr <- value
302302
} else if(method == "mtp"){
303303
assertthat::assert_that(!is.null(poi_pres),msg = "Threshold method requires supplied point data!")
@@ -431,8 +431,8 @@ methods::setMethod(
431431
#'
432432
#' @param obj A [BiodiversityScenario] object to which an existing threshold is
433433
#' to be added.
434-
#' @param value A [`numeric`] value specifying the specific threshold for scenarios
435-
#' (Default: \code{NULL} Grab from object).
434+
#' @param value A [`numeric`] value specifying
435+
#' the specific threshold for scenarios (Default: \code{NULL} grabs the value from \code{obj}).
436436
#' @param ... Any other parameter. Used to fetch value if set somehow.
437437
#'
438438
#' @rdname threshold
@@ -450,9 +450,6 @@ methods::setMethod(
450450
msg = "Parameter value not found and other numeric values not found?")
451451
}
452452

453-
# Assert that predicted raster is present
454-
assertthat::assert_that( is.Raster(obj$get_model()$get_data('prediction')) )
455-
456453
# Make a clone copy of the object
457454
new <- obj$clone(deep = TRUE)
458455

man/ibis_set_threads.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/threshold.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)