FluxPoints

class gammapy.spectrum.FluxPoints(table)[source]

Bases: object

Flux points container.

The supported formats are described here: SED

In summary, the following formats and minimum required columns are:

  • Format dnde: columns e_ref and dnde

  • Format e2dnde: columns e_ref, e2dnde

  • Format flux: columns e_min, e_max, flux

  • Format eflux: columns e_min, e_max, eflux

Parameters
tableTable

Table with flux point data

Examples

The FluxPoints object is most easily created by reading a file with flux points given in one of the formats documented above:

from gammapy.spectrum import FluxPoints
filename = '$GAMMAPY_DATA/hawc_crab/HAWC19_flux_points.fits'
flux_points = FluxPoints.read(filename)
flux_points.plot()

An instance of FluxPoints can also be created by passing an instance of astropy.table.Table, which contains the required columns, such as 'e_ref' and 'dnde'. The corresponding sed_type has to be defined in the meta data of the table:

from astropy import units as u
from astropy.table import Table
from gammapy.spectrum import FluxPoints
from gammapy.modeling.models import PowerLawSpectralModel

table = Table()
pwl = PowerLawSpectralModel()
e_ref = np.logspace(0, 2, 7) * u.TeV
table['e_ref'] = e_ref
table['dnde'] = pwl(e_ref)
table.meta['SED_TYPE'] = 'dnde'

flux_points = FluxPoints(table)
flux_points.plot()

If you have flux points in a different data format, the format can be changed by renaming the table columns and adding meta data:

from astropy import units as u
from astropy.table import Table
from gammapy.spectrum import FluxPoints

table = Table.read('$GAMMAPY_DATA/tests/spectrum/flux_points/flux_points_ctb_37b.txt',
                   format='ascii.csv', delimiter=' ', comment='#')
table.meta['SED_TYPE'] = 'dnde'
table.rename_column('Differential_Flux', 'dnde')
table['dnde'].unit = 'cm-2 s-1 TeV-1'

table.rename_column('lower_error', 'dnde_errn')
table['dnde_errn'].unit = 'cm-2 s-1 TeV-1'

table.rename_column('upper_error', 'dnde_errp')
table['dnde_errp'].unit = 'cm-2 s-1 TeV-1'

table.rename_column('E', 'e_ref')
table['e_ref'].unit = 'TeV'

flux_points = FluxPoints(table)
flux_points.plot()

Note: In order to reproduce the example you need the tests datasets folder. You may download it with the command gammapy download datasets --tests --out $GAMMAPY_DATA

Attributes
tableTable

Table with flux point data

Attributes Summary

e_edges

Edges of the energy bin.

e_max

Upper bound of energy bin.

e_min

Lower bound of energy bin.

e_ref

Reference energy.

is_ul

sed_type

SED type (str).

table_formatted

Return formatted version of the flux points table.

Methods Summary

drop_ul(self)

Drop upper limit flux points.

plot(self[, ax, energy_unit, flux_unit, …])

Plot flux points.

plot_ts_profiles(self[, ax, energy_unit, …])

Plot fit statistic SED profiles as a density plot.

read(filename, \*\*kwargs)

Read flux points.

stack(flux_points)

Create flux points by stacking list of flux points.

to_sed_type(self, sed_type[, method, model, …])

Convert to a different SED type (return new FluxPoints).

write(self, filename, \*\*kwargs)

Write flux points.

Attributes Documentation

e_edges

Edges of the energy bin.

Returns
e_edgesQuantity

Energy edges.

e_max

Upper bound of energy bin.

Defined by e_max column in table.

Returns
e_maxQuantity

Upper bound of energy bin.

e_min

Lower bound of energy bin.

Defined by e_min column in FluxPoints.table.

Returns
e_minQuantity

Lower bound of energy bin.

e_ref

Reference energy.

Defined by e_ref column in FluxPoints.table or computed as log center, if e_min and e_max columns are present in FluxPoints.table.

Returns
e_refQuantity

Reference energy.

is_ul
sed_type

SED type (str).

One of: {‘dnde’, ‘e2dnde’, ‘flux’, ‘eflux’}

table_formatted

Return formatted version of the flux points table. Used for pretty printing

