Fast multi-gaussian curve fitting
Project description
gfit
Fast multi-Gaussian curve fitting for spectra and other 1D signals.
gfit fits many independent signals at once by decomposing each into a sum of symmetric or asymmetric Gaussian peaks. It is aimed at hyperspectral and spectroscopy workflows, but works for any regularly sampled 1D data.
Features
- Fit symmetric (amplitude, centre, width) or asymmetric (separate left/right widths) Gaussians
- Batch fitting over
(n_spectra, n_points)arrays - Automatic peak-based initialisation via
initialise() - Optional Numba JIT acceleration (
pip install "gfit[all]") - Pure-Python fallback when Numba is unavailable
- Multiprocessing for large batches; automatic single-threaded fallback in Pyodide / WebAssembly
Installation
Minimal install (NumPy and tqdm only; pure-Python fallback):
pip install gfit
Recommended install (NumPy, SciPy, tqdm, Numba):
pip install "gfit[all]"
SciPy only (no Numba; slower fitting, no JIT):
pip install "gfit[scipy]"
On zsh, quote extras: pip install "gfit[all]" (unquoted brackets are treated as globs).
Gaussian fitting (gfit, initialise, evaluate) imports without SciPy, but calling the fitters raises an ImportError with install instructions unless SciPy is installed.
Quick start
import numpy as np
from gfit import gfit, initialise, evaluate
from gfit.util import split_coeff, rand_signal
# x-axis and a batch of noisy spectra (n_spectra, n_points)
x = np.linspace(-10, 10, 200)
X = np.array([rand_signal(x, snr=14)[0] for _ in range(100)])
# Fit 3 symmetric Gaussians per spectrum
n = 3
x0 = initialise(x, X, n, sym=True, d=4)
params = gfit(x, X, x0, n, sym=True, nthreads=-1, vb=True)
# Split fitted parameters into amplitude, centre, width arrays
a, b, c = split_coeff(params, sym=True)
# Evaluate the fitted model for one spectrum
y_model = evaluate(x, params[0:1], sym=True)[0]
API overview
| Function | Description |
|---|---|
gfit(x, X, ...) |
Fit Gaussians to a batch of spectra |
initialise(x, X, n, ...) |
Estimate starting parameters from local peaks |
evaluate(x, M, sym=...) |
Evaluate one or more multi-Gaussian models |
gfit parameters
| Parameter | Default | Meaning |
|---|---|---|
x |
— | 1D array of sample positions |
X |
— | 2D array (n_spectra, n_points) of values to fit |
x0 |
None |
Initial parameters; computed via initialise() if omitted |
n |
3 |
Number of Gaussian components per spectrum |
sym |
True |
Symmetric (True) or asymmetric (False) peaks |
thresh |
-1 |
Skip fitting if peak amplitude below threshold; -1 fits all |
nthreads |
-1 |
Parallel workers; 1 forces single-threaded |
vb |
True |
Show tqdm progress bar |
Additional keyword arguments (ftol, xtol, maxiter, …) are passed to scipy.optimize.least_squares.
Parameter layout
Fitted coefficients are stored as a flat array per spectrum:
- Symmetric:
[a₀, b₀, c₀, a₁, b₁, c₁, …]— amplitude, centre, width - Asymmetric:
[a₀, b₀, c₁₀, c₂₀, …]— amplitude, centre, left width, right width
Use gfit.util.split_coeff() and stack_coeff() to convert between flat arrays and separate parameter lists.
Utilities
from gfit.util import remove_hull, get_bounds, benchmark
# Remove a convex hull baseline before fitting
X_corrected = remove_hull(X, upper=True, div=True)
# Bounds derived from an initial guess (also used internally by gfit)
bounds = get_bounds(x, x0, sym=True)
# Simple performance check
benchmark(size=100, res=100, nthreads=1)
Lower-level single-spectrum fitters are available in gfit.internal:
from gfit.internal import fit_mgauss, fit_amgauss
Performance
- With Numba (
gfit[all]): hot loops are JIT-compiled; batch initialisation can use parallelprange. - With a minimal install or missing Numba: the same code runs as pure Python (correct, but slower). A warning is emitted once at import.
nthreads=-1(default) uses multiprocessing to fit spectra in parallel on desktop Python.nthreads=1uses a single process (recommended for small batches or constrained environments).
License
MIT — see LICENSE.
Author
Sam Thiele — github.com/samthiele/gfit
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gfit-0.4.tar.gz.
File metadata
- Download URL: gfit-0.4.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7fb95173066f75b7bba51ac8f06c53a050165a8bd4cf511e8d9f6e675e73e9f
|
|
| MD5 |
2100d659d34b179fe5acab1a459baaaa
|
|
| BLAKE2b-256 |
392acb18695420aa6375c1b66070c45d86535201e5292518000debbb634726ab
|
Provenance
The following attestation bundles were made for gfit-0.4.tar.gz:
Publisher:
publish.yml on samthiele/gfit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gfit-0.4.tar.gz -
Subject digest:
e7fb95173066f75b7bba51ac8f06c53a050165a8bd4cf511e8d9f6e675e73e9f - Sigstore transparency entry: 1809465306
- Sigstore integration time:
-
Permalink:
samthiele/gfit@b147d9cba39e6c51fa3e9e550e594432ae594e25 -
Branch / Tag:
refs/tags/v0.4 - Owner: https://github.com/samthiele
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b147d9cba39e6c51fa3e9e550e594432ae594e25 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gfit-0.4-py3-none-any.whl.
File metadata
- Download URL: gfit-0.4-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf415338e0c58db46d464957e6d1c13eef1322ca0b41a0bd8a454ffd7cbf62cb
|
|
| MD5 |
5d9fc179c8478822cbb96e0378078e1c
|
|
| BLAKE2b-256 |
9e7c9d7fd3cf3a3d1fdaeb7cb49756e9f03ecee99e1b0b0b84fff92bcb5bc269
|
Provenance
The following attestation bundles were made for gfit-0.4-py3-none-any.whl:
Publisher:
publish.yml on samthiele/gfit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gfit-0.4-py3-none-any.whl -
Subject digest:
cf415338e0c58db46d464957e6d1c13eef1322ca0b41a0bd8a454ffd7cbf62cb - Sigstore transparency entry: 1809465324
- Sigstore integration time:
-
Permalink:
samthiele/gfit@b147d9cba39e6c51fa3e9e550e594432ae594e25 -
Branch / Tag:
refs/tags/v0.4 - Owner: https://github.com/samthiele
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b147d9cba39e6c51fa3e9e550e594432ae594e25 -
Trigger Event:
release
-
Statement type: