mllg / batchtools

Tools for computation on batch systems
https://mllg.github.io/batchtools/
GNU Lesser General Public License v3.0
172 stars 51 forks source link

scoping errors depending on objects in global environment #275

Open sumny opened 3 years ago

sumny commented 3 years ago

I recently stumbled across the following rather weird behavior: If a custom function named eval is present in my .GlobalEnv, testing jobs with external = TRUE and potentially also submitting will fail:

library(batchtools)  # 0.9.15
library(data.table)

reg = makeExperimentRegistry(file.dir = NA, seed = 1)

addProblem(name = "test", fun = function(...) list(...), seed = 2)

eval_ = function(data, job, instance, ...) {
  xs = list(...)
  1 + xs$x1
}

eval = function(...) list(...)

addAlgorithm(name = "eval_", fun = eval_)

pdes = list(test = data.table())
ades = list(eval_ = data.table(x1 = c(1, 2)))
addExperiments(pdes, ades, repls = 1)

testJob(1)
#### [bt]: Generating problem instance for problem 'test' ...
#### [bt]: Applying algorithm 'eval_' on problem 'test' for job 1 (seed = 2) ...
#[1] 2

testJob(1, external = TRUE)
# Error: testJob() failed for job with id=1. To properly debug, re-run with external=FALSE

rm(eval)

testJob(1, external = TRUE)
### [bt]: Generating problem instance for problem 'test' ...
### [bt]: Applying algorithm 'eval_' on problem 'test' for job 1 (seed = 2) ...
[1] 2

Initially, the algorithm was named eval and I thought this caused the problem, however, it seems like it suffices to have a custom function eval present in the .GlobalEnv. My initial guess would be that this interferes with properly scoping base::eval at some later stages (not sure why though).