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

3D simulation and fitting

Prerequisites

Context

To simulate a specific observation, it is not always necessary to simulate the full photon list. For many uses cases, simulating directly a reduced binned dataset is enough: the IRFs reduced in the correct geometry are combined with a source model to predict an actual number of counts per bin. The latter is then used to simulate a reduced dataset using Poisson probability distribution.

This can be done to check the feasibility of a measurement (performance / sensitivity study), to test whether fitted parameters really provide a good fit to the data etc.

Here we will see how to perform a 3D simulation of a CTA observation, assuming both the spectral and spatial morphology of an observed source.

Objective: simulate a 3D observation of a source with CTA using the CTA 1DC response and fit it with the assumed source model.

Proposed approach:

Here we can’t use the regular observation objects that are connected to a DataStore. Instead we will create a fake gammapy.data.Observation that contain some pointing information and the CTA 1DC IRFs (that are loaded with gammapy.irf.load_cta_irfs).

Then we will create a gammapy.datasets.MapDataset geometry and create it with the gammapy.makers.MapDatasetMaker.

Then we will be able to define a model consisting of a gammapy.modeling.models.PowerLawSpectralModel and a gammapy.modeling.models.GaussianSpatialModel. We will assign it to the dataset and fake the count data.

Imports and versions

[1]:
%matplotlib inline
[2]:
import numpy as np
import astropy.units as u
from astropy.coordinates import SkyCoord
from gammapy.irf import load_cta_irfs
from gammapy.maps import WcsGeom, MapAxis
from gammapy.modeling.models import (
    PowerLawSpectralModel,
    GaussianSpatialModel,
    SkyModel,
)
from gammapy.makers import MapDatasetMaker, SafeMaskMaker
from gammapy.modeling import Fit
from gammapy.data import Observation
from gammapy.datasets import MapDataset
[3]:
!gammapy info --no-envvar --no-dependencies --no-system

Gammapy package:

        version                : 0.18.dev690+gede9fe4b8
        path                   : /home/runner/work/gammapy-docs/gammapy-docs/gammapy/gammapy

Simulation

We will simulate using the CTA-1DC IRFs shipped with gammapy. Note that for dedictaed CTA simulations, you can simply use `Observation.from_caldb() <>`__ without having to externally load the IRFs

