nirs4all.operators.filters.native module

n4m sample-filter roles as nirs4all sample filters.

n4m.roles exposes every native sample filter (Y/X outliers, leverage, spectral quality) through one role interface, NativeSampleFilter (fit / get_mask). The exclude, tag and branch controllers accept those roles directly; resolve_sample_filter() is their single entry point for a filter given as an instance, a role token string ("n4m:filters.…") or a serialized component dict.

class nirs4all.operators.filters.native.NativeRoleFilter(role: Any, reason: str | None = None, tag_name: str | None = None)[source]

Bases: SampleFilter

An n4m.roles sample filter, fitted and applied natively.

Parameters:
  • role – The n4m role instance (for example n4m.roles.YOutlierFilter()).

  • reason – Exclusion reason; defaults to the role’s class name.

  • tag_name – Tag name for the tag controller.

property exclusion_reason: str

Get the exclusion reason identifier for this filter.

Returns:

Reason string to be stored in indexer’s exclusion_reason column.

Return type:

str

fit(X: ndarray, y: ndarray | None = None) → NativeRoleFilter[source]

Compute filter criteria from training data.

This method should learn any thresholds, statistics, or models needed to identify outliers/bad samples. Override in subclasses for filters that need to learn from data.

Parameters:
  • X – Feature array of shape (n_samples, n_features).

  • y – Target array of shape (n_samples,) or (n_samples, n_targets). May be None for X-only filters.

Returns:

The fitted filter instance.

Return type:

self

get_mask(X: ndarray, y: ndarray | None = None) → ndarray[source]

Compute boolean mask indicating which samples to KEEP.

This is the core method that must be implemented by all concrete filters. Returns True for samples that should be kept, False for samples to exclude.

Parameters:
  • X – Feature array of shape (n_samples, n_features).

  • y – Target array of shape (n_samples,) or (n_samples, n_targets). May be None for X-only filters.

Returns:

Boolean array of shape (n_samples,) where:
  • True means KEEP the sample

  • False means EXCLUDE the sample

Return type:

np.ndarray

Raises:

NotImplementedError – If the subclass doesn’t implement this method.

nirs4all.operators.filters.native.as_sample_filter(obj: Any) → SampleFilter | None[source]

obj as a SampleFilter: itself, an n4m sample-filter role wrapped, else None.