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

feat: add renv_project option #33

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^LICENSE$
^.ignore$
^.editorconfig$
Expand Down
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Imports:
mlr3misc,
uuid
Suggests:
renv,
rpart,
testthat
Encoding: UTF-8
Expand Down
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* feat: `reduceResultsBatchmark` gains argument `fun` which is passed on to `batchtools::reduceResultsList`.
Useful for deleting model data to avoid running out of memory.
Thanks to Toby Dylan Hocking @tdhock for the PR (https://github.com/mlr-org/mlr3batchmark/issues/18).
* docs: A warning is now given when the loaded mlr3 version differs from the mlr3 version stored in the trained learners
* feat: support marshaling
* docs: A warning is now given when the loaded mlr3 version differs from the mlr3 version stored in the trained learners.
* feat: Support marshaling.
* feat: A `renv` project can be passed to `batchmark()` that is loaded in the job environment.

# mlr3batchmark 0.1.1

Expand Down
15 changes: 11 additions & 4 deletions R/batchmark.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#'
#' @inheritParams mlr3::benchmark
#' @param reg [batchtools::ExperimentRegistry].
#' @param renv_project `character(1)`\cr
#' Path to a renv project.
#' If not `NULL`, the renv project is activated in the job environment.
#'
#' @return [data.table()] with ids of created jobs (invisibly).
#' @export
Expand All @@ -33,12 +36,15 @@
#' batchtools::submitJobs(reg = reg)
#'
#' reduceResultsBatchmark(reg = reg)
batchmark = function(design, store_models = FALSE, reg = batchtools::getDefaultRegistry()) {
batchmark = function(design, store_models = FALSE, reg = batchtools::getDefaultRegistry(), renv_project = NULL) {
design = as.data.table(assert_data_frame(design, min.rows = 1L))
assert_names(names(design), must.include = c("task", "learner", "resampling"))
assert_flag(store_models)
batchtools::assertRegistry(reg, class = "ExperimentRegistry", writeable = TRUE, sync = TRUE,
running.ok = FALSE)
batchtools::assertRegistry(reg, class = "ExperimentRegistry", writeable = TRUE, sync = TRUE, running.ok = FALSE)
if (!is.null(renv_project)) {
require_namespaces("renv")
assert_directory_exists(renv_project)
}

assert_list(design$task, "Task")
assert_list(design$learner, "Learner")
Expand Down Expand Up @@ -114,7 +120,8 @@ batchmark = function(design, store_models = FALSE, reg = batchtools::getDefaultR
learner_hash = learner_hashes,
learner_id = map_chr(tab$learner, "id"),
param_values_hash = param_values_hashes,
store_models = store_models
store_models = store_models,
renv_project = renv_project
)

ids[[g]] = batchtools::addExperiments(
Expand Down
5 changes: 4 additions & 1 deletion R/worker.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
run_learner = function(job, data, learner_hash, param_values_hash, store_models, ...) {
run_learner = function(job, data, learner_hash, param_values_hash, store_models, renv_project = NULL, ...) {
if (!is.null(renv_project)) {
renv::load(renv_project)
}
workhorse = utils::getFromNamespace("workhorse", ns = asNamespace("mlr3"))
resampling = get(job$prob.pars$resampling_hash, envir = .GlobalEnv)
learner = get(learner_hash, envir = .GlobalEnv)
Expand Down
11 changes: 10 additions & 1 deletion man/batchmark.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test_batchmark.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ test_that("adding parameter values works", {
c(0.5, 1)
)
})

# test_that("renv_project works", {
# tasks = list(mlr3::tsk("iris"), mlr3::tsk("sonar"))
# learners = list(mlr3::lrn("classif.featureless"), mlr3::lrn("classif.rpart"))
# resamplings = list(mlr3::rsmp("cv", folds = 3), mlr3::rsmp("holdout"))

# design = mlr3::benchmark_grid(
# tasks = tasks,
# learners = learners,
# resamplings = resamplings
# )

# reg = batchtools::makeExperimentRegistry(NA, make.default = FALSE)
# batchmark(design, reg = reg, renv_project = ".")
# batchtools::submitJobs(reg = reg)
# })
Loading