Skip to content

Commit c37e49b

Browse files
committed
varying test cases for parsnip and recipes
1 parent 325b22a commit c37e49b

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

R/varying.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ varying_args.step <- function(x, id = NULL, ...) {
109109

110110
exclude <-
111111
c("terms", "role", "trained", "skip", "na.rm", "impute_with", "seed",
112-
"prefix", "naming", "denom", "outcome")
112+
"prefix", "naming", "denom", "outcome", "id")
113113
x <- x[!(names(x) %in% exclude)]
114114
x <- x[!map_lgl(x, is.null)]
115-
res <- map(x, is_varying)
116-
res <- map_lgl(res, find_varying)
115+
res <- map(x, find_varying)
116+
res <- map_lgl(res, any)
117117
tibble(
118118
name = names(res),
119119
varying = unname(res),
4.49 KB
Binary file not shown.

tests/testthat/test_varying.R

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
library(testthat)
2+
library(parsnip)
3+
library(rlang)
4+
library(dplyr)
5+
6+
context("varying parameters")
7+
8+
load("recipes_examples.RData")
9+
10+
test_that('main parsnip arguments', {
11+
mod_1 <-
12+
rand_forest() %>%
13+
varying_args(id = "")
14+
exp_1 <-
15+
tibble(
16+
name = c("mtry", "trees", "min_n"),
17+
varying = rep(FALSE, 3),
18+
id = rep("", 3),
19+
type = rep("model_spec", 3)
20+
)
21+
expect_equal(mod_1, exp_1)
22+
23+
mod_2 <-
24+
rand_forest(mtry = varying()) %>%
25+
varying_args(id = "")
26+
exp_2 <- exp_1
27+
exp_2$varying[1] <- TRUE
28+
expect_equal(mod_2, exp_2)
29+
30+
mod_3 <-
31+
rand_forest(mtry = varying(), trees = varying()) %>%
32+
varying_args(id = "wat")
33+
exp_3 <- exp_2
34+
exp_3$varying[1:2] <- TRUE
35+
exp_3$id <- "wat"
36+
expect_equal(mod_3, exp_3)
37+
})
38+
39+
40+
test_that('other parsnip arguments', {
41+
other_1 <-
42+
rand_forest(others = list(sample.fraction = varying())) %>%
43+
varying_args(id = "only others")
44+
exp_1 <-
45+
tibble(
46+
name = c("mtry", "trees", "min_n", "sample.fraction"),
47+
varying = c(rep(FALSE, 3), TRUE),
48+
id = rep("only others", 4),
49+
type = rep("model_spec", 4)
50+
)
51+
expect_equal(other_1, exp_1)
52+
53+
other_2 <-
54+
rand_forest(min_n = varying(), others = list(sample.fraction = varying())) %>%
55+
varying_args(id = "only others")
56+
exp_2 <-
57+
tibble(
58+
name = c("mtry", "trees", "min_n", "sample.fraction"),
59+
varying = c(rep(FALSE, 2), rep(TRUE, 2)),
60+
id = rep("only others", 4),
61+
type = rep("model_spec", 4)
62+
)
63+
expect_equal(other_2, exp_2)
64+
65+
other_3 <-
66+
rand_forest(
67+
others = list(
68+
strata = expr(Class),
69+
sampsize = c(varying(), varying())
70+
)
71+
) %>%
72+
varying_args(id = "add an expr")
73+
exp_3 <-
74+
tibble(
75+
name = c("mtry", "trees", "min_n", "strata", "sampsize"),
76+
varying = c(rep(FALSE, 4), TRUE),
77+
id = rep("add an expr", 5),
78+
type = rep("model_spec", 5)
79+
)
80+
expect_equal(other_3, exp_3)
81+
82+
other_4 <-
83+
rand_forest(
84+
others = list(
85+
strata = expr(Class),
86+
sampsize = c(12, varying())
87+
)
88+
) %>%
89+
varying_args(id = "num and varying in vec")
90+
exp_4 <-
91+
tibble(
92+
name = c("mtry", "trees", "min_n", "strata", "sampsize"),
93+
varying = c(rep(FALSE, 4), TRUE),
94+
id = rep("num and varying in vec", 5),
95+
type = rep("model_spec", 5)
96+
)
97+
expect_equal(other_4, exp_4)
98+
})
99+
100+
101+
test_that('recipe parameters', {
102+
rec_res_1 <- varying_args(rec_1)
103+
exp_1 <-
104+
tibble(
105+
name = c("K", "num", "threshold", "options"),
106+
varying = c(TRUE, TRUE, FALSE, FALSE),
107+
id = c("step_knnimpute", rep("step_pca", 3)),
108+
type = rep("step", 4)
109+
)
110+
expect_equal(rec_res_1, exp_1)
111+
112+
rec_res_2 <- varying_args(rec_2)
113+
exp_2 <- exp_1
114+
expect_equal(rec_res_2, exp_2)
115+
116+
rec_res_3 <- varying_args(rec_3)
117+
exp_3 <- exp_1
118+
exp_3$varying <- FALSE
119+
expect_equal(rec_res_3, exp_3)
120+
121+
rec_res_4 <- varying_args(rec_4)
122+
exp_4 <- tibble()
123+
expect_equal(rec_res_4, exp_4)
124+
})
125+

0 commit comments

Comments
 (0)