nirs4all.operators.transforms.signal_conversion module
Signal type conversion transformers.
This module provides sklearn-compatible transformers for converting between spectral signal types (absorbance, reflectance, transmittance).
Mathematical conversions: - Reflectance to Absorbance: A = -log10(R) or A = log10(1/R) - Transmittance to Absorbance: A = -log10(T) - Percent to Fraction: X_frac = X_pct / 100 - Kubelka-Munk: F(R) = (1-R)² / (2R)
All transformers follow the sklearn TransformerMixin pattern.
- class nirs4all.operators.transforms.signal_conversion.FractionToPercent[source]
Bases:
TransformerMixin,BaseEstimatorConvert fractional [0, 1] values to percentage [0, 100] range.
Simply multiplies by 100.
Examples
>>> transformer = FractionToPercent() >>> X_frac = np.array([[0.5, 0.6], [0.7, 0.8]]) >>> X_pct = transformer.fit_transform(X_frac) >>> # X_pct = [[50, 60], [70, 80]]
- class nirs4all.operators.transforms.signal_conversion.FromAbsorbance(target_type: str | SignalType = 'reflectance')[source]
Bases:
TransformerMixin,BaseEstimatorConvert absorbance to reflectance or transmittance.
Applies the inverse log transform: R/T = 10^(-A)
- Parameters:
target_type (str or SignalType) – Output signal type. Valid: “reflectance”, “reflectance%”, “transmittance”, “transmittance%”
Examples
>>> from nirs4all.operators.transforms.signal_conversion import FromAbsorbance >>> transformer = FromAbsorbance(target_type="reflectance") >>> A = np.array([[0.301, 0.398], [0.222, 0.301]]) >>> R = transformer.fit_transform(A) >>> # R ≈ [[0.5, 0.4], [0.6, 0.5]]
- class nirs4all.operators.transforms.signal_conversion.KubelkaMunk(source_type: str | SignalType = 'reflectance', epsilon: float = 1e-10)[source]
Bases:
TransformerMixin,BaseEstimatorApply Kubelka-Munk transformation for diffuse reflectance.
The Kubelka-Munk function: F(R) = (1-R)² / (2R)
This is theoretically more appropriate for scattering media (powders) than simple log(1/R), though in NIR the benefit is dataset-dependent.
- Parameters:
source_type (str or SignalType) – Input signal type. Valid: “reflectance”, “reflectance%”
epsilon (float, default=1e-10) – Small value to avoid division by zero
Examples
>>> from nirs4all.operators.transforms.signal_conversion import KubelkaMunk >>> transformer = KubelkaMunk(source_type="reflectance") >>> R = np.array([[0.5, 0.4], [0.6, 0.5]]) >>> F_R = transformer.fit_transform(R) >>> # F_R[0,0] = (1-0.5)² / (2*0.5) = 0.25 / 1 = 0.25
- class nirs4all.operators.transforms.signal_conversion.PercentToFraction[source]
Bases:
TransformerMixin,BaseEstimatorConvert percentage values to fractional [0, 1] range.
Simply divides by 100.
Examples
>>> transformer = PercentToFraction() >>> X_pct = np.array([[50, 60], [70, 80]]) >>> X_frac = transformer.fit_transform(X_pct) >>> # X_frac = [[0.5, 0.6], [0.7, 0.8]]
- class nirs4all.operators.transforms.signal_conversion.SignalTypeConverter(source_type: str | SignalType = 'reflectance', target_type: str | SignalType = 'absorbance', epsilon: float = 1e-10)[source]
Bases:
TransformerMixin,BaseEstimatorGeneral-purpose signal type converter.
Automatically determines the conversion path between source and target signal types and applies the appropriate transformation.
- Parameters:
source_type (str or SignalType) – Input signal type
target_type (str or SignalType) – Output signal type
epsilon (float, default=1e-10) – Small value to avoid numerical issues
Examples
>>> from nirs4all.operators.transforms.signal_conversion import SignalTypeConverter >>> converter = SignalTypeConverter( ... source_type="reflectance%", ... target_type="absorbance" ... ) >>> R_pct = np.array([[50, 40], [60, 50]]) >>> A = converter.fit_transform(R_pct)
- class nirs4all.operators.transforms.signal_conversion.ToAbsorbance(source_type: str | SignalType = 'reflectance', epsilon: float = 1e-10, clip_negative: bool = True)[source]
Bases:
TransformerMixin,BaseEstimatorConvert reflectance or transmittance to absorbance.
Applies the log transform: A = -log10(X)
For reflectance, this gives “pseudo-absorbance” which is widely used in NIR but is not identical to true absorbance in transmission.
- Parameters:
source_type (str or SignalType) – Input signal type. If “auto”, attempts to detect. Valid: “reflectance”, “reflectance%”, “transmittance”, “transmittance%”
epsilon (float, default=1e-10) – Small value to add to avoid log(0)
clip_negative (bool, default=True) – If True, clips negative values to epsilon before log transform
- source_type_
Detected or specified source signal type
- Type:
Examples
>>> from nirs4all.operators.transforms.signal_conversion import ToAbsorbance >>> transformer = ToAbsorbance(source_type="reflectance") >>> R = np.array([[0.5, 0.4, 0.3], [0.6, 0.5, 0.4]]) >>> A = transformer.fit_transform(R) >>> # A ≈ [[0.301, 0.398, 0.523], [0.222, 0.301, 0.398]]
- fit(X, y=None)[source]
Fit the transformer.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input spectral data
y (None) – Ignored
- Return type:
self