A library of algorithms for the baseline correction of experimental data.
npx @tessl/cli install tessl/pypi-pybaselines@1.2.0A comprehensive Python library providing over 60 algorithms for baseline correction of experimental data from scientific techniques such as Raman, FTIR, NMR, XRD, XRF, PIXE, MALDI-TOF, and LIBS spectroscopy. pybaselines offers multiple mathematical approaches including polynomial fitting, spline methods, Whittaker smoothing, morphological operations, and classification-based techniques for removing baseline interference from experimental measurements.
pip install pybaselinesimport pybaselinesObject-oriented interface:
from pybaselines import BaselineFunctional interface:
from pybaselines import whittaker, polynomial, smooth, morphological, spline, classification, misc, optimizers2D baseline correction:
from pybaselines import Baseline2Dimport numpy as np
from pybaselines import Baseline
import matplotlib.pyplot as plt
# Generate sample data with baseline
x = np.linspace(0, 1000, 1000)
signal_peaks = 100 * np.exp(-((x - 200) / 50)**2) + 50 * np.exp(-((x - 800) / 100)**2)
baseline_drift = 10 + 0.01 * x + 0.00001 * x**2
noise = np.random.normal(0, 2, x.shape)
data = signal_peaks + baseline_drift + noise
# Initialize baseline corrector
baseline_fitter = Baseline(x_data=x)
# Apply Asymmetric Least Squares (AsLS) method
baseline, params = baseline_fitter.asls(data, lam=1e6, p=1e-2)
# Get corrected signal
corrected_signal = data - baseline
# Results
print(f"Baseline fitted with {len(params['weights'])} data points")
print(f"Final tolerance: {params['tol_history'][-1]:.2e}")Functional interface:
from pybaselines.whittaker import asls
# Direct function call
baseline, params = asls(data, lam=1e6, p=1e-2, x_data=x)
corrected_signal = data - baselinepybaselines provides both functional and object-oriented interfaces:
Baseline: Main 1D baseline correction class that inherits methods from all algorithm modulesBaseline2D: 2D baseline correction class for image and 2D spectroscopic data(baseline, params) tuple for consistencyThis design enables users to choose between convenient object-oriented access and direct functional calls while maintaining consistent interfaces across all 65+ algorithms.
Penalized least squares methods using Whittaker smoothing for iteratively reweighted baseline fitting. These methods excel at handling spectra with varying peak widths and baseline curvature.
def asls(data, lam=1e6, p=1e-2, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
"""Asymmetric Least Squares baseline correction."""
def airpls(data, lam=1e6, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None, normalize_weights=False):
"""Adaptive iteratively reweighted Penalized Least Squares."""
def arpls(data, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
"""Asymmetrically reweighted Penalized Least Squares."""Polynomial-based baseline correction using various fitting strategies and outlier handling approaches. Suitable for simple baseline shapes and when interpretable parameters are desired.
def poly(data, poly_order=2, weights=None, return_coef=False, x_data=None):
"""Basic polynomial baseline fitting."""
def modpoly(data, poly_order=2, tol=1e-3, max_iter=250, weights=None, use_original=False, mask_initial_peaks=True, num_std=1.0, x_data=None, return_coef=False):
"""Modified polynomial with iterative peak masking."""
def quant_reg(data, poly_order=2, quantile=0.05, tol=1e-6, max_iter=1000, weights=None, eps=None, x_data=None, return_coef=False):
"""Quantile regression polynomial baseline."""Algorithms that use smoothing operations and iterative filtering to separate baseline from signal. Effective for noisy data and spectra with complex peak structures.
def snip(data, max_half_window=None, decreasing=False, smooth_half_window=None, filter_order=2, x_data=None, pad_kwargs=None, **kwargs):
"""Statistical Sensitive Non-linear Iterative Peak algorithm."""
def swima(data, min_half_window=3, max_half_window=None, smooth_half_window=None, x_data=None, pad_kwargs=None, **kwargs):
"""Small-window moving average baseline."""
def ipsa(data, half_window=None, max_iter=500, tol=None, roi=None, original_criteria=False, x_data=None, pad_kwargs=None, **kwargs):
"""Iterative Polynomial Smoothing Algorithm."""Mathematical morphology operations for baseline estimation using structuring elements and morphological transformations. Particularly effective for chromatographic and mass spectrometry data.
def mor(data, half_window=None, x_data=None, pad_kwargs=None, **kwargs):
"""Morphological baseline using opening operation."""
def rolling_ball(data, half_window=None, x_data=None, pad_kwargs=None, **kwargs):
"""Rolling ball baseline algorithm."""
def tophat(data, half_window=None, x_data=None, pad_kwargs=None, **kwargs):
"""Top-hat morphological baseline."""Penalized spline methods that combine the flexibility of splines with various penalty functions. Offers excellent balance between smoothness and data fidelity.
def pspline_asls(data, lam=1e3, p=1e-2, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
"""Penalized spline version of AsLS."""
def mixture_model(data, lam=1e5, p=1e-2, num_knots=100, spline_degree=3, diff_order=3, max_iter=50, tol=1e-3, weights=None, symmetric=False, num_bins=None):
"""Mixture model baseline using splines."""
def irsqr(data, lam=100, quantile=0.05, num_knots=100, spline_degree=3, diff_order=3, max_iter=100, tol=1e-6, weights=None, x_data=None):
"""Iterative reweighted spline quantile regression."""Methods that classify data points as belonging to baseline or peaks using statistical and heuristic approaches. Effective for automatic baseline detection without user parameter tuning.
def fabc(data, lam=1e5, diff_order=2, weights=None, weights_as_mask=False, x_data=None):
"""Fully automatic baseline correction."""
def dietrich(data, smooth_half_window=None, interp_half_window=None, max_iter=100, tol=1e-3, x_data=None, pad_kwargs=None, **kwargs):
"""Dietrich classification baseline."""
def cwt_br(data, poly_order=2, scales=None, num_std=1., ridge_kwargs=None, x_data=None):
"""Continuous wavelet transform baseline recognition."""Specialized algorithms including spline-based denoising and manual baseline point interpolation for specific use cases.
def beads(data, lam_0=0.5, lam_1=5, lam_2=4, asymmetry=0.1, max_iter=15, tol=1e-3, x_data=None):
"""Baseline Estimation And Denoising using Splines."""
def interp_pts(data, baseline_points, interp_method='linear', x_data=None):
"""Interpolate baseline from manually selected points."""Advanced methods for parameter optimization, collaborative baseline correction, and adaptive parameter selection for improved baseline fitting performance.
def collab_pls(data, average_dataset=True, method='asls', method_kwargs=None, x_data=None):
"""Collaborative Penalized Least Squares."""
def adaptive_minmax(data, x_data=None, poly_order=None, method='modpoly', weights=None, constrained_fraction=0.01, constrained_weight=1e5, estimation_poly_order=2, method_kwargs=None):
"""Adaptive min-max baseline correction."""
def optimize_extended_range(data, x_data=None, method='asls', side='both', width_scale=0.1, height_scale=1., sigma_scale=1./12., min_value=2, max_value=8, step=1, pad_kwargs=None, method_kwargs=None):
"""Optimize parameters using extended range."""Complete 2D versions of most baseline correction algorithms for processing images, 2D spectra, and other two-dimensional experimental data.
class Baseline2D:
"""Main interface for 2D baseline correction algorithms."""
def __init__(self, x_data=None, z_data=None, check_finite=True, assume_sorted=False, output_dtype=None):
"""Initialize 2D baseline corrector."""# Common return type for all baseline correction methods
BaselineResult = tuple[np.ndarray, dict]
# Weight arrays for iterative methods
WeightArray = np.ndarray
# Parameter dictionaries contain method-specific results
class ParameterDict(dict):
"""
Dictionary containing method results and convergence information.
Common keys:
- 'weights': Final weight array used for fitting
- 'tol_history': Convergence tolerance history for iterative methods
- Method-specific parameters vary by algorithm
"""
# Common parameter types
class PaddingKwargs(dict):
"""
Padding parameters for edge handling in windowed methods.
Common keys:
- 'mode': Padding mode ('reflect', 'constant', 'edge', etc.)
- 'constant_values': Values to use for constant padding
"""