def test_write(tmpdir, diffuse_model): filename = tmpdir / diffuse_model.spatial_model.filename diffuse_model.spatial_model.filename = None with pytest.raises(IOError): diffuse_model.spatial_model.write() with pytest.raises(IOError): Models(diffuse_model).to_dict() diffuse_model.spatial_model.filename = filename diffuse_model.spatial_model.write(overwrite=False) TemplateSpatialModel.read(filename)
def test_sky_diffuse_map(): filename = "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v18/Templates/RXJ1713_2016_250GeV.fits" model = TemplateSpatialModel.read(filename, normalize=False) lon = [258.5, 0] * u.deg lat = -39.8 * u.deg val = model(lon, lat) assert val.unit == "sr-1" desired = [3269.178107, 0] assert_allclose(val.value, desired) res = model.evaluate_geom(model.map.geom) assert_allclose(np.sum(res.value), 32816514.42078349) radius = model.evaluation_radius assert radius.unit == "deg" assert_allclose(radius.value, 0.64, rtol=1.0e-2) assert model.frame == "fk5" assert isinstance(model.to_region(), PolygonSkyRegion) with pytest.raises(TypeError): model.plot_interative() with pytest.raises(TypeError): model.plot_grid()
def spatial_model(self): """Spatial model (`~gammapy.modeling.models.SpatialModel`).""" d = self.data ra = d["RAJ2000"] dec = d["DEJ2000"] if self.is_pointlike: model = PointSpatialModel(lon_0=ra, lat_0=dec, frame="icrs") else: de = self.data_extended morph_type = de["Model_Form"].strip() e = (1 - (de["Model_SemiMinor"] / de["Model_SemiMajor"]) ** 2.0) ** 0.5 sigma = de["Model_SemiMajor"] phi = de["Model_PosAng"] if morph_type in ["Disk", "Elliptical Disk"]: r_0 = de["Model_SemiMajor"] model = DiskSpatialModel( lon_0=ra, lat_0=dec, r_0=r_0, e=e, phi=phi, frame="icrs" ) elif morph_type in ["Map", "Ring", "2D Gaussian x2"]: filename = de["Spatial_Filename"].strip() path = make_path( "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v15/Templates/" ) return TemplateSpatialModel.read(path / filename) elif morph_type in ["2D Gaussian", "Elliptical 2D Gaussian"]: model = GaussianSpatialModel( lon_0=ra, lat_0=dec, sigma=sigma, e=e, phi=phi, frame="icrs" ) else: raise ValueError(f"Invalid spatial model: {morph_type!r}") self._set_spatial_errors(model) return model
def spatial_model(self): """Spatial model (`~gammapy.modeling.models.SpatialModel`).""" d = self.data ra = d["RAJ2000"] dec = d["DEJ2000"] if self.is_pointlike: model = PointSpatialModel(lon_0=ra, lat_0=dec, frame="icrs") else: de = self.data_extended morph_type = de["Model_Form"].strip() e = (1 - (de["Model_SemiMinor"] / de["Model_SemiMajor"]) ** 2.0) ** 0.5 sigma = de["Model_SemiMajor"] phi = de["Model_PosAng"] if morph_type == "Disk": r_0 = de["Model_SemiMajor"] model = DiskSpatialModel( lon_0=ra, lat_0=dec, r_0=r_0, e=e, phi=phi, frame="icrs" ) elif morph_type in ["Map", "Ring", "2D Gaussian x2"]: filename = de["Spatial_Filename"].strip() path = make_path( "$GAMMAPY_DATA/catalogs/fermi/LAT_extended_sources_8years/Templates/" ) with warnings.catch_warnings(): # ignore FITS units warnings warnings.simplefilter("ignore", FITSFixedWarning) model = TemplateSpatialModel.read(path / filename) elif morph_type == "2D Gaussian": model = GaussianSpatialModel( lon_0=ra, lat_0=dec, sigma=sigma, e=e, phi=phi, frame="icrs" ) else: raise ValueError(f"Invalid spatial model: {morph_type!r}") self._set_spatial_errors(model) return model
def test_sky_diffuse_map(caplog): filename = "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v18/Templates/RXJ1713_2016_250GeV.fits" model = TemplateSpatialModel.read(filename, normalize=False) lon = [258.5, 0] * u.deg lat = -39.8 * u.deg val = model(lon, lat) assert "WARNING" in [_.levelname for _ in caplog.records] assert "Missing spatial template unit, assuming sr^-1" in [ _.message for _ in caplog.records ] assert val.unit == "sr-1" desired = [3269.178107, 0] assert_allclose(val.value, desired) res = model.evaluate_geom(model.map.geom) assert_allclose(np.sum(res.value), 32826159.74707) radius = model.evaluation_radius assert radius.unit == "deg" assert_allclose(radius.value, 0.64, rtol=1.0e-2) assert model.frame == "fk5" assert isinstance(model.to_region(), RectangleSkyRegion) with pytest.raises(TypeError): model.plot_interative() with pytest.raises(TypeError): model.plot_grid()
def test_read(): model = TemplateSpatialModel.read( "$GAMMAPY_DATA/tests/unbundled/fermi/gll_iem_v02_cutout.fits", normalize=False, ) assert model.map.unit == "cm-2 s-1 MeV-1 sr-1" # Check pixel inside map val = model.evaluate(0 * u.deg, 0 * u.deg, 100 * u.GeV) assert val.unit == "cm-2 s-1 MeV-1 sr-1" assert val.shape == (1,) assert_allclose(val.value, 1.396424e-12, rtol=1e-5)
def test_sky_diffuse_map(): filename = "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v18/Templates/RXJ1713_2016_250GeV.fits" model = TemplateSpatialModel.read(filename, normalize=False) lon = [258.5, 0] * u.deg lat = -39.8 * u.deg val = model(lon, lat) assert val.unit == "sr-1" desired = [3269.178107, 0] assert_allclose(val.value, desired) radius = model.evaluation_radius assert radius.unit == "deg" assert_allclose(radius.value, 0.64, rtol=1.0e-2) assert model.frame == "fk5"
def test_sky_diffuse_map_3d(): filename = "$GAMMAPY_DATA/fermi-3fhl-gc/gll_iem_v06_gc.fits.gz" model = TemplateSpatialModel.read(filename, normalize=False) lon = [258.5, 0] * u.deg lat = -39.8 * u.deg energy = 1 * u.GeV val = model(lon, lat, energy) with pytest.raises(ValueError): model(lon, lat) assert model.map.unit == "cm-2 s-1 MeV-1 sr-1" val = model(lon, lat, energy) assert val.unit == "cm-2 s-1 MeV-1 sr-1" res = model.evaluate_geom(model.map.geom) assert_allclose(np.sum(res.value), 0.11803847221522712) with pytest.raises(TypeError): model.plot()
def spatial_model(self): """ Source spatial model (`~gammapy.modeling.models.SpatialModel`). """ d = self.data pars = {} glon = d["GLON"] glat = d["GLAT"] if self.is_pointlike: pars["lon_0"] = glon pars["lat_0"] = glat return PointSpatialModel(lon_0=glon, lat_0=glat, frame="galactic") else: de = self.data_extended morph_type = de["Model_Form"].strip() if morph_type == "Disk": r_0 = de["Model_SemiMajor"].to("deg") return DiskSpatialModel(lon_0=glon, lat_0=glat, r_0=r_0, frame="galactic") elif morph_type in ["Map", "Ring", "2D Gaussian x2"]: filename = de["Spatial_Filename"].strip() path = make_path( "$GAMMAPY_DATA/catalogs/fermi/Extended_archive_v15/Templates/" ) return TemplateSpatialModel.read(path / filename) elif morph_type == "2D Gaussian": # TODO: fill elongation info as soon as model supports it sigma = de["Model_SemiMajor"].to("deg") return GaussianSpatialModel(lon_0=glon, lat_0=glat, sigma=sigma, frame="galactic") else: raise ValueError(f"Invalid spatial model: {morph_type!r}")
def make_datasets_example(): # Define which data to use and print some information energy_axis = MapAxis.from_edges(np.logspace(-1.0, 1.0, 4), unit="TeV", name="energy", interp="log") geom0 = WcsGeom.create( skydir=(0, 0), binsz=0.1, width=(2, 2), frame="galactic", proj="CAR", axes=[energy_axis], ) geom1 = WcsGeom.create( skydir=(1, 0), binsz=0.1, width=(2, 2), frame="galactic", proj="CAR", axes=[energy_axis], ) geoms = [geom0, geom1] sources_coords = [(0, 0), (0.9, 0.1)] names = ["gc", "g09"] models = Models() for idx, (lon, lat) in enumerate(sources_coords): spatial_model = PointSpatialModel(lon_0=lon * u.deg, lat_0=lat * u.deg, frame="galactic") spectral_model = ExpCutoffPowerLawSpectralModel( index=2 * u.Unit(""), amplitude=3e-12 * u.Unit("cm-2 s-1 TeV-1"), reference=1.0 * u.TeV, lambda_=0.1 / u.TeV, ) model_ecpl = SkyModel(spatial_model=spatial_model, spectral_model=spectral_model, name=names[idx]) models.append(model_ecpl) models["gc"].spectral_model.reference = models[ "g09"].spectral_model.reference obs_ids = [110380, 111140, 111159] data_store = DataStore.from_dir("$GAMMAPY_DATA/cta-1dc/index/gps/") diffuse_spatial = TemplateSpatialModel.read( "$GAMMAPY_DATA/fermi-3fhl-gc/gll_iem_v06_gc.fits.gz") diffuse_model = SkyModel(PowerLawSpectralModel(), diffuse_spatial) maker = MapDatasetMaker() datasets = Datasets() observations = data_store.get_observations(obs_ids) for idx, geom in enumerate(geoms): stacked = MapDataset.create(geom=geom, name=names[idx]) for obs in observations: dataset = maker.run(stacked, obs) stacked.stack(dataset) bkg = stacked.models.pop(0) stacked.models = [models[idx], diffuse_model, bkg] datasets.append(stacked) datasets.write( "$GAMMAPY_DATA/tests/models", prefix="gc_example", overwrite=True, write_covariance=False, )
JFAC = 3.27e19 * u.Unit("GeV2 cm-5") # <--- Reticulum II Extended #JFAC = 1.26e20 * u.Unit("GeV2 cm-5") # <--- Draco I Extended mDM = 40000 * u.Unit("GeV") channel = "b" xsection = 1e-26 * u.Unit("cm3 s-1") redshift = 0 RATIO = 2.71 # **Define 3D Sky Model** DarkMatterAnnihilationSpectralModel.THERMAL_RELIC_CROSS_SECTION = xsection flux_model = DarkMatterAnnihilationSpectralModel(mass=mDM, channel=channel, jfactor=JFAC) spatial_model = TemplateSpatialModel.read(jfactor_filename) sky_model = SkyModel(spatial_model=spatial_model, spectral_model=flux_model, name="model-simu") bkg_model = FoVBackgroundModel(dataset_name="dataset-simu") models = Models([sky_model, bkg_model]) # ## Declare observation values pointing = src_pos livetime = 100 * u.hour offset = 2.0 * u.deg #offset = 0.5 * u.deg