Persistent homology spectral line decomposition
Project description
phspectra
Persistent homology spectral line decomposition.
A Python library for decomposing 1-D astronomical spectra into Gaussian components using 0-dimensional persistent homology for peak detection. Instead of derivative-based methods (GaussPy) or brute-force parameter sweeps, it ranks peaks by their topological persistence and uses a single tuning parameter.
Installation
pip install phspectra
Requirements: Python >= 3.11, NumPy >= 1.26, SciPy >= 1.12.
An optional C extension is compiled automatically from source when available, providing ~2x faster fitting. If compilation fails, the library falls back to SciPy's curve_fit transparently.
Quick start
import numpy as np
from phspectra import fit_gaussians
# Create a synthetic spectrum: two Gaussians + noise
rng = np.random.default_rng(42)
x = np.arange(200, dtype=np.float64)
signal = (
3.0 * np.exp(-0.5 * ((x - 60) / 4.0) ** 2)
+ 1.5 * np.exp(-0.5 * ((x - 130) / 8.0) ** 2)
+ rng.normal(0, 0.2, size=200)
)
# Decompose
components = fit_gaussians(signal, beta=3.5)
for c in components:
print(f" amplitude={c.amplitude:.2f} mean={c.mean:.1f} stddev={c.stddev:.2f}")
amplitude=3.00 mean=60.1 stddev=3.97
amplitude=1.48 mean=129.9 stddev=8.12
The number of components is determined automatically -- no need to specify it in advance.
API
The public API consists of three functions:
| Symbol | Description |
|---|---|
fit_gaussians(signal, *, beta=3.5, ...) |
Decompose a 1-D spectrum into Gaussian components |
find_peaks_by_persistence(signal, *, min_persistence=0.0) |
Low-level peak detection via persistent homology |
estimate_rms(signal, *, mask_pad=2, mad_clip=5.0) |
Signal-masked noise estimation |
fit_gaussians returns a list of GaussianComponent dataclasses, each with amplitude, mean, and stddev fields.
See the full API reference for parameter details, types, and error handling.
How it works
- Noise estimation -- signal-masked MAD estimator (Riener et al. 2019)
- Peak detection -- 0-dim persistent homology ranks peaks by significance
- Curve fitting -- bounded Levenberg-Marquardt with initial guesses from persistence
- Validation -- SNR, matched-filter SNR, and FWHM checks discard unphysical components
- Refinement -- iterative residual search, negative-dip splitting, and blended-pair merging, accepted only when AICc improves
See the algorithm overview for details.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file phspectra-1.0.2.tar.gz.
File metadata
- Download URL: phspectra-1.0.2.tar.gz
- Upload date:
- Size: 28.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75d6ba3afb8dac248b106045447f4b7ae16135adfa5bff1982779fc63f62ae2c
|
|
| MD5 |
a5337cde89872aa575a45583ca6e511c
|
|
| BLAKE2b-256 |
870aa519f70c83dc61cf8128aa4143aa7391fd8b9d8af1f0faef443e9b1adab4
|
Provenance
The following attestation bundles were made for phspectra-1.0.2.tar.gz:
Publisher:
release.yml on caverac/phspectra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phspectra-1.0.2.tar.gz -
Subject digest:
75d6ba3afb8dac248b106045447f4b7ae16135adfa5bff1982779fc63f62ae2c - Sigstore transparency entry: 975263741
- Sigstore integration time:
-
Permalink:
caverac/phspectra@d29465290c7d8f4c94a8bee5f561f43cd150e290 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/caverac
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d29465290c7d8f4c94a8bee5f561f43cd150e290 -
Trigger Event:
push
-
Statement type: