Skip to content

Commit d8943e9

Browse files
authored
fix: follow-ups from roborev reviews of the review fixes (#45)
Found by roborev reviews 122-131 of #39-#44. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b835ee3 commit d8943e9

12 files changed

Lines changed: 97 additions & 22 deletions

‎DESCRIPTION‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Imports:
3131
Suggests:
3232
knitr,
3333
quarto,
34-
testthat (>= 3.1.5),
34+
testthat (>= 3.1.8),
3535
withr,
36-
xgboost
36+
xgboost (>= 3.0.0)
3737
VignetteBuilder:
3838
quarto
3939
Config/roxygen2/version: 8.1.0

‎R/SmallFuncs.R‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,25 @@ checkKmArgs <- function(...) {
237237
if (is.null(argNames) && ...length() > 0) {
238238
argNames <- rep("", ...length())
239239
}
240-
# Match like R does: exact names or unique prefixes. design, response and
241-
# control are always set by updateGP().
240+
# Match like R does: exact names first, then unique prefixes, each formal
241+
# at most once. updateGP() always sets design, response and control, so
242+
# they claim their formals first.
242243
kmArgs <- names(formals(DiceKriging::km))
243-
matched <- kmArgs[pmatch(argNames, kmArgs, duplicates.ok = TRUE)]
244-
bad <- argNames[
245-
is.na(matched) | matched %in% c("design", "response", "control")
246-
]
244+
matched <- pmatch(c("design", "response", "control", argNames), kmArgs)
245+
unmatched <- is.na(matched[-(1:3)])
246+
# Names that match a formal only when it may be used twice collide with
247+
# another argument; the rest are not km() arguments at all.
248+
collides <- unmatched &
249+
!is.na(pmatch(argNames, kmArgs, duplicates.ok = TRUE)) &
250+
argNames != ""
251+
if (any(collides)) {
252+
stop(
253+
"Arguments in ... match a DiceKriging::km() argument that is already ",
254+
"supplied or set by updateGP() (design, response, control): ",
255+
paste(argNames[collides], collapse = ", ")
256+
)
257+
}
258+
bad <- argNames[unmatched]
247259
if (length(bad) > 0) {
248260
bad[bad == ""] <- "<unnamed>"
249261
stop(

‎R/bayesOpt.R‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@
132132
#'
133133
#' \donttest{
134134
#' # Example 2 - Hyperparameter Tuning in xgboost
135-
#' if (requireNamespace('xgboost', quietly = TRUE)) {
135+
#' if (requireNamespace('xgboost', quietly = TRUE) &&
136+
#' utils::packageVersion('xgboost') >= '3.0.0') {
136137
#' library("xgboost")
137138
#'
138139
#' data(agaricus.train, package = "xgboost")

‎R/plot.R‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ plot.bayesOpt <- function(x, ...) {
8383
)
8484
)
8585

86+
# Arguments in ... override the layout defaults; unnamed ones are kept and
87+
# abbreviated names count as the plot_layout() argument they match.
88+
layoutArgs <- list(...)
89+
given <- names(formals(plot_layout))[
90+
pmatch(names(layoutArgs), names(formals(plot_layout)))
91+
]
92+
defaults <- list(ncol = 1, guides = "collect")
93+
defaults <- defaults[setdiff(names(defaults), given)]
94+
8695
p <- (sc / ut) +
87-
do.call(
88-
plot_layout,
89-
utils::modifyList(list(ncol = 1, guides = "collect"), list(...))
90-
) +
96+
do.call(plot_layout, c(defaults, layoutArgs)) +
9197
plot_annotation(
9298
title = "Bayesian Optimization Results",
9399
theme = theme(plot.title = element_text(hjust = 0.5))

‎man/bayesOpt.Rd‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-bayesOpt1D.R‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ test_that("tighter bounds in addIterations() warn and exclude rows from the GP",
9797
expect_equal(optObj$GauProList$scoreGP@n, sum(fitRows$inBounds))
9898
})
9999

100+
test_that("addIterations() refits a GP that was up to date when bounds change", {
101+
skip_on_cran()
102+
set.seed(1991)
103+
104+
optObj <- bayesOpt(
105+
function(x) list(Score = -x^2),
106+
list(x = c(-5, 5)),
107+
initGrid = data.frame(x = c(-5, -4, -1, 0, 1, 4)),
108+
iters.n = 1,
109+
verbose = 0
110+
)
111+
optObj <- updateGP(optObj, verbose = 0)
112+
expect_equal(optObj$GauProList$scoreGP@n, 7)
113+
114+
# iters.n = 0 would fail checkParameters(); iters.n = 1 adds one row after
115+
# the GP is fitted.
116+
suppressWarnings(
117+
optObj <- addIterations(
118+
optObj,
119+
bounds = list(x = c(-3, 3)),
120+
iters.n = 1,
121+
verbose = 0
122+
)
123+
)
124+
expect_equal(
125+
optObj$GauProList$scoreGP@n,
126+
sum(abs(optObj$scoreSummary$x[1:7]) <= 3)
127+
)
128+
})
129+
100130
test_that("addIterations() needs 3 usable samples within bounds", {
101131
skip_on_cran()
102132

‎tests/testthat/test-calcAcq.R‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test_that("acquisition functions behave as expected", {
4343
expect_gte(acq(gps, "poi"), 0)
4444
expect_lte(acq(gps, "poi"), 1)
4545
expect_gt(acq(gps, "ucb", k = 3), acq(gps, "ucb", k = 1))
46-
expect_lt(acq(gps, "ei", y_max = 2), acq(gps, "ei", y_max = 1))
47-
expect_lt(acq(gps, "poi", y_max = 2), acq(gps, "poi", y_max = 1))
46+
# y_max near the predicted mean, so neither value underflows to 0.
47+
expect_lt(acq(gps, "ei", y_max = 1), acq(gps, "ei", y_max = 0.9))
48+
expect_lt(acq(gps, "poi", y_max = 1), acq(gps, "poi", y_max = 0.9))
4849
})

‎tests/testthat/test-checkParameters.R‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ test_that("invalid parameters are errors", {
2525
expect_error(checkArgs(iters.n = 4.5), "must be integers")
2626
expect_error(checkArgs(iters.k = 1.5), "must be integers")
2727
expect_error(checkArgs(acq = "foo"), "Acquisition function not recognized")
28-
foreach::registerDoSEQ()
29-
expect_error(checkArgs(parallel = TRUE), "no back end is registered")
28+
expect_error(
29+
checkArgs(acq = c("ucb", "ei")),
30+
"Acquisition function not recognized"
31+
)
32+
expect_error(
33+
checkArgs(acq = NA_character_),
34+
"Acquisition function not recognized"
35+
)
3036
expect_error(
3137
checkArgs(otherHalting = list(foo = 1)),
3238
"otherHalting element not recognized"
@@ -38,3 +44,8 @@ test_that("invalid parameters are errors", {
3844
expect_error(checkArgs(plotProgress = "yes"), "plotProgress must be logical")
3945
expect_error(checkArgs(errorHandling = "foo"), "errorHandling is malformed")
4046
})
47+
48+
test_that("parallel = TRUE needs a registered backend", {
49+
local_mocked_bindings(getDoParWorkers = function() 1)
50+
expect_error(checkArgs(parallel = TRUE), "no back end is registered")
51+
})

‎tests/testthat/test-hyperparameterTuning.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
testthat::test_that("xgboost", {
22
skip_on_cran()
3-
skip_if_not_installed("xgboost")
3+
skip_if_not_installed("xgboost", "3.0.0")
44
library("xgboost")
55
set.seed(0)
66

‎tests/testthat/test-inputValidation.R‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ test_that("misspelled arguments are caught before FUN runs", {
9797
expect_no_error(checkKmArgs(covt = "gauss", noise = rep(0.1, 3)))
9898
expect_error(checkKmArgs(nug = 1e-8), "does not accept: nug")
9999
expect_error(checkKmArgs(co = 1), "does not accept: co")
100-
expect_error(checkKmArgs(control = list()), "does not accept: control")
100+
expect_error(checkKmArgs(control = list()), "already supplied or set")
101+
expect_error(
102+
checkKmArgs(covtype = "gauss", covt = "exp"),
103+
"control): covt",
104+
fixed = TRUE
105+
)
106+
expect_no_error(checkKmArgs(nugget = 1e-8, nug = TRUE))
101107
})
102108

103109
test_that("bayesOpt() accepts only exact acq names", {

0 commit comments

Comments
 (0)