示例#1
0
def test_sky_point_source():
    # Test special case of point source. Regression test for GH 2367.

    energy_axis = MapAxis.from_edges(
        [1, 10], unit="TeV", name="energy_true", interp="log"
    )
    exposure = Map.create(
        skydir=(100, 70),
        npix=(4, 4),
        binsz=0.1,
        proj="AIT",
        unit="cm2 s",
        axes=[energy_axis],
    )
    exposure.data = np.ones_like(exposure.data)

    spatial_model = PointSpatialModel(
        lon_0=100.06 * u.deg, lat_0=70.03 * u.deg, frame="icrs"
    )
    # Create a spectral model with integral flux of 1 cm-2 s-1 in this energy band
    spectral_model = ConstantSpectralModel(const="1 cm-2 s-1 TeV-1")
    spectral_model.const.value /= spectral_model.integral(1 * u.TeV, 10 * u.TeV).value
    model = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model)
    evaluator = MapEvaluator(model=model, exposure=exposure)
    flux = evaluator.compute_flux().quantity.to_value("cm-2 s-1")[0]

    expected = [
        [0, 0, 0, 0],
        [0, 0.140, 0.058, 0.0],
        [0, 0.564, 0.236, 0],
        [0, 0, 0, 0],
    ]
    assert_allclose(flux, expected, atol=0.01)

    assert_allclose(flux.sum(), 1)
示例#2
0
def test_compute_flux_spatial_no_psf():
    # check that spatial integration is not performed in the absence of a psf
    center = SkyCoord("0 deg", "0 deg", frame="galactic")
    region = CircleSkyRegion(center=center, radius=0.1 * u.deg)

    nbin = 2
    energy_axis_true = MapAxis.from_energy_bounds(".1 TeV",
                                                  "10 TeV",
                                                  nbin=nbin,
                                                  name="energy_true")

    spectral_model = ConstantSpectralModel()
    spatial_model = GaussianSpatialModel(lon_0=0 * u.deg,
                                         lat_0=0 * u.deg,
                                         frame="galactic",
                                         sigma="0.1 deg")

    models = SkyModel(spectral_model=spectral_model,
                      spatial_model=spatial_model)
    model = Models(models)

    exposure_region = RegionNDMap.create(region, axes=[energy_axis_true])
    exposure_region.data += 1.0
    exposure_region.unit = "m2 s"

    evaluator = MapEvaluator(model=model[0], exposure=exposure_region)
    flux = evaluator.compute_flux_spatial()

    assert_allclose(flux, 1.0)
示例#3
0
def test_compute_flux_spatial():
    center = SkyCoord("0 deg", "0 deg", frame="galactic")
    region = CircleSkyRegion(center=center, radius=0.1 * u.deg)

    nbin = 2
    energy_axis_true = MapAxis.from_energy_bounds(".1 TeV",
                                                  "10 TeV",
                                                  nbin=nbin,
                                                  name="energy_true")

    spectral_model = ConstantSpectralModel()
    spatial_model = PointSpatialModel(lon_0=0 * u.deg,
                                      lat_0=0 * u.deg,
                                      frame="galactic")

    models = SkyModel(spectral_model=spectral_model,
                      spatial_model=spatial_model)
    model = Models(models)

    exposure_region = RegionNDMap.create(region,
                                         axes=[energy_axis_true],
                                         binsz_wcs="0.01deg")
    exposure_region.data += 1.0
    exposure_region.unit = "m2 s"

    geom = RegionGeom(region, axes=[energy_axis_true], binsz_wcs="0.01deg")
    psf = PSFKernel.from_gauss(geom.to_wcs_geom(), sigma="0.1 deg")

    evaluator = MapEvaluator(model=model[0], exposure=exposure_region, psf=psf)
    flux = evaluator.compute_flux_spatial()

    g = Gauss2DPDF(0.1)
    reference = g.containment_fraction(0.1)
    assert_allclose(flux.value, reference, rtol=0.003)
示例#4
0
def test_map_spectrum_weight():
    axis = MapAxis.from_edges([0.1, 10, 1000], unit="TeV", name="energy")
    expo_map = WcsNDMap.create(npix=10, binsz=1, axes=[axis], unit="m2 s")
    expo_map.data += 1
    spectrum = ConstantSpectralModel(const="42 cm-2 s-1 TeV-1")

    weighted_expo = _map_spectrum_weight(expo_map, spectrum)

    assert weighted_expo.data.shape == (2, 10, 10)
    assert weighted_expo.unit == "m2 s"
    assert_allclose(weighted_expo.data.sum(), 100)
示例#5
0
    def test_npred_no_edisp(self):
        const = 1 / u.TeV / u.cm**2 / u.s
        model = ConstantSpectralModel(const)
        livetime = 1 * u.s
        dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            aeff=self.aeff,
            model=model,
            livetime=livetime,
        )

        energy = self.aeff.energy.edges * self.aeff.energy.unit
        expected = self.aeff.data.data[0] * (energy[-1] -
                                             energy[0]) * const * livetime

        assert_allclose(dataset.npred_sig().data.sum(), expected.value)
