nirs4all.api.tuning module
Public helpers for native DAG-ML tuning results.
- class nirs4all.api.tuning.NativeTuning(space: Mapping[str, Any], force_params: Mapping[str, Any] | None = None, engine: str = 'optuna', metric: str = 'rmse', direction: str = 'minimize', n_trials: int = 50, sampler: str | None = None, pruner: str | None = None, seed: int | None = None, resume: bool = False, storage: str | None = None, study_name: str | None = None, score_data: TuningScoreData | Mapping[str, Any] | tuple[Any, ...] | list[Any] | None = None, winner: TuningWinner | Mapping[str, Any] | None = None, calibration: TuningCalibration | Mapping[str, Any] | None = None, workspace_tuning_id: str | None = None, workspace_metadata: Mapping[str, Any] | None = None)[source]
Bases:
objectTyped public configuration for the current native
run(tuning=...)lane.
- class nirs4all.api.tuning.OrderedSearchSpaceSpec(parameters: tuple[SearchSpaceParameter, ...])[source]
Bases:
objectCanonical ordered tuning search space.
This is the P3 bridge between public
tuning.spacesyntax and optimizer adapters. It is intentionally JSON-native and execution-free: adapters can walkparametersin a stable order and produceParameterPatchrecords without reparsing public dict syntax.- classmethod from_mapping(space: Mapping[str, Any], *, context: str = 'run(tuning=...).space') OrderedSearchSpaceSpec[source]
Normalize a public mapping into deterministic ordered parameters.
- classmethod from_tuning_spec(tuning: DagMLTuningSpec) OrderedSearchSpaceSpec[source]
Build the ordered space from a parsed tuning spec.
- parameters: tuple[SearchSpaceParameter, ...]
- patch(path: str, value: Any, *, context: str = 'tuning parameter patch') ParameterPatch[source]
Create a validated patch for one known path.
- class nirs4all.api.tuning.ParameterPatch(path: str, segments: tuple[str, ...], value: Any)[source]
Bases:
objectOne canonical candidate or force-parameter patch.
- class nirs4all.api.tuning.SearchSpaceParameter(index: int, path: str, segments: tuple[str, ...], spec: Any)[source]
Bases:
objectOne ordered, canonical tuning-space parameter.
pathis the public patch path consumed byPipelineObjective. The parser accepts both dotted public spelling (ridge.alpha) and sklearn double-underscore spelling (ridge__alpha), then canonicalizes to dotted spelling so Optuna, n4m and native adapters see the same ordered contract.
- class nirs4all.api.tuning.TunedSingleEstimatorConformalResult(run: RunResult, calibrated: CalibratedRunResult | PredictResult)[source]
Bases:
objectResult bundle returned by
tune_single_estimator(..., calibration=...).- calibrated: CalibratedRunResult | PredictResult
- property conformal_guarantee_status: dict[str, Any] | None
Guarantee metadata exposed by the calibrated result, when present.
- property interval_coverages: tuple[float, ...]
Materialized conformal interval coverages on the calibrated result.
- metrics(y_true: Any) dict[float, Any][source]
Evaluate materialized conformal intervals against observed targets.
- robustness(*, y_true: Any, X: Any | None = None, predictor: Any | None = None, mode: str = 'clean_frozen', scenarios: Sequence[Any] | None = None, slice_by: Sequence[str] | None = None, metadata: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None, seed: int | None = None, workspace_path: str | Path | None = None, workspace_name: str = '', workspace_robustness_id: str | None = None, workspace_metadata: Mapping[str, Any] | None = None) Any[source]
Compute a robustness report from the calibrated prediction result.
- run: RunResult
- property tuning_best_params: dict[str, Any]
Best native tuning parameters attached to the underlying run.
- class nirs4all.api.tuning.TuningCalibration(y_pred: Any, prediction_sample_ids: Any, coverage: float | Sequence[float] = 0.9, method: str = 'split_absolute_residual', unit: str = 'physical_sample', workspace_conformal_id: str | None = None, workspace_metadata: Mapping[str, ~typing.Any] | None=None, as_predict_result: bool = True, extra: Mapping[str, ~typing.Any]=<factory>)[source]
Bases:
objectTyped public conformal calibration payload nested under
tuning.
- class nirs4all.api.tuning.TuningConformalScoreCalibration(X: Any | None = None, X_calibration: Any = None, features: Any = None, y_true: Any | None = None, y: Any | None = None, y_calibration: Any = None, target: Any = None, targets: Any = None, sample_ids: Any = None, calibration_sample_ids: Any = None, physical_sample_ids: Any = None, groups: Any = None, calibration_groups: Any = None, metadata: Any = None, calibration_metadata: Any = None)[source]
Bases:
objectTyped development calibration cohort for conformal-aware tuning scores.
This cohort is used only inside
run(tuning=...).score_datato rank candidates with a temporary split conformal calibrator. It is not the finalrun(..., calibration=...)calibration payload.
- class nirs4all.api.tuning.TuningPassthrough[source]
Bases:
objectTyped JSON-native marker for an optional non-final preprocessing step.
Use this in
NativeTuning.spacewhen a named preprocessing step should be searchable as a no-op without relying on the raw"passthrough"string.
- class nirs4all.api.tuning.TuningScoreData(X: Any | None = None, X_score: Any | None = None, y: Any | None = None, y_score: Any | None = None, metric: str | None = None, score_metric: str | None = None, sample_ids: Any = None, score_sample_ids: Any = None, prediction_sample_ids: Any = None, physical_sample_ids: Any = None, groups: Any = None, score_groups: Any = None, metadata: Any = None, score_metadata: Any = None, dataset: Any | None = None, selector: Mapping[str, Any] | None = None, sample_id_column: str | None = None, group_column: str | None = None, metadata_columns: str | Sequence[str] | None = None, include_augmented: bool = False, conformal_calibration: TuningConformalScoreCalibration | Mapping[str, Any] | None = None, conformal_coverage: float | None = None)[source]
Bases:
objectTyped public score cohort for
run(tuning=...).Use either explicit
X/yarrays, a positionaltupleat the call site, or an explicit dataset-backed source viadataset+selector. The object serializes to the mapping form consumed by the existing runtime.
- class nirs4all.api.tuning.TuningWinner(X: Any | None = None, y_true: Any | None = None, score: float | None = None, metric: str | None = None, sample_ids: Any = None, winner_sample_ids: Any = None, prediction_sample_ids: Any = None, physical_sample_ids: Any = None, dataset_name: str = 'tuning_winner', model_name: str | None = None, task_type: str = 'regression', metadata: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None, dataset: Any | None = None, selector: Mapping[str, Any] | None = None, sample_id_column: str | None = None, group_column: str | None = None, metadata_columns: str | Sequence[str] | None = None, include_augmented: bool = False)[source]
Bases:
objectTyped public winner projection cohort for
run(tuning=...).
- nirs4all.api.tuning.get_tuning_space_schema() dict[str, Any][source]
Return the JSON Schema for ordered tuning search-space artifacts.
- nirs4all.api.tuning.inspect_tuning_space(tuning: DagMLTuningSpec | NativeTuning | Mapping[str, Any]) dict[str, Any][source]
Inspect and fingerprint the ordered public tuning search space.
The returned mapping is JSON-native and intended for docs, CLIs, Studio and bindings. It shows the canonical dotted patch paths, deterministic parameter order, optional
force_paramsas decodedParameterPatchrecords, the search-space fingerprint and the enclosing tuning-contract fingerprint.
- nirs4all.api.tuning.load_workspace_tuning_result(workspace_path: str | Path, tuning_id: str) TuningResult[source]
Load a verified native DAG-ML tuning result from a nirs4all workspace.
- nirs4all.api.tuning.save_workspace_tuning_result(workspace_path: str | Path, result: TuningResult, *, name: str = '', tuning_id: str | None = None, metadata: Mapping[str, Any] | None = None, run_id: str | None = None, pipeline_id: str | None = None, chain_id: str | None = None) str[source]
Persist a native DAG-ML tuning result in a nirs4all workspace.
- nirs4all.api.tuning.tune_single_estimator(pipeline: Any, X: Any, y: Any, tuning: DagMLTuningSpec | NativeTuning | Mapping[str, Any], *, score_extractor: Callable[[Any], float] | None = None, X_score: Any | None = None, y_score: Any | None = None, score_metric: str | None = None, score_sample_ids: Any = None, score_groups: Any = None, score_metadata: Any = None, sample_ids: Any = None, groups: Any = None, metadata: Any = None, clone_estimator: bool = True, refit: bool = True, workspace_path: str | Path | None = None, workspace_name: str = '', workspace_tuning_id: str | None = None, workspace_metadata: Mapping[str, Any] | None = None, run_id: str | None = None, pipeline_id: str | None = None, chain_id: str | None = None, resume_tuning_result: TuningResult | None = None, resume_tuning_id: str | None = None, per_dataset: Mapping[str, Any] | None = None, winner_x: Any | None = None, winner_y_true: Any | None = None, winner_score: float | None = None, winner_metric: str | None = None, winner_sample_ids: Sequence[Any] | None = None, winner_dataset_name: str = 'tuning_winner', winner_model_name: str | None = None, winner_task_type: str = 'regression', winner_metadata: Mapping[str, Any] | None = None, calibration: TuningCalibration | Mapping[str, Any] | None = None) RunResult | TunedSingleEstimatorConformalResult[source]
Tune the explicit native linear lane and return a
RunResult.This is the narrow Python surface for the currently implemented native DAG-ML tuning seams. It accepts only the single-estimator and linear transformer→estimator forms supported by
compile_pipeline_objectiveand is not a replacement for the planned fullrun(tuning=...)pipeline compiler.Scoring is explicit. Provide exactly one of:
a custom
score_extractorcallable;explicit
X_score/y_scorearguments;NativeTuning(score_data=...)or a mappingtuning["score_data"].
When a cohort is supplied, the scorer uses
score_metric, the metric nested inscore_data, or the metric fromtuning. Optional scoring identities are validated for row alignment and forwarded topredict()when the fitted estimator accepts those keyword arguments.