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: RuntimeError

An 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: ArtifactProvider

Artifact 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.)

close() → None[source]

Close only a source opened by this standalone provider.

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

get_step_operator_type(step_index: int) → str | None[source]

Get the operator type for a step.

Parameters:

step_index – 1-based step index

Returns:

Operator type string or None

has_artifacts_for_step(step_index: int) → bool[source]

Check if artifacts exist for a step.

Parameters:

step_index – 1-based step index

Returns:

True if artifacts are available for this step

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: object

Metadata 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.

bundle_format_version: str = '1.0'
created_at: str = ''
fold_strategy: str = 'weighted_average'
classmethod from_dict(data: dict[str, Any]) → BundleMetadata[source]

Create BundleMetadata from dictionary.

Parameters:

data – Dictionary from bundle manifest.json

Returns:

BundleMetadata instance

model_step_index: int | None = None
nirs4all_version: str = ''
original_manifest: dict[str, Any]
partitioner_routing: dict[str, Any]
pipeline_uid: str = ''
preprocessing_chain: str = ''
relation_replay_manifest: dict[str, Any]
retrain_lineage: dict[str, Any]
source_type: str = ''
trace_id: str | None = None
nirs4all.pipeline.bundle.loader.load_bundle(bundle_path: str | Path) → BundleLoader[source]

Convenience function to load a bundle.

Parameters:

bundle_path – Path to the .n4a bundle file

Returns:

BundleLoader instance