SpectrumDatasetOnOff

class gammapy.spectrum.SpectrumDatasetOnOff(model=None, counts=None, counts_off=None, livetime=None, aeff=None, edisp=None, mask_safe=None, mask_fit=None, acceptance=None, acceptance_off=None, obs_id=None, gti=None)[source]

Bases: gammapy.spectrum.SpectrumDataset

Spectrum dataset for on-off likelihood fitting.

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

Parameters:
model : SpectralModel

Fit model

counts : CountsSpectrum

ON Counts spectrum

counts_off : CountsSpectrum

OFF Counts spectrum

livetime : Quantity

Livetime

aeff : EffectiveAreaTable

Effective area

edisp : EnergyDispersion

Energy dispersion

mask_safe : array

Mask defining the safe data range.

mask_fit : array

Mask to apply to the likelihood for fitting.

acceptance : array or float

Relative background efficiency in the on region.

acceptance_off : array or float

Relative background efficiency in the off region.

obs_id : int or list of int

Observation id(s) corresponding to the (stacked) dataset.

gti : ‘~gammapy.data.gti.GTI’

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

See also

SpectrumDataset, FluxPointsDataset, MapDataset

Attributes Summary

alpha Exposure ratio between signal and background regions
background
data_shape Shape of the counts data
energy_range Energy range defined by the safe mask
excess Excess (counts - alpha * counts_off)
likelihood_type
mask Combined fit and safe mask
model
parameters

Methods Summary

copy(self) A deep copy.
fake(self, background_model[, random_state]) Simulate fake counts for the current model and reduced irfs.
from_ogip_files(filename) Read SpectrumDatasetOnOff from OGIP files.
likelihood(self) Total likelihood given the current model parameters.
likelihood_per_bin(self) Likelihood per bin given the current model parameters
npred(self) Returns npred map (model + background)
npred_sig(self) Predicted counts from source model (CountsSpectrum).
peek(self[, figsize]) Quick-look summary plots.
plot_counts(self[, ax]) Plot predicted and detected counts.
plot_fit(self) Plot counts and residuals in two panels.
plot_residuals(self[, method, ax]) Plot residuals.
read(filename) Read from file
residuals(self[, method]) Compute the spectral residuals.
to_ogip_files(self[, outdir, use_sherpa, …]) Write OGIP files.

Attributes Documentation

alpha

Exposure ratio between signal and background regions

background
data_shape

Shape of the counts data

energy_range

Energy range defined by the safe mask

excess

Excess (counts - alpha * counts_off)

likelihood_type = 'wstat'
mask

Combined fit and safe mask

model
parameters

Methods Documentation

copy(self)

A deep copy.

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

Simulate fake counts for the current model and reduced irfs.

This method overwrites the counts and off counts defined on the dataset object.
Parameters:
background_model : CountsSpectrum

BackgroundModel. In the future will be part of the SpectrumDataset Class. For the moment, a CountSpectrum.

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

Defines random number generator initialisation. Passed to get_random_state.

classmethod from_ogip_files(filename)[source]

Read SpectrumDatasetOnOff from OGIP files.

BKG file, ARF, and RMF must be set in the PHA header and be present in the same folder.

Parameters:
filename : str

OGIP PHA file to read

likelihood(self)

Total likelihood given the current model parameters.

likelihood_per_bin(self)[source]

Likelihood per bin given the current model parameters

npred(self)

Returns npred map (model + background)

npred_sig(self)[source]

Predicted counts from source model (CountsSpectrum).

peek(self, figsize=(10, 10))[source]

Quick-look summary plots.

plot_counts(self, ax=None)

Plot predicted and detected counts.

Parameters:
ax : Axes

Axes object.

Returns:
ax : Axes

Axes object.

plot_fit(self)

Plot counts and residuals in two panels.

Calls plot_counts and plot_residuals.

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

Plot residuals.

Parameters:
ax : Axes

Axes object.

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

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

**kwargs : dict

Keywords passed to CountsSpectrum.plot()

Returns:
ax : Axes

Axes object.

classmethod read(filename)[source]

Read from file

For now, filename is assumed to the name of a PHA file where BKG file, ARF, and RMF names must be set in the PHA header and be present in the same folder

Parameters:
filename : str

OGIP PHA file to read

residuals(self, method='diff')

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

Residual spectrum.

to_ogip_files(self, outdir=None, use_sherpa=False, overwrite=False)[source]

Write OGIP files.

If you want to use the written files with Sherpa you have to set the use_sherpa flag. Then all files will be written in units ‘keV’ and ‘cm2’.

Parameters:
outdir : pathlib.Path

output directory, default: pwd

use_sherpa : bool, optional

Write Sherpa compliant files, default: False

overwrite : bool

Overwrite existing files?