FluxPointsDataset

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

Bases: gammapy.modeling.Dataset

Fit a set of flux points with a parametric model.

Parameters
modelsModels

Models (only spectral part needs to be set)

dataFluxPoints

Flux points.

mask_fitnumpy.ndarray

Mask to apply for fitting

mask_safenumpy.ndarray

Mask defining the safe data range.

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.spectrum import FluxPoints, FluxPointsDataset

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)
fit = Fit([dataset])
result = fit.run()
print(result)
print(result.parameters.to_table())

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

mask

Combined fit and safe mask

models

name

parameters

List of parameters (Parameters)

stat_type

tag

Methods Summary

copy(self[, name])

A deep copy.

data_shape(self)

Shape of the flux points data (tuple).

flux_pred(self)

Compute predicted flux.

from_dict(data, components, models)

Create flux point dataset from dict.

peek(self[, method])

Plot flux points, best fit model and residuals.

plot_residuals(self[, ax, method])

Plot flux point residuals.

plot_spectrum(self[, ax, fp_kwargs, …])

Plot spectrum including flux points and model.

residuals(self[, method])

Compute the flux point residuals ().

stat_array(self)

Fit statistic array.

stat_sum(self)

Total statistic given the current model parameters.

to_dict(self[, filename])

Convert to dict for YAML serialization.

write(self, filename[, overwrite])

Write flux point dataset to file.

Attributes Documentation

mask

Combined fit and safe mask

models
name
parameters

List of parameters (Parameters)

stat_type = 'chi2'
tag = 'FluxPointsDataset'

Methods Documentation

copy(self, name=None)

A deep copy.

data_shape(self)[source]

Shape of the flux points data (tuple).

flux_pred(self)[source]

Compute predicted flux.

classmethod from_dict(data, components, models)[source]

Create flux point dataset from dict.

Parameters
datadict

Dict containing data to create dataset from.

componentslist of dict

Not used.

modelslist of SkyModel

List of model components.

Returns
datasetFluxPointsDataset

Flux point datasets.

peek(self, method='diff/model', **kwargs)[source]

Plot flux points, best fit model and residuals.

Parameters
method{“diff”, “diff/model”, “diff/sqrt(model)”}

Method used to compute the residuals, see MapDataset.residuals()

plot_residuals(self, ax=None, method='diff', **kwargs)[source]

Plot flux point residuals.

Parameters
axAxes

Axes object.

method{“diff”, “diff/model”, “diff/sqrt(model)”}

Method used to compute the residuals, see MapDataset.residuals()

**kwargsdict

Keyword arguments passed to errorbar.

Returns
axAxes

Axes object.

plot_spectrum(self, ax=None, fp_kwargs=None, model_kwargs=None)[source]

Plot spectrum including flux points and model.

Parameters
axAxes

Axes object.

fp_kwargsdict

Keyword arguments passed to FluxPoints.plot.

model_kwargsdict

Keywords passed to SpectralModel.plot and SpectralModel.plot_error

Returns
axAxes

Axes object.

residuals(self, method='diff')[source]

Compute the flux point residuals ().

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

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

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

  • norm='sqrt_model' for: (flux points - model)/sqrt(model)

Returns
residualsndarray

Residuals array.

stat_array(self)[source]

Fit statistic array.

stat_sum(self)

Total statistic given the current model parameters.

to_dict(self, filename='')[source]

Convert to dict for YAML serialization.

write(self, filename, overwrite=True, **kwargs)[source]

Write flux point dataset to file.

Parameters
filenamestr

Filename to write to.

overwritebool

Overwrite existing file.

**kwargsdict

Keyword arguments passed to write.