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

don't use base pipe #1411

Merged
merged 1 commit into from
Jan 16, 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
4 changes: 2 additions & 2 deletions tests/testthat/test-center.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ test_that("centering with case weights", {
})

test_that("warns when NaN is returned due to Inf or -Inf",{
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) %>%
step_center(x)
expect_snapshot(prep(rec))

rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) %>%
step_center(x)
expect_snapshot(prep(rec))
})
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ test_that("normalizing with case weights", {
})

test_that("warns when NaN is returned due to Inf or -Inf",{
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) %>%
step_normalize(x)
expect_snapshot(prep(rec))

rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) %>%
step_normalize(x)
expect_snapshot(prep(rec))
})
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-range.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ test_that("backwards compatibility for before clipping <= 1.0.2 (#1090)", {
})

test_that("warns when NaN is returned due to zero variance",{
rec <- recipe(~., data = data.frame(x = rep(1, 10))) |>
rec <- recipe(~., data = data.frame(x = rep(1, 10))) %>%
step_range(x)
expect_snapshot(prep(rec))
})

test_that("warns when NaN is returned due to Inf or -Inf",{
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, Inf))) %>%
step_range(x)
expect_snapshot(prep(rec))

rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) |>
rec <- recipe(~., data = data.frame(x = c(2, 3, 4, -Inf))) %>%
step_range(x)
expect_snapshot(prep(rec))
})
Expand Down
127 changes: 71 additions & 56 deletions tests/testthat/test-sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ test_that("prep() accepts sparse tibbles", {
hotel_data <- sparse_hotel_rates(tibble = TRUE)

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data)

expect_no_error(
rec <- prep(rec_spec)
)

expect_true(
sparsevctrs::has_sparse_elements(rec$template)
)

expect_no_error(
rec <- prep(rec_spec, training = hotel_data)
)
Expand All @@ -62,15 +62,15 @@ test_that("bake() accepts sparse tibbles", {

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data) %>%
prep()

expect_no_condition(
res <- bake(rec_spec, new_data = NULL)
)

expect_true(
sparsevctrs::has_sparse_elements(res)
)

expect_no_error(
res <- bake(rec_spec, new_data = hotel_data)
)
Expand Down Expand Up @@ -118,15 +118,15 @@ test_that("prep() accepts sparse matrices", {
hotel_data <- sparse_hotel_rates()

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data)

expect_no_error(
rec <- prep(rec_spec)
)

expect_true(
sparsevctrs::has_sparse_elements(rec$template)
)

expect_no_error(
rec <- prep(rec_spec, training = hotel_data)
)
Expand All @@ -144,15 +144,15 @@ test_that("bake() accepts sparse matrices", {

rec_spec <- recipe(avg_price_per_room ~ ., data = hotel_data) %>%
prep()

expect_no_condition(
res <- bake(rec_spec, new_data = NULL)
)

expect_true(
sparsevctrs::has_sparse_elements(res)
)

expect_no_error(
res <- bake(rec_spec, new_data = hotel_data)
)
Expand All @@ -170,7 +170,7 @@ test_that("recipe() errors if sparse matrix has no colnames", {

expect_snapshot(
error = TRUE,
recipe(~ ., data = hotel_data)
recipe(~., data = hotel_data)
)

expect_snapshot(
Expand All @@ -183,56 +183,71 @@ 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()) |>
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 = "")
# deals with wirdness between magrittr and base pipe
clean_environments <- function(x) {
for (i in seq_along(x$steps)) {
attr(x$steps[[i]]$terms[[1]], '.Environment') <- NULL
}
x
}

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

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 = "") %>%
clean_environments()

expect_identical(
.recipes_toggle_sparse_args(rec_spec_yes_yes, "yes"),
Expand Down Expand Up @@ -292,9 +307,9 @@ test_that(".recipes_toggle_sparse_args works", {
step_normalize(all_numeric_predictors()) %>%
step_dummy(all_nominal_predictors())

exp <- rec %>% prep() %>% bake(NULL) %>% sparsevctrs::sparsity()
exp <- rec %>% prep() %>% bake(NULL) %>% sparsevctrs::sparsity()

expect_equal(
expect_equal(
.recipes_estimate_sparsity(rec),
exp
)
Expand All @@ -303,8 +318,8 @@ exp <- rec %>% prep() %>% bake(NULL) %>% sparsevctrs::sparsity()
step_normalize(all_numeric_predictors()) %>%
step_dummy(all_nominal_predictors())

expect_equal(
expect_equal(
.recipes_estimate_sparsity(rec),
exp
)
})
})
Loading