Esempio n. 1
0
def test_sky_diffuse_constant():
    model = ConstantSpatialModel(value="42 sr-1")
    lon = [1, 2] * u.deg
    lat = 45 * u.deg
    val = model(lon, lat)
    assert val.unit == "sr-1"
    assert_allclose(val.value, 42)
    radius = model.evaluation_radius
    assert radius is None
    assert isinstance(model.to_region(), EllipseSkyRegion)
Esempio n. 2
0
def test_flux_point_dataset_serialization(tmp_path):
    path = "$GAMMAPY_DATA/tests/spectrum/flux_points/diff_flux_points.fits"
    data = FluxPoints.read(path)
    data.table["e_ref"] = data.e_ref.to("TeV")
    # TODO: remove duplicate definition this once model is redefine as skymodel
    spatial_model = ConstantSpatialModel()
    spectral_model = PowerLawSpectralModel(index=2.3,
                                           amplitude="2e-13 cm-2 s-1 TeV-1",
                                           reference="1 TeV")
    model = SkyModel(spatial_model, spectral_model, name="test_model")
    dataset = FluxPointsDataset(SkyModels([model]), data, name="test_dataset")

    Datasets([dataset]).to_yaml(tmp_path, prefix="tmp")
    datasets = Datasets.from_yaml(tmp_path / "tmp_datasets.yaml",
                                  tmp_path / "tmp_models.yaml")
    new_dataset = datasets[0]
    assert_allclose(new_dataset.data.table["dnde"], dataset.data.table["dnde"],
                    1e-4)
    if dataset.mask_fit is None:
        assert np.all(new_dataset.mask_fit == dataset.mask_safe)
    assert np.all(new_dataset.mask_safe == dataset.mask_safe)
    assert new_dataset.name == "test_dataset"
Esempio n. 3
0
# %%
# Example plot
# ------------
# Here is an example plot of the model:

from gammapy.maps import WcsGeom
from gammapy.modeling.models import (
    ConstantSpatialModel,
    Models,
    PowerLawSpectralModel,
    SkyModel,
)

geom = WcsGeom.create(npix=(100, 100), binsz=0.1)
model = ConstantSpatialModel(value="42 sr-1")
model.plot(geom=geom, add_cbar=True)

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

pwl = PowerLawSpectralModel()
constant = ConstantSpatialModel()

model = SkyModel(spectral_model=pwl,
                 spatial_model=constant,
                 name="pwl-constant-model")
models = Models([model])
Esempio n. 4
0
# In[ ]:

model_diffuse = SkyDiffuseCube(diffuse_galactic, name="diffuse")
eval_diffuse = MapEvaluator(model=model_diffuse,
                            exposure=exposure,
                            psf=psf_kernel,
                            edisp=edisp)

background_gal = eval_diffuse.compute_npred()

background_gal.sum_over_axes().plot()
print("Background counts from Galactic diffuse: ", background_gal.data.sum())

# In[ ]:

model_iso = SkyModel(ConstantSpatialModel(), diffuse_iso, name="diffuse-iso")

eval_iso = MapEvaluator(model=model_iso, exposure=exposure, edisp=edisp)

background_iso = eval_iso.compute_npred()

background_iso.sum_over_axes().plot(add_cbar=True)
print("Background counts from isotropic diffuse: ", background_iso.data.sum())

# In[ ]:

background_total = background_iso + background_gal

# ## Excess and flux
#
# Let's compute an excess and flux image, by subtracting the background, and summing over the energy axis.