workflow

run.workflow

Workflow planner.

Given a Study + Experiment + workflow spec + (resolved) backend, produce a :class:WorkflowPlan describing:

  • which sweep axes the backend will vectorize internally,
  • which axes the workflow engine must fan out as wildcards / array tasks,
  • the resulting cell count, chunking, and per-cell command line.

The planner is backend-aware. It consults the capability table in :mod:tvbo.run.backends, so the same study.yaml produces a different DAG when re-rendered against a different backend. tvbo workflow emits the plan as a Slurm, Snakemake or Nextflow kit, and tvbo run --shard / --limit read its sweep axes to decide whether the backend can slice a run in process.

Attributes

Name Description
DEFAULT_CONTAINER_IMAGE
SUBJECT_AXIS The parameter a dataset fan-out’s axis names: one cell per subject.
logger

Classes

Name Description
SweepAxis One sweep dimension extracted from an Exploration.
WorkflowPlan Result of planning. Drives all emitters.

SweepAxis

run.workflow.SweepAxis(
    name,
    parameter,
    values,
    kind,
    placement='auto',
    runtime_sized=False,
)

One sweep dimension extracted from an Exploration.

Attributes

Name Description
n Number of values the axis sweeps; 0 for a runtime-sized axis.

WorkflowPlan

run.workflow.WorkflowPlan(
    study_key,
    experiment_key,
    backend,
    engine,
    out_dir,
    container,
    container_binds,
    container_args,
    retries,
    rng,
    vectorize_axes=list(),
    workflow_axes=list(),
    cohort_subjects=list(),
    cohort_result_files=list(),
    subject_selection=dict(),
    chunk=1,
    engine_block=dict(),
    overrides=list(),
    requirements=list(),
    source_spec='',
    experiment_selector=None,
    workflow_spec=dict(),
    depends_on=list(),
    reference_root='',
)

Result of planning. Drives all emitters.

Attributes

Name Description
container_exec_flags container_binds/container_args as flags for an exec call.
container_extras_venv Kit-relative dir for the requirements venv (see :attr:needs_env_layer).
n_array_tasks Array tasks the kit submits: one per chunk fanned cells, else chunk shards of the vectorised sweep.
n_vectorize_cells Cells the backend runs inside one invocation: the product of the statically sized vectorised axes.
n_workflow_cells Cells the workflow engine fans out: the product of the workflow axes’ sizes.
needs_container_layer A :attr:needs_env_layer that layers onto a declared container specifically.
needs_env_layer Whether the kit must provision declared requirements into a run environment.
pip_specs Requirements as pip-installable strings (pkg>=x or a source URL).
run_spec SPEC argument for tvbo run — the source recipe path/CURIE if known, else the experiment:<key> fallback.
subject_selection_text The fan-out’s cohort restriction as one line, or empty when no cohort narrowed it.
wildcards Workflow-axis names, in the order the engine’s wildcards take them.

Methods

Name Description
cell_iter Iterate over the cartesian product of workflow axes.
cell_iter
run.workflow.WorkflowPlan.cell_iter()

Iterate over the cartesian product of workflow axes.

Functions

Name Description
cell_out_relpath Per-cell output path under results/<key>/ — exactly the file tvbo run writes.
cell_run_flag The tvbo run flag that puts one cell of a fanned axis at value: --subject for the dataset fan-out, --pin for an exploration axis.
cohort_out_relpaths Per-subject output relpaths an on_device cohort job writes (one per subject).
default_container_ref The tvbo container reference used when a recipe asks for container-based execution without pinning a concrete image.
dynamics_parameter Where the parameter reference path binds, as :func:tvbo.templates.tvboptim.utils.parameter_keypath resolves it, and the dynamics parameter it names when that is the dynamics (None when it binds a coupling, an external input or a reserved scope, or the model declares no parameter of that name).
experiment_key The canonical short key for an experiment.
extract_axes Collect every ExplorationAxis declared on experiment.
fan_expand_kwargs axis=EXP_<RULE>_<AXIS> kwargs binding an expand() to the fan’s value lists.
fan_input_expr A Snakemake input expression matching ALL of ep’s output files.
is_per_node Whether the dynamics parameter param holds one value per node: its shape spans n_nodes, or a node declares it at a value of its own, which the generated code promotes to a per-node array; a node that names it without a value keeps the shared one.
mem_mb '8G'/'8GB'/'512M'/'2000' -> integer mebibytes.
merge_workflow_spec Merge the study’s workflow defaults with an experiment’s workflow.
per_subject_cohorts The cohorts experiment’s network reads one member of per run (:meth:Network.per_subject_cohorts); empty for an experiment with no network, or one that reads none.
plan Compute a :class:WorkflowPlan from an Experiment + spec.
refuse_per_subject_reads_on_device Raise when experiment, run as one on-device cohort job, would read something one subject at a time.
resolve_container_ref Resolve a recipe’s declared container into a reference singularity exec / apptainer exec can pull.
result_dependencies The experiments whose saved results experiment reads, each as the identifier it is named by, in the order found: the source of a from_experiment initial state, a source: naming …exp<id> on an observation or a parameter, and a used: reference that names an experiment wherever the experiment declares it (:meth:SimulationExperiment.declared_references). A plan orders the experiment after each of them.
runtime_minutes A SLURM walltime -> integer minutes, for Snakemake’s runtime resource.
workflow_config_from_spec Rebuild a datamodel WorkflowConfig from the merged workflow spec dict.
writes_per_subject Whether experiment writes one result per subject: it fans over its dataset’s subjects, or runs them as one on-device cohort.

