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

Apply stricter .eval_time checking for dynamic survival metrics #468

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
57 changes: 57 additions & 0 deletions R/validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,63 @@ validate_surv_truth_list_estimate <- function(truth,
call = call
)
}

all_eval_times_list <- lapply(estimate, function(x) x$.eval_time)
all_eval_times <- unlist(all_eval_times_list)

if (any(is.na(all_eval_times))) {
cli::cli_abort(
c(
x = "Missing values in {.field .eval_time} are not allowed."
),
call = call
)
}

if (any(all_eval_times < 0)) {
offenders <- unique(all_eval_times[all_eval_times < 0])

cli::cli_abort(
c(
x = "Negative values of {.field .eval_time} are not allowed.",
i = "The following negative values were found: {.val {offenders}}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

),
call = call
)
}

if (any(is.infinite(all_eval_times))) {
cli::cli_abort(
c(
x = "Infinite values of {.field .eval_time} are not allowed."
),
call = call
)
}

any_duplicates <- any(
vapply(all_eval_times_list, function(x) any(table(x) > 1), logical(1))
)
if (any_duplicates) {
cli::cli_abort(
c(
x = "Duplicate values of {.field .eval_time} are not allowed."
),
call = call
)
}

any_not_in_order <- any(
vapply(all_eval_times_list, function(x) is.unsorted(x), logical(1))
)
if (any_not_in_order) {
cli::cli_abort(
c(
x = "Values of {.field .eval_time} must be in increasing order."
),
call = call
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tune, we explicitly make use of the order of time points in eval_time: for Bayesian optimization and the tuning functions in finetune (racing etc), we can only optimize for one time point so the functions use the first one. If you want to optimize for 10 but also want the metrics to be calculated for 5, you would set eval_time = c(10, 5). Is the order a requirement from yardstick? If so, we need to decide where the re-ordering should happen. If not, we can just remove this check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets remove the check + add tests to make sure that out of order eval_time works in {yardstick}

}

validate_surv_truth_numeric_estimate <- function(truth,
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/_snaps/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,49 @@
Error:
! `estimate` should be a list, not a a double vector.

---

Code
validate_surv_truth_list_estimate(lung_surv_neg$surv_obj, lung_surv_neg$.pred)
Condition
Error:
x Negative values of .eval_time are not allowed.
i The following negative values were found: -100.

---

Code
validate_surv_truth_list_estimate(lung_surv_na$surv_obj, lung_surv_na$.pred)
Condition
Error:
x Missing values in .eval_time are not allowed.

---

Code
validate_surv_truth_list_estimate(lung_surv_inf$surv_obj, lung_surv_inf$.pred)
Condition
Error:
x Infinite values of .eval_time are not allowed.

---

Code
validate_surv_truth_list_estimate(lung_surv_duplicate$surv_obj,
lung_surv_duplicate$.pred)
Condition
Error:
x Duplicate values of .eval_time are not allowed.

---

Code
validate_surv_truth_list_estimate(lung_surv_order$surv_obj, lung_surv_order$
.pred)
Condition
Error:
x Values of .eval_time must be in increasing order.

# validate_case_weights errors as expected

Code
Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,58 @@ test_that("validate_surv_truth_list_estimate errors as expected", {
lung_surv$.pred_time
)
)

lung_surv_neg <- lung_surv
lung_surv_neg$.pred[[1]]$.eval_time[1] <- -100
expect_snapshot(
error = TRUE,
validate_surv_truth_list_estimate(
lung_surv_neg$surv_obj,
lung_surv_neg$.pred
)
)

lung_surv_na <- lung_surv
lung_surv_na$.pred[[1]]$.eval_time[1] <- NA
expect_snapshot(
error = TRUE,
validate_surv_truth_list_estimate(
lung_surv_na$surv_obj,
lung_surv_na$.pred
)
)

lung_surv_inf <- lung_surv
lung_surv_inf$.pred[[1]]$.eval_time[1] <- Inf
expect_snapshot(
error = TRUE,
validate_surv_truth_list_estimate(
lung_surv_inf$surv_obj,
lung_surv_inf$.pred
)
)

lung_surv_duplicate <- lung_surv
lung_surv_duplicate$.pred[[1]]$.eval_time[1] <- 200
expect_snapshot(
error = TRUE,
validate_surv_truth_list_estimate(
lung_surv_duplicate$surv_obj,
lung_surv_duplicate$.pred
)
)

lung_surv_order <- lung_surv
lung_surv_order$.pred <- lapply(
lung_surv_order$.pred, dplyr::arrange, dplyr::desc(.eval_time)
)
expect_snapshot(
error = TRUE,
validate_surv_truth_list_estimate(
lung_surv_order$surv_obj,
lung_surv_order$.pred
)
)
})

test_that("validate_case_weights errors as expected", {
Expand Down
Loading