Skip to content

register tune_args() and tunable() methods unconditionally #193

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

Merged
merged 4 commits into from
Feb 16, 2023
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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ S3method(predict,workflow)
S3method(print,control_workflow)
S3method(print,workflow)
S3method(tidy,workflow)
S3method(tunable,workflow)
S3method(tune_args,workflow)
export(.fit_finalize)
export(.fit_model)
export(.fit_pre)
Expand Down Expand Up @@ -55,6 +57,8 @@ importFrom(generics,augment)
importFrom(generics,fit)
importFrom(generics,glance)
importFrom(generics,tidy)
importFrom(generics,tunable)
importFrom(generics,tune_args)
importFrom(hardhat,extract_fit_engine)
importFrom(hardhat,extract_fit_parsnip)
importFrom(hardhat,extract_mold)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# workflows (development version)

* The workflows methods for `generics::tune_args()` and `generics::tunable()`
are now registered unconditionally (#192).

# workflows 1.1.2

* Tightens integration with parsnip's machinery for checking that needed
Expand Down
8 changes: 4 additions & 4 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ required_pkgs.workflow <- function(x, infra = TRUE, ...) {
out
}

# Lazily registered in .onLoad()
tune_args_workflow <- function(object, ...) {
#' @export
tune_args.workflow <- function(object, ...) {
model <- extract_spec_parsnip(object)

param_data <- generics::tune_args(model)
Expand All @@ -43,8 +43,8 @@ tune_args_workflow <- function(object, ...) {
param_data
}

# Lazily registered in .onLoad()
tunable_workflow <- function(x, ...) {
#' @export
tunable.workflow <- function(x, ...) {
model <- extract_spec_parsnip(x)
param_data <- generics::tunable(model)

Expand Down
2 changes: 2 additions & 0 deletions R/workflows-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @importFrom generics fit
#' @importFrom generics glance
#' @importFrom generics tidy
#' @importFrom generics tune_args
#' @importFrom generics tunable
#' @importFrom lifecycle deprecated
#' @importFrom parsnip fit_xy
#' @importFrom stats predict
Expand Down
38 changes: 0 additions & 38 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# nocov start

# Set in `.onLoad()` below
tune_contains_tune_args_method <- NULL
tune_contains_tunable_method <- NULL

.onLoad <- function(libname, pkgname) {
ns <- rlang::ns_env("workflows")

Expand All @@ -14,40 +10,6 @@ tune_contains_tunable_method <- NULL
vctrs::s3_register("butcher::axe_fitted", "workflow")

vctrs::s3_register("generics::required_pkgs", "workflow")

# - If tune isn't installed, register the method (`packageVersion()` will error here)
# - If tune >= 0.1.6.9001 is installed, register the method because it has been removed from tune
tune_contains_tune_args_method <- tryCatch(
expr = utils::packageVersion("tune") < "0.1.6.9001",
error = function(cnd) FALSE
)
assign(
x = "tune_contains_tune_args_method",
value = tune_contains_tune_args_method,
envir = ns
)

if (!tune_contains_tune_args_method) {
# `tune_args.workflow()` moved from tune to workflows
vctrs::s3_register("generics::tune_args", "workflow", tune_args_workflow)
}

# - If tune isn't installed, register the method (`packageVersion()` will error here)
# - If tune >= 0.1.6.9002 is installed, register the method because it has been removed from tune
tune_contains_tunable_method <- tryCatch(
expr = utils::packageVersion("tune") < "0.1.6.9002",
error = function(cnd) FALSE
)
assign(
x = "tune_contains_tunable_method",
value = tune_contains_tunable_method,
envir = ns
)

if (!tune_contains_tunable_method) {
# `tunable.workflow()` moved from tune to workflows
vctrs::s3_register("generics::tunable", "workflow", tunable_workflow)
}
}

# nocov end
6 changes: 0 additions & 6 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ test_that("error if not a workflow", {
# extract_parameter_set_dials()

test_that("extract parameter set from workflow with tunable recipe", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

spline_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_date(date) %>%
Expand All @@ -213,7 +212,6 @@ test_that("extract parameter set from workflow with tunable recipe", {
})

test_that("extract parameter set from workflow with tunable model", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

rm_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_rm(date, ends_with("away"))
Expand All @@ -229,7 +227,6 @@ test_that("extract parameter set from workflow with tunable model", {
})

test_that("extract parameter set from workflow with tunable recipe and model", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

spline_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_date(date) %>%
Expand Down Expand Up @@ -260,7 +257,6 @@ test_that("extract parameter set from workflow with tunable recipe and model", {
# extract_parameter_dials()

test_that("extract single parameter from workflow with tunable recipe", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

spline_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_date(date) %>%
Expand Down Expand Up @@ -296,7 +292,6 @@ test_that("extract single parameter from workflow with tunable recipe", {
})

test_that("extract single parameter from workflow with tunable model", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

rm_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_rm(date, ends_with("away"))
Expand All @@ -316,7 +311,6 @@ test_that("extract single parameter from workflow with tunable model", {
})

test_that("extract single parameter from workflow with tunable recipe and model", {
skip_if(tune_contains_tunable_method, message = "`tunable.model_spec()` is in tune")

spline_rec <- recipes::recipe(ridership ~ ., data = head(Chicago)) %>%
recipes::step_date(date) %>%
Expand Down