Skip to content

Commit

Permalink
Fix for object inheritance and class checks #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed Feb 26, 2024
1 parent 6508c7b commit 9eff17b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
6 changes: 3 additions & 3 deletions R/add_biodiversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ methods::setMethod(

# Finally set the data to the BiodiversityDistribution object
y <- x$clone(deep = TRUE)
x$set_biodiversity(id, bd)
y$set_biodiversity(id, bd)
}
)

Expand Down Expand Up @@ -475,7 +475,7 @@ methods::setMethod(

# Finally set the data to the BiodiversityDistribution object
y <- x$clone(deep = TRUE)
x$set_biodiversity(id, bd)
y$set_biodiversity(id, bd)
}
}
)
Expand Down Expand Up @@ -696,7 +696,7 @@ methods::setMethod(

# Finally set the data to the BiodiversityDistribution object
y <- x$clone(deep = TRUE)
x$set_biodiversity(id, bd)
y$set_biodiversity(id, bd)
}
}
)
Expand Down
53 changes: 25 additions & 28 deletions R/write_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ methods::setMethod(

# This function will only capture the distribution model object and will
# save them separately
if(any(class(mod) %in% getOption("ibis.engines")) ){
if(any(mod$get_name() %in% getOption("ibis.engines")) ){
# FIXME: If errors occur, check harmonization of saving among engines.
mod$save(fname = fname)
} else if(is.Raster(mod)){
Expand Down Expand Up @@ -374,7 +374,7 @@ methods::setMethod(
# Model parameters in a tibble
output[["params"]][["id"]] <- as.character(model$id)
output[["params"]][["runname"]] <- as.character(model$runname)
output[["params"]][["algorithm"]] <- class(mod)[1]
output[["params"]][["algorithm"]] <- mod$get_name()
output[["params"]][["equation"]] <- mod$get_equation()
# Collect settings and parameters if existing
if( !is.Waiver(mod$get_data("params")) ){
Expand Down Expand Up @@ -420,7 +420,7 @@ methods::setMethod(
# Model parameters in a tibble
output[["params"]][["id"]] <- as.character(mod$modelid)
output[["params"]][["runname"]] <- as.character(model$model$runname)
output[["params"]][["algorithm"]] <- class(model)[1]
output[["params"]][["algorithm"]] <- mod$get_name()
if( "settings" %in% names(mod) ){
output[["params"]][["settings"]] <- model$settings$data
}
Expand Down Expand Up @@ -715,31 +715,28 @@ methods::setMethod(
!is.Waiver(mod$get_data("fit_best"))
)

# FIXME MH: Class of mod is only R6 and DistributionModel currently but no engine
warning("Due to a bug, there is no check if the object has a valid engine.",
call. = FALSE)
# # Check that model type is known
# assertthat::assert_that( any(sapply(class(mod), function(z) z %in% getOption("ibis.engines"))) )
# # Depending on engine, check package and load them
#
# if(inherits(mod, "GDB-Model")){
# check_package("mboost")
# } else if(inherits(mod, "BART-Model")){
# check_package("dbarts")
# } else if(inherits(mod, "INLABRU-Model")){
# check_package("INLA")
# check_package("inlabru")
# } else if(inherits(mod, "BREG-Model")){
# check_package("BoomSpikeSlab")
# } else if(inherits(mod, "GLMNET-Model")){
# check_package("glmnet")
# check_package("glmnetUtils")
# } else if(inherits(mod, "STAN-Model")){
# check_package("rstan")
# check_package("cmdstanr")
# } else if(inherits(mod, "XGBOOST-Model")){
# check_package("xgboost")
# }
# Check that model type is known
assertthat::assert_that( any(sapply(mod$get_name(), function(z) z %in% getOption("ibis.engines"))) )

# Depending on engine, check package and load them
if(mod$get_name() == "GDB-Model"){
check_package("mboost")
} else if(mod$get_name() == "BART-Model"){
check_package("dbarts")
} else if(mod$get_name() == "INLABRU-Model"){
check_package("INLA")
check_package("inlabru")
} else if(mod$get_name() == "BREG-Model"){
check_package("BoomSpikeSlab")
} else if(mod$get_name() == "GLMNET-Model"){
check_package("glmnet")
check_package("glmnetUtils")
} else if(mod$get_name() == "STAN-Model"){
check_package("rstan")
check_package("cmdstanr")
} else if(mod$get_name() == "XGBOOST-Model"){
check_package("xgboost")
}

# --- #
# Return the model
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test_objectinheritance.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ test_that('Check that distribution objects are properly inherited', {
add_biodiversity_polpo(virtual_range, field_occurrence = 'Observed', name = 'Virtual points')
expect_equal(x$biodiversity$length(),0)

# For Poipa
pa <- virtual_points |> add_pseudoabsence(field_occurrence = "Observed",template = background)
x |> add_biodiversity_poipa(pa, field_occurrence = "Observed",docheck = FALSE)
expect_equal(x$biodiversity$length(),0)

# Offsets
suppressMessages( suppressWarnings( x |> add_offset_range(virtual_range) ) )
expect_s3_class(x$offset, "Waiver")
Expand All @@ -49,6 +54,13 @@ test_that('Check that distribution objects are properly inherited', {
expect_length(x$get_predictor_names(), 0)
expect_length(y$get_predictor_names(), 14)

# Remove a predictor
y |> rm_predictors("ndvi_mean_50km")
expect_length(y$get_predictor_names(), 14)
y <- y |> rm_predictors("ndvi_mean_50km")
expect_length(y$get_predictor_names(), 13)
expect_error(y |> rm_predictors("ndvi_mean_50km")) # Trying to remove it again should lead to an error

# Add elevation
y <- x |> add_predictor_elevationpref(predictors$elevation_mean_50km, 500, 1000)
expect_length(y$get_predictor_names(), 2)
Expand Down

0 comments on commit 9eff17b

Please sign in to comment.