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

spline.mddocs/

Spline-Based Methods

Baseline correction algorithms using spline functions for flexible, smooth baseline estimation. These methods combine the smoothness of splines with various fitting strategies including penalized regression, quantile fitting, and morphological operations. They excel at handling complex baseline curvature while maintaining computational efficiency through knot-based approximation.

Capabilities

Mixture Model Spline Baseline

Estimates baseline using a mixture model approach with B-spline basis functions for optimal baseline-peak separation.

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.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter for spline regularization
    - p (float): Asymmetry parameter for peak/baseline separation
    - num_knots (int): Number of knots for spline basis
    - spline_degree (int): Degree of B-spline basis functions
    - diff_order (int): Order of difference penalty matrix
    - max_iter (int): Maximum iterations for convergence
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - symmetric (bool): Whether to use symmetric weighting
    - num_bins (int, optional): Number of histogram bins for mixture modeling
    
    Returns:
    tuple: (baseline, params) containing spline coefficients and fit statistics
    """

Iterative Reweighted Spline Quantile Regression

Combines spline fitting with quantile regression for robust baseline estimation resistant to outliers.

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.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - quantile (float): Quantile level for regression (0 < quantile < 1)
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Corner-Cutting Algorithm

Geometric baseline estimation using iterative corner-cutting operations on spline representations.

def corner_cutting(data, x_data=None, max_iter=100):
    """
    Corner-cutting baseline algorithm.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values
    - max_iter (int): Maximum iterations for corner cutting
    
    Returns:
    tuple: (baseline, params) with geometric fitting information
    """

Penalized Spline AsLS (pspline_asls)

Spline-based implementation of Asymmetric Least Squares using B-spline basis functions.

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.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - p (float): Asymmetry parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline IAsLS (pspline_iasls)

Enhanced spline-based AsLS with improved convergence and parameter adaptation.

def pspline_iasls(data, x_data=None, lam=1e1, p=1e-2, lam_1=1e-4, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None):
    """
    Penalized spline version of IAsLS.
    
    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 adaptation
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    
    Returns:
    tuple: (baseline, params)
    """

Penalized Spline airPLS (pspline_airpls)

Spline implementation of adaptive iteratively reweighted PLS for automatic parameter adjustment.

def pspline_airpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None, normalize_weights=False):
    """
    Penalized spline version of airPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    - normalize_weights (bool): Whether to normalize final weights
    
    Returns:
    tuple: (baseline, params)
    """

Penalized Spline arPLS (pspline_arpls)

Spline version of asymmetrically reweighted PLS with flexible basis representation.

def pspline_arpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Penalized spline version of arPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline drPLS (pspline_drpls)

Doubly reweighted PLS using spline basis with enhanced robustness for complex spectra.

def pspline_drpls(data, lam=1e3, eta=0.5, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Penalized spline version of drPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - eta (float): Weighting parameter for dual reweighting
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline IarPLS (pspline_iarpls)

Improved spline-based arPLS with enhanced convergence and peak handling capabilities.

def pspline_iarpls(data, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Penalized spline version of IarPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline asPLS (pspline_aspls)

Adaptive smoothness PLS using splines for optimal local smoothing based on data characteristics.

def pspline_aspls(data, lam=1e4, num_knots=100, spline_degree=3, diff_order=2, max_iter=100, tol=1e-3, weights=None, alpha=None, x_data=None, asymmetric_coef=0.5):
    """
    Penalized spline version of asPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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
    - asymmetric_coef (float): Asymmetry coefficient
    
    Returns:
    tuple: (baseline, params)
    """

Penalized Spline psalsa (pspline_psalsa)

Spline implementation of peaked signal's AsLS algorithm with selective peak screening.

def pspline_psalsa(data, lam=1e3, p=0.5, k=None, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, x_data=None):
    """
    Penalized spline version of psalsa.
    
    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
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline derpsalsa (pspline_derpsalsa)

