This is a fixed-text formatted version of a Jupyter notebook

H.E.S.S. with Gammapy

This tutorial explains how to analyse H.E.S.S. data with Gammapy.

We will analyse four observation runs of the Crab nebula, which are part of the H.E.S.S. first public test data release. In this tutorial we will make an image and a spectrum. The light_curve.ipynb notbook contains an example how to make a light curve.

To do a 3D analysis, one needs to do a 3D background estimate. In background_model.ipynb we have started to make a background model, and in this notebook we have a first look at a 3D analysis. But the results aren’t OK yet, the background model needs to be improved. In this analysis, we also don’t use the energy dispersion IRF yet, and we only analyse the data in the 1 TeV to 10 TeV range. The H.E.S.S. data was only released very recently, and 3D analysis in Gammapy is new. This tutorial will be improved soon.

This tutorial also shows how to do a classical image analysis using the ring bakground.

[1]:
%matplotlib inline
import matplotlib.pyplot as plt
[2]:
import numpy as np
from scipy.stats import norm
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.convolution import Tophat2DKernel
from regions import CircleSkyRegion
from gammapy.data import DataStore
from gammapy.maps import Map, MapAxis, WcsGeom
from gammapy.cube import MapMaker, MapDataset, PSFKernel, MapMakerRing
from gammapy.cube.models import SkyModel, BackgroundModel
from gammapy.spectrum.models import PowerLaw
from gammapy.spectrum import CrabSpectrum
from gammapy.image.models import SkyPointSource
from gammapy.detect import compute_lima_on_off_image
from gammapy.scripts import SpectrumAnalysisIACT
from gammapy.utils.fitting import Fit
from gammapy.background import RingBackgroundEstimator

Data access

To access the data, we use the DataStore, and we use the obs_table to select the Crab runs.

[3]:
data_store = DataStore.from_file(
    "$GAMMAPY_DATA/hess-dl3-dr1/hess-dl3-dr3-with-background.fits.gz"
)
mask = data_store.obs_table["TARGET_NAME"] == "Crab"
obs_table = data_store.obs_table[mask]
observations = data_store.get_observations(obs_table["OBS_ID"])
[4]:
# pos_crab = SkyCoord.from_name('Crab')
pos_crab = SkyCoord(83.633, 22.014, unit="deg")

Maps

Let’s make some 3D cubes, as well as 2D images.

For the energy, we make 5 bins from 1 TeV to 10 TeV.

[5]:
energy_axis = MapAxis.from_edges(
    np.logspace(0, 1.0, 5), unit="TeV", name="energy", interp="log"
)
geom = WcsGeom.create(
    skydir=(83.633, 22.014),
    binsz=0.02,
    width=(5, 5),
    coordsys="CEL",
    proj="TAN",
    axes=[energy_axis],
)
[6]:
%%time
maker = MapMaker(geom, offset_max="2.5 deg")
maps = maker.run(observations)
images = maker.run_images()
CPU times: user 4.09 s, sys: 372 ms, total: 4.46 s
Wall time: 4.48 s
[7]:
maps.keys()
[7]:
dict_keys(['counts', 'exposure', 'background'])
[8]:
images["counts"].smooth(3).plot(stretch="sqrt", vmax=2);
../_images/notebooks_hess_11_0.png

PSF

Compute the mean PSF for these observations at the Crab position.

[9]:
from gammapy.irf import make_mean_psf

table_psf = make_mean_psf(observations, pos_crab)
[10]:
psf_kernel = PSFKernel.from_table_psf(table_psf, geom, max_radius="0.3 deg")
psf_kernel_array = psf_kernel.psf_kernel_map.sum_over_axes().data
# psf_kernel.psf_kernel_map.slice_by_idx({'energy': 0}).plot()
# plt.imshow(psf_kernel_array)

Map fit

Let’s fit this source assuming a Gaussian spatial shape and a power-law spectral shape, and a background with a flexible normalisation

