diff --git a/R/applyJobFunction.R b/R/applyJobFunction.R index 0f6cfed..ba17de6 100644 --- a/R/applyJobFunction.R +++ b/R/applyJobFunction.R @@ -1,8 +1,6 @@ -#' @method applyJobFunction ExperimentRegistry -#' @export -applyJobFunction.ExperimentRegistry = function(reg, job, cache) { - algo = cache(getAlgorithmFilePath(reg$file.dir, job$algo.id), - slot = "algo", parts = "algorithm")$fun +# internal function to define a wrappers for job/reduce functions +# that can have optional arguments job, static and dynamic +.applyJobWrapper = function(reg, job, cache, algo) { algo.use = c("job", "static", "dynamic") algo.use = setNames(algo.use %in% names(formals(algo)), algo.use) @@ -26,6 +24,17 @@ applyJobFunction.ExperimentRegistry = function(reg, job, cache) { function(...) algo(job = job, dynamic = dynamic(), ...), function(...) algo(static = static(), dynamic = dynamic(), ...), function(...) algo(job = job, static = static(), dynamic = dynamic(), ...)) + f +} + +#' @method applyJobFunction ExperimentRegistry +#' @export +applyJobFunction.ExperimentRegistry = function(reg, job, cache) { + algo = cache(getAlgorithmFilePath(reg$file.dir, job$algo.id), + slot = "algo", parts = "algorithm")$fun + + # switch on algo formals and apply algorithm function + f = .applyJobWrapper(reg, job, cache, algo) messagef("Applying Algorithm %s ...", job$algo.id) do.call(f, job$algo.pars) diff --git a/R/reduceResultsExperiments.R b/R/reduceResultsExperiments.R index cb27daa..2741806 100644 --- a/R/reduceResultsExperiments.R +++ b/R/reduceResultsExperiments.R @@ -85,10 +85,15 @@ reduceResultsExperiments = function(reg, ids, part = NA_character_, fun, ..., n = length(ids) info("Reducing %i results...", n) + # setup cache (needed for .applyJobWrapper and loading of static/dynamic) + cache = makeFileCache(use.cache = n > 1L) + impute = if (with.impute) function(job, res, ...) impute.val else fun - getRow = function(j, reg, part, .fun, missing.ok, ...) + getRow = function(j, reg, part, .fun, missing.ok, ...){ + .fun <- .applyJobWrapper(reg, j, cache, .fun) c(list(id = j$id, prob = j$prob.id), j$prob.pars, list(algo = j$algo.id), j$algo.pars, list(repl = j$repl), - .fun(j, BatchJobs:::getResult(reg, j$id, part, missing.ok), ...)) + .fun(BatchJobs:::getResult(reg, j$id, part, missing.ok), ...)) + } aggr = data.frame() ids2 = chunk(ids, chunk.size = block.size, shuffle = FALSE)