This is a fixed-text formatted version of a Jupyter notebook
- Try online
- You can contribute with your own notebooks in this GitHub repository.
- Source files: cta_data_analysis.ipynb | cta_data_analysis.py
CTA data analysis with Gammapy¶
Introduction¶
This notebook shows an example how to make a sky image and spectrum for simulated CTA data with Gammapy.
The dataset we will use is three observation runs on the Galactic center. This is a tiny (and thus quick to process and play with and learn) subset of the simulated CTA dataset that was produced for the first data challenge in August 2017.
This notebook can be considered part 2 of the introduction to CTA 1DC analysis. Part one is here:cta_1dc_introduction.ipynb
Setup¶
As usual, we’ll start with some setup …
[1]:
%matplotlib inline
import matplotlib.pyplot as plt
[2]:
!gammapy info --no-envvar --no-system
Gammapy package:
path : /Users/adonath/github/adonath/gammapy/gammapy
version : 0.13
Other packages:
numpy : 1.15.0
scipy : 1.3.0
matplotlib : 3.1.1
cython : 0.29.12
astropy : 3.2.1
astropy_healpix : 0.4
reproject : 0.4
sherpa : 4.11.0
pytest : 5.0.1
sphinx : 1.8.5
healpy : 1.12.9
regions : 0.4
iminuit : 1.3.7
naima : 0.8.3
uncertainties : 3.1.1
[3]:
import numpy as np
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.convolution import Gaussian2DKernel
from regions import CircleSkyRegion
from gammapy.utils.fitting import Fit
from gammapy.data import DataStore
from gammapy.spectrum import (
SpectrumExtraction,
models,
FluxPointsEstimator,
FluxPointsDataset,
)
from gammapy.maps import MapAxis, WcsNDMap, WcsGeom
from gammapy.cube import MapMaker
from gammapy.background import ReflectedRegionsBackgroundEstimator
from gammapy.detect import TSMapEstimator, find_peaks
[4]:
# Configure the logger, so that the spectral analysis
# isn't so chatty about what it's doing.
import logging
logging.basicConfig()
log = logging.getLogger("gammapy.spectrum")
log.setLevel(logging.ERROR)
Select observations¶
Like explained in cta_1dc_introduction.ipynb, a Gammapy analysis usually starts by creating a DataStore
and selecting observations.
This is shown in detail in the other notebook, here we just pick three observations near the galactic center.
[5]:
data_store = DataStore.from_dir("$GAMMAPY_DATA/cta-1dc/index/gps")
[6]:
# Just as a reminder: this is how to select observations
# from astropy.coordinates import SkyCoord
# table = data_store.obs_table
# pos_obs = SkyCoord(table['GLON_PNT'], table['GLAT_PNT'], frame='galactic', unit='deg')
# pos_target = SkyCoord(0, 0, frame='galactic', unit='deg')
# offset = pos_target.separation(pos_obs).deg
# mask = (1 < offset) & (offset < 2)
# table = table[mask]
# table.show_in_browser(jsviewer=True)
[7]:
obs_id = [110380, 111140, 111159]
observations = data_store.get_observations(obs_id)
[8]:
obs_cols = ["OBS_ID", "GLON_PNT", "GLAT_PNT", "LIVETIME"]
data_store.obs_table.select_obs_id(obs_id)[obs_cols]
[8]:
OBS_ID | GLON_PNT | GLAT_PNT | LIVETIME |
---|---|---|---|
deg | deg | s | |
int64 | float64 | float64 | float64 |
110380 | 359.9999912037958 | -1.299995937905366 | 1764.0 |
111140 | 358.4999833830074 | 1.3000020211954284 | 1764.0 |
111159 | 1.5000056568267741 | 1.299940468335294 | 1764.0 |
Make sky images¶
Define map geometry¶
Select the target position and define an ON region for the spectral analysis
[9]:
axis = MapAxis.from_edges(
np.logspace(-1.0, 1.0, 10), unit="TeV", name="energy", interp="log"
)
geom = WcsGeom.create(
skydir=(0, 0), npix=(500, 400), binsz=0.02, coordsys="GAL", axes=[axis]
)
geom
[9]:
WcsGeom
axes : lon, lat, energy
shape : (500, 400, 9)
ndim : 3
coordsys : GAL
projection : CAR
center : 0.0 deg, 0.0 deg
width : 10.0 deg x 8.0 deg
Compute images¶
Exclusion mask currently unused. Remove here or move to later in the tutorial?
[10]:
target_position = SkyCoord(0, 0, unit="deg", frame="galactic")
on_radius = 0.2 * u.deg
on_region = CircleSkyRegion(center=target_position, radius=on_radius)
[11]:
exclusion_mask = geom.to_image().region_mask([on_region], inside=False)
exclusion_mask = WcsNDMap(geom.to_image(), exclusion_mask)
exclusion_mask.plot();
[12]:
%%time
maker = MapMaker(geom, offset_max="2 deg")
maps = maker.run(observations)
print(maps.keys())
WARNING: Tried to get polar motions for times after IERS data is valid. Defaulting to polar motion from the 50-yr mean for those. This may affect precision at the 10s of arcsec level [astropy.coordinates.builtin_frames.utils]
WARNING:astropy:Tried to get polar motions for times after IERS data is valid. Defaulting to polar motion from the 50-yr mean for those. This may affect precision at the 10s of arcsec level
dict_keys(['counts', 'exposure', 'background'])
CPU times: user 4.03 s, sys: 512 ms, total: 4.54 s
Wall time: 4.51 s
[13]:
# The maps are cubes, with an energy axis.
# Let's also make some images:
images = maker.run_images()
excess = images["counts"].copy()
excess.data -= images["background"].data
images["excess"] = excess
Show images¶
Let’s have a quick look at the images we computed …
[14]:
images["counts"].smooth(2).plot(vmax=5);
[15]:
images["background"].plot(vmax=5);
[16]:
images["excess"].smooth(3).plot(vmax=2);
Source Detection¶
Use the class gammapy.detect.TSMapEstimator and gammapy.detect.find_peaks to detect sources on the images:
[17]:
kernel = Gaussian2DKernel(1, mode="oversample").array
plt.imshow(kernel);
[18]:
%%time
ts_image_estimator = TSMapEstimator()
images_ts = ts_image_estimator.run(images, kernel)
print(images_ts.keys())
dict_keys(['ts', 'sqrt_ts', 'flux', 'flux_err', 'flux_ul', 'niter'])
CPU times: user 967 ms, sys: 177 ms, total: 1.14 s
Wall time: 9.96 s
[19]:
sources = find_peaks(images_ts["sqrt_ts"], threshold=8)
sources
[19]:
value | x | y | ra | dec |
---|---|---|---|---|
deg | deg | |||
float32 | int64 | int64 | float64 | float64 |
21.387 | 252 | 197 | 266.42400 | -29.00490 |
10.282 | 207 | 204 | 266.82019 | -28.16314 |
[20]:
source_pos = SkyCoord(sources["ra"], sources["dec"])
source_pos
[20]:
<SkyCoord (ICRS): (ra, dec) in deg
[(266.42399798, -29.00490483), (266.82018801, -28.16313964)]>
[21]:
# Plot sources on top of significance sky image
images_ts["sqrt_ts"].plot(add_cbar=True)
plt.gca().scatter(
source_pos.ra.deg,
source_pos.dec.deg,
transform=plt.gca().get_transform("icrs"),
color="none",
edgecolor="white",
marker="o",
s=200,
lw=1.5,
);
Spatial analysis¶
See other notebooks for how to run a 3D cube or 2D image based analysis.
Spectrum¶
We’ll run a spectral analysis using the classical reflected regions background estimation method, and using the on-off (often called WSTAT) likelihood function.
Extraction¶
The first step is to “extract” the spectrum, i.e. 1-dimensional counts and exposure and background vectors, as well as an energy dispersion matrix from the data and IRFs.
[22]:
%%time
bkg_estimator = ReflectedRegionsBackgroundEstimator(
observations=observations,
on_region=on_region,
exclusion_mask=exclusion_mask,
)
bkg_estimator.run()
bkg_estimate = bkg_estimator.result
bkg_estimator.plot();
CPU times: user 4.67 s, sys: 204 ms, total: 4.87 s
Wall time: 4.86 s
[22]:
(<Figure size 504x504 with 1 Axes>,
<matplotlib.axes._subplots.WCSAxesSubplot at 0x1271d18d0>,
None)
[23]:
%%time
extract = SpectrumExtraction(
observations=observations, bkg_estimate=bkg_estimate
)
extract.run()
extract.compute_energy_threshold()
CPU times: user 788 ms, sys: 21.4 ms, total: 809 ms
Wall time: 739 ms
Model fit¶
The next step is to fit a spectral model, using all data (i.e. a “global” fit, using all energies).
[24]:
%%time
model = models.PowerLaw(
index=2, amplitude=1e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV
)
for dataset in extract.spectrum_observations:
dataset.model = model
fit = Fit(extract.spectrum_observations)
result = fit.run()
print(result)
OptimizeResult
backend : minuit
method : minuit
success : True
message : Optimization terminated successfully.
nfev : 75
total stat : 226.10
CPU times: user 247 ms, sys: 5.19 ms, total: 252 ms
Wall time: 250 ms
Spectral points¶
Finally, let’s compute spectral points. The method used is to first choose an energy binning, and then to do a 1-dim likelihood fit / profile to compute the flux and flux error.
[25]:
# Flux points are computed on stacked observation
from gammapy.spectrum import SpectrumDatasetOnOffStacker
stacker = SpectrumDatasetOnOffStacker(extract.spectrum_observations)
stacked_obs = stacker.run()
print(stacked_obs)
SpectrumDatasetOnOff
Total counts : 2376
Total predicted counts : nan
Total off counts : 34922.00
Effective area min : 1.60e+06 cm2
Effective area max : 6.08e+10 cm2
Livetime : 5.29e+03 s
Number of total bins : 72
Number of fit bins : 72
Fit statistic type : wstat
Fit statistic value (-2 log(L)) : nan
Number of parameters : 0
Number of free parameters : 0
Acceptance mean: : 1.0
[26]:
e_edges = np.logspace(0, 1.5, 5) * u.TeV
stacked_obs.model = model
fpe = FluxPointsEstimator(datasets=[dataset], e_edges=e_edges)
flux_points = fpe.run()
flux_points.table_formatted
[26]:
e_ref | e_min | e_max | ref_dnde | ref_flux | ref_eflux | ref_e2dnde | norm | loglike | norm_err | counts [1] | norm_errp | norm_errn | norm_ul | sqrt_ts | ts | norm_scan [11] | dloglike_scan [11] | dnde | dnde_ul | dnde_err | dnde_errp | dnde_errn |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TeV | TeV | TeV | 1 / (cm2 s TeV) | 1 / (cm2 s) | TeV / (cm2 s) | TeV / (cm2 s) | 1 / (cm2 s TeV) | 1 / (cm2 s TeV) | 1 / (cm2 s TeV) | 1 / (cm2 s TeV) | 1 / (cm2 s TeV) | |||||||||||
float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | int64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 | float64 |
1.565 | 1.000 | 2.448 | 1.110e-12 | 1.634e-12 | 2.437e-12 | 2.717e-12 | 0.925 | 2.286 | 0.191 | 37 | 0.202 | 0.181 | 1.350 | 7.475 | 55.881 | 0.200 .. 5.000 | 27.984966330999157 .. 150.56876383404153 | 1.026e-12 | 1.498e-12 | 2.122e-13 | 2.243e-13 | 2.006e-13 |
3.831 | 2.448 | 5.995 | 1.513e-13 | 5.455e-13 | 1.992e-12 | 2.221e-12 | 0.846 | 1.956 | 0.205 | 23 | 0.220 | 0.191 | 1.315 | 6.906 | 47.697 | 0.200 .. 5.000 | 20.634204716592542 .. 120.32186461956034 | 1.280e-13 | 1.990e-13 | 3.106e-14 | 3.324e-14 | 2.896e-14 |
8.799 | 5.995 | 12.915 | 2.379e-14 | 1.666e-13 | 1.415e-12 | 1.842e-12 | 0.561 | 10.822 | 0.268 | 7 | 0.301 | 0.237 | 1.235 | 3.123 | 9.752 | 0.200 .. 5.000 | 13.581463576745378 .. 71.41639662260916 | 1.333e-14 | 2.938e-14 | 6.369e-15 | 7.162e-15 | 5.627e-15 |
20.209 | 12.915 | 31.623 | 3.740e-15 | 7.113e-14 | 1.370e-12 | 1.528e-12 | 0.545 | 5.552 | 0.351 | 3 | 0.422 | 0.287 | 1.541 | 2.761 | 7.623 | 0.200 .. 5.000 | 7.176513233902151 .. 36.71719468221855 | 2.037e-15 | 5.764e-15 | 1.315e-15 | 1.580e-15 | 1.073e-15 |
Plot¶
Let’s plot the spectral model and points. You could do it directly, but there is a helper class. Note that a spectral uncertainty band, a “butterfly” is drawn, but it is very thin, i.e. barely visible.
[27]:
model.parameters.covariance = result.parameters.covariance
flux_points_dataset = FluxPointsDataset(data=flux_points, model=model)
[28]:
plt.figure(figsize=(8, 6))
flux_points_dataset.peek();
Exercises¶
- Re-run the analysis above, varying some analysis parameters, e.g.
- Select a few other observations
- Change the energy band for the map
- Change the spectral model for the fit
- Change the energy binning for the spectral points
- Change the target. Make a sky image and spectrum for your favourite source.
- If you don’t know any, the Crab nebula is the “hello world!” analysis of gamma-ray astronomy.
[29]:
# print('hello world')
# SkyCoord.from_name('crab')
What next?¶
- This notebook showed an example of a first CTA analysis with Gammapy, using simulated 1DC data.
- This was part 2 for CTA 1DC turorial, the first part was here: cta_1dc_introduction.ipynb
- Let us know if you have any question or issues!