nirs4all.pipeline.bundle.generator module

Bundle Generator - Export trained pipelines as standalone bundles.

This module provides the BundleGenerator class for exporting trained pipelines to various bundle formats (.n4a, .n4a.py) for deployment, sharing, or archival.

Bundle Formats:
.n4a: Full nirs4all bundle (ZIP archive) containing:
  • manifest.json: Bundle metadata and version info

  • pipeline.json: Minimal pipeline configuration

  • trace.json: Execution trace for deterministic replay

  • artifacts/: Directory with artifact binaries

  • fold_weights.json: CV fold weights (if applicable)

.n4a.py: Portable Python script with:
  • Embedded artifacts (base64 encoded)

  • Standalone predict() function

  • No nirs4all dependency (only numpy, joblib required)

Example

>>> from nirs4all.pipeline.bundle import BundleGenerator
>>>
>>> generator = BundleGenerator(workspace_path)
>>> bundle_path = generator.export(best_prediction, "exports/model.n4a")
>>> print(f"Bundle saved to: {bundle_path}")
class nirs4all.pipeline.bundle.generator.BundleFormat(value)[source]

Bases: str, Enum

Supported bundle export formats.

N4A

Full ZIP bundle with all artifacts and metadata

N4A_PY

Portable Python script with embedded artifacts

N4A = 'n4a'
N4A_PY = 'n4a.py'
class nirs4all.pipeline.bundle.generator.BundleGenerator(workspace_path: str | Path, verbose: int = 0)[source]

Bases: object

Generate standalone prediction bundles from trained pipelines.

This class exports trained pipelines to bundle formats that can be used for deployment, sharing, or archival without requiring the original workspace or full nirs4all installation.

workspace_path

Path to the workspace root

resolver

PredictionResolver for resolving prediction sources

verbose

Verbosity level for logging

Example

>>> generator = BundleGenerator(workspace_path)
>>> generator.export(best_prediction, "model.n4a")
>>>
>>> # Export to portable script
>>> generator.export(best_prediction, "model.n4a.py", format="n4a.py")
export(source: Dict[str, Any] | str | Path, output_path: str | Path, format: str | BundleFormat = BundleFormat.N4A, include_metadata: bool = True, compress: bool = True) Path[source]

Export a prediction source to a bundle.

Parameters:
  • source – Prediction source (prediction dict, folder path, etc.)

  • output_path – Path for the output bundle

  • format – Bundle format (‘n4a’ or ‘n4a.py’)

  • include_metadata – Whether to include full metadata in bundle

  • compress – Whether to compress artifacts (for .n4a format)

Returns:

Path to the created bundle

Raises: