Skip to content

Commit af64759

Browse files
authored
Merge pull request #368 from tidymodels/args-level-vs-size
Error instead of warn with wrong argument capturing the size of the grid
2 parents e3ab478 + 9d3d741 commit af64759

File tree

7 files changed

+56
-88
lines changed

7 files changed

+56
-88
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
* For space-filling designs for $p$ parameters, there is a higher likelihood of finding a space-filling design for `1 < size <= p`. Also, single-point designs now default to a random grid (#363).
1313

14+
* The `grid_*()` functions now error instead of warn when provided with the wrong argument to control the grid size. So `grid_space_filling()`, `grid_random()`, `grid_max_entropy()`, and `grid_latin_hypercube()` now error if used with a `levels` argument and `grid_regular()` now errors if used with a `size` argument (#368).
15+
16+
1417
# dials 1.3.0
1518

1619
## Improvements

R/grids.R

+10-8
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
grid_regular <- function(x, ..., levels = 3, original = TRUE, filter = NULL) {
5050
dots <- list(...)
5151
if (any(names(dots) == "size")) {
52-
cli::cli_warn(
53-
c(
54-
"{.arg size} is not an argument to {.fn grid_regular}.",
55-
i = "Did you mean {.arg levels}?"
56-
)
52+
cli::cli_abort(
53+
c(
54+
"{.arg size} is not an argument to {.fn grid_regular}.",
55+
i = "Did you mean {.arg levels}?"
56+
)
5757
)
5858
}
5959
UseMethod("grid_regular")
@@ -172,9 +172,11 @@ make_regular_grid <- function(...,
172172
grid_random <- function(x, ..., size = 5, original = TRUE, filter = NULL) {
173173
dots <- list(...)
174174
if (any(names(dots) == "levels")) {
175-
cli::cli_warn(c(
176-
"{.arg levels} is not an argument to {.fn grid_random}.",
177-
i = "Did you mean {.arg size}?")
175+
cli::cli_abort(
176+
c(
177+
"{.arg levels} is not an argument to {.fn grid_random}.",
178+
i = "Did you mean {.arg size}?"
179+
)
178180
)
179181
}
180182
UseMethod("grid_random")

R/space_filling.R

+15-9
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@
9191
grid_space_filling <- function(x, ..., size = 5, type = "any", original = TRUE) {
9292
dots <- list(...)
9393
if (any(names(dots) == "levels")) {
94-
cli::cli_warn(c(
95-
"{.arg levels} is not an argument to {.fn grid_space_filling}.",
96-
i = "Did you mean {.arg size}?")
94+
cli::cli_abort(
95+
c(
96+
"{.arg levels} is not an argument to {.fn grid_space_filling}.",
97+
i = "Did you mean {.arg size}?"
98+
)
9799
)
98100
}
99101
UseMethod("grid_space_filling")
@@ -281,9 +283,11 @@ grid_max_entropy <- function(x,
281283

282284
dots <- list(...)
283285
if (any(names(dots) == "levels")) {
284-
cli::cli_warn(c(
285-
"{.arg levels} is not an argument to {.fn grid_max_entropy}.",
286-
i = "Did you mean {.arg size}?")
286+
cli::cli_abort(
287+
c(
288+
"{.arg levels} is not an argument to {.fn grid_max_entropy}.",
289+
i = "Did you mean {.arg size}?"
290+
)
287291
)
288292
}
289293
UseMethod("grid_max_entropy")
@@ -411,9 +415,11 @@ grid_latin_hypercube <- function(x, ..., size = 3, original = TRUE) {
411415

412416
dots <- list(...)
413417
if (any(names(dots) == "levels")) {
414-
cli::cli_warn(c(
415-
"{.arg levels} is not an argument to {.fn grid_latin_hypercube}.",
416-
i = "Did you mean {.arg size}?")
418+
cli::cli_abort(
419+
c(
420+
"{.arg levels} is not an argument to {.fn grid_latin_hypercube}.",
421+
i = "Did you mean {.arg size}?"
422+
)
417423
)
418424
}
419425
UseMethod("grid_latin_hypercube")

tests/testthat/_snaps/grids.md

+10-52
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
Code
1212
grid_regular(mixture(), trees(), size = 3)
1313
Condition
14-
Warning:
15-
`size` is not an argument to `grid_regular()`.
14+
Error in `grid_regular()`:
15+
! `size` is not an argument to `grid_regular()`.
1616
i Did you mean `levels`?
17-
Error in `parameters()`:
18-
! The objects should all be <param> objects.
1917

2018
---
2119

@@ -30,76 +28,36 @@
3028
Code
3129
grid_space_filling(p, levels = 5, type = "latin_hypercube")
3230
Condition
33-
Warning:
34-
`levels` is not an argument to `grid_space_filling()`.
31+
Error in `grid_space_filling()`:
32+
! `levels` is not an argument to `grid_space_filling()`.
3533
i Did you mean `size`?
36-
Output
37-
# A tibble: 5 x 2
38-
penalty mixture
39-
<dbl> <dbl>
40-
1 0.00563 0.367
41-
2 0.0689 0.754
42-
3 0.0000374 0.841
43-
4 0.00000000349 0.440
44-
5 0.0000000402 0.198
4534

4635
---
4736

4837
Code
4938
grid_space_filling(p, levels = 5, type = "max_entropy")
5039
Condition
51-
Warning:
52-
`levels` is not an argument to `grid_space_filling()`.
40+
Error in `grid_space_filling()`:
41+
! `levels` is not an argument to `grid_space_filling()`.
5342
i Did you mean `size`?
54-
Output
55-
# A tibble: 5 x 2
56-
penalty mixture
57-
<dbl> <dbl>
58-
1 1.37e- 1 0.925
59-
2 5.80e- 1 0.237
60-
3 1.31e- 5 0.526
61-
4 3.77e-10 0.946
62-
5 2.56e- 8 0.108
6343

6444
---
6545

6646
Code
6747
grid_random(p, levels = 5)
6848
Condition
69-
Warning:
70-
`levels` is not an argument to `grid_random()`.
49+
Error in `grid_random()`:
50+
! `levels` is not an argument to `grid_random()`.
7151
i Did you mean `size`?
72-
Output
73-
# A tibble: 5 x 2
74-
penalty mixture
75-
<dbl> <dbl>
76-
1 0.00148 0.499
77-
2 0.982 0.858
78-
3 0.00000201 0.850
79-
4 0.00000000125 0.493
80-
5 0.0000864 0.968
8152

8253
---
8354

8455
Code
8556
grid_regular(p, size = 5)
8657
Condition
87-
Warning:
88-
`size` is not an argument to `grid_regular()`.
58+
Error in `grid_regular()`:
59+
! `size` is not an argument to `grid_regular()`.
8960
i Did you mean `levels`?
90-
Output
91-
# A tibble: 9 x 2
92-
penalty mixture
93-
<dbl> <dbl>
94-
1 0.0000000001 0
95-
2 0.00001 0
96-
3 1 0
97-
4 0.0000000001 0.5
98-
5 0.00001 0.5
99-
6 1 0.5
100-
7 0.0000000001 1
101-
8 0.00001 1
102-
9 1 1
10361

10462
# new param grid from conventional data frame
10563

tests/testthat/_snaps/space_filling.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727
Code
2828
grid_max_entropy(mixture(), levels = 11)
2929
Condition
30-
Warning:
31-
`levels` is not an argument to `grid_max_entropy()`.
30+
Error in `grid_max_entropy()`:
31+
! `levels` is not an argument to `grid_max_entropy()`.
3232
i Did you mean `size`?
33-
Error in `parameters()`:
34-
! The objects should all be <param> objects.
3533

3634
# `grid_latin_hypercube()` is deprecated
3735

@@ -62,18 +60,16 @@
6260
Code
6361
grid_latin_hypercube(mixture(), levels = 11)
6462
Condition
65-
Warning:
66-
`levels` is not an argument to `grid_latin_hypercube()`.
63+
Error in `grid_latin_hypercube()`:
64+
! `levels` is not an argument to `grid_latin_hypercube()`.
6765
i Did you mean `size`?
68-
Error in `parameters()`:
69-
! The objects should all be <param> objects.
7066

7167
# S3 methods for space-filling
7268

7369
Code
74-
des <- grid_space_filling(prm, levels = size, type = "uniform")
70+
grid_space_filling(prm, levels = size, type = "uniform")
7571
Condition
76-
Warning:
77-
`levels` is not an argument to `grid_space_filling()`.
72+
Error in `grid_space_filling()`:
73+
! `levels` is not an argument to `grid_space_filling()`.
7874
i Did you mean `size`?
7975

tests/testthat/test-grids.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ test_that("wrong argument name", {
7272
p <- parameters(penalty(), mixture())
7373
set.seed(1)
7474

75-
expect_snapshot(grid_space_filling(p, levels = 5, type = "latin_hypercube"))
76-
expect_snapshot(grid_space_filling(p, levels = 5, type = "max_entropy"))
77-
expect_snapshot(grid_random(p, levels = 5))
78-
expect_snapshot(grid_regular(p, size = 5))
75+
expect_snapshot(error = TRUE, {
76+
grid_space_filling(p, levels = 5, type = "latin_hypercube")
77+
})
78+
expect_snapshot(error = TRUE, {
79+
grid_space_filling(p, levels = 5, type = "max_entropy")
80+
})
81+
expect_snapshot(error = TRUE, grid_random(p, levels = 5))
82+
expect_snapshot(error = TRUE, grid_regular(p, size = 5))
7983
})
8084

8185
test_that("filter arg yields same results", {

tests/testthat/test-space_filling.R

+3-4
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,9 @@ test_that("S3 methods for space-filling", {
313313
expect_equal(design_paramset, design_list)
314314

315315
## also:
316-
expect_snapshot(
317-
des <- grid_space_filling(prm, levels = size, type = "uniform")
318-
)
319-
316+
expect_snapshot(error = TRUE, {
317+
grid_space_filling(prm, levels = size, type = "uniform")
318+
})
320319
})
321320

322321
test_that("1-point grid", {

0 commit comments

Comments
 (0)