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 sourcesSpatialModel
: models to describe spatial shapes (morphologies) of sourcesTemporalModel
: models to describe temporal flux evolution of sources, such as light and phase curvesSkyModel
andSkyDiffuseCube
: 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)
Reference/API¶
gammapy.modeling Package¶
Models and fitting.
Functions¶
|
Corner plot for each parameter explored by the walkers. |
|
Plot the trace of walkers for every steps |
|
Run the MCMC sampler. |
|
Uniform prior distribution. |
gammapy.modeling.models Package¶
Built-in models in Gammapy.
Functions¶
|
Create a Crab nebula reference spectral model. |
|
Cosmic a cosmic ray spectral model at Earth. |
Classes¶
|
Sky model base class |
|
Sky model collection. |
|
Sky model component. |
|
Cube sky map template model (3D). |
|
Background model. |
|
Gamma-ray absorption models. |
|
Spatial model base class. |
|
Spectral model base class. |
|
Temporal model base class. |
|
Spatially constant (isotropic) spatial model. |
|
Spatial sky map template model (2D). |
|
Constant disk model. |
|
Two-dimensional Gaussian model. |
|
Point Source. |
|
Shell model. |
|
Constant model. |
|
Arithmetic combination of two spectral models. |
|
Spectral power-law model. |
|
Spectral power-law model with integral as amplitude parameter. |
|
Spectral exponential cutoff power-law model. |
|
Spectral exponential cutoff power-law model used for 3FGL. |
|
Spectral super exponential cutoff power-law model used for 3FGL. |
|
Spectral super exponential cutoff power-law model used for 4FGL. |
|
Spectral log parabola model. |
|
A model generated from a table of energy and value arrays. |
|
Gaussian spectral model. |
|
Spectral model with EBL absorption. |
|
A wrapper for Naima models. |
|
Wrapper to scale another spectral model by a norm factor. |
|
Constant temporal model. |
|
Temporal phase curve model. |
Temporal light curve model. |
Variables¶
Built-in spatial models. |
|
Built-in temporal models. |
|
Built-in spectral models. |