Skip to content

Commit

Permalink
improve error message for unsupported model modes
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Jan 19, 2024
1 parent 72ffdbd commit bd62087
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# stacks (development version)

* Improved error message for unsupported model modes (#152).

# stacks 1.0.3

* Refine package alt text (#202).
Expand Down
8 changes: 8 additions & 0 deletions R/add_candidates.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ add_candidates.default <- function(data_stack, candidates, name, ...) {
new_mode <- wf_spec$mode
old_mode <- attr(stack, "mode")

if (isFALSE(new_mode %in% c("regression", "classification"))) {
cli_abort(
"The {.pkg stacks} package does not support stacking models with mode
{.val {new_mode}}.",
call = NULL
)
}

attr(stack, "mode") <- new_mode

stack
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ should_run_examples <- function(suggests = NULL) {
}

mode_is_regression <- function(x) {
isTRUE(x[["mode"]] %in% c("regression", "censored regression"))
isTRUE(x[["mode"]] %in% "regression")
}


Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/add_candidates.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
Error:
! The supplied candidates were tuned/fitted using only metrics that rely on hard class predictions. Please tune/fit with at least one class probability-based metric, such as `roc_auc` (`?yardstick::roc_auc()`).

---

Code
stacks() %>% add_candidates(reg_res_lr)
Condition
Error:
! The stacks package does not support stacking models with mode "censored regression".

# model definition naming works as expected

Code
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test_add_candidates.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ test_that("add_candidates errors informatively with bad arguments", {
error = TRUE
)

# fake a censored regression tuning result
wf <- attr(reg_res_lr, "workflow")
wf$fit$actions$model$spec$mode <- "censored regression"
attr(reg_res_lr, "workflow") <- wf

expect_snapshot(
stacks() %>% add_candidates(reg_res_lr),
error = TRUE
)

# warn when stacking may fail due to tuning failure
# TODO: re-implement tests--devel tune now errors on previous failure
})
Expand Down

0 comments on commit bd62087

Please sign in to comment.