cell_out_relpath

run.workflow.cell_out_relpath(ep, wildcard=_wildcard)

Per-cell output path under results/<key>/ — exactly the file tvbo run writes.

A dataset (subject) fan-out writes sub-<subject>_<stem>.h5; an exploration fan writes one nested <axis>=<val>/…/<stem>.h5 per cell; a group run writes <stem>.h5. wildcard spells an axis’s placeholder in the launcher’s own syntax. The default is Snakemake’s, doubled (see :func:_wildcard) so the carrying f-string leaves it intact. This is the single source of truth for the fanned-cell path — the Snakefile’s rule all, each figure rule that depends on a fanned experiment and the Nextflow task’s output directory resolve to the same pattern.

cell_run_flag

run.workflow.cell_run_flag(parameter, value)

The tvbo run flag that puts one cell of a fanned axis at value: --subject for the dataset fan-out, --pin for an exploration axis.

--pin sets the axis’s parameter and drops it from the sweep, so the cell is a single point, its base run and every declared observation computed there; a plain --set of a swept parameter would neither resolve nor collapse the sweep.

cohort_out_relpaths

run.workflow.cohort_out_relpaths(ep)

Per-subject output relpaths an on_device cohort job writes (one per subject).

The whole cohort runs as ONE vectorised job that saves one result per subject (the same filenames the fan-out produces), so the rule declares them all as its outputs. The filenames are the canonical :func:_cohort_result_files set computed at plan time. Empty for a non-cohort experiment.

default_container_ref

run.workflow.default_container_ref()

The tvbo container reference used when a recipe asks for container-based execution without pinning a concrete image.

The tag matches the running CLI’s version (see :func:_default_container_tag) so a kit runs against the image built from the same source it was emitted with. Every part is overridable from the environment: TVBO_CONTAINER supplies a full reference verbatim, otherwise TVBO_CONTAINER_IMAGE sets the repository.

dynamics_parameter

run.workflow.dynamics_parameter(experiment, path)

Where the parameter reference path binds, as :func:tvbo.templates.tvboptim.utils.parameter_keypath resolves it, and the dynamics parameter it names when that is the dynamics (None when it binds a coupling, an external input or a reserved scope, or the model declares no parameter of that name).

experiment_key

run.workflow.experiment_key(exp)

The canonical short key for an experiment.

An explicit key if set, else its id (the usual identifier, e.g. 40), else name. Used for job names, result stems, and kit paths so they read …-40 rather than a generic fallback — one source of truth shared by every emitter. :func:tvbo.run.study.experiment_ids is the wider match set --experiment selects by.

extract_axes

run.workflow.extract_axes(experiment)

Collect every ExplorationAxis declared on experiment.

The axis kind is inferred from the parameter path (see :func:tvbo.run.backends.axis_kind_of); placement defaults to "auto" and is resolved by :func:plan.

fan_expand_kwargs

run.workflow.fan_expand_kwargs(ep)

axis=EXP_<RULE>_<AXIS> kwargs binding an expand() to the fan’s value lists.

The value lists (EXP_<RULE>_<AXIS> = [...]) are emitted at the top of the Snakefile, so any rule in the same Snakefile (including an include:-d figure rule) can reference them to expand a fanned experiment’s whole grid of cells.

fan_input_expr

run.workflow.fan_input_expr(ep)

A Snakemake input expression matching ALL of ep’s output files.

A group run (no axes) is the single f"{OUT_DIR}/<key>/<stem>.h5"; a fanned experiment is the expand() over its wildcard-value lists — every cell. Emitted verbatim into a rule’s input:, so a figure that reads a fanned experiment depends on its whole grid.

is_per_node

run.workflow.is_per_node(param, leaf, nodes)

Whether the dynamics parameter param holds one value per node: its shape spans n_nodes, or a node declares it at a value of its own, which the generated code promotes to a per-node array; a node that names it without a value keeps the shared one.

mem_mb

