CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pybaselines

A library of algorithms for the baseline correction of experimental data.

Pending
Overview
Eval results
Files

whittaker.mddocs/

Whittaker-Smoothing Methods

Penalized least squares methods using Whittaker smoothing for baseline correction. These methods iteratively reweight data points to minimize the influence of peaks while maintaining baseline smoothness through regularization. They excel at handling spectra with varying peak widths and complex baseline curvature.

Capabilities

Asymmetric Least Squares (AsLS)

The foundational asymmetric least squares method that assigns lower weights to positive deviations (peaks) and higher weights to negative deviations (baseline).

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.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter. Higher values create smoother baselines
    - p (float): Asymmetry parameter (0 < p < 1). Lower values give less weight to positive residuals
    - diff_order (int): Order of differential matrix (typically 2)
    - max_iter (int): Maximum iterations for convergence
    - tol (float): Convergence tolerance for iterative fitting
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params) where params contains 'weights' and 'tol_history'
    """

Improved AsLS (IAsLS)

Enhanced version of AsLS with adaptive parameter adjustment for better convergence and peak handling.

def iasls(data, x_data=None, lam=1e6, p=1e-2, lam_1=1e-4, max_iter=50, tol=1e-3, weights=None, diff_order=2):
    """
    Improved Asymmetric Least Squares baseline correction.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values  
    - lam (float): Primary smoothing parameter
    - p (float): Asymmetry parameter
    - lam_1 (float): Secondary smoothing parameter for adaptive adjustment
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - diff_order (int): Order of differential matrix
    
    Returns:
    tuple: (baseline, params)
    """

Adaptive Iteratively Reweighted PLS (airPLS)

Automatically adjusts weights based on data characteristics without requiring manual parameter tuning.

def airpls(data, lam=1e6, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None, check_finite=True):
    """
    Adaptive iteratively reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    - check_finite (bool): Whether to check for finite values in input data
    
    Returns:
    tuple: (baseline, params)
    """

Asymmetrically Reweighted PLS (arPLS)

Uses asymmetric reweighting to handle both positive and negative deviations from the baseline.

def arpls(data, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Asymmetrically reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Doubly Reweighted PLS (drPLS)

Applies dual reweighting strategies for improved baseline estimation in complex spectra.

def drpls(data, lam=1e5, eta=0.5, max_iter=50, tol=1e-3, weights=None, diff_order=2, x_data=None):
    """
    Doubly reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - eta (float): Weighting parameter for dual reweighting (0 < eta < 1)
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - diff_order (int): Order of differential matrix
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Improved arPLS (IarPLS)

Enhanced version of arPLS with better convergence properties and peak detection.

def iarpls(data, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Improved asymmetrically reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Adaptive Smoothness PLS (asPLS)

Adapts smoothness based on local data characteristics for optimal baseline-peak separation.

def aspls(data, lam=1e5, diff_order=2, max_iter=100, tol=1e-3, weights=None, alpha=None, x_data=None):
    """
    Adaptive smoothness Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - alpha (float, optional): Adaptive smoothness parameter
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Peaked Signal's AsLS Algorithm (psalsa)

Specifically designed for data with sharp, well-defined peaks using selective peak screening.

def psalsa(data, lam=1e5, p=0.5, k=None, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Peaked Signal's Asymmetric Least Squares Algorithm.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - p (float): Asymmetry parameter
    - k (int, optional): Number of points for peak screening
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Derivative Peak-Screening AsLS (derpsalsa)

Uses derivative information for enhanced peak detection and screening in complex spectra.

def derpsalsa(data, lam=1e6, p=1e-2, k=None, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Derivative Peak-Screening Asymmetric Least Squares Algorithm.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - p (float): Asymmetry parameter
    - k (int, optional): Number of points for peak screening
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    
    Returns:
    tuple: (baseline, params)
    """

Bayesian Reweighted PLS (BrPLS)

Incorporates Bayesian statistical principles for optimal weight determination.

def brpls(data, x_data=None, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, max_iter_2=50, tol_2=1e-3, weights=None):
    """
    Bayesian Reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations for outer loop
    - tol (float): Convergence tolerance for outer loop
    - max_iter_2 (int): Maximum iterations for inner loop
    - tol_2 (float): Convergence tolerance for inner loop
    - weights (array-like, optional): Initial weight array
    
    Returns:
    tuple: (baseline, params)
    """

Locally Symmetric Reweighted PLS (LSRPLS)

Uses local symmetry properties for improved baseline estimation in symmetric peak regions.

def lsrpls(data, x_data=None, lam=1e5, diff_order=2, max_iter=50, tol=1e-3, weights=None):
    """
    Locally Symmetric Reweighted Penalized Least Squares.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values
    - lam (float): Smoothing parameter
    - diff_order (int): Order of differential matrix
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    
    Returns:
    tuple: (baseline, params)
    """

Usage Examples

Basic AsLS baseline correction:

import numpy as np
from pybaselines.whittaker import asls

# Sample data with peaks and baseline drift
x = np.linspace(0, 1000, 1000)
data = 100 * np.exp(-((x - 200) / 30)**2) + 10 + 0.01 * x

# Apply AsLS baseline correction
baseline, params = asls(data, lam=1e6, p=1e-2, x_data=x)
corrected = data - baseline

print(f"Converged in {len(params['tol_history'])} iterations")

Automatic baseline correction with airPLS:

from pybaselines.whittaker import airpls

# No manual parameter tuning required
baseline, params = airpls(data, lam=1e6)
corrected = data - baseline

Install with Tessl CLI

npx tessl i tessl/pypi-pybaselines

docs

classification.md

index.md

misc.md

morphological.md

optimizers.md

polynomial.md

smooth.md

spline.md

two-d.md

whittaker.md

tile.json