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

add .recipes_toggle_sparse_args() #1409

Merged
merged 2 commits into from
Jan 14, 2025
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ S3method(tune_args,step)
S3method(update,step)
export("%>%")
export(.get_data_types)
export(.recipes_toggle_sparse_args)
export(add_check)
export(add_role)
export(add_step)
Expand Down
28 changes: 28 additions & 0 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@ NULL
is_sparse_matrix <- function(x) {
methods::is(x, "sparseMatrix")
}

#' Toggle all auto sparse arguments
#'
#' @param x A recipe.
#' @param choice A character string for separating values.
#'
#' @details
#' If a step has an argument `sparse = "auto"`, then workflows can use this
#' function to fill these values with the preferred action. This preferred
#' action is calculated in workflows dependent on the model and data heuristics.
#' Hence why it has to be passed in.
#'
#' Only arguments where `sparse = "auto"` are affected, thus a user can set
#' `sparse = "no"` and it will be respected.
#'
#' @return A recipe
#'
#' @keywords internal
#'
#' @export
.recipes_toggle_sparse_args <- function(x, choice) {
for (i in seq_along(x$steps)) {
if (!is.null(x$steps[[i]]$sparse) && x$steps[[i]]$sparse == "auto") {
x$steps[[i]]$sparse <- choice
}
}
x
}
29 changes: 29 additions & 0 deletions man/dot-recipes_toggle_sparse_args.Rd

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

103 changes: 103 additions & 0 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,106 @@ test_that("recipe() errors if sparse matrix has no colnames", {
recipe(hotel_data)
)
})

test_that(".recipes_toggle_sparse_args works", {
skip_if_not_installed("modeldata")
data("ames", package = "modeldata")

rec_spec <- recipe(Sale_Price ~ ., data = ames) |>
step_center(all_numeric_predictors()) |>
step_center(all_numeric_predictors())

expect_identical(
.recipes_toggle_sparse_args(rec_spec, "yes"),
rec_spec
)

rec_spec_yes_yes <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, sparse = "yes", id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "yes", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_no_no <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, sparse = "no", id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "no", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_yes_no <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, sparse = "yes", id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "no", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_no_yes <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, sparse = "no", id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "yes", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_auto_yes <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "yes", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_auto_no <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), sparse = "no", id = "") |>
step_center(all_numeric_predictors(), id = "")

rec_spec_auto_auto <- recipe(Sale_Price ~ ., data = ames) |>
step_dummy(MS_Zoning, Street, id = "") |>
step_center(all_numeric_predictors(), id = "") |>
step_dummy(all_nominal_predictors(), id = "") |>
step_center(all_numeric_predictors(), id = "")

expect_identical(
.recipes_toggle_sparse_args(rec_spec_yes_yes, "yes"),
rec_spec_yes_yes
)
expect_identical(
.recipes_toggle_sparse_args(rec_spec_yes_yes, "no"),
rec_spec_yes_yes
)

expect_identical(
.recipes_toggle_sparse_args(rec_spec_no_no, "yes"),
rec_spec_no_no
)
expect_identical(
.recipes_toggle_sparse_args(rec_spec_no_no, "no"),
rec_spec_no_no
)

expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_auto, "yes"),
rec_spec_yes_yes
)
expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_auto, "no"),
rec_spec_no_no
)

expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_yes, "yes"),
rec_spec_yes_yes
)
expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_yes, "no"),
rec_spec_no_yes
)

expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_no, "yes"),
rec_spec_yes_no
)
expect_identical(
.recipes_toggle_sparse_args(rec_spec_auto_no, "no"),
rec_spec_no_no
)


})
Loading