Estimators#

This tutorial provides an overview of the Estimator API. All estimators live in the gammapy.estimators sub-module, offering a range of algorithms and classes for high-level flux and significance estimation. This is accomplished through a common functionality allowing the estimation of flux points, light curves, flux maps and profiles via a common API.

Key Features#

  • Hypothesis Testing: Estimations are based on testing a reference model against a null hypothesis, deriving flux and significance values.

  • Estimation via Two Methods:

    • Model Fitting (Forward Folding): Refit the flux of a model component within specified energy, time, or spatial regions.

    • Excess Calculation (Backward Folding): Use the analytical solution by Li and Ma for significance based on excess counts, currently available in ExcessMapEstimator.

For further information on these details please refer to Estimators (DL4 to DL5, and DL6).

The setup#

import numpy as np
import matplotlib.pyplot as plt
from astropy import units as u
from IPython.display import display
from gammapy.datasets import SpectrumDatasetOnOff, Datasets, MapDataset
from gammapy.estimators import (
    FluxPointsEstimator,
    ExcessMapEstimator,
    FluxPoints,
)
from gammapy.modeling import Fit, Parameter
from gammapy.modeling.models import SkyModel, PowerLawSpectralModel
from gammapy.utils.scripts import make_path

Flux Points Estimation#

We start with a simple example for flux points estimation taking multiple datasets into account. In this section we show the steps to estimate the flux points. First we read the pre-computed datasets from $GAMMAPY_DATA.

datasets = Datasets()
path = make_path("$GAMMAPY_DATA/joint-crab/spectra/hess/")

for filename in path.glob("pha_obs*.fits"):
    dataset = SpectrumDatasetOnOff.read(filename)
    datasets.append(dataset)

Next we define a spectral model and set it on the datasets:

pwl = PowerLawSpectralModel(index=2.7, amplitude="5e-11  cm-2 s-1 TeV-1")
datasets.models = SkyModel(spectral_model=pwl, name="crab")

Before using the estimators, it is necessary to first ensure that the model is properly fitted. This applies to all scenarios, including light curve estimation. To optimize the model parameters to best fit the data we utilise the following:

OptimizeResult

        backend    : minuit
        method     : migrad
        success    : True
        message    : Optimization terminated successfully.
        nfev       : 37
        total stat : 156.99

CovarianceResult

        backend    : minuit
        method     : hesse
        success    : True
        message    : Hesse terminated successfully.

A fully configured Flux Points Estimation#

The FluxPointsEstimator estimates flux points for a given list of datasets, energies and spectral model. The most simple way to call the estimator is by defining both the name of the source and its energy_edges. Here we prepare a full configuration of the flux point estimation. Firstly we define the backend for the fit:

fit = Fit(
    optimize_opts={"backend": "minuit"},
    confidence_opts={"backend": "scipy"},
)

Define the fully configured flux points estimator:

energy_edges = np.geomspace(0.7, 100, 9) * u.TeV
norm = Parameter(name="norm", value=1.0, interp="log")

fp_estimator = FluxPointsEstimator(
    source="crab",
    energy_edges=energy_edges,
    n_sigma=1,
    n_sigma_ul=2,
    selection_optional="all",
    fit=fit,
    norm=norm,
    n_jobs=4,
)

The norm parameter can be adjusted in a few different ways; either by specifying them during configuration of the norm as before, or after defining the estimator as shown below. For example, we can change its minimum and maximum values that it scans over, as follows.

Note: The default scan range of the norm parameter is between 0.1 to 10. In case the upper limit values lie outside this range, nan values will be returned. It may thus be useful to increase this range, specially for the computation of upper limits from weak sources.

For bright sources, you should also increase norm.scan_n_values if the range is increased, so as not to lose resolution close to the minima. Normally, the min and max values are not set. If you want to add, eg: a positivity constraint you can configure the same

fp_estimator.norm.min = 0
#
# The various quantities utilised in this tutorial are described here:
#
# -  ``source``: which source from the model to compute the flux points for
# -  ``energy_edges``: edges of the flux points energy bins
# -  ``n_sigma``: number of sigma for the flux error
# -  ``n_sigma_ul``: the number of sigma for the flux upper limits
# -  ``selection_optional``: what additional maps to compute
# -  ``fit``: the fit instance (as defined above)
# -  ``reoptimize``: whether to reoptimize the flux points with other model parameters, aside from the ``norm``
# -  ``norm``: normalisation parameter for the fit
# -  ``n_jobs``: Number of processes used in parallel for the computation, limited to the no of CPUs
#
# **Important note**: the output ``energy_edges`` are taken from the parent dataset energy bins,
# selecting the bins closest to the requested ``energy_edges``. To match the input bins directly,
# specific binning must be defined based on the parent dataset geometry. This could be done in the following way:
# ``energy_edges = datasets[0].geoms["geom"].axes["energy"].downsample(factor=5).edges``

