nirs4all.pipeline.bundle.loader module
Bundle Loader - Load and predict from exported bundles.
This module provides the BundleLoader class for loading legacy Python prediction bundles (.n4a format) and running predictions without the original workspace. This replay path is deprecated; DAG-ML/Core archives use their native replay sessions and never execute BundleLoader model artifacts.
- The loader supports:
Loading bundle metadata and structure
Extracting artifacts on-demand
Building artifact provider for prediction
Resolving bundles for PredictionResolver
Full support for branching pipelines
Full support for meta-models (stacking)
CV ensemble prediction with fold weights
Example
>>> from nirs4all.pipeline.bundle import BundleLoader
>>>
>>> loader = BundleLoader("model.n4a")
>>> print(loader.metadata.pipeline_uid)
>>> y_pred = loader.predict(X_new)
>>>
>>> # Or via PredictionResolver
>>> resolver = PredictionResolver(workspace_path)
>>> resolved = resolver.resolve("model.n4a") # Automatically uses BundleLoader
- exception nirs4all.pipeline.bundle.loader.BundleArtifactLoadError[source]
Bases:
RuntimeErrorAn indexed Python artifact could not be read or deserialized.
An unavailable or damaged fitted transformer must never be treated as an absent optional step: continuing could silently change model predictions.
- class nirs4all.pipeline.bundle.loader.BundleArtifactProvider(bundle_path: str | Path, artifact_index: dict[str, str], fold_weights: dict[int, float] | None = None, step_info: dict[int, dict[str, Any]] | None = None, trace: ExecutionTrace | None = None, *, source: N4aSource | None = None)[source]
Bases:
ArtifactProviderArtifact provider for bundles.
Provides artifacts from a loaded bundle, with lazy loading and caching. Supports branching pipelines and meta-models.
- Variables:
bundle_path – Path to the bundle file
artifact_cache – Cache of loaded artifacts
artifact_index – Index mapping step/fold to artifact filenames
fold_weights – Fold weights for CV ensemble
step_info – Information about each step (operator_type, branch_path, etc.)
- get_artifact(step_index: int, fold_id: int | str | None = None) Any | None[source]
Get a single artifact for a step.
- Parameters:
step_index – 1-based step index
fold_id – Optional fold ID for fold-specific artifacts
- Returns:
Artifact object or None if not found
- get_artifacts_for_step(step_index: int, branch_path: list[int] | None = None, branch_id: int | None = None, source_index: int | None = None, substep_index: int | None = None) list[tuple[str, Any]][source]
Get all artifacts for a step.
- Parameters:
step_index – 1-based step index
branch_path – Optional branch path filter
- Returns:
List of (artifact_id, artifact_object) tuples
- get_fold_artifacts(step_index: int, branch_path: list[int] | None = None) list[tuple[int, Any]][source]
Get all fold-specific artifacts for a step.
- Parameters:
step_index – 1-based step index
branch_path – Optional branch path filter
- Returns:
List of (fold_id, artifact_object) tuples, sorted by fold_id
- get_fold_weights() dict[int, float][source]
Get fold weights for CV ensemble.
- Returns:
Fold weights dictionary
- get_meta_model_sources(step_index: int) list[tuple[int, str]][source]
Get source model info for a meta-model step.
- Parameters:
step_index – Step index of the meta-model
- Returns:
List of (source_step_index, artifact_id) tuples
- class nirs4all.pipeline.bundle.loader.BundleMetadata(bundle_format_version: str = '1.0', nirs4all_version: str = '', created_at: str = '', pipeline_uid: str = '', source_type: str = '', model_step_index: int | None = None, fold_strategy: str = 'weighted_average', preprocessing_chain: str = '', trace_id: str | None = None, original_manifest: dict[str, ~typing.Any]=<factory>, partitioner_routing: dict[str, ~typing.Any]=<factory>, relation_replay_manifest: dict[str, ~typing.Any]=<factory>, retrain_lineage: dict[str, ~typing.Any]=<factory>)[source]
Bases:
objectMetadata for a prediction bundle.
Contains information about the bundle format, source, and contents.
- Variables:
bundle_format_version (str) – Version of the bundle format
nirs4all_version (str) – Version of nirs4all that created the bundle
created_at (str) – ISO timestamp of bundle creation
pipeline_uid (str) – UID of the source pipeline
source_type (str) – Type of source that was exported
model_step_index (int | None) – Index of the model step
fold_strategy (str) – Strategy for combining fold predictions
preprocessing_chain (str) – Summary of preprocessing steps
trace_id (str | None) – ID of the execution trace (if available)
original_manifest (dict[str, Any]) – Subset of original manifest metadata
partitioner_routing (dict[str, Any]) – Routing info for metadata partitioner branches
relation_replay_manifest (dict[str, Any]) – Reference to the optional N9 relation replay manifest embedded in the bundle.
retrain_lineage (dict[str, Any]) – Additive API-004 provenance for a natively retrained artifact, empty for ordinary prediction bundles.