Skip to content

Commit

Permalink
Rename apply_metrics() to apply_rules()
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosbosse committed Dec 19, 2023
1 parent d4f257c commit e5dad2e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
15 changes: 8 additions & 7 deletions R/score.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ score.scoringutils_binary <- function(data, metrics = metrics_binary, ...) {
data <- remove_na_observed_predicted(data)
metrics <- validate_metrics(metrics)

data <- apply_metrics(
data <- apply_rules(
data, metrics,
data$observed, data$predicted, ...
)
Expand All @@ -101,7 +101,7 @@ score.scoringutils_point <- function(data, metrics = metrics_point, ...) {
data <- remove_na_observed_predicted(data)
metrics <- validate_metrics(metrics)

data <- apply_metrics(
data <- apply_rules(
data, metrics,
data$observed, data$predicted, ...
)
Expand Down Expand Up @@ -135,7 +135,7 @@ score.scoringutils_sample <- function(data, metrics = metrics_sample, ...) {
predicted <- do.call(rbind, data$predicted)
data[, c("observed", "predicted", "scoringutils_N") := NULL]

data <- apply_metrics(
data <- apply_rules(
data, metrics,
observed, predicted, ...
)
Expand Down Expand Up @@ -178,7 +178,7 @@ score.scoringutils_quantile <- function(data, metrics = metrics_quantile, ...) {
quantile <- unlist(unique(data$quantile))
data[, c("observed", "predicted", "quantile", "scoringutils_quantile") := NULL]

data <- apply_metrics(
data <- apply_rules(
data, metrics,
observed, predicted, quantile, ...
)
Expand All @@ -194,14 +194,15 @@ score.scoringutils_quantile <- function(data, metrics = metrics_quantile, ...) {

#' @title Apply A List Of Functions To A Data Table Of Forecasts
#' @description This helper function applies scoring rules (stored as a list of
#' functions) to a data table of. `apply_metrics` is used within `score()` to
#' apply all scoring rules to the data.
#' functions) to a data table of forecasts. `apply_rules` is used within
#' `score()` to apply all scoring rules to the data.
#' Scoring rules are wrapped in [run_safely()] to catch errors and to make
#' sure that only arguments are passed to the scoring rule that are actually
#' accepted by it.
#' @inheritParams score
#' @return A data table with the forecasts and the calculated metrics
apply_metrics <- function(data, metrics, ...) {
#' @keywords internal
apply_rules <- function(data, metrics, ...) {
expr <- expression(
data[, (metric_name) := do.call(run_safely, list(..., fun = fun))]
)
Expand Down
11 changes: 6 additions & 5 deletions man/apply_metrics.Rd → man/apply_rules.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-score.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,29 +278,29 @@ test_that("function throws an error if data is missing", {
})

# =============================================================================
# `apply_metrics()`
# `apply_rules()`
# =============================================================================

test_that("apply_metrics() works", {
test_that("apply_rules() works", {

dt <- data.table::data.table(x = 1:10)
scoringutils:::apply_metrics(
scoringutils:::apply_rules(
data = dt, metrics = list("test" = function(x) x + 1),
dt$x
)
expect_equal(dt$test, 2:11)

# additional named argument works
expect_no_condition(
scoringutils:::apply_metrics(
scoringutils:::apply_rules(
data = dt, metrics = list("test" = function(x) x + 1),
dt$x, y = dt$test)
)

# additional unnamed argument does not work

expect_warning(
scoringutils:::apply_metrics(
scoringutils:::apply_rules(
data = dt, metrics = list("test" = function(x) x + 1),
dt$x, dt$test)
)
Expand Down

0 comments on commit e5dad2e

Please sign in to comment.