Skip to content

Commit

Permalink
Make step_range() backwards compatible with clipping argument (#1093)
Browse files Browse the repository at this point in the history
* only clip if clipping argument was set

* add test

* add news bullet

---------

Co-authored-by: Max Kuhn <[email protected]>
  • Loading branch information
EmilHvitfeldt and topepo authored Feb 20, 2023
1 parent 7f71d4e commit 5271dc5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/range.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5271dc5

Please sign in to comment.