def test_names(geom, geom_etrue, sky_model): m = Map.from_geom(geom) m.quantity = 0.2 * np.ones(m.data.shape) background_model1 = BackgroundModel(m, name="bkg1") assert background_model1.name == "bkg1" c_map1 = Map.from_geom(geom) c_map1.quantity = 0.3 * np.ones(c_map1.data.shape) model1 = sky_model.copy() assert model1.name != sky_model.name model1 = sky_model.copy(name="model1") assert model1.name == "model1" model2 = sky_model.copy(name="model2") dataset1 = MapDataset( counts=c_map1, background_model=background_model1, models=Models([model1, model2]), exposure=get_exposure(geom_etrue), ) dataset2 = dataset1.copy() assert dataset2.name != dataset1.name assert dataset2.background_model dataset2 = dataset1.copy(name="dataset2") assert dataset2.name == "dataset2" assert dataset2.background_model.name == "bkg1" assert dataset1.background_model is not dataset2.background_model assert dataset1.models.names == dataset2.models.names assert dataset1.models is not dataset2.models
def test_to_image(geom): counts = Map.read( "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-counts-cube.fits.gz") background = Map.read( "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-background-cube.fits.gz") background = BackgroundModel(background) exposure = Map.read( "$GAMMAPY_DATA/fermi-3fhl-gc/fermi-3fhl-gc-exposure-cube.fits.gz") exposure = exposure.sum_over_axes(keepdims=True) dataset = MapDataset(counts=counts, background_model=background, exposure=exposure) dataset_im = dataset.to_image() assert dataset_im.mask_safe is None assert dataset_im.counts.data.sum() == dataset.counts.data.sum() assert_allclose(dataset_im.background_model.map.data.sum(), 28548.625, rtol=1e-5) ebounds = np.logspace(-1.0, 1.0, 3) axis = MapAxis.from_edges(ebounds, name="energy", unit=u.TeV, interp="log") geom = WcsGeom.create(skydir=(0, 0), binsz=0.5, width=(1, 1), frame="icrs", axes=[axis]) dataset = MapDataset.create(geom) # Check map_safe handling data = np.array([[[False, True], [True, True]], [[False, False], [True, True]]]) dataset.mask_safe = WcsNDMap.from_geom(geom=geom, data=data) dataset_im = dataset.to_image() assert dataset_im.mask_safe.data.dtype == bool desired = np.array([[False, True], [True, True]]) assert (dataset_im.mask_safe.data == desired).all() # Check that missing entries in the dataset do not break dataset_copy = dataset.copy() dataset_copy.exposure = None dataset_copy.background_model = None dataset_im = dataset_copy.to_image() assert dataset_im.exposure is None assert dataset_im.background_model is None dataset_copy = dataset.copy() dataset_copy.counts = None dataset_im = dataset_copy.to_image() assert dataset_im.counts is None