pybaselines
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
-
AttributeError: module 'pybaselines' has no attribute 'window'
cause Attempting to import or access the `window` module, which was deprecated in v0.7.0 and removed in v0.8.0.fixReplace any usage of `pybaselines.window` with `pybaselines.smooth`. For example, change `from pybaselines.window import imodpoly` to `from pybaselines.smooth import imodpoly`. -
TypeError: 'Baseline' object is not callable
cause Attempting to call the `Baseline` class instance directly like a function, instead of calling one of its method algorithms.fixEnsure you are calling a specific algorithm method on the `Baseline` instance. For example, `baseliner = Baseline(); baseline, params = baseliner.asls(data)` instead of `baseline, params = baseliner(data)`. -
ImportError: cannot import name 'Baseline' from 'pybaselines' (.../site-packages/pybaselines/__init__.py)
cause Attempting to import the `Baseline` class directly from the top-level `pybaselines` package, or using a version prior to 1.0.0 where the class-based API did not exist.fixCorrect the import statement to `from pybaselines.api import Baseline`. If you are using an older version, either upgrade to v1.0.0+ or use the functional API (e.g., `from pybaselines.whittaker import asls`).
Warnings
- deprecated The `pybaselines.window` module was deprecated in version 0.7.0 and completely removed in version 0.8.0. Attempting to import or use functions from this module will result in an `AttributeError` or `ImportError`.
- gotcha A new class-based API, `pybaselines.api.Baseline`, was introduced in v1.0.0. While the functional API (e.g., `pybaselines.whittaker.asls`) is maintained for backward compatibility, the class-based approach is now the recommended pattern for new development and offers better organization and potential future features.
- gotcha The `tol` and `eps` parameters for `pybaselines.polynomial.quant_reg` had their default values and internal usage changed in version 0.5.1. Specifically, `tol` default changed to 1e-6 and `eps` is now used directly rather than its square. This could subtly alter results if not adjusted.
- gotcha For optimal performance, especially with Whittaker-smoothing-based algorithms, the optional dependencies `numba` and `pentapy` are highly recommended. Without them, calculations might be significantly slower.
Install
-
pip install pybaselines -
pip install pybaselines[full]
Imports
- Baseline
from pybaselines import Baseline
from pybaselines.api import Baseline
- Baseline2D
from pybaselines.api import Baseline2D
- asls
from pybaselines.whittaker import asls
- window
import pybaselines.window as window_algos
import pybaselines.smooth as smooth_algos
Quickstart
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)}")