Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value_seq() and value_sample() respect inclusive #375

Merged
merged 4 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* 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).

* `value_seq()` and `value_sample()` now respect the `inclusive` argument of quantitative parameters (#347).

## Breaking changes

* 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).
Expand Down
68 changes: 59 additions & 9 deletions R/aaa_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,19 @@
n_safely <- min(length(object$values), n)
res <- object$values[seq_len(n_safely)]
} else {
range_lower <- min(unlist(object$range))
if (!object$inclusive["lower"]) {
range_lower <- range_lower + .Machine$double.eps
}

range_upper <- max(unlist(object$range))
if (!object$inclusive["upper"]) {
range_upper <- range_upper - .Machine$double.eps
}

res <- seq(
from = min(unlist(object$range)),
to = max(unlist(object$range)),
from = range_lower,
to = range_upper,
length.out = n
)
}
Expand All @@ -161,9 +171,19 @@
n_safely <- min(length(object$values), n)
res <- object$values[seq_len(n_safely)]
} else {
range_lower <- min(unlist(object$range))
if (!object$inclusive["lower"]) {
range_lower <- range_lower + 1L
}

range_upper <- max(unlist(object$range))
if (!object$inclusive["upper"]) {
range_upper <- range_upper - 1L
}

res <- seq(
from = min(unlist(object$range)),
to = max(unlist(object$range)),
from = range_lower,
to = range_upper,
length.out = n
)
}
Expand Down Expand Up @@ -202,10 +222,20 @@

value_samp_dbl <- function(object, n, original = TRUE) {
if (is.null(object$values)) {
range_lower <- min(unlist(object$range))
if (!object$inclusive["lower"]) {
range_lower <- range_lower + .Machine$double.eps

Check warning on line 227 in R/aaa_values.R

View check run for this annotation

Codecov / codecov/patch

R/aaa_values.R#L227

Added line #L227 was not covered by tests
}

range_upper <- max(unlist(object$range))
if (!object$inclusive["upper"]) {
range_upper <- range_upper - .Machine$double.eps

Check warning on line 232 in R/aaa_values.R

View check run for this annotation

Codecov / codecov/patch

R/aaa_values.R#L232

Added line #L232 was not covered by tests
}

res <- runif(
n,
min = min(unlist(object$range)),
max = max(unlist(object$range))
min = range_lower,
max = range_upper
)
} else {
res <- sample(
Expand All @@ -223,8 +253,18 @@
value_samp_int <- function(object, n, original = TRUE) {
if (is.null(object$trans)) {
if (is.null(object$values)) {
range_lower <- min(unlist(object$range))
if (!object$inclusive["lower"]) {
range_lower <- range_lower + 1L
}

range_upper <- max(unlist(object$range))
if (!object$inclusive["upper"]) {
range_upper <- range_upper - 1L
}

res <- sample(
min(unlist(object$range)):max(unlist(object$range)),
seq(from = range_lower, to = range_upper),
size = n,
replace = TRUE
)
Expand All @@ -237,10 +277,20 @@
}
} else {
if (is.null(object$values)) {
range_lower <- min(unlist(object$range))
if (!object$inclusive["lower"]) {
range_lower <- range_lower + .Machine$double.eps
}

range_upper <- max(unlist(object$range))
if (!object$inclusive["upper"]) {
range_upper <- range_upper - .Machine$double.eps
}

res <- runif(
n,
min = min(unlist(object$range)),
max = max(unlist(object$range))
min = range_lower,
max = range_upper
)
} else {
res <- sample(
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test-aaa_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,59 @@ test_that("value_set() checks inputs", {
value_set(cost_complexity(), numeric(0))
})
})

test_that("`value_seq()` respects `inclusive` #347", {
double_non_incl <- new_quant_param(
type = "double",
range = c(0, 1),
inclusive = c(FALSE, FALSE),
trans = NULL,
label = c(param_non_incl = "some label"),
finalize = NULL
)

vals_double <- value_seq(double_non_incl, 10)
expect_gt(min(vals_double), 0)
expect_lt(max(vals_double), 1)

int_non_incl <- new_quant_param(
type = "integer",
range = c(0, 2),
inclusive = c(FALSE, FALSE),
trans = NULL,
label = c(param_non_incl = "some label"),
finalize = NULL
)

vals_int <- value_seq(int_non_incl, 10)
expect_gt(min(vals_int), 0)
expect_lt(max(vals_int), 2)
})

test_that("`value_sample()` respects `inclusive` #347", {
int_non_incl <- new_quant_param(
type = "integer",
range = c(0, 2),
inclusive = c(FALSE, FALSE),
trans = NULL,
label = c(param_non_incl = "some label"),
finalize = NULL
)

vals_int <- value_sample(int_non_incl, 10)
expect_gt(min(vals_int), 0)
expect_lt(max(vals_int), 2)

int_non_incl_trans <- new_quant_param(
type = "integer",
range = c(0, 2),
inclusive = c(FALSE, FALSE),
trans = scales::transform_log(),
label = c(param_non_incl = "some label"),
finalize = NULL
)

vals_int <- value_sample(int_non_incl_trans, n = 10, original = FALSE)
expect_gt(min(vals_int), 0)
expect_lt(max(vals_int), 2)
})
Loading