Skip to content

Commit

Permalink
chore: export with_rng_state
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Feb 6, 2024
1 parent 6b6122e commit 3fb462a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(rush_available)
export(rush_config)
export(rush_plan)
export(start_worker)
export(with_rng_state)
export(worker_loop_callr)
export(worker_loop_default)
import(checkmate)
Expand Down
3 changes: 2 additions & 1 deletion R/Rush.R
Original file line number Diff line number Diff line change
Expand Up @@ -1338,11 +1338,12 @@ Rush = R6::R6Class("Rush",

# find globals
if (!is.null(globals)) {
global_names = if (!is.null(names(globals))) names(globals) else globals
globals = set_names(map(globals, function(global) {
value = get(global, envir = parent.frame(), inherits = TRUE)
if (is.null(value)) stopf("Global `%s` not found", global)
value
}), globals)
}), global_names)
}

# arguments needed for initializing RushWorker
Expand Down
26 changes: 19 additions & 7 deletions R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ get_hostname = function() {
host[1]
}


#' @title Set RNG Sate before Running a Function
#'
#' @description
#' This function sets the rng state before running a function.
#'
#' @param fun (`function`)\cr
#' Function to run.
#' @param args (`list`)\cr
#' Arguments to pass to `fun`.
#' @param seed (`integer`)\cr
#' RNG state to set before running `fun`.
#'
#' @export
with_rng_state = function(fun, args, seed) {
if (!is.null(seed)) assign(".Random.seed", seed, envir = globalenv())
mlr3misc::invoke(fun, .args = args)
}

is_lecyer_cmrg_seed = function(seed) {
is.numeric(seed) && length(seed) == 7L && all(is.finite(seed)) && (seed[1] %% 10000L == 407L)
}
Expand Down Expand Up @@ -59,13 +78,6 @@ safe_bin_to_object = function(bin) {
redux::bin_to_object(bin)
}

# runs code with a specific rng state
with_rng_state = function(fun, args, seed) {
if (!is.null(seed)) assign(".Random.seed", seed, envir = globalenv())
mlr3misc::invoke(fun, .args = args)
}


is_retriable = function(task) {
if (is.null(task$max_retries)) return(FALSE)
assert_int(task$max_retries)
Expand Down
21 changes: 21 additions & 0 deletions man/with_rng_state.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fb462a

Please sign in to comment.