Skip to content

Commit

Permalink
Fixing previous 🐛 introduced through controls revamping
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed Dec 20, 2023
1 parent b9acffd commit 1d073d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
10 changes: 4 additions & 6 deletions R/bdproto-biodiversitydistribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BiodiversityDistribution <- bdproto(
pio <- ifelse(is.Waiver(self$priors),
'<Default>', paste0('Priors specified (',self$priors$length(), ')') )
bv <- ifelse(is.Waiver(self$control), '',
paste0( "\n control: <", name_atomic(
paste0( "\n control: <", name_atomic(
paste0( self$control$type, " - ", self$control$method)
), ">" ) )
li <- ifelse(is.Waiver(self$limits), '',
Expand Down Expand Up @@ -247,7 +247,7 @@ BiodiversityDistribution <- bdproto(
assertthat::assert_that(missing(x) || is.Raster(x),
all(is.numeric(value)))
# Check type of control
type <- match.arg(type, c("bias", "extrapolation"), several.ok = FALSE)
type <- match.arg(type, c("bias"), several.ok = FALSE)
if(type == "bias"){
if(missing(x)) {
assertthat::assert_that(method == "proximity",
Expand All @@ -256,17 +256,15 @@ BiodiversityDistribution <- bdproto(
}
bdproto(NULL, self, control = list(type = type, layer = x,
method = method, bias_value = value) )
} else if(type == "extrapolation"){
bdproto(NULL, self, control = list(type = type, layer = x, method = method, value = value) )
}
},
# Get bias control (print name)
get_control = function(self, type = "bias"){
# Check type of control
type <- match.arg(type, c("bias", "extrapolation"), several.ok = FALSE)
type <- match.arg(type, c("bias"), several.ok = FALSE)
control <- self$control
if(is.Waiver(control)) return( control )
if(control$type == "bias" && type == "bias") names( control )
if(control$type == "bias" && type == "bias") return( control )
},
# Remove bias controls
rm_control = function(self){
Expand Down
2 changes: 1 addition & 1 deletion man/ensemble.Rd

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

18 changes: 17 additions & 1 deletion tests/testthat/test_controls.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ---- #
# Train a full distribution model with glm base model
test_that('Test controls', {
test_that('Test controls and limits', {

skip_on_travis()
skip_on_cran()
Expand Down Expand Up @@ -31,6 +31,7 @@ test_that('Test controls', {
zones <- terra::as.factor( predictors$koeppen_50km )
y <- x |> add_control_extrapolation(layer = zones, method = "zones")
expect_false(y$get_limits() |> is.Waiver())
expect_length(y$get_limits(), 4) # Those are saved in limits in the object

# Add mcp limits
y <- x |> add_control_extrapolation(method = "mcp")
Expand Down Expand Up @@ -72,4 +73,19 @@ test_that('Test controls', {
# Create a scenario object and reuse limits
expect_no_error( scenario(mod, reuse_limits = TRUE) )

# --- #
# Bias control checks
x <- x$rm_limits()
x <- x |> add_control_bias(layer = predictors$hmi_mean_50km)
# Train with bias
expect_no_error(
suppressWarnings(
mod <- train(x |> engine_glm(), "test", inference_only = FALSE, only_linear = TRUE,
varsel = "none", verbose = FALSE)
)
)
expect_length( x$get_control(), 4 )
settings <- mod$settings
expect_equal(settings$get("bias_variable"), "hmi_mean_50km")
})

0 comments on commit 1d073d1

Please sign in to comment.