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

Issue #498: Implement a simple print function for forecast objects. #592

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4678f32
Implement a simple print function for forecast objects.
toshiakiasakura Jan 10, 2024
d4d0446
Merge branch 'main' into simple_print_forecast_object
nikosbosse Jan 16, 2024
aeaf420
Merge branch 'main' into simple_print_forecast_object
nikosbosse Jan 22, 2024
ac40f01
Fix a `get_forecast_object` returning object type and add test for that.
toshiakiasakura Jan 28, 2024
e368ebf
Refactored print.forecast_* functions.
toshiakiasakura Jan 28, 2024
db0323f
Add a test for a print function on forecast objects.
toshiakiasakura Jan 28, 2024
5ceb924
Remove print.forecast_integer.
toshiakiasakura Jan 29, 2024
1c862e3
Merge branch 'main' into simple_print_forecast_object
nikosbosse Jan 29, 2024
d464e5a
Update R/utils.R keywords is changed to check-forecasts
toshiakiasakura Jan 29, 2024
c13600e
Update R/utils.R, Capitalise the title.
toshiakiasakura Jan 30, 2024
8f51247
Update R/utils.R, Forecast units -> Forecast unit.
toshiakiasakura Jan 30, 2024
d3a75cd
Update the docstring, rename `dat` as `x`, and minor change on the test.
toshiakiasakura Jan 30, 2024
d38d0e9
Add Toshiaki as a contributor.
toshiakiasakura Jan 30, 2024
52e0fc9
Change the variable name from `forecast_units` to `forecast_unit`
toshiakiasakura Jan 30, 2024
75888bb
Add an item to NEWS.md
toshiakiasakura Jan 30, 2024
d00663f
Update R/utils.R
toshiakiasakura Feb 5, 2024
d80cf63
Update the test for print.forecast_*.
toshiakiasakura Feb 5, 2024
71abb17
Merge branch 'main' into simple_print_forecast_object
seabbs Feb 8, 2024
b1e3dfd
Small update to NEWS.md
nikosbosse Feb 8, 2024
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
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Authors@R: c(
role = c("ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-3777-1410")),
person(given = "Toshiaki Asakura",
role = c("ctb"),
email = "[email protected]",
comment = c(ORCID = "0000-0001-8838-785X")),
person("Sebastian", "Funk",
email = "[email protected]",
role = c("aut")))
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ S3method(is_forecast,forecast_binary)
S3method(is_forecast,forecast_point)
S3method(is_forecast,forecast_quantile)
S3method(is_forecast,forecast_sample)
S3method(print,forecast_binary)
S3method(print,forecast_point)
S3method(print,forecast_quantile)
S3method(print,forecast_sample)
S3method(print,scoringutils_check)
S3method(quantile_to_interval,data.frame)
S3method(quantile_to_interval,numeric)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The update introduces breaking changes. If you want to keep using the older vers
- Added unit tests for `interval_coverage_quantile()` and `interval_coverage_dev_quantile()` in order to make sure that the functions provide the correct warnings when insufficient quantiles are provided.
- Documentation pkgdown pages are now created both for the stable and dev versions.
- Output columns for pairwise comparisons have been renamed to contain the name of the metric used for comparing.
- Added a method for `print()` that prints out additional information for `forecast` objects.

# scoringutils 1.2.2

Expand Down
2 changes: 1 addition & 1 deletion R/available_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ get_forecast_counts <- function(data,
data <- data[data[, .I[1], by = collapse_by]$V1]

# count number of rows = number of forecasts
out <- data[, .(count = .N), by = by]
out <- as.data.table(data)[, .(count = .N), by = by]

# make sure that all combinations in "by" are included in the output (with
# count = 0). To achieve that, take the unique values in data and expand grid
Expand Down
50 changes: 50 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,53 @@ ensure_data.table <- function(data) {
}
return(data)
}

#' @title Print Information About A Forecast Object
#' @description This function prints information about a forecast object,
#' including "Forecast type", "Score columns",
#' "Forecast unit".
#'
#' @param x An object of class 'forecast_*' object as produced by
#' `as_forecast()`
#' @param ... additional arguments for [print()]
#' @return NULL
#' @export
#' @keywords check-forecasts
#' @examples
#' dat <- as_forecast(example_quantile)
#' print(dat)
print.forecast_binary <- function(x, ...) {
# Obtain forecast object information for printing
forecast_type <- get_forecast_type(x)
score_cols <- get_score_names(x)
forecast_unit <- get_forecast_unit(x)

# Print forecast object information
cat("Forecast type:\n")
print(forecast_type)

if (!is.null(score_cols)) {
cat("\nScore columns:\n")
print(score_cols)
}

cat("\nForecast unit:\n")
print(forecast_unit)

cat("\n")
NextMethod(x, ...)

return(invisible(x))
}

#' @rdname print.forecast_binary
#' @export
print.forecast_quantile <- print.forecast_binary

#' @rdname print.forecast_binary
#' @export
print.forecast_point <- print.forecast_binary

#' @rdname print.forecast_binary
#' @export
print.forecast_sample <- print.forecast_binary
33 changes: 33 additions & 0 deletions man/print.forecast_binary.Rd

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

1 change: 1 addition & 0 deletions man/scoringutils-package.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-available_forecasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ test_that("get_forecast_counts() works as expected", {
expect_equal(nrow(af), 4)
expect_equal(af$`count`, c(256, 256, 128, 247))

# Ensure the returning object class is exactly same as a data.table.
expect_s3_class(af, c("data.table", "data.frame"), exact = TRUE)

# Setting `collapse = c()` means that all quantiles and samples are counted
af <- get_forecast_counts(
na.omit(example_quantile),
Expand Down
35 changes: 35 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,38 @@ test_that("get_score_names() works as expected", {
"but are no longer column names of the data: `crps`"
)
})

test_that("print() works on forecast_* objects", {
# Check print works on each forecast object
test_dat <- list(example_binary, example_quantile,
example_point, example_continuous, example_integer)
for (dat in test_dat){
dat <- suppressMessages(as_forecast(dat))
forecast_type <- get_forecast_type(dat)
forecast_unit <- get_forecast_unit(dat)

# Check Forecast type
expect_output(print(dat), "Forecast type")
expect_output(print(dat), forecast_type)
# Check Forecast unit
expect_output(print(dat), "Forecast unit")
expect_output(print(dat), pattern = paste(forecast_unit, collapse = " "))

# Check print.data.table works.
output_original <- capture.output(print(dat))
output_test <- capture.output(print(data.table(dat)))
expect_contains(output_original, output_test)
}

# Check Score columns are printed
dat <- example_quantile %>%
set_forecast_unit(c("location", "target_end_date",
"target_type", "horizon", "model")) %>%
as_forecast() %>%
add_coverage() %>%
suppressMessages

expect_output(print(dat), "Score columns")
score_cols <- get_score_names(dat)
expect_output(print(dat), pattern = paste(score_cols, collapse = " "))
})