Skip to content

Commit

Permalink
transition to snapshot testing (#1190)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Emil Hvitfeldt <[email protected]>
  • Loading branch information
simonpcouch and EmilHvitfeldt authored Sep 10, 2024
1 parent 6109bc1 commit b180de8
Show file tree
Hide file tree
Showing 67 changed files with 1,671 additions and 213 deletions.
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/adds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# adding row indicies

Code
as.matrix(mtcars) %>% add_rowindex()
Condition
Error in `add_rowindex()`:
! `x` should be a data frame.

32 changes: 32 additions & 0 deletions tests/testthat/_snaps/args_and_modes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
# pipe arguments

Code
rand_forest() %>% set_args()
Condition
Error in `set_args()`:
! Please pass at least one named argument.

# pipe engine

Code
rand_forest() %>% set_mode()
Condition
Error in `set_mode()`:
! Available modes for model type rand_forest are: "unknown", "classification", "regression", and "censored regression".

---

Code
rand_forest() %>% set_mode(2)
Condition
Error in `set_mode()`:
! 2 is not a known mode for model `rand_forest()`.

---

Code
rand_forest() %>% set_mode("haberdashery")
Condition
Error in `set_mode()`:
! "haberdashery" is not a known mode for model `rand_forest()`.

# can't set a mode that isn't allowed by the model spec

Code
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/_snaps/boost_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@
Error in `boost_tree()`:
! "bogus" is not a known mode for model `boost_tree()`.

---

Code
translate(boost_tree(mode = "classification"), engine = NULL)
Message
Used `engine = 'xgboost'` for translation.
Output
Boosted Tree Model Specification (classification)
Computational engine: xgboost
Model fit template:
parsnip::xgb_train(x = missing_arg(), y = missing_arg(), weights = missing_arg(),
nthread = 1, verbose = 0)

---

Code
translate(boost_tree(formula = y ~ x))
Condition
Error in `boost_tree()`:
! unused argument (formula = y ~ x)

# check_args() works

Code
Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/_snaps/boost_tree_C5.0.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# C5.0 execution

Code
res <- fit(lc_basic, funded_amnt ~ term, data = lending_club, engine = "C5.0",
control = ctrl)
Condition
Error in `glue()`:
! Expecting '}'

# submodel prediction

Code
Expand All @@ -6,3 +15,21 @@
Error in `multi_predict()`:
! Please use `new_data` instead of `newdata`.

# argument checks for data dimensions

Code
f_fit <- spec %>% fit(species ~ ., data = penguins)
Condition
Warning:
! 1000 samples were requested but there were 333 rows in the data.
i 333 will be used.

---

Code
xy_fit <- spec %>% fit_xy(x = penguins[, -1], y = penguins$species)
Condition
Warning:
! 1000 samples were requested but there were 333 rows in the data.
i 333 will be used.

74 changes: 74 additions & 0 deletions tests/testthat/_snaps/boost_tree_xgboost.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# xgboost execution, classification

Code
res <- parsnip::fit(hpc_xgboost, class ~ novar, data = hpc, control = ctrl)
Condition
Error:
! object 'novar' not found

# xgboost execution, regression

Code
res <- parsnip::fit_xy(car_basic, x = mtcars[, num_pred], y = factor(mtcars$vs),
control = ctrl)
Condition
Error in `check_outcome()`:
! For a regression model, the outcome should be `numeric`, not a `factor`.

# submodel prediction

Code
Expand All @@ -6,6 +23,35 @@
Error in `multi_predict()`:
! Please use `new_data` instead of `newdata`.

# validation sets

Code
reg_fit <- boost_tree(trees = 20, mode = "regression") %>% set_engine("xgboost",
validation = 3) %>% fit(mpg ~ ., data = mtcars[-(1:4), ])
Condition
Error in `parsnip::xgb_train()`:
! `validation` should be on [0, 1).

# early stopping

Code
reg_fit <- boost_tree(trees = 20, stop_iter = 30, mode = "regression") %>%
set_engine("xgboost", validation = 0.1) %>% fit(mpg ~ ., data = mtcars[-(1:4),
])
Condition
Warning:
`early_stop` was reduced to 19.

---

Code
reg_fit <- boost_tree(trees = 20, stop_iter = 0, mode = "regression") %>%
set_engine("xgboost", validation = 0.1) %>% fit(mpg ~ ., data = mtcars[-(1:4),
])
Condition
Error in `parsnip::xgb_train()`:
! `early_stop` should be on [2, 20).

# xgboost data conversion

Code
Expand All @@ -14,6 +60,34 @@
Warning:
`event_level` can only be set for binary outcomes.

# argument checks for data dimensions

Code
f_fit <- spec %>% fit(species ~ ., data = penguins, control = ctrl)
Condition
Warning:
! 1000 samples were requested but there were 333 rows in the data.
i 333 will be used.

---

Code
xy_fit <- spec %>% fit_xy(x = penguins_dummy, y = penguins$species, control = ctrl)
Condition
Warning:
! 1000 samples were requested but there were 333 rows in the data.
i 333 will be used.

# count/proportion parameters

Code
boost_tree(mtry = 0.9, trees = 4) %>% set_engine("xgboost") %>% set_mode(
"regression") %>% fit(mpg ~ ., data = mtcars)
Condition
Error in `recalc_param()`:
! The option `counts = TRUE` was used but `colsample_bynode` was given as 0.9.
i Please use a value >= 1 or use `counts = FALSE`.

# interface to param arguments

! Please supply elements of the `params` list argument as main arguments to `set_engine()` rather than as part of `params`.
Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/_snaps/convert_data.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
# numeric y and mixed x, fail missing data

Code
.convert_form_to_xy_fit(rate ~ ., data = Puromycin_miss, na.action = na.fail,
indicators = "traditional", remove_intercept = TRUE)
Condition
Error in `na.fail.default()`:
! missing values in object

# numeric x and factor y

Code
expected <- glm(class ~ ., data = hpc, x = TRUE, y = TRUE, family = binomial())
Condition
Warning:
glm.fit: fitted probabilities numerically 0 or 1 occurred

# bad args

Code
.convert_form_to_xy_fit(mpg ~ ., data = mtcars, composition = "tibble",
indicators = "traditional", remove_intercept = TRUE)
Condition
Error in `.convert_form_to_xy_fit()`:
! `composition` should be either "data.frame", "matrix", or "dgCMatrix".

---

Code
.convert_form_to_xy_fit(mpg ~ ., data = mtcars, weights = letters[1:nrow(mtcars)],
indicators = "traditional", remove_intercept = TRUE)
Condition
Error in `.convert_form_to_xy_fit()`:
! `weights` must be a numeric vector.

---

Code
.convert_xy_to_form_fit(mtcars$disp, mtcars$mpg, remove_intercept = TRUE)
Condition
Error in `.convert_xy_to_form_fit()`:
! `x` cannot be a vector.

---

Code
.convert_xy_to_form_fit(mtcars[, 1:3], mtcars[, 2:5], remove_intercept = TRUE)
Condition
Error in `.convert_xy_to_form_fit()`:
! `x` and `y` have the names "cyl" and "disp" in common.
i Please ensure that `x` and `y` don't share any column names.

# convert to matrix

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/descriptors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# can be temporarily overriden at evaluation time

Code
.cols()
Condition
Error in `descr_env$.cols()`:
! Descriptor context not set

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/extract.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
i The parsnip extension package baguette implements support for this specification.
i Please install (if needed) and load to continue.

# extract single parameter from model with no parameters

Code
extract_parameter_dials(lm_model, parameter = "none there")
Condition
Error in `extract_parameter_dials()`:
! No parameter exists with id "none there".

# extract_fit_time() works

Code
Expand Down
51 changes: 51 additions & 0 deletions tests/testthat/_snaps/failed_models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# numeric model

Code
num_res <- predict(lm_mod, hpc_bad[1:11, -1])
Condition
Warning:
Model fit failed; cannot make predictions.

---

Code
ci_res <- predict(lm_mod, hpc_bad[1:11, -1], type = "conf_int")
Condition
Warning:
Model fit failed; cannot make predictions.

---

Code
pi_res <- predict(lm_mod, hpc_bad[1:11, -1], type = "pred_int")
Condition
Warning:
Model fit failed; cannot make predictions.

# classification model

Code
cls_res <- predict(log_reg, lending_club %>% dplyr::slice(1:7) %>% dplyr::select(
-Class))
Condition
Warning:
Model fit failed; cannot make predictions.

---

Code
prb_res <- predict(log_reg, lending_club %>% dplyr::slice(1:7) %>% dplyr::select(
-Class), type = "prob")
Condition
Warning:
Model fit failed; cannot make predictions.

---

Code
ci_res <- predict(log_reg, lending_club %>% dplyr::slice(1:7) %>% dplyr::select(
-Class), type = "conf_int")
Condition
Warning:
Model fit failed; cannot make predictions.

40 changes: 40 additions & 0 deletions tests/testthat/_snaps/fit_interfaces.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
# wrong args

Code
tester_xy(NULL, x = sprk, y = hpc, model = rmod)
Condition
Error in `tester_xy()`:
! `x` should be a <data.frame/matrix>, not an <integer> object.

---

Code
tester(NULL, f, data = as.matrix(hpc[, 1:4]))
Condition
Error in `tester()`:
! `data` should be a <data.frame/dgCMatrix/tbl_spark>, not a double matrix.

# unknown modes

Code
fit(mars_spec, am ~ ., data = mtcars)
Condition
Error in `fit()`:
! Please set the mode in the model specification.

---

Code
fit_xy(mars_spec, x = mtcars[, -1], y = mtcars[, 1])
Condition
Error in `fit_xy()`:
! Please set the mode in the model specification.

---

Code
fit_xy(mars_spec, x = lending_club[, 1:2], y = lending_club$Class)
Condition
Error in `fit_xy()`:
! Please set the mode in the model specification.

# misspecified formula argument

Code
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/gen_additive_model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# regression

Code
xy_res <- fit_xy(reg_mod, x = mtcars[, 1:5], y = mtcars$mpg, control = ctrl)
Condition
Error in `fit_xy()`:
! Please use `fit()` rather than `fit_xy()` to train generalized additive models with the "mgcv" engine.
i See `?model_formula()` to learn more.

# classification

Code
xy_res <- fit_xy(cls_mod, x = two_class_dat[, 2:3], y = two_class_dat$Class,
control = ctrl)
Condition
Error in `fit_xy()`:
! Please use `fit()` rather than `fit_xy()` to train generalized additive models with the "mgcv" engine.
i See `?model_formula()` to learn more.

Loading

0 comments on commit b180de8

Please sign in to comment.