nirs4all.pipeline.portable_n4m_trained module

Read and replay a cross-language trained n4m pipeline without host pickles.

The envelope carries a shared JSON recipe, fitted native preprocessing state, and one native N4MM model. Numerical transforms and prediction remain in Methods. This is a bounded native pipeline format, not a general DAG-ML archive.

class nirs4all.pipeline.portable_n4m_trained.PortableN4MTrainedPipeline(document: dict[str, Any])[source]

Bases: object

An imported native PLS or sparse PLS-DA predictor and retraining recipe.

Use close() or a context manager to release the N4MM handle. The original JSON recipe remains available for training a fresh model through the normal Python PipelineConfigs/StepParser path. Version 3 adds supervised SPA state to regression PLS recipes; its wire indices are zero based and ranked, while prediction projects columns in spectral order. Version 4 applies the same state contract to generic native selectors. Version 5 carries a predict-only affine N4MM and a declarative n4m regression recipe for fresh fitting; the payload does not attest which algorithm originally fitted its coefficients. Version 6 instead carries an ordered fitted N4MP preprocessing artifact beside the N4MM predictor. Version 7 adds an ordered, train-only native X augmentation prefix to the recipe; prediction still replays only N4MP and N4MM.

close() → None[source]

Release native model resources, idempotently.

classmethod fit_recipe(recipe: dict[str, Any], X: Any, y: Any, *, preprocessing: str = 'legacy') → PortableN4MTrainedPipeline[source]

Train a native n4m recipe in Python and produce the portable envelope.

Methods owns every transform, SPA selection and PLS fit. For MSC/EMSC, the portable reference comes from the native getter when available. The published 1.0.21 binding lacks that getter, so only its documented column-mean reference is reconstructed from the same training rows as a fallback.

classmethod from_json(source: str | Path) → PortableN4MTrainedPipeline[source]

Read a JSON document or filesystem path.

predict(X: Any) → ndarray[source]

Predict regression values or ordered classification labels.

predict_proba(X: Any) → ndarray[source]

Return uncalibrated softmax probabilities for sparse PLS-DA.

predict_scores(X: Any) → ndarray[source]

Return raw native Methods scores for a classification pipeline.

retrain(X: Any, y: Any) → RefittedN4MPipeline | PortableN4MTrainedPipeline[source]

Fit the shared n4m recipe afresh on new Python training rows.

This does not reuse the imported model or MSC/EMSC references. The returned in-memory pipeline is a new Methods fit, not a serialized cross-language package.

to_json(file: str | Path | None = None) → str[source]

Return or write the exact portable trained-pipeline envelope.

class nirs4all.pipeline.portable_n4m_trained.RefittedN4MPipeline(steps: list[Any], model: Any, width: int, feature_names: list[str] | None, task: str = 'regression', classes: list[str] | None = None)[source]

Bases: object

Fresh native Python fit from a loaded cross-language recipe.

predict(X: Any) → ndarray[source]

Predict with the freshly fitted Methods operators and model.