FluxPointsDataset

class gammapy.spectrum.FluxPointsDataset(model, data, mask_fit=None, likelihood='chi2', mask_safe=None)[source]

Bases: gammapy.utils.fitting.Dataset

Fit a set of flux points with a parametric model.

Parameters:
model : SpectralModel

Spectral model

data : FluxPoints

Flux points.

mask_fit : numpy.ndarray

Mask to apply to the likelihood for fitting.

likelihood : {“chi2”, “chi2assym”}

Likelihood function to use for the fit.

mask_safe : numpy.ndarray

Mask defining the safe data range.

Examples

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

from astropy import units as u
from gammapy.spectrum import FluxPoints, FluxPointsDataset
from gammapy.utils.fitting import Fit
from gammapy.spectrum.models import PowerLaw

filename = '$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits'
flux_points = FluxPoints.read(filename)

model = PowerLaw()

dataset = FluxPointsDataset(model, flux_points)
fit = Fit(dataset)
result = fit.run()
print(result)
print(result.parameters.to_table())

Attributes Summary

mask Combined fit and safe mask

Methods Summary

copy(self) A deep copy.
data_shape(self) Shape of the flux points data (tuple).
flux_pred(self) Compute predicted flux.
likelihood(self) Total likelihood given the current model parameters.
likelihood_per_bin(self) Likelihood per bin given the current model parameters.
peek(self[, norm]) 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 ().

Attributes Documentation

mask

Combined fit and safe mask

Methods Documentation

copy(self)

A deep copy.

data_shape(self)[source]

Shape of the flux points data (tuple).

flux_pred(self)[source]

Compute predicted flux.

likelihood(self)

Total likelihood given the current model parameters.

likelihood_per_bin(self)[source]

Likelihood per bin given the current model parameters.

peek(self, norm=None, **kwargs)[source]

Plot flux points, best fit model and residuals.

Parameters:
norm: `str`

normalization used to compute the spectral residuals. Choose between None, model and sqrt_model.

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

Plot flux point residuals.

Parameters:
ax : Axes

Axes object.

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

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

**kwargs : dict

Keyword arguments passed to errorbar.

Returns:
ax : Axes

Axes object.

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

Plot spectrum including flux points and model.

Parameters:
ax : Axes

Axes object.

fp_kwargs : dict

Keyword arguments passed to FluxPoints.plot.

model_kwargs : dict

Keywords passed to SpectralModel.plot and SpectralModel.plot_error

Returns:
ax : Axes

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:
residuals : ndarray

Residuals array.