Skip to content

Commit 78fab13

Browse files
authored
Merge pull request #375 from tidymodels/respect-inclusive-347
`value_seq()` and `value_sample()` respect `inclusive`
2 parents da8400f + 2919395 commit 78fab13

File tree

3 files changed

+118
-9
lines changed

3 files changed

+118
-9
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
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+
* `value_seq()` and `value_sample()` now respect the `inclusive` argument of quantitative parameters (#347).
15+
1416
* The constructors, `new_*_parameter()`, now label unlabeled parameter (i.e., constructed with `label = NULL`) as such (#349).
1517

1618
## Breaking changes

R/aaa_values.R

+60-9
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,19 @@ value_seq_dbl <- function(object, n, original = TRUE) {
143143
n_safely <- min(length(object$values), n)
144144
res <- object$values[seq_len(n_safely)]
145145
} else {
146+
range_lower <- min(unlist(object$range))
147+
if (!object$inclusive["lower"]) {
148+
range_lower <- range_lower + .Machine$double.eps
149+
}
150+
151+
range_upper <- max(unlist(object$range))
152+
if (!object$inclusive["upper"]) {
153+
range_upper <- range_upper - .Machine$double.eps
154+
}
155+
146156
res <- seq(
147-
from = min(unlist(object$range)),
148-
to = max(unlist(object$range)),
157+
from = range_lower,
158+
to = range_upper,
149159
length.out = n
150160
)
151161
}
@@ -161,9 +171,19 @@ value_seq_int <- function(object, n, original = TRUE) {
161171
n_safely <- min(length(object$values), n)
162172
res <- object$values[seq_len(n_safely)]
163173
} else {
174+
range_lower <- min(unlist(object$range))
175+
if (!object$inclusive["lower"]) {
176+
range_lower <- range_lower + 1L
177+
}
178+
179+
range_upper <- max(unlist(object$range))
180+
if (!object$inclusive["upper"]) {
181+
range_upper <- range_upper - 1L
182+
}
183+
164184
res <- seq(
165-
from = min(unlist(object$range)),
166-
to = max(unlist(object$range)),
185+
from = range_lower,
186+
to = range_upper,
167187
length.out = n
168188
)
169189
}
@@ -202,10 +222,20 @@ value_sample <- function(object, n, original = TRUE) {
202222

203223
value_samp_dbl <- function(object, n, original = TRUE) {
204224
if (is.null(object$values)) {
225+
range_lower <- min(unlist(object$range))
226+
if (!object$inclusive["lower"]) {
227+
range_lower <- range_lower + .Machine$double.eps
228+
}
229+
230+
range_upper <- max(unlist(object$range))
231+
if (!object$inclusive["upper"]) {
232+
range_upper <- range_upper - .Machine$double.eps
233+
}
234+
205235
res <- runif(
206236
n,
207-
min = min(unlist(object$range)),
208-
max = max(unlist(object$range))
237+
min = range_lower,
238+
max = range_upper
209239
)
210240
} else {
211241
res <- sample(
@@ -223,11 +253,22 @@ value_samp_dbl <- function(object, n, original = TRUE) {
223253
value_samp_int <- function(object, n, original = TRUE) {
224254
if (is.null(object$trans)) {
225255
if (is.null(object$values)) {
256+
range_lower <- min(unlist(object$range))
257+
if (!object$inclusive["lower"]) {
258+
range_lower <- range_lower + 1L
259+
}
260+
261+
range_upper <- max(unlist(object$range))
262+
if (!object$inclusive["upper"]) {
263+
range_upper <- range_upper - 1L
264+
}
265+
226266
res <- sample(
227-
min(unlist(object$range)):max(unlist(object$range)),
267+
seq(from = range_lower, to = range_upper),
228268
size = n,
229269
replace = TRUE
230270
)
271+
res <- as.integer(res)
231272
} else {
232273
res <- sample(
233274
object$values,
@@ -237,10 +278,20 @@ value_samp_int <- function(object, n, original = TRUE) {
237278
}
238279
} else {
239280
if (is.null(object$values)) {
281+
range_lower <- min(unlist(object$range))
282+
if (!object$inclusive["lower"]) {
283+
range_lower <- range_lower + .Machine$double.eps
284+
}
285+
286+
range_upper <- max(unlist(object$range))
287+
if (!object$inclusive["upper"]) {
288+
range_upper <- range_upper - .Machine$double.eps
289+
}
290+
240291
res <- runif(
241292
n,
242-
min = min(unlist(object$range)),
243-
max = max(unlist(object$range))
293+
min = range_lower,
294+
max = range_upper
244295
)
245296
} else {
246297
res <- sample(

tests/testthat/test-aaa_values.R

+56
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,59 @@ test_that("value_set() checks inputs", {
276276
value_set(cost_complexity(), numeric(0))
277277
})
278278
})
279+
280+
test_that("`value_seq()` respects `inclusive` #347", {
281+
double_non_incl <- new_quant_param(
282+
type = "double",
283+
range = c(0, 1),
284+
inclusive = c(FALSE, FALSE),
285+
trans = NULL,
286+
label = c(param_non_incl = "some label"),
287+
finalize = NULL
288+
)
289+
290+
vals_double <- value_seq(double_non_incl, 10)
291+
expect_gt(min(vals_double), 0)
292+
expect_lt(max(vals_double), 1)
293+
294+
int_non_incl <- new_quant_param(
295+
type = "integer",
296+
range = c(0, 2),
297+
inclusive = c(FALSE, FALSE),
298+
trans = NULL,
299+
label = c(param_non_incl = "some label"),
300+
finalize = NULL
301+
)
302+
303+
vals_int <- value_seq(int_non_incl, 10)
304+
expect_gt(min(vals_int), 0)
305+
expect_lt(max(vals_int), 2)
306+
})
307+
308+
test_that("`value_sample()` respects `inclusive` #347", {
309+
int_non_incl <- new_quant_param(
310+
type = "integer",
311+
range = c(0, 2),
312+
inclusive = c(FALSE, FALSE),
313+
trans = NULL,
314+
label = c(param_non_incl = "some label"),
315+
finalize = NULL
316+
)
317+
318+
vals_int <- value_sample(int_non_incl, 10)
319+
expect_gt(min(vals_int), 0)
320+
expect_lt(max(vals_int), 2)
321+
322+
int_non_incl_trans <- new_quant_param(
323+
type = "integer",
324+
range = c(0, 2),
325+
inclusive = c(FALSE, FALSE),
326+
trans = scales::transform_log(),
327+
label = c(param_non_incl = "some label"),
328+
finalize = NULL
329+
)
330+
331+
vals_int <- value_sample(int_non_incl_trans, n = 10, original = FALSE)
332+
expect_gt(min(vals_int), 0)
333+
expect_lt(max(vals_int), 2)
334+
})

0 commit comments

Comments
 (0)