Skip to main content

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 parallel prange.
  • 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=1 uses 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

gfit-0.4.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gfit-0.4-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

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

Hashes for gfit-0.4.tar.gz
Algorithm Hash digest
SHA256 e7fb95173066f75b7bba51ac8f06c53a050165a8bd4cf511e8d9f6e675e73e9f
MD5 2100d659d34b179fe5acab1a459baaaa
BLAKE2b-256 392acb18695420aa6375c1b66070c45d86535201e5292518000debbb634726ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfit-0.4.tar.gz:

Publisher: publish.yml on samthiele/gfit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for gfit-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cf415338e0c58db46d464957e6d1c13eef1322ca0b41a0bd8a454ffd7cbf62cb
MD5 5d9fc179c8478822cbb96e0378078e1c
BLAKE2b-256 9e7c9d7fd3cf3a3d1fdaeb7cb49756e9f03ecee99e1b0b0b84fff92bcb5bc269

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfit-0.4-py3-none-any.whl:

Publisher: publish.yml on samthiele/gfit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page