From 3c6d15516a049f90d68ab1a1542f44ff475a0091 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 14:31:26 -0700 Subject: [PATCH 01/11] add themis to suggests --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 4afdafe..54681ba 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,6 +29,7 @@ Suggests: sparklyr, testthat (>= 3.0.0), tidypredict, + themis, workflows Config/testthat/edition: 3 Encoding: UTF-8 From da7ed673ee53260861209da0bcf770b30d7358a9 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 14:31:34 -0700 Subject: [PATCH 02/11] add step_upsample --- NAMESPACE | 1 + R/step_upsample.R | 7 +++ tests/testthat/_snaps/step_upsample.md | 8 ++++ tests/testthat/test-step_upsample.R | 60 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_upsample.R create mode 100644 tests/testthat/_snaps/step_upsample.md create mode 100644 tests/testthat/test-step_upsample.R diff --git a/NAMESPACE b/NAMESPACE index eefa54e..90cb643 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,6 +33,7 @@ S3method(orbital,step_scale) S3method(orbital,step_select) S3method(orbital,step_sqrt) S3method(orbital,step_unknown) +S3method(orbital,step_upsample) S3method(orbital,step_zv) S3method(orbital,workflow) S3method(predict,orbital_class) diff --git a/R/step_upsample.R b/R/step_upsample.R new file mode 100644 index 0000000..8759027 --- /dev/null +++ b/R/step_upsample.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_upsample <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_upsample} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_upsample.md b/tests/testthat/_snaps/step_upsample.md new file mode 100644 index 0000000..fc9e337 --- /dev/null +++ b/tests/testthat/_snaps/step_upsample.md @@ -0,0 +1,8 @@ +# step_upsample errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_upsample()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_upsample.R b/tests/testthat/test-step_upsample.R new file mode 100644 index 0000000..28f0c1c --- /dev/null +++ b/tests/testthat/test-step_upsample.R @@ -0,0 +1,60 @@ +test_that("step_upsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_upsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_upsample errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_upsample(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_upsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_upsample <- dplyr::as_tibble(mtcars) + mtcars_upsample$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_upsample) %>% + themis::step_upsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_upsample, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_upsample") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From cf3a2f26e87446c29cbd5575462e62e2402860e5 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 14:41:56 -0700 Subject: [PATCH 03/11] add step_downsample --- NAMESPACE | 1 + R/step_downsample.R | 7 +++ tests/testthat/_snaps/step_downsample.md | 8 ++++ tests/testthat/test-step_downsample.R | 60 ++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_downsample.R create mode 100644 tests/testthat/_snaps/step_downsample.md create mode 100644 tests/testthat/test-step_downsample.R diff --git a/NAMESPACE b/NAMESPACE index 90cb643..13bb369 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ S3method(orbital,recipe) S3method(orbital,step_BoxCox) S3method(orbital,step_center) S3method(orbital,step_corr) +S3method(orbital,step_downsample) S3method(orbital,step_dummy) S3method(orbital,step_filter_missing) S3method(orbital,step_impute_mean) diff --git a/R/step_downsample.R b/R/step_downsample.R new file mode 100644 index 0000000..6dfa7ce --- /dev/null +++ b/R/step_downsample.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_downsample <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_downsample} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_downsample.md b/tests/testthat/_snaps/step_downsample.md new file mode 100644 index 0000000..9fd0ff5 --- /dev/null +++ b/tests/testthat/_snaps/step_downsample.md @@ -0,0 +1,8 @@ +# step_downsample errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_downsample()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_downsample.R b/tests/testthat/test-step_downsample.R new file mode 100644 index 0000000..5d49897 --- /dev/null +++ b/tests/testthat/test-step_downsample.R @@ -0,0 +1,60 @@ +test_that("step_downsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_downsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_downsample errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_downsample(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_downsample works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_downsample <- dplyr::as_tibble(mtcars) + mtcars_downsample$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_downsample) %>% + themis::step_downsample(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_downsample, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_downsample") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From d3836a2064df7a832fd9539c1a1260ced630e90f Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 15:51:08 -0700 Subject: [PATCH 04/11] add step_smote --- R/step_smote.R | 7 ++++ tests/testthat/_snaps/step_smote.md | 8 ++++ tests/testthat/test-step_smote.R | 60 +++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 R/step_smote.R create mode 100644 tests/testthat/_snaps/step_smote.md create mode 100644 tests/testthat/test-step_smote.R diff --git a/R/step_smote.R b/R/step_smote.R new file mode 100644 index 0000000..0b9870a --- /dev/null +++ b/R/step_smote.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_smote <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_smote} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_smote.md b/tests/testthat/_snaps/step_smote.md new file mode 100644 index 0000000..95100c9 --- /dev/null +++ b/tests/testthat/_snaps/step_smote.md @@ -0,0 +1,8 @@ +# step_smote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_smote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_smote.R b/tests/testthat/test-step_smote.R new file mode 100644 index 0000000..c0a1b76 --- /dev/null +++ b/tests/testthat/test-step_smote.R @@ -0,0 +1,60 @@ +test_that("step_smote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_smote errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smote(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_smote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_smote <- dplyr::as_tibble(mtcars) + mtcars_smote$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_smote) %>% + themis::step_smote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_smote, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_smote") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From e542dcf5d84ea73c46ff50fc65a37a5983f02a96 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 15:53:08 -0700 Subject: [PATCH 05/11] add step_smotenc --- NAMESPACE | 2 + R/step_smotenc.R | 7 ++++ tests/testthat/_snaps/step_smotenc.md | 8 ++++ tests/testthat/test-step_smotenc.R | 60 +++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 R/step_smotenc.R create mode 100644 tests/testthat/_snaps/step_smotenc.md create mode 100644 tests/testthat/test-step_smotenc.R diff --git a/NAMESPACE b/NAMESPACE index 13bb369..82793c7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,8 @@ S3method(orbital,step_rename) S3method(orbital,step_rm) S3method(orbital,step_scale) S3method(orbital,step_select) +S3method(orbital,step_smote) +S3method(orbital,step_smotenc) S3method(orbital,step_sqrt) S3method(orbital,step_unknown) S3method(orbital,step_upsample) diff --git a/R/step_smotenc.R b/R/step_smotenc.R new file mode 100644 index 0000000..7b7eed1 --- /dev/null +++ b/R/step_smotenc.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_smotenc <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_smotenc} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_smotenc.md b/tests/testthat/_snaps/step_smotenc.md new file mode 100644 index 0000000..4e32424 --- /dev/null +++ b/tests/testthat/_snaps/step_smotenc.md @@ -0,0 +1,8 @@ +# step_smotenc errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_smotenc()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_smotenc.R b/tests/testthat/test-step_smotenc.R new file mode 100644 index 0000000..fc7144a --- /dev/null +++ b/tests/testthat/test-step_smotenc.R @@ -0,0 +1,60 @@ +test_that("step_smotenc works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smotenc(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_smotenc errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_smotenc(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_smotenc works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_smotenc <- dplyr::as_tibble(mtcars) + mtcars_smotenc$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_smotenc) %>% + themis::step_smotenc(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_smotenc, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_smotenc") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From 95d5b0a7670ab542a3a5914e558769184d1f0bfb Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 15:54:56 -0700 Subject: [PATCH 06/11] add step_bsmote --- NAMESPACE | 1 + R/step_bsmote.R | 7 ++++ tests/testthat/_snaps/step_bsmote.md | 8 ++++ tests/testthat/test-step_bsmote.R | 60 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_bsmote.R create mode 100644 tests/testthat/_snaps/step_bsmote.md create mode 100644 tests/testthat/test-step_bsmote.R diff --git a/NAMESPACE b/NAMESPACE index 82793c7..67a6432 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ S3method(orbital,model_fit) S3method(orbital,model_spec) S3method(orbital,recipe) S3method(orbital,step_BoxCox) +S3method(orbital,step_bsmote) S3method(orbital,step_center) S3method(orbital,step_corr) S3method(orbital,step_downsample) diff --git a/R/step_bsmote.R b/R/step_bsmote.R new file mode 100644 index 0000000..fb63384 --- /dev/null +++ b/R/step_bsmote.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_bsmote <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_bsmote} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_bsmote.md b/tests/testthat/_snaps/step_bsmote.md new file mode 100644 index 0000000..ad8080a --- /dev/null +++ b/tests/testthat/_snaps/step_bsmote.md @@ -0,0 +1,8 @@ +# step_bsmote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_bsmote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_bsmote.R b/tests/testthat/test-step_bsmote.R new file mode 100644 index 0000000..71683c2 --- /dev/null +++ b/tests/testthat/test-step_bsmote.R @@ -0,0 +1,60 @@ +test_that("step_bsmote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_bsmote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_bsmote errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_bsmote(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_bsmote works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_bsmote <- dplyr::as_tibble(mtcars) + mtcars_bsmote$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_bsmote) %>% + themis::step_bsmote(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_bsmote, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_bsmote") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From c10d6e05cc75918e8d99ee4cc4b1ee723fcbe4ad Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 15:59:17 -0700 Subject: [PATCH 07/11] add step_adasyn --- NAMESPACE | 1 + R/step_adasyn.R | 7 ++++ tests/testthat/_snaps/step_adasyn.md | 8 ++++ tests/testthat/test-step_adasyn.R | 60 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_adasyn.R create mode 100644 tests/testthat/_snaps/step_adasyn.md create mode 100644 tests/testthat/test-step_adasyn.R diff --git a/NAMESPACE b/NAMESPACE index 67a6432..c3a1993 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ S3method(orbital,model_fit) S3method(orbital,model_spec) S3method(orbital,recipe) S3method(orbital,step_BoxCox) +S3method(orbital,step_adasyn) S3method(orbital,step_bsmote) S3method(orbital,step_center) S3method(orbital,step_corr) diff --git a/R/step_adasyn.R b/R/step_adasyn.R new file mode 100644 index 0000000..a07f58d --- /dev/null +++ b/R/step_adasyn.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_adasyn <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_adasyn} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_adasyn.md b/tests/testthat/_snaps/step_adasyn.md new file mode 100644 index 0000000..ad8080a --- /dev/null +++ b/tests/testthat/_snaps/step_adasyn.md @@ -0,0 +1,8 @@ +# step_bsmote errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_bsmote()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_adasyn.R b/tests/testthat/test-step_adasyn.R new file mode 100644 index 0000000..b6bc9d3 --- /dev/null +++ b/tests/testthat/test-step_adasyn.R @@ -0,0 +1,60 @@ +test_that("step_adasyn works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_adasyn(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_adasyn errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_adasyn(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_adasyn works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_adasyn <- dplyr::as_tibble(mtcars) + mtcars_adasyn$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_adasyn) %>% + themis::step_adasyn(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_adasyn, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_adasyn") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From 86beb1b345a37e82de8dcb56511f04798c248394 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 16:00:38 -0700 Subject: [PATCH 08/11] add step_rose --- NAMESPACE | 1 + R/step_rose.R | 7 ++++ tests/testthat/_snaps/step_rose.md | 8 ++++ tests/testthat/test-step_rose.R | 60 ++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_rose.R create mode 100644 tests/testthat/_snaps/step_rose.md create mode 100644 tests/testthat/test-step_rose.R diff --git a/NAMESPACE b/NAMESPACE index c3a1993..7c324e2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ S3method(orbital,step_range) S3method(orbital,step_ratio) S3method(orbital,step_rename) S3method(orbital,step_rm) +S3method(orbital,step_rose) S3method(orbital,step_scale) S3method(orbital,step_select) S3method(orbital,step_smote) diff --git a/R/step_rose.R b/R/step_rose.R new file mode 100644 index 0000000..a428eec --- /dev/null +++ b/R/step_rose.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_rose <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_rose} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_rose.md b/tests/testthat/_snaps/step_rose.md new file mode 100644 index 0000000..7c1862b --- /dev/null +++ b/tests/testthat/_snaps/step_rose.md @@ -0,0 +1,8 @@ +# step_rose errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_rose()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_rose.R b/tests/testthat/test-step_rose.R new file mode 100644 index 0000000..b1756b9 --- /dev/null +++ b/tests/testthat/test-step_rose.R @@ -0,0 +1,60 @@ +test_that("step_rose works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_rose(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_rose errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_rose(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_rose works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_rose <- dplyr::as_tibble(mtcars) + mtcars_rose$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_rose) %>% + themis::step_rose(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_rose, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_rose") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From cb52d4ff38d3363e377b3f7ccb192a9201b41d56 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 16:03:05 -0700 Subject: [PATCH 09/11] add step_nearmiss --- NAMESPACE | 1 + R/step_nearmiss.R | 7 +++ tests/testthat/_snaps/step_nearmiss.md | 8 ++++ tests/testthat/test-step_nearmiss.R | 60 ++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_nearmiss.R create mode 100644 tests/testthat/_snaps/step_nearmiss.md create mode 100644 tests/testthat/test-step_nearmiss.R diff --git a/NAMESPACE b/NAMESPACE index 7c324e2..29aaa93 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ S3method(orbital,step_lag) S3method(orbital,step_lincomb) S3method(orbital,step_log) S3method(orbital,step_mutate) +S3method(orbital,step_nearmiss) S3method(orbital,step_normalize) S3method(orbital,step_novel) S3method(orbital,step_nzv) diff --git a/R/step_nearmiss.R b/R/step_nearmiss.R new file mode 100644 index 0000000..86ea3c2 --- /dev/null +++ b/R/step_nearmiss.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_nearmiss <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_nearmiss} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_nearmiss.md b/tests/testthat/_snaps/step_nearmiss.md new file mode 100644 index 0000000..105bf28 --- /dev/null +++ b/tests/testthat/_snaps/step_nearmiss.md @@ -0,0 +1,8 @@ +# step_nearmiss errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_nearmiss()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_nearmiss.R b/tests/testthat/test-step_nearmiss.R new file mode 100644 index 0000000..c0a33d6 --- /dev/null +++ b/tests/testthat/test-step_nearmiss.R @@ -0,0 +1,60 @@ +test_that("step_nearmiss works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_nearmiss(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_nearmiss errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_nearmiss(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_nearmiss works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_nearmiss <- dplyr::as_tibble(mtcars) + mtcars_nearmiss$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_nearmiss) %>% + themis::step_nearmiss(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_nearmiss, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_nearmiss") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From bcf363b609a69ee79d453e0510fc74bc13b5582d Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 16:04:39 -0700 Subject: [PATCH 10/11] add step_tomek --- NAMESPACE | 1 + R/step_tomek.R | 7 ++++ tests/testthat/_snaps/step_tomek.md | 8 ++++ tests/testthat/test-step_tomek.R | 60 +++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 R/step_tomek.R create mode 100644 tests/testthat/_snaps/step_tomek.md create mode 100644 tests/testthat/test-step_tomek.R diff --git a/NAMESPACE b/NAMESPACE index 29aaa93..bdd25f8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -39,6 +39,7 @@ S3method(orbital,step_select) S3method(orbital,step_smote) S3method(orbital,step_smotenc) S3method(orbital,step_sqrt) +S3method(orbital,step_tomek) S3method(orbital,step_unknown) S3method(orbital,step_upsample) S3method(orbital,step_zv) diff --git a/R/step_tomek.R b/R/step_tomek.R new file mode 100644 index 0000000..a4d2853 --- /dev/null +++ b/R/step_tomek.R @@ -0,0 +1,7 @@ +#' @export +orbital.step_tomek <- function(x, all_vars, ...) { + cli::cli_abort( + "{.fn orbital} method doesn't work for {.fn step_tomek} when \\ + {.arg skip} is {.code FALSE}." + ) +} \ No newline at end of file diff --git a/tests/testthat/_snaps/step_tomek.md b/tests/testthat/_snaps/step_tomek.md new file mode 100644 index 0000000..55fb940 --- /dev/null +++ b/tests/testthat/_snaps/step_tomek.md @@ -0,0 +1,8 @@ +# step_tomek errors with skip = FALSE + + Code + orbital(rec) + Condition + Error in `orbital()`: + ! `orbital()` method doesn't work for `step_tomek()` when `skip` is `FALSE`. + diff --git a/tests/testthat/test-step_tomek.R b/tests/testthat/test-step_tomek.R new file mode 100644 index 0000000..6fd1264 --- /dev/null +++ b/tests/testthat/test-step_tomek.R @@ -0,0 +1,60 @@ +test_that("step_tomek works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_tomek(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars, !!!orbital_inline(orbital(rec))) + + exp <- recipes::bake(rec, new_data = mtcars) + exp <- exp[names(res)] + + expect_equal(res, exp) +}) + +test_that("step_tomek errors with skip = FALSE", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + + mtcars <- dplyr::as_tibble(mtcars) + mtcars$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars) %>% + themis::step_tomek(vs, skip = FALSE) %>% + recipes::prep() + + expect_snapshot( + error = TRUE, + orbital(rec) + ) +}) + +test_that("spark - step_tomek works", { + skip_if_not_installed("recipes") + skip_if_not_installed("themis") + skip_if_not_installed("sparklyr") + skip_if(is.na(testthat_spark_env_version())) + + mtcars_tomek <- dplyr::as_tibble(mtcars) + mtcars_tomek$vs <- as.factor(mtcars$vs) + + rec <- recipes::recipe(mpg ~ ., data = mtcars_tomek) %>% + themis::step_tomek(vs, skip = TRUE) %>% + recipes::prep() + + res <- dplyr::mutate(mtcars_tomek, !!!orbital_inline(orbital(rec))) + res$vs <- as.character(res$vs) + + sc <- testthat_spark_connection() + mtcars_tbl <- testthat_tbl("mtcars_tomek") + + res_spark <- dplyr::mutate(mtcars_tbl, !!!orbital_inline(orbital(rec))) %>% + dplyr::collect() + + expect_equal(res_spark, res) +}) From f95a11ab4561c42e3de398a833193d5d737b0b3c Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Thu, 18 Jul 2024 16:07:34 -0700 Subject: [PATCH 11/11] add news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index d66ef2d..17e7f40 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,6 +36,8 @@ * Support for `step_rename()` has been added. (#17) +* Support for `step_upsample()`, `step_smote()`, `step_smotenc()`, `step_bsmote()`, `step_adasyn()`, `step_rose()`, `step_downsample()`, `step_nearmiss()`, and `step_tomek()` has been added. (#21) + * `orbital()` now works on `tune::last_fit()` objects. (#13) * `orbital_predict()` has been removed and replaced with the more idiomatic `predict()` method. (#10)