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

support tuning postprocessors in workflows #266

Merged
merged 3 commits into from
Oct 21, 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ Config/Needs/website:
tidyverse/tidytemplate,
yardstick
Remotes:
tidymodels/dials#358,
tidymodels/recipes,
tidymodels/parsnip,
tidymodels/tailor,
tidymodels/tailor#51,
r-lib/sparsevctrs
Config/testthat/edition: 3
Encoding: UTF-8
Expand Down
7 changes: 7 additions & 0 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ extract_parameter_set_dials.workflow <- function(x, ...) {
param_data <- vctrs::vec_rbind(param_data, recipe_param_data)
}

if (has_postprocessor_tailor(x)) {
tailor <- extract_postprocessor(x)
tailor_param_data <- extract_parameter_set_dials(tailor)

param_data <- vctrs::vec_rbind(param_data, tailor_param_data)
}

dials::parameters_constr(
param_data$name,
param_data$id,
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/extract.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@
Error in `extract_recipe()`:
! The workflow must have a recipe preprocessor.

# extract parameter set from workflow with potentially conflicting ids (#266)

Code
extract_parameter_set_dials(wflow)
Condition
Error in `extract_parameter_set_dials()`:
x Element id should have unique values.
i Duplicates exist for item: threshold

80 changes: 80 additions & 0 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ test_that("extract parameter set from workflow with tunable model", {
expect_true(all(wf_info$source == "model_spec"))
})

test_that("extract parameter set from workflow with tunable postprocessor", {
wflow <- workflow()
wflow <- add_recipe(wflow, recipes::recipe(mpg ~ ., mtcars))
wflow <- add_model(wflow, parsnip::linear_reg())
wflow <- add_tailor(
wflow,
tailor::tailor() %>%
tailor::adjust_numeric_range(lower_limit = hardhat::tune())
)

wflow_info <- extract_parameter_set_dials(wflow)

check_parameter_set_tibble(wflow_info)
expect_equal(nrow(wflow_info), 1)
expect_true(all(wflow_info$source == "tailor"))
})

test_that("extract parameter set from workflow with tunable recipe and model", {

spline_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
Expand All @@ -252,6 +269,69 @@ test_that("extract parameter set from workflow with tunable recipe and model", {
)
})

test_that("extract parameter set from workflow with tunable recipe, model, and tailor", {
wflow <- workflow()
wflow <- add_recipe(
wflow,
recipes::recipe(mpg ~ ., mtcars) %>%
recipes::step_impute_knn(
recipes::all_predictors(),
neighbors = hardhat::tune("imputation")
)
)
wflow <- add_model(
wflow,
parsnip::linear_reg(engine = "glmnet", penalty = tune())
)
wflow <- add_tailor(
wflow,
tailor::tailor() %>%
tailor::adjust_numeric_range(lower_limit = hardhat::tune())
)

wflow_info <- extract_parameter_set_dials(wflow)

check_parameter_set_tibble(wflow_info)
expect_equal(nrow(wflow_info), 3)
expect_true(all(wflow_info$source %in% c("recipe", "model_spec", "tailor")))
})

test_that("extract parameter set from workflow with potentially conflicting ids (#266)", {
# re: https://github.com/tidymodels/workflows/pull/266#issuecomment-2417772184
# specifically concerned that duplicated "threshold" parameters result in
# an informative error
wflow <- workflow()
wflow <- add_recipe(
wflow,
recipes::recipe(mpg ~ ., mtcars) %>%
recipes::step_pca(recipes::all_predictors(), threshold = hardhat::tune())
)
wflow <- add_model(wflow, parsnip::linear_reg())
wflow <- add_tailor(
wflow,
tailor::tailor() %>%
tailor::adjust_probability_threshold(threshold = hardhat::tune())
)

expect_snapshot(
error = TRUE,
extract_parameter_set_dials(wflow)
)

# ensure that the user can actually do something about it
wflow <- remove_tailor(wflow)
wflow <- add_tailor(
wflow,
tailor::tailor() %>%
tailor::adjust_probability_threshold(threshold = hardhat::tune("unique id"))
)

wflow_info <- extract_parameter_set_dials(wflow)

check_parameter_set_tibble(wflow_info)
expect_equal(nrow(wflow_info), 2)
expect_true(all(wflow_info$source %in% c("recipe", "tailor")))
})

# ------------------------------------------------------------------------------
# extract_parameter_dials()
Expand Down
Loading