From 3fb462a91d64335afb158359c46c26a81a0c71ec Mon Sep 17 00:00:00 2001 From: be-marc Date: Tue, 6 Feb 2024 11:48:17 +0100 Subject: [PATCH] chore: export with_rng_state --- NAMESPACE | 1 + R/Rush.R | 3 ++- R/helper.R | 26 +++++++++++++++++++------- man/with_rng_state.Rd | 21 +++++++++++++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 man/with_rng_state.Rd diff --git a/NAMESPACE b/NAMESPACE index 0c93c06..fb9a33c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/Rush.R b/R/Rush.R index a0d2025..c45a583 100644 --- a/R/Rush.R +++ b/R/Rush.R @@ -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 diff --git a/R/helper.R b/R/helper.R index ca5cdfe..e814943 100644 --- a/R/helper.R +++ b/R/helper.R @@ -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) } @@ -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) diff --git a/man/with_rng_state.Rd b/man/with_rng_state.Rd new file mode 100644 index 0000000..c839bda --- /dev/null +++ b/man/with_rng_state.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/helper.R +\name{with_rng_state} +\alias{with_rng_state} +\title{Set RNG Sate before Running a Function} +\usage{ +with_rng_state(fun, args, seed) +} +\arguments{ +\item{fun}{(\code{function})\cr +Function to run.} + +\item{args}{(\code{list})\cr +Arguments to pass to \code{fun}.} + +\item{seed}{(\code{integer})\cr +RNG state to set before running \code{fun}.} +} +\description{ +This function sets the rng state before running a function. +}