Exemple #1
0
 def spectral_model(self):
     """Best fit spectral model `~gammapy.modeling.models.SpectralModel`."""
     pars, errs = {}, {}
     pars["amplitude"] = self.data["Flux"]
     pars["emin"], pars["emax"] = self.energy_range
     pars["index"] = self.data["Spectral_Index"]
     errs["amplitude"] = self.data["Unc_Flux"]
     errs["index"] = self.data["Unc_Spectral_Index"]
     model = PowerLaw2SpectralModel(**pars)
     model.parameters.set_parameter_errors(errs)
     return model
Exemple #2
0
def get_test_cases():
    e_true = Quantity(np.logspace(-1, 2, 120), "TeV")
    e_reco = Quantity(np.logspace(-1, 2, 100), "TeV")
    return [
        dict(model=PowerLawSpectralModel(amplitude="1e2 TeV-1"),
             e_true=e_true,
             npred=999),
        dict(
            model=PowerLaw2SpectralModel(amplitude="1",
                                         emin="0.1 TeV",
                                         emax="100 TeV"),
            e_true=e_true,
            npred=1,
        ),
        dict(
            model=PowerLawSpectralModel(amplitude="1e-11 TeV-1 cm-2 s-1"),
            aeff=EffectiveAreaTable.from_parametrization(e_true),
            livetime="10 h",
            npred=1448.05960,
        ),
        dict(
            model=PowerLawSpectralModel(reference="1 GeV",
                                        amplitude="1e-11 GeV-1 cm-2 s-1"),
            aeff=EffectiveAreaTable.from_parametrization(e_true),
            livetime="30 h",
            npred=4.34417881,
        ),
        dict(
            model=PowerLawSpectralModel(amplitude="1e-11 TeV-1 cm-2 s-1"),
            aeff=EffectiveAreaTable.from_parametrization(e_true),
            edisp=EnergyDispersion.from_gauss(e_reco=e_reco,
                                              e_true=e_true,
                                              bias=0,
                                              sigma=0.2),
            livetime="10 h",
            npred=1437.450076,
        ),
        dict(
            model=TemplateSpectralModel(
                energy=[0.1, 0.2, 0.3, 0.4] * u.TeV,
                values=[4.0, 3.0, 1.0, 0.1] * u.Unit("TeV-1"),
            ),
            e_true=[0.1, 0.2, 0.3, 0.4] * u.TeV,
            npred=0.554513062,
        ),
    ]
    def sky_model(self, sigma=0.3, A0=1e-12):
        spatial_model = GaussianSpatialModel(frame="galactic")
        spatial_model.lon_0.value = self.skydir.galactic.l.value
        spatial_model.lat_0.value = self.skydir.galactic.b.value
        spatial_model.sigma.value = sigma
        spectral_model = PowerLaw2SpectralModel()

        amplitude = A0 * sigma * sigma
        spectral_model.amplitude.value = amplitude
        spectral_model.index.value = 2.3
        spectral_model.emin.quantity = 1.0 * u.TeV
        spectral_model.emax.quantity = 10.0 * u.TeV
        model_simu = SkyModel(
            spatial_model=spatial_model,
            spectral_model=spectral_model,
            name="model-simu",
        )

        return model_simu
Exemple #4
0
 dict(
     name="norm-powerlaw",
     model=PowerLawNormSpectralModel(
         tilt=2 * u.Unit(""),
         norm=4.0 * u.Unit(""),
         reference=1 * u.TeV,
     ),
     val_at_2TeV=u.Quantity(1.0, ""),
     integral_1_10TeV=u.Quantity(3.6, "TeV"),
     eflux_1_10TeV=u.Quantity(9.210340371976184, "TeV2"),
 ),
 dict(
     name="powerlaw2",
     model=PowerLaw2SpectralModel(
         amplitude=u.Quantity(2.9227116204223784, "cm-2 s-1"),
         index=2.3 * u.Unit(""),
         emin=1 * u.TeV,
         emax=10 * u.TeV,
     ),
     val_at_2TeV=u.Quantity(4 * 2.0 ** (-2.3), "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(2.9227116204223784, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(6.650836884969039, "TeV cm-2 s-1"),
 ),
 dict(
     name="ecpl",
     model=ExpCutoffPowerLawSpectralModel(
         index=1.6 * u.Unit(""),
         amplitude=4 / u.cm ** 2 / u.s / u.TeV,
         reference=1 * u.TeV,
         lambda_=0.1 / u.TeV,
     ),
     val_at_2TeV=u.Quantity(1.080321705479446, "cm-2 s-1 TeV-1"),
Exemple #5
0
See also: https://fermi.gsfc.nasa.gov/ssc/data/analysis/scitools/source_models.html
"""

# %%
# Example plot
# ------------
# Here is an example plot of the model:

from astropy import units as u
import matplotlib.pyplot as plt
from gammapy.modeling.models import Models, PowerLaw2SpectralModel, SkyModel

energy_range = [0.1, 100] * u.TeV
model = PowerLaw2SpectralModel(
    amplitude=u.Quantity(1e-12, "cm-2 s-1"),
    index=2.3,
    emin=1 * u.TeV,
    emax=10 * u.TeV,
)
model.plot(energy_range)
plt.grid(which="both")

# %%
# YAML representation
# -------------------
# Here is an example YAML file using the model:

model = SkyModel(spectral_model=model, name="power-law2-model")
models = Models([model])

print(models.to_yaml())
Exemple #6
0
# Note that even when doing a 2D analysis, it is better to use fine energy bins in the beginning and then sum them over. This is to ensure that the background shape can be approximated by a power law function in each energy bin. The `run_images()` can be used to compute maps in fine bins and then squash them to have one bin. This can be done by specifying `keep_dims = True`. This will compute a summed counts and background maps, and a spectral weighted exposure map.

# In[ ]:

stacked = MapDataset.create(geom)

# In[ ]:

get_ipython().run_cell_magic(
    'time', '',
    'maker = MapDatasetMaker(selection=["counts", "exposure", "background"])\nmaker_safe_mask = SafeMaskMaker(methods=["offset-max"], offset_max=4.0 * u.deg)\n\ndatasets = []\n\nfor obs in observations:\n    cutout = stacked.cutout(obs.pointing_radec, width="8 deg")\n    dataset = maker.run(cutout, obs)\n    dataset = maker_safe_mask.run(dataset, obs)\n    datasets.append(dataset)\n    stacked.stack(dataset)'
)

# In[ ]:

spectrum = PowerLaw2SpectralModel(index=2)
dataset_2d = stacked.to_image(spectrum=spectrum)

maps2D = {
    "counts": dataset_2d.counts,
    "exposure": dataset_2d.exposure,
    "background": dataset_2d.background_model.map,
}

# For a typical 2D analysis, using an energy dispersion usually does not make sense. A PSF map can be made as in the regular 3D case, taking care to weight it properly with the spectrum.

# In[ ]:

# mean PSF
geom2d = maps2D["exposure"].geom
src_pos = SkyCoord(0, 0, unit="deg", frame="galactic")