modeling - Models and fitting

Introduction

gammapy.modeling contains all the functionality related to modeling and fitting data. This includes spectral, spatial and temporal model classes, as well as the fit and parameter API. A list of available model can be found further down this page. In general the models are grouped into the following categories:

  • SpectralModel: models to describe spectral shapes of sources
  • SpatialModel: models to describe spatial shapes (morphologies) of sources
  • TemporalModel: models to describe temporal flux evolution of sources, such as light and phase curves
  • SkyModel and SkyDiffuseCube: model to combine the spectral and spatial model components

The models follow a naming scheme which contains the category as a suffix to the class name.

Getting Started

Spectral Models

Models are imported from the gammapy.modeling.models namespace:

from gammapy.modeling.models import PowerLawSpectralModel

pwl = PowerLawSpectralModel()
print(pwl)

Spectral models all come with default parameters. Different parameter values can be passed on creation of the model, either as a string defining the value and unit or as an Quantity object directly:

from astropy import units as u
from gammapy.modeling.models import PowerLawSpectralModel

pwl = PowerLawSpectralModel(amplitude="2.7e-12 TeV-1 cm-2 s-1", index=2.2)
print(pwl)

amplitude = 1e-12 * u.Unit("TeV-1 cm-2 s-1")
pwl = PowerLawSpectralModel(amplitude=amplitude, index=2.2)
print(pwl)

The model can be evaluated at given energies by calling the model instance:

from astropy import units as u
from gammapy.modeling.models import PowerLawSpectralModel

pwl = PowerLawSpectralModel()

energy = [1, 3, 10, 30] * u.TeV
dnde = pwl(energy)
print(dnde)

For spectral models you can computed in addition the integrated and energy flux in a given energy range:

from astropy import units as u
from gammapy.modeling.models import PowerLawSpectralModel

pwl = PowerLawSpectralModel(index=2.2)

flux = pwl.integral(emin=1 * u.TeV, emax=10 * u.TeV)
print(flux)

eflux = pwl.energy_flux(emin=1 * u.TeV, emax=10 * u.TeV)
print(eflux)

# with an array of emins and emaxs
energy = [1, 3, 10, 30] * u.TeV
flux = pwl.integral(emin=energy[:-1], emax=energy[1:])

Spatial Models

Spatial models are imported from the same gammapy.modeling.models namespace:

from gammapy.modeling.models import GaussianSpatialModel

gauss = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")
print(gauss)

The default coordinate frame is "icrs", but the frame can be modified using the frame argument:

from gammapy.modeling.models import GaussianSpatialModel

gauss = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg", frame="galactic")
print(gauss)

print(gauss.position)

Spatial models can be evaluated again by calling the instance:

from gammapy.modeling.models import GaussianSpatialModel
from astropy import units as u

gauss = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")

lon = [0, 0.1] * u.deg
lat = [0, 0.1] * u.deg

flux_per_omega = gauss(lon, lat)

The returned quantity corresponds to a surface brightness. Spatial model can be also evaluated using gammapy.maps.Map and gammapy.maps.Geom objects:

import matplotlib.pyplot as plt
from gammapy.modeling.models import GaussianSpatialModel
from gammapy.maps import Map

gauss = GaussianSpatialModel(lon_0="0 deg", lat_0="0 deg", sigma="0.2 deg")
m = Map.create(skydir=(0, 0), width=(1, 1), binsz=0.02)
m.quantity = gauss.evaluate_geom(m.geom)
m.plot()
plt.show()

SkyModel and SkyDiffuseCube

The SkyModel class combines a spectral and a spatial model. It can be created from existing spatial and spectral model components:

from gammapy.modeling.models import SkyModel, GaussianSpatialModel, PowerLawSpectralModel

pwl = PowerLawSpectralModel()
gauss = GaussianSpatialModel("0 deg", "0 deg", "0.2 deg")

source = SkyModel(spectral_model=pwl, spatial_model=gauss, name="my-source")
print(source)

In addition a name can be assigned to model as an identifier for models with multiple components.

The gammapy.modeling.models.SkyDiffuseCube can be used to represent source models based on templates. It can be created from an existing FITS file:

from gammapy.modeling.models import SkyDiffuseCube