[4]:
# Loading IRFs
irfs = load_cta_irfs(
    "$GAMMAPY_DATA/cta-1dc/caldb/data/cta/1dc/bcf/South_z20_50h/irf_file.fits"
)
Invalid unit found in background table! Assuming (s-1 MeV-1 sr-1)
[5]:
# Define the observation parameters (typically the observation duration and the pointing position):
livetime = 2.0 * u.hr
pointing = SkyCoord(0, 0, unit="deg", frame="galactic")
[6]:
# Define map geometry for binned simulation
energy_reco = MapAxis.from_edges(
    np.logspace(-1.0, 1.0, 10), unit="TeV", name="energy", interp="log"
)
geom = WcsGeom.create(
    skydir=(0, 0),
    binsz=0.02,
    width=(6, 6),
    frame="galactic",
    axes=[energy_reco],
)
# It is usually useful to have a separate binning for the true energy axis
energy_true = MapAxis.from_edges(
    np.logspace(-1.5, 1.5, 30), unit="TeV", name="energy", interp="log"
)
[7]:
# Define sky model to used simulate the data.
# Here we use a Gaussian spatial model and a Power Law spectral model.
spatial_model = GaussianSpatialModel(
    lon_0="0.2 deg", lat_0="0.1 deg", sigma="0.3 deg", frame="galactic"
)
spectral_model = PowerLawSpectralModel(
    index=3, amplitude="1e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)
model_simu = SkyModel(
    spatial_model=spatial_model,
    spectral_model=spectral_model,
    name="model-simu",
)
print(model_simu)
SkyModel

  Name                      : model-simu
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       : GaussianSpatialModel
  Temporal model type       :
  Parameters:
    index                   :   3.000
    amplitude               :   1.00e-11  1 / (cm2 s TeV)
    reference    (frozen)   :   1.000  TeV
    lon_0                   :   0.200  deg
    lat_0                   :   0.100  deg
    sigma                   :   0.300  deg
    e            (frozen)   :   0.000
    phi          (frozen)   :   0.000  deg


Now, comes the main part of dataset simulation. We create an in-memory observation and an empty dataset. We then predict the number of counts for the given model, and Poission fluctuate it using fake() to make a simulated counts maps. Keep in mind that it is important to specify the selection of the maps that you want to produce

[8]:
# Create an in-memory observation
obs = Observation.create(pointing=pointing, livetime=livetime, irfs=irfs)
print(obs)
Observation

        obs id            : 0
        tstart            : 51544.00
        tstop             : 51544.08
        duration          : 7200.00 s
        pointing (icrs)   : 266.4 deg, -28.9 deg

        deadtime fraction : 0.0%

[9]:
# Make the MapDataset
empty = MapDataset.create(geom, name="dataset-simu")
maker = MapDatasetMaker(selection=["exposure", "background", "psf", "edisp"])
maker_safe_mask = SafeMaskMaker(methods=["offset-max"], offset_max=4.0 * u.deg)
dataset = maker.run(empty, obs)
dataset = maker_safe_mask.run(dataset, obs)
print(dataset)
MapDataset
----------

  Name                            : dataset-simu

  Total counts                    : nan
  Total predicted counts          : 161422.06
  Total background counts         : 161422.06

  Exposure min                    : 6.41e+07 m2 s
  Exposure max                    : 2.53e+10 m2 s

  Number of total bins            : 0
  Number of fit bins              : 804492

  Fit statistic type              : cash
  Fit statistic value (-2 log(L)) : nan

  Number of models                : 1
  Number of parameters            : 3
  Number of free parameters       : 1

  Component 0: BackgroundModel

    Name                      : dataset-simu-bkg
    Datasets names            : ['dataset-simu']
    Parameters:
      norm                    :   1.000
      tilt         (frozen)   :   0.000
      reference    (frozen)   :   1.000  TeV


[10]:
# Add the model on the dataset and Poission fluctuate
dataset.models.append(model_simu)
dataset.fake()
# Do a print on the dataset - there is now a counts maps
print(dataset)
MapDataset
----------

  Name                            : dataset-simu

  Total counts                    : 169592
  Total predicted counts          : 169861.06
  Total background counts         : 161422.06

  Exposure min                    : 6.41e+07 m2 s
  Exposure max                    : 2.53e+10 m2 s

  Number of total bins            : 810000
  Number of fit bins              : 804492

  Fit statistic type              : cash
  Fit statistic value (-2 log(L)) : 563558.97

  Number of models                : 2
  Number of parameters            : 11
  Number of free parameters       : 6

  Component 0: BackgroundModel

    Name                      : dataset-simu-bkg
    Datasets names            : ['dataset-simu']
    Parameters:
      norm                    :   1.000
      tilt         (frozen)   :   0.000
      reference    (frozen)   :   1.000  TeV

  Component 1: SkyModel

    Name                      : model-simu
    Datasets names            : None
    Spectral model type       : PowerLawSpectralModel
    Spatial  model type       : GaussianSpatialModel
    Temporal model type       :
    Parameters:
      index                   :   3.000
      amplitude               :   1.00e-11  1 / (cm2 s TeV)
      reference    (frozen)   :   1.000  TeV
      lon_0                   :   0.200  deg
      lat_0                   :   0.100  deg
      sigma                   :   0.300  deg
      e            (frozen)   :   0.000
      phi          (frozen)   :   0.000  deg


Now use this dataset as you would in all standard analysis. You can plot the maps, or proceed with your custom analysis. In the next section, we show the standard 3D fitting as in analysis_3d.

[11]:
# To plot, eg, counts:
dataset.counts.smooth(0.05 * u.deg).plot_interactive(
    add_cbar=True, stretch="linear"
)

Fit

In this section, we do a usual 3D fit with the same model used to simulated the data and see the stability of the simulations. Often, it is useful to simulate many such datasets and look at the distribution of the reconstructed parameters.

[12]:
# Make a copy of the dataset
dataset_fit = dataset.copy(name="dataset-fit")
[13]:
print(dataset_fit.models)
ProperModels

Component 0: BackgroundModel

  Name                      : dataset-fit-bkg
  Datasets names            : ['dataset-fit']
  Parameters:
    norm                    :   1.000
    tilt         (frozen)   :   0.000
    reference    (frozen)   :   1.000  TeV

Component 1: SkyModel

  Name                      : model-simu
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       : GaussianSpatialModel
  Temporal model type       :
  Parameters:
    index                   :   3.000
    amplitude               :   1.00e-11  1 / (cm2 s TeV)
    reference    (frozen)   :   1.000  TeV
    lon_0                   :   0.200  deg
    lat_0                   :   0.100  deg
    sigma                   :   0.300  deg
    e            (frozen)   :   0.000
    phi          (frozen)   :   0.000  deg


[14]:
# Define sky model to fit the data
spatial_model1 = GaussianSpatialModel(
    lon_0="0.1 deg", lat_0="0.1 deg", sigma="0.5 deg", frame="galactic"
)
spectral_model1 = PowerLawSpectralModel(
    index=2, amplitude="1e-11 cm-2 s-1 TeV-1", reference="1 TeV"
)
model_fit = SkyModel(
    spatial_model=spatial_model1,
    spectral_model=spectral_model1,
    name="model-fit",
)

dataset_fit.models = [model_fit, dataset_fit.models[0]]
print(dataset_fit.models)
ProperModels

Component 0: SkyModel

  Name                      : model-fit
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       : GaussianSpatialModel
  Temporal model type       :
  Parameters:
    index                   :   2.000
    amplitude               :   1.00e-11  1 / (cm2 s TeV)
    reference    (frozen)   :   1.000  TeV
    lon_0                   :   0.100  deg
    lat_0                   :   0.100  deg
    sigma                   :   0.500  deg
    e            (frozen)   :   0.000
    phi          (frozen)   :   0.000  deg

Component 1: BackgroundModel

  Name                      : dataset-fit-bkg
  Datasets names            : ['dataset-fit']
  Parameters:
    norm                    :   1.000
    tilt         (frozen)   :   0.000
    reference    (frozen)   :   1.000  TeV


[15]:
# We do not want to fit the background in this case, so we will freeze the parameters
background_model = dataset_fit.background_model
background_model.parameters["norm"].value = 1.0
background_model.parameters["norm"].frozen = True
background_model.parameters["tilt"].frozen = True

print(background_model)
BackgroundModel

  Name                      : dataset-fit-bkg
  Datasets names            : ['dataset-fit']
  Parameters:
    norm         (frozen)   :   1.000
    tilt         (frozen)   :   0.000
    reference    (frozen)   :   1.000  TeV


[16]:
print(dataset_fit)
MapDataset
----------

  Name                            : dataset-fit

  Total counts                    : 169592
  Total predicted counts          : 164532.16
  Total background counts         : 161422.06

  Exposure min                    : 6.41e+07 m2 s
  Exposure max                    : 2.53e+10 m2 s

  Number of total bins            : 810000
  Number of fit bins              : 804492

  Fit statistic type              : cash
  Fit statistic value (-2 log(L)) : 568663.20

  Number of models                : 2
  Number of parameters            : 11
  Number of free parameters       : 5

  Component 0: SkyModel

    Name                      : model-fit
    Datasets names            : None
    Spectral model type       : PowerLawSpectralModel
    Spatial  model type       : GaussianSpatialModel
    Temporal model type       :
    Parameters:
      index                   :   2.000
      amplitude               :   1.00e-11  1 / (cm2 s TeV)
      reference    (frozen)   :   1.000  TeV
      lon_0                   :   0.100  deg
      lat_0                   :   0.100  deg
      sigma                   :   0.500  deg
      e            (frozen)   :   0.000
      phi          (frozen)   :   0.000  deg

  Component 1: BackgroundModel

    Name                      : dataset-fit-bkg
    Datasets names            : ['dataset-fit']
    Parameters:
      norm         (frozen)   :   1.000
      tilt         (frozen)   :   0.000
      reference    (frozen)   :   1.000  TeV


[17]:
%%time
fit = Fit([dataset_fit])
result = fit.run(optimize_opts={"print_level": 1})
------------------------------------------------------------------
| FCN = 5.636e+05               |     Ncalls=210 (210 total)     |
| EDM = 1e-05 (Goal: 0.0002)    |            up = 1.0            |
------------------------------------------------------------------
|  Valid Min.   | Valid Param.  | Above EDM | Reached call limit |
------------------------------------------------------------------
|     True      |     True      |   False   |       False        |
------------------------------------------------------------------
| Hesse failed  |   Has cov.    | Accurate  | Pos. def. | Forced |
------------------------------------------------------------------
|     False     |     True      |   True    |   True    | False  |
------------------------------------------------------------------
CPU times: user 18.9 s, sys: 22 ms, total: 18.9 s
Wall time: 18.9 s
[18]:
dataset_fit.plot_residuals(method="diff/sqrt(model)", vmin=-0.5, vmax=0.5)
/usr/share/miniconda/envs/gammapy-dev/lib/python3.7/site-packages/astropy/visualization/wcsaxes/core.py:211: MatplotlibDeprecationWarning: Passing parameters norm and vmin/vmax simultaneously is deprecated since 3.3 and will become an error two minor releases later. Please pass vmin/vmax directly to the norm when creating it.
  return super().imshow(X, *args, origin=origin, **kwargs)
[18]:
(<WCSAxesSubplot:xlabel='Galactic Longitude', ylabel='Galactic Latitude'>,
 None)
../_images/tutorials_simulate_3d_25_2.png

Compare the injected and fitted models:

[19]:
print("True model: \n", model_simu, "\n\n Fitted model: \n", model_fit)
True model:
 SkyModel

  Name                      : model-simu
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       : GaussianSpatialModel
  Temporal model type       :
  Parameters:
    index                   :   3.000
    amplitude               :   1.00e-11  1 / (cm2 s TeV)
    reference    (frozen)   :   1.000  TeV
    lon_0                   :   0.200  deg
    lat_0                   :   0.100  deg
    sigma                   :   0.300  deg
    e            (frozen)   :   0.000
    phi          (frozen)   :   0.000  deg



 Fitted model:
 SkyModel

  Name                      : model-fit
  Datasets names            : None
  Spectral model type       : PowerLawSpectralModel
  Spatial  model type       : GaussianSpatialModel
  Temporal model type       :
  Parameters:
    index                   :   3.020
    amplitude               :   9.70e-12  1 / (cm2 s TeV)
    reference    (frozen)   :   1.000  TeV
    lon_0                   :   0.190  deg
    lat_0                   :   0.101  deg
    sigma                   :   0.296  deg
    e            (frozen)   :   0.000
    phi          (frozen)   :   0.000  deg


Get the errors on the fitted parameters from the parameter table

[20]:
result.parameters.to_table()
[20]:
Table length=11
namevalueunitminmaxfrozenerror
str9float64str14float64float64boolfloat64
index3.020e+00nannanFalse2.018e-02
amplitude9.702e-12cm-2 s-1 TeV-1nannanFalse3.255e-13
reference1.000e+00TeVnannanTrue0.000e+00
lon_01.900e-01degnannanFalse5.819e-03
lat_01.012e-01deg-9.000e+019.000e+01False5.791e-03
sigma2.958e-01deg0.000e+00nanFalse3.892e-03
e0.000e+000.000e+001.000e+00True0.000e+00
phi0.000e+00degnannanTrue0.000e+00
norm1.000e+000.000e+00nanTrue0.000e+00
tilt0.000e+00nannanTrue0.000e+00
reference1.000e+00TeVnannanTrue0.000e+00