Pipeline Workflow
The workflow layer turns intent into controlled execution. It takes your pipeline definition and datasets, expands variants from generators, executes steps with context-aware controller logic, and records outputs in a reproducible store.
Workflow Mission
Transform a high-level pipeline definition into:
Expanded variants from generators (
_or_,_range_)Controlled execution with context-aware step logic
Traceable outputs with fold/partition/branch context
Durable persistence in workspace store
Primary Execution Stack
The execution engine is layered for modularity:
Component |
Role |
|---|---|
|
Top-level facade for run/predict/explain/retrain |
|
Coordinates Cartesian product of datasets × pipeline configs |
|
Executes a single pipeline on a single dataset |
|
Parses, routes, and executes individual steps |
|
Normalizes raw step syntax into |
|
Selects the matching controller by priority |
|
Behavior adapter that executes the step |
End-to-End Training Flow
Here’s what happens when you call nirs4all.run():
sequenceDiagram
participant U as User
participant R as PipelineRunner
participant O as PipelineOrchestrator
participant E as PipelineExecutor
participant S as StepRunner
participant C as Controller
participant W as WorkspaceStore
U->>R: run(pipeline, dataset)
R->>O: execute(...)
O->>W: begin_run(...)
O->>E: execute steps per dataset/config
E->>W: begin_pipeline(...)
loop each step
E->>S: execute(step, context)
S->>S: parse step
S->>S: route controller
S->>C: execute(...)
C-->>S: context updates + artifacts
S-->>E: StepResult
end
E->>W: save chains, predictions, artifact refs
E->>W: complete_pipeline(...)
O->>W: complete_run(...)
O-->>R: run predictions + per-dataset outputs
R-->>U: RunResult
Pipeline Normalization Phase
Before execution, PipelineConfigs normalizes your pipeline definition. It accepts:
Lists of step objects
Dicts with pipeline keys
JSON/YAML file paths
Generator expansion happens here:
_or_(variant alternatives) → multiple pipeline configs_range_(parameter sweeps) → grid of parameter values
Each expanded variant receives a generated identity.
Example
pipeline = [
{"_or_": [SNV(), MSC(), Detrend()]},
PLSRegression(n_components={"_range_": [5, 10, 15]}),
]
# Expands to 9 variants:
# SNV + PLS(5), SNV + PLS(10), SNV + PLS(15)
# MSC + PLS(5), MSC + PLS(10), MSC + PLS(15)
# Detrend + PLS(5), Detrend + PLS(10), Detrend + PLS(15)
Dataset Normalization Phase
Dataset inputs are normalized via DatasetConfigs, which handles:
Path-based loading (
"sample_data/regression")Config parsing (dicts with loader settings)
Direct
SpectroDatasetinstances
Once normalized, the orchestrator operates on consistent dataset objects.
Orchestration Topology
The orchestrator executes a Cartesian product of each dataset with each expanded pipeline config.
This allows sweep-style experiments naturally:
3 datasets × 20 pipeline variants = 60 executions
During this phase, the orchestrator:
Starts run-level persistence
Initializes shared runtime components
Manages post-run summaries
Context Initialization
The executor initializes an ExecutionContext containing:
Component |
Purpose |
|---|---|
DataSelector |
Immutable data selection parameters (partition, processing, layout, etc.) |
PipelineState |
Mutable state (current step number, y_processing name, execution mode) |
StepMetadata |
Controller coordination data (keyword, operator type) |
Custom dict |
Controller-specific state (e.g., branch paths) |
Aggregate column |
For prediction aggregation policy |
RuntimeContext Role
RuntimeContext is the infrastructure context. It carries runtime services rather than data state:
Service |
Purpose |
|---|---|
|
WorkspaceStore for SQLite-backed persistence |
|
Artifact management during training |
|
For predict/explain modes |
|
For executing sub-steps (e.g., inside branches) |
|
Identifiers |
|
Recording execution traces |
|
Step-level caching |
|
Retrain mode control |
|
Current execution phase ( |
This separates data semantics from infrastructure wiring.
Step Processing Pipeline
Each step goes through three mini-phases:
Parse — convert syntax into
ParsedStepRoute — select the best matching controller by priority
Execute — apply controller logic with context and dataset
Example: Transform Step
# Pipeline definition
pipeline = [MinMaxScaler()]
# Step processing:
# 1. Parse: MinMaxScaler() → ParsedStep(operator=MinMaxScaler(), keyword=None)
# 2. Route: Check all controllers → TransformerMixinController matches (priority 10)
# 3. Execute: Controller calls fit_transform on training data, stores artifact
Keyword Semantics
Pipeline keywords influence controller matching. Key keywords:
Keyword |
Purpose |
|---|---|
|
Define model step |
|
Target scaling |
|
Mark samples (non-removal) |
|
Exclude samples from training |
|
Parallel/separation branches |
|
Combine branch outputs |
|
Generator for variant alternatives |
|
Parameter sweep generator |
Additional internal keywords: feature_augmentation, sample_augmentation, auto_transfer_preprocessing, concat_transform.
Controller Routing Logic
Routing is simple and deterministic:
Evaluate all registered controllers
Keep those whose
matches()returnsTrueSort by numeric
priority(lower wins)Instantiate the first match
Priority Strategy
Lower priority values win earlier. Use low priority for specific behaviors and higher priority for generic ones.
Example: TensorFlowModelController (priority 4) wins over SklearnModelController (priority 6) for TensorFlow objects.
Execution Modes
NIRS4ALL supports four execution modes:
Train Mode
The richest lifecycle:
Split controllers define folds
Transform controllers fit and transform
Model controllers train and score
Predictions are stored with fold/partition context
Traces capture step-level artifact references
Predict Mode
Replays persisted artifacts:
Training-only steps are skipped
Controllers load stored artifacts
Transformations replay in pipeline order
Model artifacts generate final predictions
Explain Mode
Resembles predict mode but:
Collects explanation artifacts (SHAP values)
Generates SHAP charts
Returns feature importance
Refit Mode
Retrains the winning configuration:
Runs after CV evaluation
Trains on full training partition (excluding holdout/test)
Produces deployment-ready final model
Out-of-Fold (OOF) Training and Prediction Lifecycle
OOF is the core mechanism behind honest validation and stacking-safe features. Each prediction is produced by a model that did not train on that row.
flowchart TD
A[Split definitions in dataset] --> B[Fold 0 train]
A --> C[Fold 1 train]
A --> D[Fold K train]
B --> E[Val preds fold 0]
C --> F[Val preds fold 1]
D --> G[Val preds fold K]
E --> H[OOF assembly]
F --> H
G --> H
H --> I[Ranking, diagnostics, stacking features]
The OOF contract:
Each row linked to sample identity
Fold identity persisted for auditability
Partition and branch context retained
Model/preprocessing signatures retained
This allows:
Fair variant comparison (no leakage)
Leak-safe meta-model features (for stacking)
Fold-specific instability diagnosis
Stacking Example
When building a meta-model (stacking), base model predictions for meta-model training must be OOF-generated to avoid target leakage.
Wrong: Use in-sample predictions from base models
Meta-model sees predictions from models trained on the same data
Optimistically biased
Right: Use OOF predictions from base models
Meta-model sees predictions from models that never saw those samples
Honest generalization
CV-to-Refit Lifecycle Pattern
A robust production pattern: train across folds first, then retrain the winner on full training data.
sequenceDiagram
participant O as Orchestrator
participant CV as CV Execution
participant S as Selector/Ranker
participant R as Refit Executor
participant W as Workspace
O->>CV: run all candidates on folds
CV->>W: store fold metrics + OOF + fold artifacts
CV-->>S: candidate score table
S-->>R: winner config id
R->>R: train winner on full training partition
R->>W: store refit artifact and deployment chain
W-->>O: both CV history and final model available
Lifecycle steps:
Phase A: Run candidate pipelines with fold-based evaluation. Persist fold metrics, artifacts, and OOF outputs.
Phase B: Rank candidates using a predefined metric. Select the winner with deterministic tie-break rules.
Phase C: Refit only the winning configuration on the full training partition (excluding holdout/test).
Phase D: Persist refit artifacts as the deployment-default chain. Keep CV artifacts for audit.
Why this matters:
CV answers model-selection quality
Refit answers deployment-readiness
Conflating them creates either under-trained deployment artifacts or biased selection.
Retrain and Transfer Workflow
Retrain uses a source model/chain plus new data. Modes include:
Full retrain — retrain all steps from scratch
Transfer-style reuse — freeze preprocessing, retrain model
Fine-tune — load model weights and continue training
Per-step mode control determines whether to train or reuse artifacts.
Branching Workflow
Branching enables parallel subflows in two families:
Duplication Branches
All samples go to each branch (parallel preprocessing/modeling):
{"branch": [
[SNV(), PLSRegression(10)],
[MSC(), RandomForestRegressor()],
]}
Use case: Compare preprocessing chains, then merge for ranking.
Separation Branches
Samples are partitioned across branches (by metadata, tag, or source):
{"branch": {"by_metadata": "site"}}
{"branch": {"by_tag": "outlier_flag", "values": {"clean": False, "outliers": True}}}
Use case: Subgroup-specific models (e.g., one model per site).
Branch context is tracked in the execution context and becomes important for artifact and prediction resolution.
Merge Workflow
Merge operations reconcile branch outputs. Strategies:
Strategy |
Use case |
Branch type |
|---|---|---|
|
Use OOF predictions as stacking features |
Duplication branches |
|
Use transformed features from branches |
Duplication branches |
|
Reassemble samples in original order |
Separation branches |
Merge logic must handle asymmetry (shape differences, model availability differences). Merging is one of the highest-complexity areas.
Feature Augmentation Workflow
Feature augmentation orchestrates repeated preprocessing operations in extend, add, or replace modes.
Extend: Add new processing chains (grow 3D array)
Add: Add derived features to existing processing
Replace: Overwrite existing processing
This changes processing growth patterns and directly influences memory footprint.
Sample Augmentation Workflow
Sample augmentation generates synthetic variants in:
Standard count mode: Fixed number of augmented samples per base sample
Balancing-aware mode: Based on labels or metadata (e.g., oversample minority class)
Especially useful for imbalanced classification.
Trace and Chain Lifecycle
Execution trace captures step outcomes and artifacts. The chain builder converts trace data into replayable chains containing:
Ordered steps
Model step index
Fold artifact references
Shared preprocessing references
Branch/source metadata
Chains are first-class deployment and replay units.
Prediction Persistence Workflow
Prediction rows are persisted with rich metadata:
dataset_name,pipeline_id,pipeline_uid,pipeline_namefold_id,partitionbranch_id,branch_namemodel_class,preprocessing_signatureScalar metrics (
rmse,r2,accuracy, etc.)
Array payloads (y_true, y_pred, y_proba) are stored in a dedicated array table.
This split keeps scalar query paths efficient while preserving detailed replay capability.
Workflow State Transitions
stateDiagram-v2
[*] --> Normalized
Normalized --> RunningVariants
RunningVariants --> RunningSteps
RunningSteps --> Persisting
Persisting --> Completed
RunningSteps --> Failed
Persisting --> Failed
Failed --> [*]
Completed --> [*]
Workflow Contracts
Key contracts that controllers and the execution engine must uphold:
Step controllers must return context updates consistently
Prediction mode must never assume training-time fitting
Artifact naming and indexing must remain deterministic for replay
Branch and fold semantics must be preserved through persistence
Run metadata must reflect actual execution conditions
Next Steps
Now that you understand how pipelines execute, explore:
Controllers Intro — Learn how to extend execution behavior
Workspace Intro — See how results are persisted
Data Workflow — Review how data flows through execution
See also
Related Examples:
U01: Multi-Model - Generator syntax with
_or_for model comparisonD01: Generator Syntax - Complete generator syntax reference
D02: Generator Advanced - Advanced generators with constraints
D01: Branching Basics - Pipeline branching and merging