Runs one task: generate data, fit model, compute metrics. All errors are captured and converted to task results.
Usage
run_task(
task,
config_spec,
fitter,
metrics,
retain = c("metrics", "diagnostics")
)Arguments
- task
A task specification list (from get_task_spec) containing:
task_id: Unique task identifier stringdata_spec: Data generation specificationfit_spec: Model fitting specificationrng_seed: Precomputed RNG seed for this tasktask_ctx: Task context with indices (data_idx, fit_idx, rep_idx)
- config_spec
Plain list config spec (for worker transport) containing:
data_generator: Function to generate dataOther configuration parameters
- fitter
S7 Fitter object that implements the fit(), predict_fit(), log_lik(), and loo() methods
- metrics
List of S7 Metric objects to compute
- retain
Character vector of what to retain in results. Options:
"metrics": Always retained (default)
"diagnostics": Fit diagnostics (default)
"fit": The raw fit object
"draws": Posterior draws matrix
Value
A bayesim_task_result S3 object containing:
task_id: Task identifierstatus: "success" or "failed"metrics: Named list of computed metrics (if successful)diagnostics: Named list of fit diagnostics (if successful)timing: List withtotalelapsed timewarnings: Character vector of warning messageserror: Error information (if failed)
Details
The task execution follows these steps:
Set the RNG state for deterministic reproducibility
Generate data using the data_generator function
Validate the data bundle
Fit the model using the fitter
Build context for metric computation (predictions, log_lik, loo)
Compute all metrics
Apply retention policy to manage memory
Error handling:
Data generation errors are normalized to bayesim_data_error
Fit errors are normalized to bayesim_fit_error
Metric errors are handled in compute_all_metrics()
Fatal errors (config, contract) propagate and stop the simulation
Examples
if (FALSE) { # \dontrun{
# Create a task specification
task <- list(
task_id = "d001_f001_r00001",
data_spec = list(n = 100, effect_size = 0.5),
fit_spec = list(prior = "normal(0, 1)"),
rng_seed = create_task_rng_streams(42, 1)[[1]],
task_ctx = list(data_idx = 1, fit_idx = 1, rep_idx = 1)
)
# Run the task
result <- run_task(
task = task,
config_spec = config_spec,
fitter = my_fitter,
metrics = list(rmse_metric, coverage_metric),
retain = c("metrics", "diagnostics")
)
# Check result
if (result$status == "success") {
print(result$metrics)
} else {
print(result$error$error_message)
}
} # }