[11]:
spatial_model = SkyPointSource(
    lon_0="83.6 deg", lat_0="22.0 deg", frame="icrs"
)
spectral_model = PowerLaw(
    index=2.6, amplitude="5e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)
model = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model)
background_model = BackgroundModel(maps["background"], norm=1.0)
background_model.parameters["tilt"].frozen = False
[12]:
%%time
dataset = MapDataset(
    model=model,
    counts=maps["counts"],
    exposure=maps["exposure"],
    background_model=background_model,
    psf=psf_kernel,
)
fit = Fit(dataset)
result = fit.run()
print(result)
OptimizeResult

        backend    : minuit
        method     : minuit
        success    : True
        nfev       : 258
        total stat : 61227.19
        message    : Optimization terminated successfully.

CPU times: user 2.37 s, sys: 58.5 ms, total: 2.43 s
Wall time: 2.45 s

Best fit parameters:

[13]:
result.parameters.to_table()
[13]:
Table length=8
namevalueerrorunitminmaxfrozen
str9float64float64str14float64float64bool
lon_08.362e+012.978e-03deg-1.800e+021.800e+02False
lat_02.202e+012.789e-03deg-9.000e+019.000e+01False
index2.634e+009.307e-02nannanFalse
amplitude4.990e-114.168e-12cm-2 s-1 TeV-1nannanFalse
reference1.000e+000.000e+00TeVnannanTrue
norm2.920e+006.038e-020.000e+00nanFalse
tilt-1.984e-022.060e-02nannanFalse
reference1.000e+000.000e+00TeVnannanTrue

Parameters covariance:

[14]:
result.parameters.covariance_to_table()
[14]:
Table length=8
namelon_0lat_0indexamplitudereferencenormtilt
str9float64float64float64float64float64float64float64
lon_08.869e-06-3.314e-071.360e-06-2.569e-170.000e+001.786e-07-5.760e-08
lat_0-3.314e-077.779e-066.848e-071.014e-160.000e+00-3.847e-07-6.601e-08
index1.360e-066.848e-078.661e-033.099e-130.000e+00-2.035e-04-8.816e-05
amplitude-2.569e-171.014e-163.099e-131.737e-230.000e+00-1.311e-14-3.551e-15
reference0.000e+000.000e+000.000e+000.000e+000.000e+000.000e+000.000e+00
norm1.786e-07-3.847e-07-2.035e-04-1.311e-140.000e+003.646e-031.010e-03
tilt-5.760e-08-6.601e-08-8.816e-05-3.551e-150.000e+001.010e-034.242e-04
reference0.000e+000.000e+000.000e+000.000e+000.000e+000.000e+000.000e+00

Residual image

We compute a residual image as residual = counts - model. Note that this is counts per pixel and our pixel size is 0.02 deg. Smoothing is counts-preserving. The residual image shows that currently both the source and the background modeling isn’t very good. The background model is underestimated (so residual is positive), and the source model is overestimated.

[15]:
npred = dataset.npred()
residual = Map.from_geom(maps["counts"].geom)
residual.data = maps["counts"].data - npred.data
[16]:
residual.sum_over_axes().smooth("0.1 deg").plot(
    cmap="coolwarm", vmin=-0.2, vmax=0.2, add_cbar=True
);
../_images/notebooks_hess_24_0.png

Spectrum

We could try to improve the background modeling and spatial model of the source. But let’s instead turn to one of the classic IACT analysis techniques: use a circular on region and reflected regions for background estimation, and derive a spectrum for the source without having to assume a spatial model, or without needing a 3D background model.

[17]:
%%time
on_region = CircleSkyRegion(pos_crab, 0.11 * u.deg)
exclusion_mask = Map.from_geom(geom.to_image())
exclusion_mask.data = np.ones_like(exclusion_mask.data, dtype=bool)

