Skip to main content
Logo Current Pypi Version Current conda Version GitHub Actions test status Documentation Status Supported Python versions Zenodo DOI

pybaselines is a library of algorithms for the baseline correction of experimental data.

Introduction

pybaselines is a Python library that provides many different algorithms for performing baseline correction on data from experimental techniques such as Raman, FTIR, NMR, XRD, XRF, PIXE, MALDI-TOF, LIBS, etc. The aim of the project is to provide a semi-unified API to allow quickly testing and comparing multiple baseline correction algorithms to find the best one for a set of data.

pybaselines has 50+ baseline correction algorithms. These include popular algorithms, such as AsLS, airPLS, ModPoly, and SNIP, as well as many lesser known algorithms. Most algorithms are adapted directly from literature, although there are a few that are unique to pybaselines, such as penalized spline versions of Whittaker-smoothing-based algorithms. The full list of implemented algorithms can be found in the documentation.

Installation

Stable Release

pybaselines can be installed from pypi using pip, by running the following command in the terminal:

pip install pybaselines

pybaselines can alternatively be installed from the conda-forge channel using conda by running:

conda install -c conda-forge pybaselines

Development Version

The sources for pybaselines can be downloaded from the GitHub repo. To install the current version of pybaselines from GitHub, run:

pip install git+https://github.com/derb12/pybaselines.git

Dependencies

pybaselines requires Python version 3.9 or later and the following libraries:

All of the required libraries should be automatically installed when installing pybaselines using any of the installation methods above.

The optional dependencies for pybaselines are listed in the documentation . To also install the optional dependencies when installing pybaselines with pip, run:

pip install pybaselines[full]

If installing with conda, the optional dependencies have to be specified manually.

Quick Start

To use the various functions in pybaselines, simply input the measured data and any required parameters. All baseline correction functions in pybaselines will output two items: a numpy array of the calculated baseline and a dictionary of potentially useful parameters. The main interface for all baseline correction algorithms in pybaselines is through the Baseline object for one dimensional data and Baseline2D for two dimensional data.

For more details on each baseline algorithm, refer to the algorithms section of pybaselines’s documentation. For examples of their usage, refer to the examples section.

A simple example is shown below.

import matplotlib.pyplot as plt
import numpy as np
from pybaselines import Baseline, utils

x = np.linspace(1, 1000, 1000)
# a measured signal containing several Gaussian peaks
signal = (
    utils.gaussian(x, 4, 120, 5)
    + utils.gaussian(x, 5, 220, 12)
    + utils.gaussian(x, 5, 350, 10)
    + utils.gaussian(x, 7, 400, 8)
    + utils.gaussian(x, 4, 550, 6)
    + utils.gaussian(x, 5, 680, 14)
    + utils.gaussian(x, 4, 750, 12)
    + utils.gaussian(x, 5, 880, 8)
)
# exponentially decaying baseline
true_baseline = 2 + 10 * np.exp(-x / 400)
noise = np.random.default_rng(1).normal(0, 0.2, x.size)

y = signal + true_baseline + noise

baseline_fitter = Baseline(x_data=x)

bkg_1, params_1 = baseline_fitter.modpoly(y, poly_order=3)
bkg_2, params_2 = baseline_fitter.asls(y, lam=1e7, p=0.02)
bkg_3, params_3 = baseline_fitter.mor(y, half_window=30)
bkg_4, params_4 = baseline_fitter.snip(
    y, max_half_window=40, decreasing=True, smooth_half_window=3
)

plt.plot(x, y, label='raw data', lw=1.5)
plt.plot(x, true_baseline, lw=3, label='true baseline')
plt.plot(x, bkg_1, '--', label='modpoly')
plt.plot(x, bkg_2, '--', label='asls')
plt.plot(x, bkg_3, '--', label='mor')
plt.plot(x, bkg_4, '--', label='snip')

plt.legend()
plt.show()

The above code will produce the image shown below.

various baselines

Contributing

Contributions are welcomed and greatly appreciated. For information on submitting bug reports, pull requests, or general feedback, please refer to the contributing guide.

Changelog

Refer to the changelog for information on pybaselines’s changes.

License

pybaselines is open source and freely available under the BSD-3-Clause license. For more information, refer to the license.

Citing

If you use pybaselines for published research, please consider citing by following the guidelines in the documentation.

Author

Release files for pybaselines 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybaselines 1.2.1
File Size Uploaded
pybaselines-1.2.1.tar.gz 340.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybaselines 1.2.1
File Interpreter ABI Platform
pybaselines-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 552.0 kB

Release files / pybaselines-1.2.1.tar.gz

Download URL pybaselines-1.2.1.tar.gz
Size 340.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5cae63e00cc9f469e446d05e64c731b87a54dbfd9b70ef049e46cf80fc91ad1a
BLAKE2b-256 checksum
How to use checksums
7b4560ed7d059023b45eb45a4279356379d13ef883b7efd1c48242e0c1a30d3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.12

Release files / pybaselines-1.2.1-py3-none-any.whl

Download URL pybaselines-1.2.1-py3-none-any.whl
Size 211.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8f224a0b5ac4cdcef861bc60533131c37255b4d1193f18a410bc37fe5217c73
BLAKE2b-256 checksum
How to use checksums
3d66f044d53935b142d47ce2a65b8c4f51fdb5ca85ee1035fb2b7857971b122e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.12

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page