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

Use condense_control() #540

Merged
merged 11 commits into from
Sep 22, 2022
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
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Imports:
GPfit,
hardhat (>= 1.2.0),
lifecycle (>= 1.0.0),
parsnip (>= 1.0.0),
parsnip (>= 1.0.1.9000),
purrr (>= 0.3.2),
recipes (>= 1.0.0),
rlang (>= 1.0.2),
Expand All @@ -54,4 +54,6 @@ Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1.9000
RoxygenNote: 7.2.0.9000
Remotes:
tidymodels/parsnip
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tune (development version)

* `last_fit()`, `fit_resamples()`, `tune_grid()`, and `tune_bayes()` do not automatically error if the wrong type of `control` object is passed. If the passed control object is not a superset of the one that is needed, the function will still error. As an example, passing `control_grid()` to `tune_bayes()` will fail but passing `control_bayes()` to `tune_grid()` will not. ([#449](https://github.com/tidymodels/tune/issues/449))

# tune 1.0.0

* `show_notes()` is a new function that can better help understand warnings and errors.
Expand Down
19 changes: 16 additions & 3 deletions R/control.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#' Control aspects of the grid search process
#'
#' @inheritParams control_bayes
#' @param allow_par A logical to allow parallel processing (if a parallel
#' backend is registered).
#'
#' @details
#'
Expand Down Expand Up @@ -33,6 +31,9 @@ control_grid <- function(verbose = FALSE, allow_par = TRUE,
pkgs = NULL, save_workflow = FALSE,
event_level = "first",
parallel_over = NULL) {
# Any added arguments should also be added in superset control functions
# in other packages

# add options for seeds per resample

val_class_and_single(verbose, "logical", "control_grid()")
Expand Down Expand Up @@ -84,6 +85,9 @@ control_last_fit <- function(
verbose = FALSE,
event_level = "first"
) {
# Any added arguments should also be added in superset control functions
# in other packages

extr <- function(x) x
control <-
control_resamples(
Expand Down Expand Up @@ -148,6 +152,8 @@ print.control_last_fit <- function(x, ...) {
#' `"everything"` describing how to use parallel processing. Alternatively,
#' `NULL` is allowed, which chooses between `"resamples"` and `"everything"`
#' automatically.
#' @param allow_par A logical to allow parallel processing (if a parallel
#' backend is registered).
#'
#' If `"resamples"`, then tuning will be performed in parallel over resamples
#' alone. Within each resample, the preprocessor (i.e. recipe or formula) is
Expand Down Expand Up @@ -201,7 +207,11 @@ control_bayes <-
save_workflow = FALSE,
save_gp_scoring = FALSE,
event_level = "first",
parallel_over = NULL) {
parallel_over = NULL,
allow_par = TRUE) {
# Any added arguments should also be added in superset control functions
# in other packages

# add options for seeds per resample

val_class_and_single(verbose, "logical", "control_bayes()")
Expand All @@ -216,6 +226,8 @@ control_bayes <-
val_class_or_null(pkgs, "character", "control_bayes()")
val_class_and_single(event_level, "character", "control_bayes()")
val_parallel_over(parallel_over, "control_bayes()")
val_class_and_single(allow_par, "logical", "control_bayes()")


if (!is.infinite(uncertain) && uncertain > no_improve) {
cli::cli_alert_warning(
Expand All @@ -226,6 +238,7 @@ control_bayes <-
res <-
list(
verbose = verbose,
allow_par = allow_par,
no_improve = no_improve,
uncertain = uncertain,
seed = seed,
Expand Down
5 changes: 5 additions & 0 deletions R/last_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ last_fit.model_spec <- function(object, preprocessor, split, ..., metrics = NULL
))
}

control <- parsnip::condense_control(control, control_last_fit())

empty_ellipses(...)

wflow <- add_model(workflow(), object)
Expand All @@ -101,6 +103,9 @@ last_fit.model_spec <- function(object, preprocessor, split, ..., metrics = NULL
#' @export
last_fit.workflow <- function(object, split, ..., metrics = NULL, control = control_last_fit()) {
empty_ellipses(...)

control <- parsnip::condense_control(control, control_last_fit())

last_fit_workflow(object, split, metrics, control)
}

Expand Down
4 changes: 4 additions & 0 deletions R/resample.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ fit_resamples.model_spec <- function(object,
))
}

control <- parsnip::condense_control(control, control_resamples())

empty_ellipses(...)

wflow <- add_model(workflow(), object)
Expand Down Expand Up @@ -109,6 +111,8 @@ fit_resamples.workflow <- function(object,
control = control_resamples()) {
empty_ellipses(...)

control <- parsnip::condense_control(control, control_resamples())

res <-
resample_workflow(
workflow = object,
Expand Down
4 changes: 4 additions & 0 deletions R/tune_bayes.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ tune_bayes.model_spec <- function(object,
))
}

control <- parsnip::condense_control(control, control_bayes())

wflow <- add_model(workflow(), object)

if (is_recipe(preprocessor)) {
Expand Down Expand Up @@ -190,6 +192,8 @@ tune_bayes.workflow <-
initial = 5,
control = control_bayes()) {

control <- parsnip::condense_control(control, control_bayes())

res <-
tune_bayes_workflow(
object,
Expand Down
4 changes: 4 additions & 0 deletions R/tune_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ tune_grid.model_spec <- function(object, preprocessor, resamples, ...,
))
}

control <- parsnip::condense_control(control, control_grid())

empty_ellipses(...)

wflow <- add_model(workflow(), object)
Expand All @@ -283,6 +285,8 @@ tune_grid.workflow <- function(object, resamples, ..., param_info = NULL,
grid = 10, metrics = NULL, control = control_grid()) {
empty_ellipses(...)

control <- parsnip::condense_control(control, control_grid())

# Disallow `NULL` grids in `tune_grid()`, as this is the special signal
# used when no tuning is required
if (is.null(grid)) {
Expand Down
8 changes: 6 additions & 2 deletions man/control_bayes.Rd

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

42 changes: 21 additions & 21 deletions man/control_grid.Rd

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

21 changes: 0 additions & 21 deletions tests/testthat/_snaps/bayes.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,46 +127,25 @@
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 1 missing value was found and removed before fitting
the Gaussian process model.
! The Gaussian process model is being fit using 1 features but only has 2
data points to do so. This may cause errors or a poor model fit.
! Gaussian process model: X should be in range (0, 1)
! For the rsq estimates, 1 missing value was found and removed before fitting
the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 2 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 3 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 4 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 5 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 6 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 7 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 8 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! For the rsq estimates, 9 missing values were found and removed before
fitting the Gaussian process model.
! Gaussian process model: X should be in range (0, 1)
! validation: internal: A correlation computation is required, but `estimate` is constant and ha...
! No improvement for 10 iterations; returning current results.

---

Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-condense_control.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
test_that("control functions respect hierarchy with condense_control", {

ctrl <- parsnip::condense_control(
EmilHvitfeldt marked this conversation as resolved.
Show resolved Hide resolved
control_grid(),
control_resamples()
)

expect_equal(
ctrl,
control_resamples()
)

ctrl <- parsnip::condense_control(
control_last_fit(),
control_grid()
)

expect_equal(
ctrl,
control_grid(extract = control_last_fit()$extract, save_pred = TRUE)
)

ctrl <- parsnip::condense_control(
control_last_fit(),
control_resamples()
)

expect_equal(
ctrl,
control_resamples(extract = control_last_fit()$extract, save_pred = TRUE)
)

ctrl <- parsnip::condense_control(
control_bayes(),
control_grid()
)

expect_equal(
ctrl,
control_grid()
)

ctrl <- parsnip::condense_control(
control_bayes(),
control_resamples()
)

expect_equal(
ctrl,
control_resamples()
)
})