fp_result = fp_estimator.run(datasets=datasets)

Accessing and visualising the results#

print(fp_result)
FluxPoints
----------

  geom                   : RegionGeom
  axes                   : ['lon', 'lat', 'energy']
  shape                  : (1, 1, 8)
  quantities             : ['norm', 'norm_err', 'norm_errn', 'norm_errp', 'norm_ul', 'norm_sensitivity', 'ts', 'npred', 'npred_excess', 'stat', 'stat_null', 'stat_scan', 'counts', 'success']
  ref. model             : pl
  n_sigma                : 1
  n_sigma_ul             : 2
  sqrt_ts_threshold_ul   : 2
  sed type init          : likelihood

We can specify the SED type to plot:

fp_result.plot(sed_type="dnde")
plt.show()
estimators

We can also access the quantities names through fp_result.available_quantities. Here we show how you can plot a different plot type and define the axes units, we also overlay the TS profile.

ax = plt.subplot()
ax.xaxis.set_units(u.eV)
ax.yaxis.set_units(u.Unit("TeV cm-2 s-1"))
fp_result.plot(ax=ax, sed_type="e2dnde", color="tab:orange")
fp_result.plot_ts_profiles(ax=ax, sed_type="e2dnde")
plt.show()
estimators

The actual data members are N-dimensional RegionNDMap objects. So you can also plot them:

print(type(fp_result.dnde))
<class 'gammapy.maps.region.ndmap.RegionNDMap'>
estimators

From the above, we can see that we access to many quantities.

Access the data:

print(fp_result.e2dnde.quantity.to("TeV cm-2 s-1"))
[[[4.45589510e-11]]

 [[3.25789552e-11]]

 [[2.13945682e-11]]

 [[1.51960075e-11]]

 [[6.92056246e-12]]

 [[2.03644001e-12]]

 [[5.39933945e-27]]

 [[4.70798405e-12]]] TeV / (s cm2)
(8, 1, 1)
print(fp_result.dnde.quantity[:, 0, 0])
[4.99959653e-11 1.03023702e-11 1.90679290e-12 4.28281682e-13
 5.49719816e-14 4.55902130e-15 3.40675288e-30 8.37211110e-16] 1 / (TeV s cm2)

Or even extract an energy range:

fp_result.dnde.slice_by_idx({"energy": slice(3, 10)})
RegionNDMap

    geom  : RegionGeom
    axes  : ['lon', 'lat', 'energy']
    shape : (1, 1, 5)
    ndim  : 3
    unit  : 1 / (TeV s cm2)
    dtype : float64


Plotting the likelihood profile. It is often useful to visualise the likelihood profiles to ensure that the minima was scanned over for all the energy bands. This is available in fp_result.ts_scan

# The scanned points may be printed as:
print(fp_result.ts_scan.geom.axes["norm"].center)
[ 0.05        0.09102821  0.1657227   0.30170882  0.54928027  1.
  1.8205642   3.31445402  6.03417634 10.98560543 20.        ]

The TS values are stored in fp_result.ts_scan.data To the plot likelihood profile for the, say, 4th energy slice:

plt.plot(
    fp_result.ts_scan.geom.axes["norm"].center,
    fp_result.ts_scan.data[3][:][:][:].ravel(),
)
plt.title(f"Likelihood scan for energy {fp_result.energy_axis.slice(3).edges}")
plt.xscale("log")
plt.show()
Likelihood scan for energy [4.46683592e+09 7.94328235e+09] keV

A note on the internal representation#

The result contains a reference spectral model, which defines the spectral shape. Typically, it is the best fit model:

SkyModel

  Name                      : hk6O6XG4
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       :
  Temporal model type       :
  Parameters:
    index                         :      2.700   +/-    0.07
    amplitude                     :   4.58e-11   +/- 2.8e-12 1 / (TeV s cm2)
    reference             (frozen):      1.000       TeV

