Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for get_forecast_counts() and streamline some tests #923

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ get_forecast_counts <- function(forecast,
collapse = c("quantile_level", "sample_id")) {
forecast <- clean_forecast(forecast, copy = TRUE, na.omit = TRUE)
forecast_unit <- get_forecast_unit(forecast)
assert_subset(by, names(forecast))
assert_subset(by, names(forecast), empty.ok = FALSE)
forecast <- as.data.table(forecast)

# collapse several rows to 1, e.g. treat a set of 10 quantiles as one,
Expand Down
39 changes: 15 additions & 24 deletions tests/testthat/test-get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that("get_forecast_type() works as expected", {
fixed = TRUE
)

test <- test <- as_forecast_quantile(na.omit(example_quantile))
test <- test <- data.table::copy(example_quantile)
class(test) <- c("forecast", "data.table", "data.frame")
expect_error(
get_forecast_type(test),
Expand Down Expand Up @@ -246,8 +246,6 @@ ex_coverage <- example_quantile[model == "EuroCOVIDhub-ensemble"]

test_that("get_coverage() works as expected", {
cov <- example_quantile %>%
na.omit() %>%
as_forecast_quantile() %>%
get_coverage(by = get_forecast_unit(example_quantile))

expect_equal(
Expand All @@ -268,17 +266,14 @@ test_that("get_coverage() works as expected", {
})

test_that("get_coverage() outputs an object of class c('data.table', 'data.frame'", {
ex <- as_forecast_quantile(na.omit(example_quantile))
cov <- get_coverage(ex)
cov <- get_coverage(example_quantile)
expect_s3_class(cov, c("data.table", "data.frame"), exact = TRUE)
})

test_that("get_coverage() can deal with non-symmetric prediction intervals", {
# the expected result is that `get_coverage()` just works. However,
# all interval coverages with missing values should just be `NA`
test <- data.table::copy(example_quantile) %>%
na.omit() %>%
as_forecast_quantile()
test <- data.table::copy(example_quantile)
test <- test[!quantile_level %in% c(0.2, 0.3, 0.5)]

expect_no_condition(cov <- get_coverage(test))
Expand All @@ -293,10 +288,7 @@ test_that("get_coverage() can deal with non-symmetric prediction intervals", {

# test for a version where values are not missing, but just `NA`
# since `get_coverage()` calls `na.omit`, the result should be the same.
test <- data.table::copy(example_quantile) %>%
na.omit() %>%
as_forecast_quantile() %>%
suppressMessages()
test <- data.table::copy(example_quantile)
test <- test[quantile_level %in% c(0.2, 0.3, 0.5), predicted := NA]
cov2 <- get_coverage(test)
expect_equal(cov, cov2)
Expand All @@ -321,7 +313,7 @@ test_that("get_protected_columns() works as expected", {
# `get_forecast_counts()`
# ==============================================================================
test_that("get_forecast_counts() works as expected", {
af <- suppressMessages(as_forecast_quantile(example_quantile))
af <- data.table::copy(example_quantile)
af <- get_forecast_counts(
af,
by = c("model", "target_type", "target_end_date")
Expand All @@ -331,8 +323,7 @@ test_that("get_forecast_counts() works as expected", {
expect_type(af$target_type, "character")
expect_type(af$`count`, "integer")
expect_equal(nrow(af[is.na(`count`)]), 0)
af <- na.omit(example_quantile) %>%
as_forecast_quantile() %>%
af <- example_quantile %>%
get_forecast_counts(by = "model")
expect_equal(nrow(af), 4)
expect_equal(af$`count`, c(256, 256, 128, 247))
Expand All @@ -341,26 +332,26 @@ test_that("get_forecast_counts() works as expected", {
expect_s3_class(af, c("data.table", "data.frame"), exact = TRUE)

# Setting `collapse = c()` means that all quantiles and samples are counted
af <- na.omit(example_quantile) %>%
as_forecast_quantile() %>%
af <- example_quantile %>%
get_forecast_counts(by = "model", collapse = c())
expect_equal(nrow(af), 4)
expect_equal(af$`count`, c(5888, 5888, 2944, 5681))

# setting by = NULL, the default, results in by equal to forecast unit
af <- na.omit(example_quantile) %>%
as_forecast_quantile() %>%
af <- example_quantile %>%
get_forecast_counts()
expect_equal(nrow(af), 50688)

expect_error(
get_forecast_counts(example_quantile, by = NULL),
"Assertion on 'by' failed: Must be a subset of"
)

# check whether collapsing also works for model-based forecasts
af <- na.omit(example_sample_discrete) %>%
as_forecast_sample() %>%
af <- example_sample_discrete %>%
get_forecast_counts(by = "model")
expect_equal(nrow(af), 4)

af <- na.omit(example_sample_discrete) %>%
as_forecast_sample() %>%
af <- example_sample_discrete %>%
get_forecast_counts(by = "model", collapse = c())
expect_equal(af$count, c(10240, 10240, 5120, 9880))
})