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.1.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.1-py3-none-any.whl (43.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: scispectrum-0.3.1.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for scispectrum-0.3.1.tar.gz
Algorithm Hash digest
SHA256 088f651a103ea92ef07df6c7ca7b8d0063a9be07ce741dea011a4f5d9c93cb01
MD5 1907253ff2c8fe2078cf5a7f43656abc
BLAKE2b-256 7c1225f81d77f83ab3acd69fd5510b6c493ae220dc88a82091fc4f4ac366d4a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for scispectrum-0.3.1.tar.gz:

Publisher: workflow.yml on achiyaAmrusi/scispectrum

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

File details

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

File metadata

  • Download URL: scispectrum-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 43.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for scispectrum-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1208139e67e63653dd4ed98eb18e5d6a6bc950860dc03c6d8f3e838974cc14a3
MD5 392d57e2165f249712f290c32533400d
BLAKE2b-256 b184bb67d46bbd9b7f584c9301adf2eba42a7c2b564195d0bea621a3e04c85de

See more details on using hashes here.

Provenance

The following attestation bundles were made for scispectrum-0.3.1-py3-none-any.whl:

Publisher: workflow.yml on achiyaAmrusi/scispectrum

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