FluxPointsDataset#

class gammapy.datasets.FluxPointsDataset(models=None, data=None, mask_fit=None, mask_safe=None, name=None, meta_table=None)[source]#

Bases: gammapy.datasets.core.Dataset

Bundle a set of flux points with a parametric model, to compute fit statistic function using chi2 statistics.

Parameters
modelsModels

Models (only spectral part needs to be set)

dataFluxPoints

Flux points. Must be sorted along the energy axis

mask_fitnumpy.ndarray

Mask to apply for fitting

mask_safenumpy.ndarray

Mask defining the safe data range. By default upper limit values are excluded.

meta_tableTable

Table listing information on observations used to create the dataset. One line per observation for stacked datasets.

Examples

Load flux points from file and fit with a power-law model:

>>> from gammapy.modeling import Fit
>>> from gammapy.modeling.models import PowerLawSpectralModel, SkyModel
>>> from gammapy.estimators import FluxPoints
>>> from gammapy.datasets import FluxPointsDataset
>>> filename = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits"
>>> dataset = FluxPointsDataset.read(filename)
>>> model = SkyModel(spectral_model=PowerLawSpectralModel())
>>> dataset.models = model

Make the fit

>>> fit = Fit()
>>> result = fit.run([dataset])
>>> print(result)
OptimizeResult

    backend    : minuit
    method     : migrad
    success    : True
    message    : Optimization terminated successfully.
    nfev       : 135
    total stat : 25.21

CovarianceResult

    backend    : minuit
    method     : hesse
    success    : True
    message    : Hesse terminated successfully.
>>> print(result.parameters.to_table())
  type      name     value         unit      ... max frozen is_norm link
-------- --------- ---------- -------------- ... --- ------ ------- ----
spectral     index 2.2159e+00                ... nan  False   False
spectral amplitude 2.1619e-13 cm-2 s-1 TeV-1 ... nan  False    True
spectral reference 1.0000e+00            TeV ... nan   True   False

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 Summary

gti

Good time interval info (GTI)

mask

Combined fit and safe mask

models

name

stat_type

tag

Methods Summary

copy([name])

A deep copy.

data_shape()

Shape of the flux points data (tuple).

flux_pred()

Compute predicted flux.

from_dict(data, **kwargs)

Create flux point dataset from dict.

plot_fit([ax_spectrum, ax_residuals, ...])

Plot flux points, best fit model and residuals in two panels.

plot_residuals([ax, method])

Plot flux point residuals.

plot_spectrum([ax, kwargs_fp, kwargs_model])

Plot spectrum including flux points and model.

read(filename[, name, format])

Read pre-computed flux points and create a dataset

residuals([method])

Compute flux point residuals

stat_array()

Fit statistic array.

stat_sum()

Total statistic given the current model parameters.

to_dict()

Convert to dict for YAML serialization.

write(filename[, overwrite])

Write flux point dataset to file.

Attributes Documentation

gti#

Good time interval info (GTI)

mask#

Combined fit and safe mask

models#
name#
stat_type = 'chi2'#
tag = 'FluxPointsDataset'#

Methods Documentation

copy(name=None)#

A deep copy.

Parameters
namestr

Name of the copied dataset

Returns
datasetDataset

Copied datasets.

data_shape()[source]#

Shape of the flux points data (tuple).

flux_pred()[source]#

Compute predicted flux.

classmethod from_dict(data, **kwargs)[source]#

Create flux point dataset from dict.

Parameters
datadict

Dict containing data to create dataset from.

Returns
datasetFluxPointsDataset

Flux point datasets.

plot_fit(ax_spectrum=None, ax_residuals=None, kwargs_spectrum=None, kwargs_residuals=None)[source]#

Plot flux points, best fit model and residuals in two panels.

Calls plot_spectrum and plot_residuals.

Parameters
ax_spectrumAxes

Axes to plot flux points and best fit model on.

ax_residualsAxes

Axes to plot residuals on.

kwargs_spectrumdict

Keyword arguments passed to plot_spectrum.

kwargs_residualsdict

Keyword arguments passed to plot_residuals.

Returns
ax_spectrum, ax_residualsAxes

Flux points, best fit model and residuals plots.

Examples

>>> from gammapy.modeling.models import PowerLawSpectralModel, SkyModel
>>> from gammapy.estimators import FluxPoints
>>> from gammapy.datasets import FluxPointsDataset
>>> #load precomputed flux points
>>> filename = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits"
>>> flux_points = FluxPoints.read(filename)
>>> model = SkyModel(spectral_model=PowerLawSpectralModel())
>>> dataset = FluxPointsDataset(model, flux_points)
>>> #configuring optional parameters
>>> kwargs_spectrum = {"kwargs_model": {"color":"red", "ls":"--"}, "kwargs_fp":{"color":"green", "marker":"o"}}  # noqa: E501
>>> kwargs_residuals = {"color": "blue", "markersize":4, "marker":'s', }
>>> dataset.plot_fit(kwargs_residuals=kwargs_residuals, kwargs_spectrum=kwargs_spectrum) 
plot_residuals(ax=None, method='diff', **kwargs)[source]#

Plot flux point residuals.

Parameters
axAxes

Axes to plot on.

method{“diff”, “diff/model”}

Normalization used to compute the residuals, see FluxPointsDataset.residuals.

**kwargsdict

Keyword arguments passed to errorbar.

Returns
axAxes

Axes object.

plot_spectrum(ax=None, kwargs_fp=None, kwargs_model=None)[source]#

Plot spectrum including flux points and model.

Parameters
axAxes

Axes to plot on.

kwargs_fpdict

Keyword arguments passed to gammapy.estimators.FluxPoints.plot.

kwargs_modeldict

Keyword arguments passed to gammapy.modeling.models.SpectralModel.plot and gammapy.modeling.models.SpectralModel.plot_error.

Returns
axAxes

Axes object.

Examples

>>> from gammapy.modeling.models import PowerLawSpectralModel, SkyModel
>>> from gammapy.estimators import FluxPoints
>>> from gammapy.datasets import FluxPointsDataset
>>> #load precomputed flux points
>>> filename = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits"
>>> flux_points = FluxPoints.read(filename)
>>> model = SkyModel(spectral_model=PowerLawSpectralModel())
>>> dataset = FluxPointsDataset(model, flux_points)
>>> #configuring optional parameters
>>> kwargs_model = {"color":"red", "ls":"--"}
>>> kwargs_fp = {"color":"green", "marker":"o"}
>>> dataset.plot_spectrum(kwargs_fp=kwargs_fp, kwargs_model=kwargs_model) 
classmethod read(filename, name=None, format='gadf-sed')[source]#

Read pre-computed flux points and create a dataset

Parameters
filenamestr

Filename to read from.

namestr

Name of the new dataset.

format{“gadf-sed”}

Format of the dataset file.

Returns
datasetFluxPointsDataset

FluxPointsDataset

residuals(method='diff')[source]#

Compute flux point residuals

Parameters
method: {“diff”, “diff/model”}
Method used to compute the residuals. Available options are:
  • diff (default): data - model

  • diff/model: (data - model) / model

Returns
residualsndarray

Residuals array.

stat_array()[source]#

Fit statistic array.

stat_sum()#

Total statistic given the current model parameters.

to_dict()#

Convert to dict for YAML serialization.

write(filename, overwrite=False, **kwargs)[source]#

Write flux point dataset to file.

Parameters
filenamestr

Filename to write to.

overwritebool

Overwrite existing file.

**kwargsdict

Keyword arguments passed to write.