nirs4all.api.native_n4a module

Canonical Core .n4a bridge: one Core-owned fit, sealed bytes, thin glue.

Core’s native producer (nirs4all_core.produce_n4a_native) is the one and only fit of this profile. It runs cross-validation, selection and refit once and returns an already sealed canonical container plus its owner view. Canonical retrain is the same single fit: nirs4all_core.retrain_n4a_native reads the validated recipe and source stored in the container and runs a fresh owner cross-validation, selection and refit on a new record. This module only validates request shapes at the public boundary, calls Core, projects the owner scores into a RunResult, and publishes the sealed bytes. It never trains, re-inspects at export, or invents scientific fingerprints; routing, validation and replay semantics stay with Core, DAG-ML and Methods.

The profile is selected explicitly with engine="native" and native_profile=CANONICAL_N4A_NATIVE_PROFILE. It never falls back to the interim Archive V2/V3 lanes or to the bundle path.

nirs4all.api.native_n4a.CANONICAL_N4A_NATIVE_PROFILE = 'nirs4all.n4a.container.v1'

Explicit native_profile selecting the canonical Core .n4a producer.

nirs4all.api.native_n4a.CANONICAL_N4A_SCHEMA = 'nirs4all.n4a.container.v1'

Exact manifest schema that identifies a canonical container.

exception nirs4all.api.native_n4a.CanonicalN4aDependencyError[source]

Bases: CanonicalN4aError

The installed nirs4all_core lacks the canonical .n4a bridge.

exception nirs4all.api.native_n4a.CanonicalN4aError[source]

Bases: RuntimeError

The canonical Core .n4a profile refused or could not complete.

class nirs4all.api.native_n4a.CanonicalN4aRunResult(projected: RunResult, *, archive_bytes: bytes, view: Mapping[str, Any], methods_library_path: str, selection: Mapping[str, Any])[source]

Bases: RunResult

Score projection of one Core-sealed canonical container.

export publishes the sealed bytes produced by the single Core fit; it never trains, replays or re-inspects. best_score (and the metric shortcut that matches the owner’s selection metric) is Core’s selected OOF score, never a retained-test measurement; other shortcuts are NaN rather than a test substitute. The train-only producer refuses non-train records, so it supplies no retained-test scores (best["test_score"] and final_score are None).

property best_mae: float | None

Core’s selected OOF score when the owner selected on mae; otherwise None.

property best_score: float

Core’s selected OOF score for the owner-selected variant.

property canonical_view: dict[str, Any]

Return the owner view (manifest, receipt, training) from the single fit.

close() → None[source]

No native handle is retained; the sealed bytes stay immutable.

export(output_path: str | Path, format: str = 'n4a', source: dict[str, Any] | None = None, chain_id: str | None = None, *, compatibility: str | None = None) → Path[source]

Atomically write the already sealed canonical bytes.

property native_archive_reference: dict[str, str]

Return the container id and SHA-256 of the sealed bytes.

property selection: dict[str, Any]

Return Core’s owner SELECT decision (variant, metric, objective, level, OOF score).

nirs4all.api.native_n4a.inspect_n4a(path: str | Path, *, source: N4aSource | None = None, include_host_json: bool = False) → dict[str, Any][source]

Inspect current container metadata without loading fitted Python objects.

Defaults preserve the existing Core view. source borrows an SDK N4aSource; use open_n4a_source(core_file=...) to adapt a retained Core file. Borrowed sources remain open. Host JSON mode returns exact owner documents and opaque fitted-artifact descriptors, never models. JSON inspection and the existing bundle pre-model checks do not establish scientific scoring or executability.

nirs4all.api.native_n4a.predict_canonical_n4a(path: str | Path, data: Any, *, methods_library_path: str | Path | None = None, verbose: int = 0, source: N4aSource | None = None) → PredictResult[source]

Replay a canonical container on one external IO record through Core.

data is {"record": <IO record path or bytes>, "partition": <partition>}. Core validates the container at this boundary and every refusal propagates; nothing is fitted and no other route is tried.

Core’s replay block is ordered by sample id, not by record row. Core also returns the physical source/partition order as sample_ids; y_pred and metadata["sample_ids"] follow that order (the signed replay and receipt are returned untouched). The SDK never parses the record: a missing, duplicated or differently-populated id list is refused.

nirs4all.api.native_n4a.retrain_canonical_n4a(source: Any, data: Any, *, name: str = 'retrain_dataset', new_model: Any = None, epochs: int | None = None, verbose: int = 1, save_artifacts: bool = True, options: Mapping[str, Any] | None = None, opened_source: N4aSource | None = None) → CanonicalN4aRunResult[source]

Retrain a canonical container on a new IO record through Core’s single fit.

source is a canonical .n4a path or the CanonicalN4aRunResult it was produced as; data is the new exact IO record (path or bytes, or {"record": ...}). Core reads the validated stored recipe and source id and runs fresh cross-validation, selection and refit through the same producer: this is always a full retrain, never a transfer or fine-tune, and the caller supplies no recipe or source id. The source container and result are left unchanged; the returned result owns new sealed bytes.

Every option Core cannot honor is refused before any fit. The only supported option is methods_library_path.

nirs4all.api.native_n4a.run_native_n4a(pipeline: Any, dataset: Any, *, name: str = '', verbose: int = 1, save_artifacts: bool = True, save_charts: bool = False, plots_visible: bool = False, random_state: int | None = None, refit: Any = True, cache: Any = None, project: str | None = None, report_naming: str = 'nirs', results_path: Any = None, session: Any = None, tuning: Any = None, calibration: Any = None, terminal_predict: Any = None, runner_kwargs: Mapping[str, Any] | None = None) → CanonicalN4aRunResult[source]

Produce one canonical container through Core’s single fit.

Every option Core’s producer cannot honor is refused explicitly instead of being ignored: seeds, cross-validation/tuning controls, calibration, sessions and workspaces.