FluxPoints are the represented by the “norm” scaling factor with respect to the reference model:

estimators

Dataset specific quantities (“counts like”)#

While the flux estimate and associated errors are common to all datasets, the result also stores some dataset specific quantities, which can be useful for debugging. Here we remind the user of the meaning of the forthcoming quantities:

  • counts: observed counts outside the masked bins

  • npred: predicted number of counts from best fit hypothesis,

  • npred_excess: predicted number of excess counts from best fit hypothesis.

The RegionNDMap allows for plotting of multidimensional data as well, by specifying the primary axis_name:

fp_result.counts.plot(axis_name="energy")
plt.show()
estimators
fp_result.npred.plot(axis_name="energy")
plt.show()
estimators
fp_result.npred_excess.plot(axis_name="energy")
plt.show()
estimators

Table conversion#

Flux points can be converted to tables:

table = fp_result.to_table(sed_type="flux", format="gadf-sed")
display(table)
      e_ref        ...                 norm_scan
       keV         ...
------------------ ... -----------------------------------------
 944060876.2859229 ... 0.05000000000000001 .. 19.999999999999996
  1778279410.03893 ... 0.05000000000000001 .. 19.999999999999996
3349654391.5782814 ... 0.05000000000000001 .. 19.999999999999996
 5956621435.290098 ... 0.05000000000000001 .. 19.999999999999996
11220184543.019672 ... 0.05000000000000001 .. 19.999999999999996
21134890398.366486 ... 0.05000000000000001 .. 19.999999999999996
39810717055.349655 ... 0.05000000000000001 .. 19.999999999999996
 74989420933.24579 ... 0.05000000000000001 .. 19.999999999999996
table = fp_result.to_table(sed_type="likelihood", format="gadf-sed", formatted=True)
display(table)
     e_ref           e_min           e_max       ... success    norm_scan
      keV             keV             keV        ...
--------------- --------------- ---------------- ... ------- ---------------
  944060876.286   707945784.384   1258925411.794 ...    True 0.050 .. 20.000
 1778279410.039  1258925411.794   2511886431.510 ...    True 0.050 .. 20.000
 3349654391.578  2511886431.510   4466835921.510 ...    True 0.050 .. 20.000
 5956621435.290  4466835921.510   7943282347.243 ...    True 0.050 .. 20.000
11220184543.020  7943282347.243  15848931924.611 ...    True 0.050 .. 20.000
21134890398.366 15848931924.611  28183829312.645 ...    True 0.050 .. 20.000
39810717055.350 28183829312.645  56234132519.035 ...    True 0.050 .. 20.000
74989420933.246 56234132519.035 100000000000.000 ...    True 0.050 .. 20.000

Common API#

In GAMMAPY_DATA we have access to other FluxPoints objects which have been created utilising the above method. Here we read the PKS 2155-304 light curve and create a FluxMaps object and show the data structure of such objects. We emphasize that these follow a very similar structure.

Load the light curve for the PKS 2155-304 as a FluxPoints object.#

lightcurve = FluxPoints.read(
    "$GAMMAPY_DATA/estimators/pks2155_hess_lc/pks2155_hess_lc.fits", format="lightcurve"
)

display(lightcurve.available_quantities)
['norm', 'norm_err', 'norm_errn', 'norm_errp', 'norm_ul', 'ts', 'sqrt_ts', 'npred', 'npred_excess', 'stat', 'stat_null', 'stat_scan', 'is_ul', 'counts', 'success']

Create a FluxMaps object through one of the estimators.#

dataset = MapDataset.read("$GAMMAPY_DATA/cta-1dc-gc/cta-1dc-gc.fits.gz")
estimator = ExcessMapEstimator(correlation_radius="0.1 deg")
result = estimator.run(dataset)
display(result)
FluxMaps
--------

  geom                   : WcsGeom
  axes                   : ['lon', 'lat', 'energy']
  shape                  : (np.int64(320), np.int64(240), 1)
  quantities             : ['npred', 'npred_excess', 'counts', 'ts', 'sqrt_ts', 'norm', 'norm_err']
  ref. model             : pl
  n_sigma                : 1
  n_sigma_ul             : 2
  sqrt_ts_threshold_ul   : 2
  sed type init          : likelihood
['npred', 'npred_excess', 'counts', 'ts', 'sqrt_ts', 'norm', 'norm_err']

Gallery generated by Sphinx-Gallery