CountsSpectrum

class gammapy.spectrum.CountsSpectrum(energy_lo, energy_hi, data=None, interp_kwargs=None)[source]

Bases: object

Generic counts spectrum.

Parameters:

energy_lo : Quantity

Lower bin edges of energy axis

energy_hi : Quantity

Upper bin edges of energy axis

data : Quantity, array-like

Counts

Examples

from gammapy.spectrum import CountsSpectrum
import numpy as np
import astropy.units as u

ebounds = np.logspace(0,1,11) * u.TeV
data = np.arange(10) * u.ct
spec = CountsSpectrum(
    energy_lo=ebounds[:-1],
    energy_hi=ebounds[1:],
    data=data,
)
spec.plot(show_poisson_errors=True)

(Source code, png, hires.png, pdf)

../_images/gammapy-spectrum-CountsSpectrum-1.png

Attributes Summary

default_interp_kwargs Default interpolation kwargs
energy
total_counts Total number of counts

Methods Summary

copy() A deep copy of self.
fill(events) Fill with list of events
from_hdulist(hdulist[, hdu1, hdu2]) Read OGIP format hdulist
peek([figsize]) Quick-look summary plots.
plot([ax, energy_unit, show_poisson_errors, ...]) Plot as datapoint
plot_hist([ax, energy_unit, show_energy]) Plot as histogram
read(filename[, hdu1, hdu2])
rebin(parameter) Rebin
to_hdulist() Convert to HDUList
to_table() Convert to Table.
write(filename, **kwargs)

Attributes Documentation

default_interp_kwargs = {'method': 'nearest', 'bounds_error': False}

Default interpolation kwargs

energy
total_counts

Total number of counts

Methods Documentation

copy()[source]

A deep copy of self.

fill(events)[source]

Fill with list of events

TODO: Move to NDDataArray

Parameters:

events: `~astropy.units.Quantity`, `gammapy.data.EventList`,

List of event energies

classmethod from_hdulist(hdulist, hdu1='COUNTS', hdu2='EBOUNDS')[source]

Read OGIP format hdulist

peek(figsize=(5, 10))[source]

Quick-look summary plots.

plot(ax=None, energy_unit='TeV', show_poisson_errors=False, show_energy=None, **kwargs)[source]

Plot as datapoint

kwargs are forwarded to errorbar

Parameters:

ax : axis (optional)

Axis instance to be used for the plot

energy_unit : str, Unit, optional

Unit of the energy axis

show_poisson_errors : bool, optional

Show poisson errors on the plot

show_energy : Quantity, optional

Show energy, e.g. threshold, as vertical line

Returns:

ax: axis

Axis instance used for the plot

plot_hist(ax=None, energy_unit='TeV', show_energy=None, **kwargs)[source]

Plot as histogram

kwargs are forwarded to hist

Parameters:

ax : axis (optional)

Axis instance to be used for the plot

energy_unit : str, Unit, optional

Unit of the energy axis

show_energy : Quantity, optional

Show energy, e.g. threshold, as vertical line

classmethod read(filename, hdu1='COUNTS', hdu2='EBOUNDS', **kwargs)[source]
rebin(parameter)[source]

Rebin

Parameters:

parameter, int

Number of bins to merge

Returns:

rebinned_spectrum : CountsSpectrum

Rebinned spectrum

to_hdulist()[source]

Convert to HDUList

This adds an EBOUNDS extension to the BinTableHDU produced by to_table, in order to store the energy axis

to_table()[source]

Convert to Table.

Data format specification: PHA file

write(filename, **kwargs)[source]