Skip to content

Commit

Permalink
Merge pull request #362 from tidymodels/2024-new-snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrick authored Dec 16, 2024
2 parents caa3acc + d84c713 commit 7c2e0bb
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 17 deletions.
16 changes: 9 additions & 7 deletions R/aaa_ranges.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ range_validate <- function(object,
if (length(range) != 2) {
cli::cli_abort(
c(
x = "{.arg range} must have two values: an upper and lower bound.",
x = "{.arg range} must have two values: an upper and lower bound.",
i = "{length(range)} value{?s} {?was/were} provided.",
ukn_txt
),
Expand All @@ -85,13 +85,13 @@ range_validate <- function(object,
} else {
if (any(is_na[!is_unk])) {
cli::cli_abort(
c(x = "Value ranges must be non-missing.", ukn_txt),
c(x = "Value ranges must be non-missing.", ukn_txt),
call = call
)
}
if (any(!is_num[!is_unk])) {
cli::cli_abort(
c("Value ranges must be numeric.", ukn_txt),
c("Value ranges must be numeric.", ukn_txt),
call = call
)
}
Expand All @@ -112,10 +112,11 @@ range_get <- function(object, original = TRUE) {

#' @export
#' @rdname range_validate
range_set <- function(object, range) {
range_set <- function(object, range, call = caller_env()) {
if (length(range) != 2) {
cli::cli_abort(
"{.arg range} should have two elements, not {length(range)}."
"{.arg range} should have two elements, not {length(range)}.",
call = call
)
}
if (inherits(object, "quant_param")) {
Expand All @@ -130,8 +131,9 @@ range_set <- function(object, range) {
)
} else {
cli::cli_abort(
"{.arg object} should be a {.cls quant_param} object,
not {.obj_type_friendly {object}}."
"{.arg object} should be a {.cls quant_param} object,
not {.obj_type_friendly {object}}.",
call = call
)
}
object
Expand Down
2 changes: 1 addition & 1 deletion R/encode_unit.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ encode_unit.qual_param <- function(x, value, direction, ...) {

cli::cli_abort(
"Some values are not in the reference set of possible values:
{.val bad_vals}}."
{.val {bad_vals}}."
)
}
fac <- factor(value, levels = ref_vals)
Expand Down
2 changes: 1 addition & 1 deletion R/finalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ finalize.default <- function(object, x, force = TRUE, ...) {
return(object)
} else {
cli::cli_abort(
"Cannot finalize an object with class{?es}: {.cls {class(x)}}."
"Cannot finalize {.obj_type_friendly {object}}."
)
}
object
Expand Down
3 changes: 2 additions & 1 deletion R/grids.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ make_random_grid <- function(...,
new_param_grid <- function(x = new_data_frame(), call = caller_env()) {
if (!is.data.frame(x)) {
cli::cli_abort(
"{.arg x} must be a data frame to construct a new grid from."
"{.arg x} must be a data frame to construct a new grid from.",
call = call
)
}

Expand Down
2 changes: 1 addition & 1 deletion man/range_validate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/_snaps/aaa_ranges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# setting ranges

Code
range_set(mtry(), 1)
Condition
Error:
! `range` should have two elements, not 1.

---

Code
range_set(activation(), 1:2)
Condition
Error:
! `object` should be a <quant_param> object, not a <qual_param> object.

---

Code
range_validate(mtry(), letters[1:2])
Condition
Error:
! Value ranges must be numeric.
i `Inf` and `unknown()` are acceptable values.

---

Code
range_validate(mtry(), letters[1:2], ukn_ok = FALSE)
Condition
Error:
! `range` should be numeric.

31 changes: 31 additions & 0 deletions tests/testthat/_snaps/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@
Range (transformed scale): [-10, -1]
Values: 2

---

Code
mtry_ish <- mtry()
mtry_ish$label <- NULL
print(mtry_ish)
Message
Quantitative Parameter
Range: [1, ?]

---

Code
fun_ish <- weight_func()
fun_ish$label <- NULL
print(fun_ish)
Message
Qualitative Parameter
10 possible values include:
'rectangular', 'triangular', 'epanechnikov', 'biweight', 'triweight', 'cos',
'inv', 'gaussian', 'rank', and 'optimal'

---

Code
signed_hash()
Message
Signed Hash Value (qualitative)
2 possible values include:
TRUE and FALSE

# bad ranges

Code
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/_snaps/encode_unit.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# to [0, 1] for qualitative values

Code
encode_unit(prune_method(), "don't prune", direction = "forward")
Condition
Error in `encode_unit()`:
! Some values are not in the reference set of possible values: "don't prune".

---

Code
encode_unit(prune_method(), 13, direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a character vector.

# to [0, 1] for quantitative values

Code
encode_unit(penalty(), "penalty", direction = "forward")
Condition
Error in `encode_unit()`:
! `value` should be a numeric vector.

# bad args

Code
Expand Down Expand Up @@ -95,3 +119,11 @@
Error in `encode_unit()`:
! Values should be on [0, 1].

---

Code
encode_unit(mtry(), 1:2, direction = "backward")
Condition
Error in `encode_unit()`:
! The parameter object contains unknowns.

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/finalize.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@
Error in `get_rbf_range()`:
! The matrix version of the initialization data is not numeric.

# finalize interfaces

Code
finalize("threshold", mtcars)
Condition
Error in `finalize()`:
! Cannot finalize a string.

---

Code
finalize(list(TRUE, 3), mtcars)
Condition
Error in `map()`:
i In index: 2.
Caused by error in `.f()`:
! Cannot finalize a number.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/grids.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@
8 0.00001 1
9 1 1

# new param grid from conventional data frame

Code
new_param_grid(as.matrix(x))
Condition
Error:
! `x` must be a data frame to construct a new grid from.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@
Error:
! `inclusive` must be a logical vector of length 2, not an integer vector.

# vctrs-helpers-parameters

Code
dials:::df_size(2)
Condition
Error in `dials:::df_size()`:
! Cannot get the df size of a non-list.
i This is an internal error that was detected in the dials package.
Please report it at <https://github.com/tidymodels/dials/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.

15 changes: 12 additions & 3 deletions tests/testthat/_snaps/parameters.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# parameters_const() input checks

Code
parameters_constr(2)
parameters_constr(name = 2)
Condition
Error:
! `name` must be a character vector, not the number 2.
Expand All @@ -10,7 +10,7 @@

Code
ab <- c("a", "b")
parameters_constr(ab, c("a", "a"), ab, ab, ab)
parameters_constr(ab, id = c("a", "a"), ab, ab, ab)
Condition
Error:
x Element id should have unique values.
Expand All @@ -20,11 +20,20 @@

Code
ab <- c("a", "b")
parameters_constr(ab, ab, ab, ab, ab, "not a params list")
parameters_constr(ab, ab, ab, ab, ab, object = "not a params list")
Condition
Error:
! `object` must be a list of <param> objects.

---

Code
ab <- c("a", "b")
parameters_constr(ab, ab, ab, ab, ab, object = list(penalty(), "not a param"))
Condition
Error:
! `object` elements in the following positions must be `NA` or a <param> object: 2.

---

Code
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/_snaps/space_filling.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_max_entropy(mixture(), levels = 11)
Condition
Warning:
`levels` is not an argument to `grid_max_entropy()`.
i Did you mean `size`?
Error in `parameters()`:
! The objects should all be <param> objects.

# `grid_latin_hypercube()` is deprecated

Code
Expand All @@ -46,6 +57,17 @@
x This argument contains unknowns: `mtry`.
i See the `dials::finalize()` function.

---

Code
grid_latin_hypercube(mixture(), levels = 11)
Condition
Warning:
`levels` is not an argument to `grid_latin_hypercube()`.
i Did you mean `size`?
Error in `parameters()`:
! The objects should all be <param> objects.

# S3 methods for space-filling

Code
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-aaa_ranges.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ test_that("setting ranges", {
expect_equal(
range_set(mtry(), c(unknown(), 10L))$range, list(lower = unknown(), upper = 10L)
)
expect_snapshot(range_set(mtry(), 1), error = TRUE)
expect_snapshot(range_set(activation(), 1:2), error = TRUE)

expect_snapshot(range_validate(mtry(), letters[1:2]), error = TRUE)
expect_snapshot(range_validate(mtry(), letters[1:2], ukn_ok = FALSE), error = TRUE)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-aaa_unknown.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ test_that("has_unknown", {
expect_true(
has_unknowns(mtry())
)
expect_false(
is_unknown(NA)
)
expect_false(
dials:::has_unknowns_val(NA)
)
expect_equal(
has_unknowns(list(mtry(), mixture())),
c(TRUE, FALSE)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-aaa_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ test_that("transforms", {
expect_equal(
value_transform(mtry(), 1:3), 1:3
)
expect_false(value_validate(prior_terminal_node_coef(), 0))
expect_false(value_validate(activation(), "ham"))
})


Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ test_that("printing", {
expect_snapshot(
value_set(cost_complexity(), log10(c(.09, .0001)))
)

expect_snapshot({
mtry_ish <- mtry()
mtry_ish$label <- NULL
print(mtry_ish)
})

expect_snapshot({
fun_ish <- weight_func()
fun_ish$label <- NULL
print(fun_ish)
})

expect_snapshot(signed_hash())
})


Expand Down
Loading

0 comments on commit 7c2e0bb

Please sign in to comment.