Skip to content

Commit

Permalink
changes for RNG consistency in #389 (#390)
Browse files Browse the repository at this point in the history
* changes for RNG consistency in #389

* refactor RNG kind fixes
  • Loading branch information
topepo authored Jul 19, 2021
1 parent 3f530f3 commit 85550b8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Imports:
tibble (>= 3.1.0),
tidyr,
vctrs (>= 0.3.0),
withr,
workflows (>= 0.2.2),
yardstick (>= 0.0.7)
Suggests:
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tune (development version)

* When using `load_pkgs()`, packages that use random numbers on start-up do not affect the state of the RNG. We also added more control of the RNGkind to make it consistent with the user's previous value (#389).

# tune 0.1.5

* Fixed a bug where the resampled confusion matrix is transposed when `conf_mat_resamped(tidy = FALSE)` (#372)
Expand Down
4 changes: 4 additions & 0 deletions R/grid_code_paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ tune_grid_loop_iter <- function(iteration,

# After package loading to avoid potential package RNG manipulation
if (!is.null(seed)) {
# `assign()`-ing the random seed alters the `kind` type to L'Ecuyer-CMRG,
# so we have to ensure it is restored on exit
old_kind <- RNGkind()[[1]]
assign(".Random.seed", seed, envir = globalenv())
on.exit(RNGkind(kind = old_kind), add = TRUE)
}

control_parsnip <- parsnip::control_parsnip(verbosity = 0, catch = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/load_ns.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ load_pkgs <- function(x, ...) {

#' @export
load_pkgs.character <- function(x, ...) {
load_namespace(x)
withr::with_preserve_seed(load_namespace(x))
}

#' @export
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ preprocessed
preprocessor
reprex
RBF
RNGkind
stanford
tibble
tibbles
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test_that('tune recipe only', {
extr_1_1 <- function(x) {
extract_recipe(x) %>% tidy(number = 2)
}
before_kind <- RNGkind()[[1]]
expect_error(
res_1_1 <-
workflow() %>%
Expand All @@ -36,6 +37,8 @@ test_that('tune recipe only', {
tune_grid(resamples = mt_folds, control = control_grid(extract = extr_1_1)),
NA
)
after_kind <- RNGkind()[[1]]
expect_equal(before_kind, after_kind)
expect_error(extract_1_1 <- dplyr::bind_rows(res_1_1$.extracts), NA)

expect_true(all(names(extract_1_1) == c("num_comp", ".extracts", ".config")))
Expand Down

0 comments on commit 85550b8

Please sign in to comment.