nirs4all.api.session module
Session context manager for nirs4all API.
A Session maintains shared resources across multiple nirs4all operations, including a reusable PipelineRunner instance, consistent workspace paths, and shared logging configuration.
Two usage patterns:
- Context manager for shared runner (resource reuse):
>>> with nirs4all.session(verbose=2, save_artifacts=True) as s: ... r1 = nirs4all.run(pipeline1, data1, session=s) ... r2 = nirs4all.run(pipeline2, data2, session=s) ... # Both runs share workspace and configuration
- Stateful session with pipeline (training workflow):
>>> session = nirs4all.Session(pipeline=pipeline, name="MyModel") >>> result = session.run(dataset) >>> predictions = session.predict(new_data) >>> session.save("model.n4a")
- exception nirs4all.api.session.SessionClosedError[source]
Bases:
RuntimeErrorAn operation was attempted on a terminally closed Session.
- nirs4all.api.session.load_native_archive_session(path: str | Path, *, methods_library_path: str | Path | None = None, source: N4aSource | None = None) NativeArchiveSession[source]
Open a validated native Package V2/V3 session without a legacy fallback.
Core opens the container once; its declared native root selects Package V2 or V3 and Core’s owner reader validates the selected closure. An already opened
sourceis adopted by the returned session on success (the caller still closes it on failure).
- nirs4all.api.session.load_session(path: str | Path, *, engine: str | None = None, methods_library_path: str | Path | None = None) Session | NativeArchiveSession[source]
Load a session from a saved bundle file.
- Parameters:
path – Path to a host
.n4abundle or a native Package V2/V3 container.methods_library_path – Optional exact Methods runtime used by a native Package V2 session. Use the same runtime path for training and loading within a process; native identity checks remain enforced.
- Returns:
Session ready for prediction.
Notes
Core opens a native Package V2 container once; the Session retains that handle, so the cached package, descriptor set and libn4m identity stay bound to the verified container bytes and Core refuses a changed file at the next read.
Example
>>> session = nirs4all.load_session("exports/model.n4a") >>> predictions = session.predict(new_data)
- nirs4all.api.session.session(pipeline: list[Any] | None = None, name: str = '', **kwargs: Any) Generator[Session, None, None][source]
Create an execution session context manager.
This is a convenience function that creates a Session and yields it within a context manager block.
- Parameters:
pipeline – Optional pipeline definition for stateful mode.
name – Name for the session/pipeline.
**kwargs – Arguments passed to Session (and ultimately PipelineRunner). Common options: - verbose (int): Verbosity level (0-3). Default: 1 - save_artifacts (bool): Save model artifacts. Default: True - workspace_path (str|Path): Workspace directory. - random_state (int): Random seed for reproducibility.
- Yields:
Session – The active session for use within the block.
- Example (resource sharing):
>>> with nirs4all.session(verbose=2, save_artifacts=True) as s: ... r1 = nirs4all.run(pipeline1, data1, session=s) ... r2 = nirs4all.run(pipeline2, data2, session=s) ... print(f"PLS: {r1.best_score:.4f}, RF: {r2.best_score:.4f}")
- Example (stateful pipeline):
>>> with nirs4all.session(pipeline=my_pipeline, name="Demo") as s: ... result = s.run("sample_data/regression") ... print(f"Best score: {result.best_score:.4f}")