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
Metadata
Release files for gfit 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gfit-0.4.tar.gz | 21.6 kB | With provenance | Details for gfit-0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gfit-0.4-py3-none-any.whl | Python 3 | none | any | Details for gfit-0.4-py3-none-any.whl |
Total release size: 38.4 kB
Release files / gfit-0.4.tar.gz
| Download URL | gfit-0.4.tar.gz |
|---|---|
| Size | 21.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7fb95173066f75b7bba51ac8f06c53a050165a8bd4cf511e8d9f6e675e73e9f
|
|
BLAKE2b-256 checksum How to use checksums |
392acb18695420aa6375c1b66070c45d86535201e5292518000debbb634726ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 13, 2026.
Transparency logRelease files / gfit-0.4-py3-none-any.whl
| Download URL | gfit-0.4-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf415338e0c58db46d464957e6d1c13eef1322ca0b41a0bd8a454ffd7cbf62cb
|
|
BLAKE2b-256 checksum How to use checksums |
9e7c9d7fd3cf3a3d1fdaeb7cb49756e9f03ecee99e1b0b0b84fff92bcb5bc269
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 13, 2026.
Transparency logSecurity for gfit 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for gfit-0.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 13, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.