Derivative peak-screening AsLS using splines with enhanced peak detection capabilities.

def pspline_derpsalsa(data, lam=1e2, p=1e-2, k=None, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None, smooth_half_window=None, num_smooths=16, x_data=None, pad_kwargs=None, **kwargs):
    """
    Penalized spline version of derpsalsa.
    
    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
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    - smooth_half_window (int, optional): Half-window for derivative smoothing
    - num_smooths (int): Number of smoothing operations
    - x_data (array-like, optional): Input x-values
    - pad_kwargs (dict, optional): Padding parameters
    
    Returns:
    tuple: (baseline, params)
    """

Morphological Penalized Spline (mpspline)

Combines morphological operations with penalized spline fitting for robust baseline estimation.

def mpspline(data, half_window=None, lam=1e3, p=0.0, num_knots=100, spline_degree=3, diff_order=2, tol=1e-3, max_iter=50, weights=None, x_data=None, pad_kwargs=None, **kwargs):
    """
    Morphological penalized spline baseline correction.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - half_window (int, optional): Half-window size for morphological operations
    - lam (float): Smoothing parameter for spline regularization
    - p (float): Asymmetry parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - tol (float): Convergence tolerance
    - max_iter (int): Maximum iterations
    - weights (array-like, optional): Initial weight array
    - x_data (array-like, optional): Input x-values
    - pad_kwargs (dict, optional): Padding parameters for morphological operations
    
    Returns:
    tuple: (baseline, params)
    """

Penalized Spline BrPLS (pspline_brpls)

Bayesian reweighted PLS using splines with statistical weight optimization.

def pspline_brpls(data, x_data=None, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, max_iter_2=50, tol_2=1e-3, weights=None):
    """
    Penalized spline version of BrPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - 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)
    """

Penalized Spline LSRPLS (pspline_lsrpls)

Locally symmetric reweighted PLS using splines for symmetric peak region handling.

def pspline_lsrpls(data, x_data=None, lam=1e3, num_knots=100, spline_degree=3, diff_order=2, max_iter=50, tol=1e-3, weights=None):
    """
    Penalized spline version of LSRPLS.
    
    Parameters:
    - data (array-like): Input y-values to fit baseline
    - x_data (array-like, optional): Input x-values
    - lam (float): Smoothing parameter
    - num_knots (int): Number of spline knots
    - spline_degree (int): Degree of B-spline basis
    - diff_order (int): Order of difference penalty
    - max_iter (int): Maximum iterations
    - tol (float): Convergence tolerance
    - weights (array-like, optional): Initial weight array
    
    Returns:
    tuple: (baseline, params)
    """

Usage Examples

Flexible spline baseline with mixture model:

import numpy as np
from pybaselines.spline import mixture_model

# Sample data with complex baseline curvature
x = np.linspace(0, 1000, 2000)
baseline_true = 50 + 20 * np.sin(0.01 * x) + 0.005 * x**2
peaks = 100 * np.exp(-((x - 200) / 30)**2) + 80 * np.exp(-((x - 600) / 40)**2)
data = baseline_true + peaks + np.random.normal(0, 2, len(x))

# Estimate baseline using mixture model
baseline, params = mixture_model(data, lam=1e5, num_knots=50, spline_degree=3)
corrected = data - baseline

print(f"Used {params.get('num_knots', 50)} knots for spline approximation")

Robust quantile spline fitting:

from pybaselines.spline import irsqr

# Robust baseline at 5th percentile using splines
baseline, params = irsqr(data, quantile=0.05, num_knots=100, lam=100)
corrected = data - baseline

# More flexible than polynomial quantile regression for complex baselines

High-efficiency spline AsLS:

from pybaselines.spline import pspline_asls

# Fast spline-based AsLS with fewer knots for efficiency
baseline, params = pspline_asls(data, lam=1e3, p=1e-2, num_knots=50)
corrected = data - baseline

# Computationally efficient while maintaining smoothness

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