示例#6
0
    def test_npred_no_edisp(self):
        const = 1 * u.Unit("cm-2 s-1 TeV-1")
        model = SkyModel(spectral_model=ConstantSpectralModel(const=const))
        livetime = 1 * u.s

        e_reco = self.on_counts.geom.axes[0].edges
        aeff = EffectiveAreaTable(e_reco[:-1], e_reco[1:], np.ones(4) * u.cm ** 2)
        dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            aeff=aeff,
            models=model,
            livetime=livetime,
        )

        energy = aeff.energy.edges
        expected = aeff.data.data[0] * (energy[-1] - energy[0]) * const * livetime

        assert_allclose(dataset.npred_sig().data.sum(), expected.value)
示例#7
0
    def test_npred_no_edisp(self):
        const = 1 * u.Unit("cm-2 s-1 TeV-1")
        model = SkyModel(spectral_model=ConstantSpectralModel(const=const))
        livetime = 1 * u.s

        aeff = RegionNDMap.create(region=self.on_region,
                                  unit="cm2",
                                  axes=[self.e_reco.copy(name="energy_true")])
        aeff.data += 1
        dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            aeff=aeff,
            models=model,
            livetime=livetime,
        )

        energy = aeff.geom.axes[0].edges
        expected = aeff.data[0] * (energy[-1] - energy[0]) * const * livetime

        assert_allclose(dataset.npred_sig().data.sum(), expected.value)
示例#8
0
def test_large_oversampling():
    nbin = 2
    energy_axis_true = MapAxis.from_energy_bounds(".1 TeV",
                                                  "10 TeV",
                                                  nbin=nbin,
                                                  name="energy_true")
    geom = WcsGeom.create(width=1, binsz=0.02, axes=[energy_axis_true])

    spectral_model = ConstantSpectralModel()
    spatial_model = GaussianSpatialModel(lon_0=0 * u.deg,
                                         lat_0=0 * u.deg,
                                         sigma=1e-4 * u.deg,
                                         frame="icrs")

    models = SkyModel(spectral_model=spectral_model,
                      spatial_model=spatial_model)
    model = Models(models)

    exposure = Map.from_geom(geom, unit="m2 s")
    exposure.data += 1.0

    psf = PSFKernel.from_gauss(geom, sigma="0.1 deg")

    evaluator = MapEvaluator(model=model[0], exposure=exposure, psf=psf)
    flux_1 = evaluator.compute_flux_spatial()

    spatial_model.sigma.value = 0.001
    flux_2 = evaluator.compute_flux_spatial()

    spatial_model.sigma.value = 0.01
    flux_3 = evaluator.compute_flux_spatial()

    spatial_model.sigma.value = 0.03
    flux_4 = evaluator.compute_flux_spatial()

    assert_allclose(flux_1.data.sum(), nbin, rtol=1e-4)
    assert_allclose(flux_2.data.sum(), nbin, rtol=1e-4)
    assert_allclose(flux_3.data.sum(), nbin, rtol=1e-4)
    assert_allclose(flux_4.data.sum(), nbin, rtol=1e-4)
示例#9
0
 dict(
     name="logpar10",
     model=LogParabolaSpectralModel.from_log10(
         alpha=2.3 * u.Unit(""),
         amplitude=4 / u.cm ** 2 / u.s / u.TeV,
         reference=1 * u.TeV,
         beta=1.151292546497023 * u.Unit(""),
     ),
     val_at_2TeV=u.Quantity(0.6387956571420305, "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(2.255689748270628, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(3.9586515834989267, "TeV cm-2 s-1"),
     e_peak=0.74082 * u.TeV,
 ),
 dict(
     name="constant",
     model=ConstantSpectralModel(const=4 / u.cm ** 2 / u.s / u.TeV),
     val_at_2TeV=u.Quantity(4, "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(35.9999999999999, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(198.00000000000006, "TeV cm-2 s-1"),
 ),
 dict(
     name="powerlaw_index1",
     model=PowerLawSpectralModel(
         index=1 * u.Unit(""),
         amplitude=2 / u.cm ** 2 / u.s / u.TeV,
         reference=1 * u.TeV,
     ),
     val_at_2TeV=u.Quantity(1.0, "cm-2 s-1 TeV-1"),
     integral_1_10TeV=u.Quantity(4.605170185, "cm-2 s-1"),
     eflux_1_10TeV=u.Quantity(18.0, "TeV cm-2 s-1"),
 ),
=======================

This model takes a constant value along the spectral range.

    .. math:: \phi(E) = k
"""

# %%
# 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 ConstantSpectralModel, Models, SkyModel

energy_bounds = [0.1, 100] * u.TeV
model = ConstantSpectralModel(const="1 / (cm2 s TeV)")
model.plot(energy_bounds)
plt.grid(which="both")

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

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

print(models.to_yaml())
示例#11
0
=======================

This model takes a constant value along the spectral range.

    .. math:: \phi(E) = k
"""

# %%
# 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 ConstantSpectralModel, Models, SkyModel

energy_range = [0.1, 100] * u.TeV
model = ConstantSpectralModel(const="1 / (cm2 s 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="constant-model")
models = Models([model])

print(models.to_yaml())