SpectrumDataset

class gammapy.datasets.SpectrumDataset(models=None, counts=None, livetime=None, aeff=None, edisp=None, mask_safe=None, mask_fit=None, name=None, gti=None, meta_table=None)[source]

Bases: gammapy.datasets.Dataset

Spectrum dataset for likelihood fitting.

The spectrum dataset bundles reduced counts data, with a spectral model, background model and instrument response function to compute the fit-statistic given the current model and data.

Parameters
modelsModels

Fit model

countsRegionNDMap

Counts spectrum

livetimeQuantity

Livetime

aeffRegionNDMap

Effective area

edispEDispKernelMap

Energy dispersion kernel.

mask_safeRegionNDMap

Mask defining the safe data range.

mask_fitRegionNDMap

Mask to apply to the likelihood for fitting.

namestr

Dataset name.

gtiGTI

GTI of the observation or union of GTI if it is a stacked observation

meta_tableTable

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

Attributes Summary

background_model

data_shape

Shape of the counts data

energy_range

Energy range defined by the safe mask

evaluators

Model evaluators

excess

exposure

Excess (aeff * livetime)

mask

Combined fit and safe mask

mask_safe

models

Models (gammapy.modeling.models.Models).

name

stat_type

tag

Methods Summary

copy([name])

A deep copy.

create(e_reco[, e_true, region, …])

Creates empty spectrum dataset.

fake([random_state])

Simulate fake counts for the current model and reduced irfs.

info_dict([in_safe_energy_range])

Info dict with summary statistics, summed over energy

npred()

Predicted counts from source and background model (RegionNDMap).

peek([figsize])

Quick-look summary plots.

plot_counts([ax])

Plot predicted and detected counts.

plot_fit()

Plot counts and residuals in two panels.

plot_residuals([method, ax])

Plot residuals.

residuals([method])

Compute the spectral residuals.

slice_by_idx(slices[, name])

Slice sub dataset.

stack(other)

Stack this dataset with another one.

stat_array()

Likelihood per bin given the current model parameters

stat_sum()

Total statistic given the current model parameters.

Attributes Documentation

background_model
data_shape

Shape of the counts data

energy_range

Energy range defined by the safe mask

evaluators

Model evaluators

excess
exposure

Excess (aeff * livetime)

mask

Combined fit and safe mask

mask_safe
models

Models (gammapy.modeling.models.Models).

name
stat_type = 'cash'
tag = 'SpectrumDataset'

Methods Documentation

copy(name=None)

A deep copy.

classmethod create(e_reco, e_true=None, region=None, reference_time='2000-01-01', name=None, meta_table=None)[source]

Creates empty spectrum dataset.

Empty containers are created with the correct geometry. counts, background and aeff are zero and edisp is diagonal.

The safe_mask is set to False in every bin.

Parameters
e_recoMapAxis

counts energy axis. Its name must be “energy”.

e_trueMapAxis

effective area table energy axis. Its name must be “energy-true”. If not set use reco energy values. Default : None

regionSkyRegion

Region to define the dataset for.

reference_timeTime

reference time of the dataset, Default is “2000-01-01”

meta_tableTable

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

fake(random_state='random-seed')[source]

Simulate fake counts for the current model and reduced irfs.

This method overwrites the counts defined on the dataset object.

Parameters
random_state{int, ‘random-seed’, ‘global-rng’, RandomState}

Defines random number generator initialisation. Passed to get_random_state.

info_dict(in_safe_energy_range=True)[source]

Info dict with summary statistics, summed over energy

Parameters
in_safe_energy_rangebool

Whether to sum only in the safe energy range

Returns
info_dictdict

Dictionary with summary info.

npred()[source]

Predicted counts from source and background model (RegionNDMap).

peek(figsize=(16, 4))[source]

Quick-look summary plots.

plot_counts(ax=None)[source]

Plot predicted and detected counts.

Parameters
axAxes

Axes object.

Returns
axAxes

Axes object.

plot_fit()[source]

Plot counts and residuals in two panels.

Calls plot_counts and plot_residuals.

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

Plot residuals.

Parameters
axAxes

Axes object.

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

Normalization used to compute the residuals, see SpectrumDataset.residuals()

**kwargsdict

Keywords passed to RegionNDMap.plot()

Returns
axAxes

Axes object.

residuals(method='diff')[source]

Compute the spectral 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)

Returns
residualsRegionNDMap

Residual spectrum

slice_by_idx(slices, name=None)[source]

Slice sub dataset.

The slicing only applies to the maps that define the corresponding axes.

Parameters
slicesdict

Dict of axes names and integers or slice object pairs. Contains one element for each non-spatial dimension. For integer indexing the corresponding axes is dropped from the map. Axes not specified in the dict are kept unchanged.

namestr

Name of the sliced dataset.

Returns
map_outMap

Sliced map object.

stack(other)[source]

Stack this dataset with another one.

Safe mask is applied to compute the stacked counts vector. Counts outside each dataset safe mask are lost.

Stacking is performed in-place.

The stacking of 2 datasets is implemented as follows. Here, \(k\) denotes a bin in reconstructed energy and \(j = {1,2}\) is the dataset number

The mask_safe of each dataset is defined as:

\[\begin{split}\epsilon_{jk} =\left\{\begin{array}{cl} 1, & \mbox{if bin k is inside the energy thresholds}\\ 0, & \mbox{otherwise} \end{array}\right.\end{split}\]

Then the total counts and model background bkg are computed according to:

\[ \begin{align}\begin{aligned}\overline{\mathrm{n_{on}}}_k = \mathrm{n_{on}}_{1k} \cdot \epsilon_{1k} + \mathrm{n_{on}}_{2k} \cdot \epsilon_{2k}\\\overline{bkg}_k = bkg_{1k} \cdot \epsilon_{1k} + bkg_{2k} \cdot \epsilon_{2k}\end{aligned}\end{align} \]

The stacked safe_mask is then:

\[\overline{\epsilon_k} = \epsilon_{1k} OR \epsilon_{2k}\]
Parameters
otherSpectrumDataset

the dataset to stack to the current one

stat_array()[source]

Likelihood per bin given the current model parameters

stat_sum()

Total statistic given the current model parameters.