From 5271dc541907db4593bfbbc5d3b40fc4357b2ccc Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Mon, 20 Feb 2023 08:37:54 -0800 Subject: [PATCH] Make step_range() backwards compatible with clipping argument (#1093) * only clip if clipping argument was set * add test * add news bullet --------- Co-authored-by: Max Kuhn --- NEWS.md | 3 +++ R/range.R | 2 +- tests/testthat/test_range.R | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 69a334604..44460a138 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,8 +8,11 @@ * The `recipe`, `step`, and `check` methods for `generics::tune_args()` are now registered unconditionally (tidymodels/workflows#192). +* `step_range()` is now backwards compatible with respect to the `clipping` argument that was added 1.0.3, and old saved recipes can now be baked. (#1090) + * Added a `conditionMessage()` method for `recipes_error`s to consistently point out which step errors occurred in when reporting errors. (#1080) + # recipes 1.0.4 * Added missing tidy method for `step_intercept()` and `step_lag()`. (#730) diff --git a/R/range.R b/R/range.R index 690b67b26..0fd1c4b7b 100644 --- a/R/range.R +++ b/R/range.R @@ -130,7 +130,7 @@ bake.step_range <- function(object, new_data, ...) { new_data[[column]] <- (new_data[[column]] - min) * (object$max - object$min) / (max - min) + object$min - if (object$clipping) { + if (!is.null(object$clipping) && object$clipping) { new_data[[column]] <- pmax(new_data[[column]], object$min) new_data[[column]] <- pmin(new_data[[column]], object$max) } diff --git a/tests/testthat/test_range.R b/tests/testthat/test_range.R index e15b291b1..0ba78f115 100644 --- a/tests/testthat/test_range.R +++ b/tests/testthat/test_range.R @@ -142,6 +142,36 @@ test_that("correct values", { expect_equal(exp_pred, obs_pred) }) +test_that("backwards compatibility for before clipping <= 1.0.2 (#1090)", { + standardized <- rec %>% + step_range(carbon, hydrogen, min = -12, id = "", clipping = FALSE) + + standardized_trained <- prep(standardized, training = biomass_tr, verbose = FALSE) + + # simulates old recipe + standardized_trained$steps[[1]]$clipping <- NULL + + obs_pred <- bake(standardized_trained, new_data = biomass_te, all_predictors()) + obs_pred <- as.matrix(obs_pred) + + mins <- apply(biomass_tr[, c("carbon", "hydrogen")], 2, min) + maxs <- apply(biomass_tr[, c("carbon", "hydrogen")], 2, max) + + new_min <- -12 + new_max <- 1 + new_range <- new_max - new_min + + carb <- ((new_range * (biomass_te$carbon - mins["carbon"])) / + (maxs["carbon"] - mins["carbon"])) + new_min + + hydro <- ((new_range * (biomass_te$hydrogen - mins["hydrogen"])) / + (maxs["hydrogen"] - mins["hydrogen"])) + new_min + + exp_pred <- cbind(carb, hydro) + colnames(exp_pred) <- c("carbon", "hydrogen") + expect_equal(exp_pred, obs_pred) +}) + test_that("printing", { standardized <- rec %>% step_range(carbon, hydrogen, min = -12)