pybaselines

1.2.1 · active · verified Thu Apr 16

pybaselines is a Python library providing a comprehensive collection of algorithms for baseline correction of experimental data, particularly useful in fields like spectroscopy and chromatography. It is currently at version 1.2.1 and maintains an active release cadence with minor versions and patches released every few months, and major versions less frequently.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the recommended class-based API (available since v1.0.0) to perform baseline correction using the Asymmetric Least Squares (ASLS) algorithm. It generates synthetic data with a peak and a sloping baseline, then applies the correction to estimate and remove the baseline.

import numpy as np
from pybaselines.api import Baseline

# 1. Generate some example data
x = np.linspace(0, 100, 500)
y_peak = 10 * np.exp(-((x - 50)**2) / 20) # A simple peak
y_baseline = 0.1 * x + 5 # A sloping baseline
y_data = y_peak + y_baseline + np.random.normal(0, 0.5, x.shape)

# 2. Initialize the Baseline object (using the class-based API, recommended for v1.0.0+)
baseline_fitter = Baseline()

# 3. Apply a baseline correction algorithm, e.g., Asymmetric Least Squares (ASLS)
# The first return value is the estimated baseline, the second is a dictionary of parameters
estimated_baseline, params = baseline_fitter.asls(y_data, lam=1e6, p=0.01)

# 4. The corrected signal is the original data minus the estimated baseline
corrected_signal = y_data - estimated_baseline

print("Baseline estimation complete.")
print(f"First 5 original data values: {y_data[:5].round(2)}")
print(f"First 5 estimated baseline values: {estimated_baseline[:5].round(2)}")
print(f"First 5 corrected signal values: {corrected_signal[:5].round(2)}")

view raw JSON →