nirs4all.api.explanation_envelope module

The nirs4all.explanation.v1 envelope shared by every SDK explain backend.

The schema is owned by nirs4all-methods (docs/explain/explanation_v1.schema.json). Hosts fill it from their attribution result and recorded metadata. Axis values and units come only from recorded dataset headers; unknown or derived feature spaces are labelled feature_index with a null unit. rows.sample_indices[i] is the index, within the supplied data, of explained row i; explained-row positions and source indices are never interchanged.

nirs4all.api.explanation_envelope.build_explanation_v1(*, kind: str, engine: dict[str, str], exactness: str, additive: bool, group_aggregation: str, attribution_space: str, output: dict[str, Any], axis: dict[str, Any], feature_names: Sequence[str], groups: list[dict[str, Any]], sample_indices: Sequence[int], attributions: ndarray, std_error: ndarray | None, expected: ndarray, prediction: ndarray, additivity_max_abs_error: float | None, settings: dict[str, Any]) → dict[str, Any][source]

Assemble a JSON-native nirs4all.explanation.v1 envelope.

Parameters:
  • kind – Attribution kind, such as permutation_shap or tree_shap.

  • engine – Producing engine name, version and optional ABI.

  • exactness – exact, sampled, surrogate or perturbation.

  • additive – Whether attributions plus expected values equal predictions.

  • group_aggregation – group_game, sum_of_feature_values or none.

  • attribution_space – raw_input or transformed_features.

  • output – Explained output {"index", "label", "space"}; index addresses the last axis of attributions.

  • axis – Axis of the explained features.

  • feature_names – Explained feature names.

  • groups – Group records from describe_groups().

  • sample_indices – Source index (within the supplied data) of each explained row.

  • attributions – (rows, groups, outputs) attributions.

  • std_error – Optional Monte-Carlo standard errors with the same shape; undefined entries are stored as null.

  • expected – (rows, outputs) expected values.

  • prediction – (rows, outputs) explained predictions.

  • additivity_max_abs_error – Maximum additivity error, None when not additive.

  • settings – Scalar explainer settings.

Returns:

The envelope dictionary.

nirs4all.api.explanation_envelope.describe_groups(names: Sequence[str], axis: dict[str, Any], feature_group: ndarray | None, n_groups: int, sources: Sequence[str]) → list[dict[str, Any]][source]

Describe contiguous feature groups for the envelope.

Parameters:
  • names – Explained feature names.

  • axis – Envelope axis of the explained features.

  • feature_group – Group of each feature, or None for one group per feature.

  • n_groups – Number of groups.

  • sources – Group source per group (per_feature, fixed_width, ranges or remainder).

Returns:

One record per group with half-open [feature_start, feature_end) bounds and inclusive axis coordinates.

nirs4all.api.explanation_envelope.feature_index_axis(n_features: int) → dict[str, Any][source]

Return the axis of features without a recorded physical coordinate.

nirs4all.api.explanation_envelope.recorded_axis(dataset: Any, n_features: int) → dict[str, Any][source]

Return the recorded spectral axis of a single-source dataset.

Parameters:
  • dataset – Materialized dataset whose rows are explained.

  • n_features – Number of explained raw input columns.

Returns:

A wavelength (nm) or wavenumber (cm-1) axis when the dataset has one source with numeric headers in that explicitly declared unit, otherwise a feature_index axis with a null unit.