Methods Documentation

drop_ul(self)[source]

Drop upper limit flux points.

Returns
flux_pointsFluxPoints

Flux points with upper limit points removed.

Examples

>>> from gammapy.spectrum import FluxPoints
>>> filename = '$GAMMAPY_DATA/tests/spectrum/flux_points/flux_points.fits'
>>> flux_points = FluxPoints.read(filename)
>>> print(flux_points)
FluxPoints(sed_type="flux", n_points=24)
>>> print(flux_points.drop_ul())
FluxPoints(sed_type="flux", n_points=19)

Note: In order to reproduce the example you need the tests datasets folder. You may download it with the command gammapy download datasets --tests --out $GAMMAPY_DATA

plot(self, ax=None, energy_unit='TeV', flux_unit=None, energy_power=0, **kwargs)[source]

Plot flux points.

Parameters
axAxes

Axis object to plot on.

energy_unitstr, Unit, optional

Unit of the energy axis

flux_unitstr, Unit, optional

Unit of the flux axis

energy_powerint

Power of energy to multiply y axis with

kwargsdict

Keyword arguments passed to matplotlib.pyplot.errorbar()

Returns
axAxes

Axis object

plot_ts_profiles(self, ax=None, energy_unit='TeV', add_cbar=True, y_values=None, y_unit=None, **kwargs)[source]

Plot fit statistic SED profiles as a density plot.

Parameters
axAxes

Axis object to plot on.

energy_unitstr, Unit, optional

Unit of the energy axis

y_valuesastropy.units.Quantity

Array of y-values to use for the fit statistic profile evaluation.

y_unitstr or astropy.units.Unit

Unit to use for the y-axis.

add_cbarbool

Whether to add a colorbar to the plot.

kwargsdict

Keyword arguments passed to matplotlib.pyplot.pcolormesh()

Returns
axAxes

Axis object

classmethod read(filename, **kwargs)[source]

Read flux points.

Parameters
filenamestr

Filename

kwargsdict

Keyword arguments passed to astropy.table.Table.read.

classmethod stack(flux_points)[source]

Create flux points by stacking list of flux points.

The first FluxPoints object in the list is taken as a reference to infer column names and units for the stacked object.

Parameters
flux_pointslist of FluxPoints

List of flux points to stack.

Returns
flux_pointsFluxPoints

Flux points without upper limit points.

to_sed_type(self, sed_type, method='log_center', model=None, pwl_approx=False)[source]

Convert to a different SED type (return new FluxPoints).

See: https://ui.adsabs.harvard.edu/abs/1995NIMPA.355..541L for details on the 'lafferty' method.

Parameters
sed_type{‘dnde’}

SED type to convert to.

modelSpectralModel

Spectral model assumption. Note that the value of the amplitude parameter does not matter. Still it is recommended to use something with the right scale and units. E.g. amplitude = 1e-12 * u.Unit('cm-2 s-1 TeV-1')

method{‘lafferty’, ‘log_center’, ‘table’}

Flux points e_ref estimation method:

  • 'laferty' Lafferty & Wyatt model-based e_ref

  • 'log_center' log bin center e_ref

  • 'table' using column ‘e_ref’ from input flux_points

pwl_approxbool

Use local power law appoximation at e_ref to compute differential flux from the integral flux. This method is used by the Fermi-LAT catalogs.

Returns
flux_pointsFluxPoints

Flux points including differential quantity columns dnde and dnde_err (optional), dnde_ul (optional).

Examples

>>> from gammapy.spectrum import FluxPoints
>>> from gammapy.modeling.models import PowerLawSpectralModel
>>> filename = '$GAMMAPY_DATA/tests/spectrum/flux_points/flux_points.fits'
>>> flux_points = FluxPoints.read(filename)
>>> model = PowerLawSpectralModel(index=2.2)
>>> flux_points_dnde = flux_points.to_sed_type('dnde', model=model)

Note: In order to reproduce the example you need the tests datasets folder. You may download it with the command gammapy download datasets --tests --out $GAMMAPY_DATA

write(self, filename, **kwargs)[source]

Write flux points.

Parameters
filenamestr

Filename

kwargsdict

Keyword arguments passed to astropy.table.Table.write.