run.workflow.mem_mb(mem)

'8G'/'8GB'/'512M'/'2000' -> integer mebibytes.

Feeds Snakemake’s mem_mb resource, which the SLURM executor renders as --mem. Every suffix sbatch --mem accepts is understood; an unrecognised one returns None rather than a wrong number, and the caller omits the resource. A sub-mebibyte request rounds up to 1, since 0 would reserve nothing.

merge_workflow_spec

run.workflow.merge_workflow_spec(study, experiment=None)

Merge the study’s workflow defaults with an experiment’s workflow.

The experiment block refines the study block: only the fields it sets take precedence, the rest are inherited. Pass the experiment object directly — it need not carry a key. With no experiment, only the study block is returned.

per_subject_cohorts

run.workflow.per_subject_cohorts(experiment)

The cohorts experiment’s network reads one member of per run (:meth:Network.per_subject_cohorts); empty for an experiment with no network, or one that reads none.

plan

run.workflow.plan(
    study_key,
    experiment,
    backend,
    engine='local',
    workflow_spec=None,
    overrides=None,
    source_spec='',
    experiment_selector=None,
    study=None,
)

Compute a :class:WorkflowPlan from an Experiment + spec.

workflow_spec mirrors the Study-level workflow: block from study.yaml (§4.10.1). Missing keys use sensible defaults, and an unset backend is the experiment’s own (see :func:tvbo.run.backends.effective_backend). study is the study the experiment belongs to, which tells whether an experiment it reads the result of writes one per subject (:func:refuse_per_subject_reads_on_device).

refuse_per_subject_reads_on_device

run.workflow.refuse_per_subject_reads_on_device(
    experiment,
    per_subject_experiments=(),
)

Raise when experiment, run as one on-device cohort job, would read something one subject at a time.

The job has no subject of its own. A cohort reference that follows the run (:meth:SimulationExperiment.per_subject_cohorts) would read every member at once in the cohort’s order, and the result of one of per_subject_experiments would be read as every shard at once or as the first shard, while the batch runs in the dataset’s order. A reference pinned to named members reads the same values for every subject and is not refused.

resolve_container_ref

run.workflow.resolve_container_ref(raw)

Resolve a recipe’s declared container into a reference singularity exec / apptainer exec can pull.

A local .sif/.simg path, or a reference that names its transport, passes through unchanged. A registry reference written without a transport (ghcr.io/org/tvbo:0.7.0, ubuntu, the spellings Docker takes) gains docker://, since Singularity reads a bare name as a local file path. Only tvbo’s own image has its version filled in, so an unpinned request for it pulls the image built from the running CLI’s source (:func:default_container_ref):

  • the symbolic requests tvbo / default;
  • tvbo’s repository written without a tag or digest (ghcr.io/virtual-twin/tvbo, or the repository TVBO_CONTAINER_IMAGE names), with or without docker://.

Any other reference without a tag (ubuntu, docker://rocker/r-ver) stays untagged, so the registry’s default tag applies as it would for docker pull: tvbo’s version is not a tag a third-party image carries.

No container declared ⇒ None: tasks run in the surrounding environment (bare, or the requirements venv setup.sh provisions — see :attr:WorkflowPlan.needs_env_layer). requirements are provisioned by whichever substrate the container field selects; they do NOT force a container of their own.

result_dependencies

run.workflow.result_dependencies(experiment)

The experiments whose saved results experiment reads, each as the identifier it is named by, in the order found: the source of a from_experiment initial state, a source: naming …exp<id> on an observation or a parameter, and a used: reference that names an experiment wherever the experiment declares it (:meth:SimulationExperiment.declared_references). A plan orders the experiment after each of them.

runtime_minutes

run.workflow.runtime_minutes(t)

A SLURM walltime -> integer minutes, for Snakemake’s runtime resource.

Accepts every spelling sbatch --time documents: minutes, minutes:seconds, hours:minutes:seconds, days-hours, days-hours:minutes and days-hours:minutes:seconds. The day-prefixed forms matter — without them a 3-00:00:00 walltime parses as nothing, the runtime resource is omitted, and jobs silently inherit the partition default instead of the declared limit. Any leftover seconds round up to a whole minute. Returns None when unset or unparseable.

workflow_config_from_spec

run.workflow.workflow_config_from_spec(spec)

Rebuild a datamodel WorkflowConfig from the merged workflow spec dict.

Lets an emitted kit freeze the effective configuration (study < experiment < --set) into its spec, so the spec re-emits identically without the flags being re-supplied — full, self-contained provenance. Returns None when the spec carries no workflow settings.

writes_per_subject

run.workflow.writes_per_subject(experiment)

Whether experiment writes one result per subject: it fans over its dataset’s subjects, or runs them as one on-device cohort.