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, BaseEstimator

Convert 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]]
fit(X, y=None)[source]

Fit the transformer.

inverse_transform(X, y=None)[source]

Transform percent to fraction.

transform(X, y=None)[source]

Transform fraction to percent.

class nirs4all.operators.transforms.signal_conversion.FromAbsorbance(target_type: str | SignalType = 'reflectance')[source]

Bases: TransformerMixin, BaseEstimator

Convert 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]]
fit(X, y=None)[source]

Fit the transformer.

inverse_transform(X, y=None)[source]

Convert back to absorbance.

transform(X, y=None)[source]

Transform absorbance to reflectance/transmittance.

class nirs4all.operators.transforms.signal_conversion.KubelkaMunk(source_type: str | SignalType = 'reflectance', epsilon: float = 1e-10)[source]

Bases: TransformerMixin, BaseEstimator

Apply 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
fit(X, y=None)[source]

Fit the transformer.

inverse_transform(X, y=None)[source]

Inverse Kubelka-Munk to recover reflectance.

From F(R) = (1-R)² / (2R), solving for R: R = 1 + F - sqrt(F² + 2F)

transform(X, y=None)[source]

Apply Kubelka-Munk transformation.

class nirs4all.operators.transforms.signal_conversion.PercentToFraction[source]

Bases: TransformerMixin, BaseEstimator

Convert 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]]
fit(X, y=None)[source]

Fit the transformer.

inverse_transform(X, y=None)[source]

Transform fraction to percent.

transform(X, y=None)[source]

Transform percent to fraction.

class nirs4all.operators.transforms.signal_conversion.SignalTypeConverter(source_type: str | SignalType = 'reflectance', target_type: str | SignalType = 'absorbance', epsilon: float = 1e-10)[source]

Bases: TransformerMixin, BaseEstimator

General-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)
fit(X, y=None)[source]

Fit the converter by determining the conversion path.

inverse_transform(X, y=None)[source]

Apply inverse transformation.

transform(X, y=None)[source]

Apply the conversion transformation.

class nirs4all.operators.transforms.signal_conversion.ToAbsorbance(source_type: str | SignalType = 'reflectance', epsilon: float = 1e-10, clip_negative: bool = True)[source]

Bases: TransformerMixin, BaseEstimator

Convert 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:

SignalType

is_percent_

Whether source was in percent (requires /100)

Type:

bool

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:
Return type:

self

inverse_transform(X, y=None)[source]

Convert absorbance back to reflectance/transmittance.

Parameters:

X (array-like of shape (n_samples, n_features)) – Absorbance values

Returns:

X_original – Reflectance or transmittance values

Return type:

ndarray of shape (n_samples, n_features)

transform(X, y=None)[source]

Transform reflectance/transmittance to absorbance.

Parameters:

X (array-like of shape (n_samples, n_features)) – Input spectral data

Returns:

X_transformed – Absorbance values

Return type:

ndarray of shape (n_samples, n_features)