Skip to main content

1D detector spectrum analysis — energy calibration, background estimation, peak detection and fitting

Project description

scispectrum

A Python package for 1D spectrum analysis, designed for physicists and scientists working with detector data such as gamma-ray, X-ray, or particle spectra.

scispectrum provides a clean, extensible framework for the full spectrum analysis pipeline — from raw detector output to calibrated, fitted, background-subtracted results — with proper uncertainty propagation throughout.


Features

  • Spectrum object with axis calibration, resolution calibration, and error propagation using the uncertainties package
  • Domain slicing by physical axis values (e.g. energy in keV) or channels
  • List-mode parser for time-channel detector data, with chunked reading for large files
  • Domain fitting and analysis — multiple fitting and analysis methods for a domain including sum of Gaussians fitting, find peaks, centers and fwhm
  • Background estimation — multiple global background extimation methods including Asymmetric Least Squares (ALS)
  • Extensible base classes — build your own fitting, background, or analysis procedures with a consistent interface
  • xarray throughout — labeled, sliceable data with named coordinates
  • Uncertainty propagation via the uncertainties library

Installation

scispectrum is not yet available on PyPI. Install directly from GitHub:

git clone https://github.com/achiyaAmrusi/pySpectrum
cd pySpectrum
pip install -e .

This installs the package in editable mode, so any changes you make to the source are reflected immediately.


Quick Start

Load a spectrum from a DataFrame

import pandas as pd
from scispectrum.core import Spectrum

df = pd.read_csv("my_spectrum.csv")
spectrum = Spectrum.from_dataframe(df, channel_col="channel", counts_col="counts")

Apply an energy calibration

from scispectrum.calibration import AxisCalibration

# Linear calibration: energy = 0.5 * channel + 1.2
calib = AxisCalibration(lambda ch: 0.5 * ch + 1.2, name="energy_keV")
spectrum.set_axis_calibration(calib)

Slice a domain by axis values

# Select the region between 1460 and 1480 keV
domain = spectrum.domain(1460, 1480)

Fit peaks

from scispectrum.domain_fitting import SumOfGaussians

result = SumOfGaussians.fit(domain)

print(result["center"].values)   # peak centers in keV
print(result["fwhm"].values)     # peak widths
print(result["amplitude"].values) # peak amplitudes

Estimate and subtract background

from scispectrum.background import ALSBackground

bg_estimator = ALSBackground(lam=1e5, p=0.001, max_iter=50)
als_bg = bg_estimator.estimate(spectrum.axis, spectrum.counts)
domain_subtracted = domains.subtract_background(als_bg[domain.indices])

Parse raw list-mode data

from scispectrum.parsers import TimeChannelParser

# From a large file — processed in chunks to save memory
spectrum = TimeChannelParser.from_file(
    "detector_run.csv",
    axis_calib=calib,
    num_of_channels=2**14
)

# From an in-memory DataFrame
spectrum = TimeChannelParser.from_dataframe(df, axis_calib=calib)

Uncertainty Propagation

scispectrum propagates measurement uncertainties through arithmetic operations using the uncertainties library. Poisson errors are assigned automatically when parsing list-mode data.

# Arithmetic preserves errors
subtraction = spectrum_a - spectrum_b
subtraction.counts_err  # propagated uncertainties

Extending scispectrum

scispectrum is designed to be extended. Each analysis category has an abstract base class that defines the interface:

Custom fitting method

from scispectrum.domain_fitting.abstract_fitting_class import PeakFit

class MyFitter(PeakFit):
    @classmethod
    def fit(cls, domain, **kwargs):
        # your fitting logic here
        ...

Custom background estimator

from scispectrum.background.base import BackgroundEstimator

class MyBackground(BackgroundEstimator):
    def estimate(self, x, y):
        # your background logic here
        ...

All custom classes integrate seamlessly with Domain, Spectrum, and the rest of the pipeline.


Examples

Full worked examples are available in the examples directory:

Core

Background Estimation

Domain Analysis and Fitting

SNR Identification

Library Sample data files for running the examples are provided in the Library directory.


Requirements

  • numpy>=2.0.0,<3.0
  • pandas>=2.3,<4.0
  • scipy>=1.14.0
  • xarray>=2024.6.0
  • uncertainties>=3.1

License

MIT License. See LICENSE for details.


Author

Achiya Yosef Amrusi — GitHub

Contributions and feedback are welcome.

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

scispectrum-0.3.0.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

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

scispectrum-0.3.0-py3-none-any.whl (43.3 kB view details)

Uploaded Python 3

File details

Details for the file scispectrum-0.3.0.tar.gz.

File metadata

  • Download URL: scispectrum-0.3.0.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for scispectrum-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4dd38d0c5270c81321fa4e95ae71a35dfe2a7b1f9042d2e5084ca5da1a169d53
MD5 fa28516c1e2cb6d23825c5b31b9423ca
BLAKE2b-256 903563668948b3f18fb0e592d9d31320958089a37e2a79c71bb3ed879b4256a6

See more details on using hashes here.

File details

Details for the file scispectrum-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: scispectrum-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 43.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for scispectrum-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82882622391518630635cab45d30de450e96353f48750f71b1df43883f749a6d
MD5 cdea155551c557b8b13d0d9d2daf0b63
BLAKE2b-256 dfa58efb6015f0e0a9f54d00cffcd323bf61cbce63c244b2aef46ea5093073df

See more details on using hashes here.

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