model_pwl = PowerLaw(
    index=2.6, amplitude="5e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)

config = {
    "outdir": ".",
    "background": {"on_region": on_region, "exclusion_mask": exclusion_mask},
    "extraction": {"containment_correction": True},
    "fit": {"model": model_pwl, "fit_range": [1, 10] * u.TeV},
    "fp_binning": np.logspace(0, 1, 11) * u.TeV,
}
analysis = SpectrumAnalysisIACT(observations=observations, config=config)
analysis.run()
/Users/deil/work/code/gammapy-docs/build/dev/gammapy/gammapy/spectrum/extract.py:230: RuntimeWarning: invalid value encountered in true_divide
  self.containment = new_aeff.data.data.value / self._aeff.data.data.value
CPU times: user 6.68 s, sys: 101 ms, total: 6.78 s
Wall time: 6.69 s
[18]:
print(model_pwl)
PowerLaw

Parameters:

           name     value     error        unit      min max frozen
        --------- --------- --------- -------------- --- --- ------
            index 2.560e+00 1.080e-01                nan nan  False
        amplitude 4.373e-11 3.981e-12 cm-2 s-1 TeV-1 nan nan  False
        reference 1.000e+00 0.000e+00            TeV nan nan   True

Covariance:

           name     index   amplitude reference
        --------- --------- --------- ---------
            index 1.166e-02 3.316e-13 0.000e+00
        amplitude 3.316e-13 1.585e-23 0.000e+00
        reference 0.000e+00 0.000e+00 0.000e+00
[19]:
plt.figure(figsize=(10, 8))
crab_ref = CrabSpectrum("hess_pl").model

dataset_fp = analysis.spectrum_result

plot_kwargs = {
    "energy_range": [1, 10] * u.TeV,
    "flux_unit": "erg-1 cm-2 s-1",
    "energy_power": 2,
}

model_kwargs = {"label": "1D best fit model"}
model_kwargs.update(plot_kwargs)
ax_spectrum, ax_residuals = dataset_fp.peek(model_kwargs=model_kwargs)

crab_ref.plot(ax=ax_spectrum, label="H.E.S.S. 2006 PWL", **plot_kwargs)
model.spectral_model.plot(
    ax=ax_spectrum, label="3D best fit model", **plot_kwargs
)

ax_spectrum.set_ylim(1e-11, 1e-10)
ax_spectrum.legend();
../_images/notebooks_hess_28_0.png

Classical Ring Background Analysis

In this section, we do a classical ring background analysis on the MSH 15-52 region. Let us first select these runs in the datastore

[20]:
data_store = DataStore.from_file(
    "$GAMMAPY_DATA/hess-dl3-dr1/hess-dl3-dr3-with-background.fits.gz"
)
data_sel = data_store.obs_table["TARGET_NAME"] == "MSH 15-52"
obs_table = data_store.obs_table[data_sel]
observations = data_store.get_observations(obs_table["OBS_ID"])
[21]:
# pos_msh1552 = SkyCoord.from_name('MSH15-52')
pos_msh1552 = SkyCoord(228.32, -59.08, unit="deg")

We first have to define the geometry on which we make our 2D map.

[22]:
energy_axis = MapAxis.from_edges(
    np.logspace(0, 5.0, 5), unit="TeV", name="energy", interp="log"
)
geom = WcsGeom.create(
    skydir=pos_msh1552,
    binsz=0.02,
    width=(5, 5),
    coordsys="CEL",
    proj="TAN",
    axes=[energy_axis],
)

Choose an exclusion mask.

We choose an exclusion mask on the position of MSH 1552.

[23]:
regions = CircleSkyRegion(center=pos_msh1552, radius=0.3 * u.deg)
mask = Map.from_geom(geom)
mask.data = mask.geom.region_mask([regions], inside=False)
mask.get_image_by_idx([0]).plot();
../_images/notebooks_hess_35_0.png

Now, we instantiate the ring background

[24]:
ring_bkg = RingBackgroundEstimator(r_in="0.5 deg", width="0.3 deg")

To facilitate classical image analysis, we have a special class called MapMakerRing. Here, we do the analysis over the integrated energy range. To do an analysis for each image slice of the map (eg: to investigate the significance of the source detection with energy, just call run instead of run_images

[25]:
%%time
im = MapMakerRing(
    geom=geom,
    offset_max=2.0 * u.deg,
    exclusion_mask=mask,
    background_estimator=ring_bkg,
)
images = im.run_images(observations)
CPU times: user 6.97 s, sys: 560 ms, total: 7.53 s
Wall time: 7.51 s

We will use compute_lima_on_off_image in gammapy.detect to compute significance and excess maps. A common debug plot during Ring Analysis is to make histograms of the off count rates, so we will plot that as well.

For this we will first create a Tophat2DKernel convolution kernel for the significance maps. To convert from angles to pixel scales, we use geom.pixel_scales:

[26]:
scale = geom.pixel_scales[0].to("deg")
# Using a convolution radius of 0.05 degrees
theta = 0.05 * u.deg / scale
tophat = Tophat2DKernel(theta)
tophat.normalize("peak")
[27]:
lima_maps = compute_lima_on_off_image(
    images["on"],
    images["off"],
    images["exposure_on"],
    images["exposure_off"],
    tophat,
)
[28]:
significance_map = lima_maps["significance"]
excess_map = lima_maps["excess"]
[29]:
plt.figure(figsize=(10, 10))
ax1 = plt.subplot(221, projection=significance_map.geom.wcs)
ax2 = plt.subplot(222, projection=excess_map.geom.wcs)

ax1.set_title("Significance map")
significance_map.plot(ax=ax1, add_cbar=True)

ax2.set_title("Excess map")
excess_map.plot(ax=ax2, add_cbar=True);
../_images/notebooks_hess_44_0.png
[30]:
# create a 2D mask for the images
image_mask = mask.slice_by_idx({"energy": 0})
significance_map_off = significance_map * image_mask
[31]:
significance_all = significance_map.data[np.isfinite(significance_map.data)]
significance_off = significance_map_off.data[
    np.isfinite(significance_map_off.data)
]

plt.hist(
    significance_all,
    density=True,
    alpha=0.5,
    color="red",
    label="all bins",
    bins=21,
)

plt.hist(
    significance_off,
    density=True,
    alpha=0.5,
    color="blue",
    label="off bins",
    bins=21,
)

# Now, fit the off distribution with a Gaussian
mu, std = norm.fit(significance_off)
x = np.linspace(-8, 8, 50)
p = norm.pdf(x, mu, std)
plt.plot(x, p, lw=2, color="black")
plt.legend()
plt.xlabel("Significance")
plt.yscale("log")
plt.ylim(1e-5, 1)
xmin, xmax = np.min(significance_all), np.max(significance_all)
plt.xlim(xmin, xmax)

print("Fit results: mu = {:.2f}, std = {:.2f}".format(mu, std))
Fit results: mu = -0.07, std = 1.03
../_images/notebooks_hess_46_1.png

The significance and excess maps clearly indicate a bright source at the position of MSH 1552. This is also evident from the significance distribution. The off distribution should ideally be a Gaussian with mu=0, sigma=1

[32]:
print(
    "Excess from entire map: {:.1f}".format(
        np.nansum(lima_maps["excess"].data)
    )
)
print(
    "Excess from off regions: {:.1f}".format(
        np.nansum((lima_maps["excess"] * image_mask).data)
    )
)
Excess from entire map: 4896.3
Excess from off regions: -1030.4

Again: please note that this tutorial notebook was put together quickly, the results obtained here are very preliminary. We will work on Gammapy and the analysis of data from the H.E.S.S. test release and update this tutorial soon.

Exercises

  • Try analysing another source, e.g. RX J1713.7−3946

  • Try another model, e.g. a Gaussian spatial shape or exponential cutoff power-law spectrum.

[ ]: