spectrum - 1D spectrum analysis


gammapy.spectrum holds functions and classes related to 1D region based spectral analysis. This includes also simulation tools.

The basic of 1D spectral analysis are explained in this talk. A good reference for the forward-folding on-off likelihood fitting methods is Section 7.5 “Spectra and Light Curves” in [Naurois2012], in publications usually the reference [Piron2001] is used. A standard reference for the unfolding method is [Albert2007].

Getting Started

The following code snippet demonstrates how to load an observation stored in OGIP format and fit a spectral model.

from gammapy.spectrum import SpectrumDatasetOnOff
from gammapy.utils.fitting import Fit
from gammapy.spectrum.models import PowerLaw

filename = '$GAMMAPY_DATA/joint-crab/spectra/hess/pha_obs23523.fits'
dataset = SpectrumDatasetOnOff.read(filename)

model = PowerLaw(
    amplitude='1e-12 cm-2 s-1 TeV-1',
    reference='1 TeV',

dataset.model = model

fit = Fit([dataset])
result = fit.run()
model.parameters.covariance = result.parameters.covariance

It will print the following output to the console:



       name     value     error        unit      min max frozen
    --------- --------- --------- -------------- --- --- ------
        index 2.817e+00 1.496e-01                nan nan  False
    amplitude 5.142e-11 6.423e-12 cm-2 s-1 TeV-1 nan nan  False
    reference 1.000e+00 0.000e+00            TeV nan nan   True


       name     index   amplitude reference
    --------- --------- --------- ---------
        index 2.239e-02 6.160e-13 0.000e+00
    amplitude 6.160e-13 4.126e-23 0.000e+00
    reference 0.000e+00 0.000e+00 0.000e+00

Using gammapy.spectrum

For more advanced use cases please go to the tutorial notebooks:

The following pages describe gammapy.spectrum in more detail:


gammapy.spectrum Package

1D spectrum analysis.


cosmic_ray_flux(energy[, particle])

Cosmic ray flux at Earth.

integrate_spectrum(func, xmin, xmax[, …])

Integrate 1d function using the log-log trapezoidal rule.


CountsSpectrum(energy_lo, energy_hi[, data, …])

Generic counts spectrum.


Crab nebula spectral model.

FluxPointEstimator(datasets, e_edges, model)

Flux point estimator.


Flux points container.

FluxPointsDataset(model, data[, mask, …])

Fit a set of flux points with a parametric model.

PHACountsSpectrum(energy_lo, energy_hi[, …])

Counts spectrum corresponding to OGIP PHA format.


List of PHACountsSpectrum objects.

SensitivityEstimator(arf, rmf, bkg, livetime)

Estimate differential sensitivity.

SpectrumDataset([model, counts, livetime, …])

Compute spectral model fit statistic on a CountsSpectrum.

SpectrumDatasetOnOff([model, counts_on, …])

Compute spectral model fit statistic on a ON OFF Spectrum.

SpectrumEvaluator(model[, aeff, edisp, …])

Calculate number of predicted counts (npred).

SpectrumExtraction(observations, bkg_estimate)

Creating input data to 1D spectrum fitting.

SpectrumObservation(on_vector[, aeff, …])

1D spectral analysis storage class.


List of SpectrumObservation objects.


Stack observations in a SpectrumObservationList.

SpectrumSimulation(livetime, source_model[, …])

Simulate SpectrumObservation.


Spectrum stats.

gammapy.spectrum.models Module

Spectral models for Gammapy.



Spectral model base class.


Constant model

CompoundSpectralModel(model1, model2, operator)

Represents the algebraic combination of two SpectralModel

PowerLaw([index, amplitude, reference])

Spectral power-law model.

PowerLaw2([amplitude, index, emin, emax])

Spectral power-law model with integral as amplitude parameter.

ExponentialCutoffPowerLaw([index, …])

Spectral exponential cutoff power-law model.

ExponentialCutoffPowerLaw3FGL([index, …])

Spectral exponential cutoff power-law model used for 3FGL.

PLSuperExpCutoff3FGL([index_1, index_2, …])

Spectral super exponential cutoff power-law model used for 3FGL.

LogParabola([amplitude, reference, alpha, beta])

Spectral log parabola model.

TableModel(energy, values[, norm, …])

A model generated from a table of energy and value arrays.

AbsorbedSpectralModel(spectral_model, …[, …])

Spectral model with EBL absorption.

Absorption(energy_lo, energy_hi, param_lo, …)

Gamma-ray absorption models.