AirPls#
- class chemotools.baseline.AirPls(lam: float = 10000.0, nr_iterations: int = 100, solver_type: Literal['banded', 'sparse'] = 'banded', max_iter_after_warmstart: int = 20)[source]
Bases:
_BaselineWhittakerMixin,_BaseWhittakerAdaptive Iteratively Reweighted Penalized Least Squares (AirPls) baseline correction.
AirPls is a widely used algorithm for removing baselines from spectroscopic signals. It iteratively reweights residuals to suppress positive deviations (peaks) while adapting baseline estimates using an exponential weight update. A second-order difference operator (recommended) is used as the penalty term, ensuring the estimated baseline is smooth.
The Whittaker smoothing step can be solved using either:
a banded solver (fast and memory-efficient, recommended for most spectra)
a sparse LU solver (more stable for ill-conditioned problems)
For efficiency, AirPls supports warm-starting: when processing multiple spectra with similar baseline structure, weights from a previous fit can be reused, typically reducing the number of iterations required.
- Parameters:
lam (float, default=1e4) – Regularization parameter controlling smoothness of the baseline. Larger values yield smoother baselines.
nr_iterations (int, default=100) – Maximum number of reweighting iterations.
solver_type (Literal["banded", "sparse"], default="banded") – If “banded”, use the banded solver for Whittaker smoothing. If “sparse”, use a sparse LU decomposition.
max_iter_after_warmstart (int, default=20) – Maximum iterations allowed when warm-starting from previous weights.
- Variables:
n_features_in (int) – The number of features in the input data.
DtD (np.ndarray) –
The precomputed banded representation of \(D^T D\) for the second-order difference operator.
Stored as a banded representation (
solveh_bandedformat) ifsolver_type='banded'Stored as a
scipy.sparseCSC matrix ifsolver_type='sparse'
self.w_init (np.ndarray) – The weights set for warm-starting.
References
- [1] Z.-M. Zhang, S. Chen, Y.-Z. Liang.
“Baseline correction using adaptive iteratively reweighted penalized least squares.” Analyst 135 (5), 1138–1146 (2010).
Examples
>>> from chemotools.baseline import AirPls >>> from chemotools.datasets import load_fermentation_train >>> # Load sample data >>> X, _ = load_fermentation_train() >>> # Instantiate the transformer >>> transformer = AirPls(lam=1e4, nr_iterations=100) AirPls() >>> transformer.fit(X) >>> # Remove baselines >>> X_corrected = transformer.transform(X)
Attributes
w_init_- fit(X: ndarray, y=None) AirPls[source]
Fit AirPls model to spectra.
- Parameters:
X (np.ndarray of shape (n_samples, n_features)) – The input spectra to fit the model to.
y (None) – Ignored to align with API.
- Returns:
self – Fitted estimator.
- Return type:
AirPls
- transform(X: ndarray, y=None) ndarray[source]
Apply AirPls baseline correction.
- Parameters:
X (np.ndarray of shape (n_samples, n_features)) – The input spectra to transform.
y (None) – Ignored to align with API.
- Returns:
X_transformed – The baseline-corrected spectra.
- Return type:
np.ndarray of shape (n_samples, n_features)