Skip to content

Commit

Permalink
unit tests and warning check corrections #121
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed Aug 3, 2024
1 parent b2ed538 commit d0af641
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions R/add_predictors.R
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ methods::setMethod(

# Get model object
obj <- x$get_model()
assertthat::assert_that(!(is.null(obj) || is.Waiver(obj)),
msg = "No model object found in scenario?")
model <- obj$model

# Subset to target predictors only
Expand Down
8 changes: 8 additions & 0 deletions R/class-biodiversityscenario.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ BiodiversityScenario <- R6::R6Class(
return(self$limits)
},

#' @description
#' Remove current limits.
#' @return Invisible
rm_limits = function(){
self$limits <- new_waiver()
invisible()
},

#' @description
#' Get names of predictors for scenario object.
#' @return A [`character`] vector with the names.
Expand Down
2 changes: 1 addition & 1 deletion R/project.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ methods::setMethod(
}
if(getOption('ibis.setupmessages', default = TRUE)) myLog('[Scenario]','green', "Found latent factors and added them to future predictors stack.")
}
try({rm(pn,ind,sps)},silent = TRUE)
suppressWarnings( try({rm(pn,ind,sps)},silent = TRUE) )
}

# Get limits if flagged as present in scenario object
Expand Down
21 changes: 18 additions & 3 deletions tests/testthat/test_Scenarios.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,19 @@ test_that('Scenarios and constraints', {
expect_s3_class(sc$get_model(),"DistributionModel")# Model correctly inherited?
expect_equal(sc$modelid, fit$model$id)

# Add limits
sc <- scenario(fit, limits = classify(pred_current$crops, c(0, 100, 300, 500)))
expect_s3_class(sc$get_limits(), "sf")
sc$rm_limits()
expect_null(sc$get_limits())

# Add covariates in various transformations
sc <- scenario(fit)
x <- sc |> add_predictors(pred_future, transform = "none")
expect_length(x$get_predictor_names(), 9)
x <- sc |> add_predictors(pred_future, transform = "scale")
suppressWarnings(
x <- sc |> add_predictors(pred_future, transform = "scale")
)
expect_length(x$get_predictor_names(), 9)
# Error as we used a different transform earlier
expect_error( x <- sc |> add_predictors(pred_future, transform = "norm") )
Expand Down Expand Up @@ -264,7 +273,11 @@ test_that('Scenarios and constraints', {
# Apply some transformations
expect_error( x <- sc |> add_predictors(obj, transform = "norm") )
# This uses the correct transformation
expect_no_error( x <- sc |> add_predictors(obj, transform = "scale") )
expect_no_error(
suppressWarnings(
x <- sc |> add_predictors(obj, transform = "scale")
)
)
expect_length(x$get_predictor_names(), 9)

# Predict
Expand All @@ -277,7 +290,9 @@ test_that('Scenarios and constraints', {
expect_no_error(
mod <- sc |> add_predictors(pred_future) |> project()
)
suppressWarnings( expect_s3_class(summary(mod), "data.frame") )
suppressPackageStartupMessages(
suppressWarnings( expect_s3_class(summary(mod), "data.frame") )
)
invisible(
suppressWarnings( expect_s3_class(mod$calc_scenarios_slope(plot = FALSE), "stars") )
)
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test_trainOtherEngines.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@ test_that('Train a distribution model with INLABRU', {
x <- distribution(background) |>
add_biodiversity_poipo(virtual_points, field_occurrence = 'Observed', name = 'Virtual points') |>
add_predictors(predictors, transform = 'none',derivates = 'none') |>
engine_inlabru() |>
add_latent_spatial()
engine_inlabru()

# Train the model
suppressWarnings(
Expand Down

0 comments on commit d0af641

Please sign in to comment.