nirs4all.operators.base.spectra_mixin module
SpectraTransformerMixin base class for wavelength-aware transformations.
This module provides a base class for spectral transformations that require wavelength information. The controller automatically provides wavelengths from the dataset when available and when the operator declares it needs them.
- class nirs4all.operators.base.spectra_mixin.SpectraTransformerMixin[source]
Bases:
TransformerMixin,BaseEstimatorBase class for spectral transformations that require wavelength information.
This mixin extends sklearn’s TransformerMixin to support wavelength-aware transformations. The controller automatically provides wavelengths from the dataset when available and when the operator declares it needs them.
Subclasses must implement transform_with_wavelengths() instead of transform().
- Parameters:
parameters. (None - this is a mixin class. Subclasses define their own)
- _requires_wavelengths
Class-level flag indicating whether this operator requires wavelengths. If True (default), transform() will raise ValueError if wavelengths are not provided. Subclasses can set this to False if wavelengths are optional.
- Type:
Examples
>>> class TemperatureAugmenter(SpectraTransformerMixin): ... def __init__(self, temperature_delta: float = 5.0): ... self.temperature_delta = temperature_delta ... ... def transform_with_wavelengths( ... self, X: np.ndarray, wavelengths: np.ndarray ... ) -> np.ndarray: ... # Apply temperature-dependent spectral changes ... # ... implementation ... ... return X_transformed
Notes
The controller detects SpectraTransformerMixin instances via:
needs_wavelengths = ( isinstance(op, SpectraTransformerMixin) and getattr(op, '_requires_wavelengths', False) )
Wavelengths are extracted from the dataset using dataset.wavelengths_nm(source).
- fit(X, y=None, **fit_params)[source]
Fit is a no-op for most spectral transformations.
- Parameters:
- Returns:
self – Returns self.
- Return type:
- set_transform_request(*, wavelengths: bool | None | str = '$UNCHANGED$') SpectraTransformerMixin
Configure whether metadata should be requested to be passed to the
transformmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed totransformif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it totransform.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- transform(X, wavelengths: ndarray | None = None)[source]
Transform method that delegates to transform_with_wavelengths.
If wavelengths are not provided and the operator requires them, this will raise a ValueError.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input spectra array.
wavelengths (ndarray of shape (n_features,) or None, default=None) – Wavelength array in nm. Required if _requires_wavelengths is True.
- Returns:
X_transformed – Transformed spectra array.
- Return type:
ndarray of shape (n_samples, n_features)
- Raises:
ValueError – If wavelengths are not provided and _requires_wavelengths is True.
- abstractmethod transform_with_wavelengths(X: ndarray, wavelengths: ndarray | None) ndarray[source]
Apply the transformation using wavelength information.
Subclasses must implement this method to perform the actual transformation.
- Parameters:
X (ndarray of shape (n_samples, n_features)) – Input spectra.
wavelengths (ndarray of shape (n_features,) or None) – Wavelength array in nm. May be None if _requires_wavelengths is False.
- Returns:
X_transformed – Transformed spectra.
- Return type:
ndarray of shape (n_samples, n_features)
- Raises:
NotImplementedError – If the subclass does not implement this method.