SubtractReference#
- class chemotools.baseline.SubtractReference(reference: ndarray | None = None, scale_reference: bool = False, start: int = 0, end: int = -1, x_axis: ndarray | None = None)[source]
Bases:
XAxisMixin,TransformerMixin,OneToOneFeatureMixin,BaseEstimatorSubtract a reference spectrum from spectral data.
By default, the transformer computes \(x - r\) for each sample. When
scale_reference=True, the reference is first scaled by an optimal factor \(a\) that solves:\[\min_a \|x - a \cdot r\|_2\]and returns \(x - a \cdot r\). The factor can be computed over a sub-range of the spectrum defined by
startandend.- Parameters:
reference (np.ndarray, optional, default=None) – The reference spectrum to subtract from the input data. If None, the original spectrum is returned.
scale_reference (bool, default=False) – If True, the reference is scaled by a factor \(a\) before subtraction, where \(a\) minimises \(\|x - a \cdot r\|_2\) (or over the sub-range defined by
start/end). If False, a simple subtraction \(x - r\) is performed andstart,end, andx_axisare ignored.start (int, default=0) – Index or x-axis value of the start of the range used to compute the scaling factor. Only used when
scale_reference=True.end (int, default=-1) – Index or x-axis value of the end of the range used to compute the scaling factor. Only used when
scale_reference=True.x_axis (array-like, optional) – X-axis values corresponding to columns. When provided,
startandendare interpreted as x-axis values and the closest indices are used. Must be ascending if provided.
- Variables:
n_features_in (int) – The number of features in the input data.
reference (np.ndarray or None) – The reference spectrum to subtract from the input data. None if the reference parameter was not provided.
Examples
>>> from chemotools.baseline import SubtractReference >>> from chemotools.datasets import load_fermentation_train >>> # Load sample data >>> X, _ = load_fermentation_train() >>> # Convert X to a numpy array >>> X = np.array(X) >>> # Instantiate the transformer with a reference spectrum >>> reference = X[0] >>> transformer = SubtractReference(reference=reference) SubtractReference() >>> transformer.fit(X) >>> # Generate baseline-corrected data >>> X_corrected = transformer.transform(X)
- fit(X: ndarray, y=None) SubtractReference[source]
Fit the transformer to the input data.
- Parameters:
X (np.ndarray of shape (n_samples, n_features)) – The input data to fit the transformer to.
y (None) – Ignored to align with API.
- Returns:
self – The fitted transformer.
- Return type:
SubtractReference
- transform(X: ndarray, y=None) ndarray[source]
Transform the input data by subtracting the reference spectrum.
- Parameters:
X (np.ndarray of shape (n_samples, n_features)) – The input data to transform.
y (None) – Ignored to align with API.
- Returns:
X_transformed – The transformed data.
- Return type:
np.ndarray of shape (n_samples, n_features)