diffuse = SkyDiffuseCube.read("$GAMMAPY_DATA/fermi-3fhl-gc/gll_iem_v06_gc.fits.gz")
print(diffuse)

Fitting

For examples how to fit models to data please check out the following tutorials:

Reference/API

gammapy.modeling Package

Models and fitting.

Functions

plot_corner(sampler, dataset[, nburn]) Corner plot for each parameter explored by the walkers.
plot_trace(sampler, dataset) Plot the trace of walkers for every steps
run_mcmc(dataset[, nwalkers, nrun, threads]) Run the MCMC sampler.
uniform_prior(value, umin, umax) Uniform prior distribution.

Classes

Dataset Dataset abstract base class.
Datasets(datasets) Join multiple datasets.
Fit(datasets) Fit class.
Model([params]) Model base class.
Parameter(name, factor[, unit, scale, min, …]) A model parameter.
Parameters([parameters, covariance, …]) List of Parameter.

gammapy.modeling.models Package

Built-in models in Gammapy.

Functions

create_crab_spectral_model([reference]) Create a Crab nebula reference spectral model.
create_cosmic_ray_spectral_model([particle]) Cosmic a cosmic ray spectral model at Earth.

Classes

SkyModelBase([params]) Sky model base class
SkyModels(skymodels) Collection of SkyModel
SkyModel(spatial_model, spectral_model[, name]) Sky model component.
SkyDiffuseCube(map[, norm, tilt, reference, …]) Cube sky map template model (3D).
BackgroundModel(map[, norm, tilt, …]) Background model.
Absorption(energy, param, data[, filename, …]) Gamma-ray absorption models.
SpatialModel([params]) Spatial model base class.
SpectralModel([params]) Spectral model base class.
TemporalModel([params]) Temporal model base class.
ConstantSpatialModel([value]) Spatially constant (isotropic) spatial model.
TemplateSpatialModel(map[, norm, meta, …]) Spatial sky map template model (2D).
DiskSpatialModel(lon_0, lat_0, r_0[, e, …]) Constant disk model.
GaussianSpatialModel(lon_0, lat_0, sigma[, …]) Two-dimensional Gaussian model.
PointSpatialModel(lon_0, lat_0[, frame]) Point Source.
ShellSpatialModel(lon_0, lat_0, radius, width) Shell model.
ConstantSpectralModel(const) Constant model.
CompoundSpectralModel(model1, model2, operator) Arithmetic combination of two spectral models.
PowerLawSpectralModel([index, amplitude, …]) Spectral power-law model.
PowerLaw2SpectralModel([amplitude, index, …]) Spectral power-law model with integral as amplitude parameter.
ExpCutoffPowerLawSpectralModel([index, …]) Spectral exponential cutoff power-law model.
ExpCutoffPowerLaw3FGLSpectralModel([index, …]) Spectral exponential cutoff power-law model used for 3FGL.
SuperExpCutoffPowerLaw3FGLSpectralModel([…]) Spectral super exponential cutoff power-law model used for 3FGL.
SuperExpCutoffPowerLaw4FGLSpectralModel([…]) Spectral super exponential cutoff power-law model used for 4FGL.
LogParabolaSpectralModel([amplitude, …]) Spectral log parabola model.
TemplateSpectralModel(energy, values[, …]) A model generated from a table of energy and value arrays.
GaussianSpectralModel([norm, mean, sigma]) Gaussian spectral model.
LogGaussianSpectralModel([norm, mean, sigma]) Log Gaussian spectral model.
AbsorbedSpectralModel(spectral_model, …[, …]) Spectral model with EBL absorption.
NaimaSpectralModel(radiative_model[, …]) A wrapper for Naima models.
ScaleSpectralModel(model[, norm]) Wrapper to scale another spectral model by a norm factor.
ConstantTemporalModel(norm) Constant temporal model.
PhaseCurveTemplateTemporalModel(table, …) Temporal phase curve model.
LightCurveTemplateTemporalModel(table) Temporal light curve model.

Variables

SPATIAL_MODELS Built-in spatial models.
TEMPORAL_MODELS Built-in temporal models.
SPECTRAL_MODELS Built-in spectral models.