def test_mapdatasetonoff_to_image(): axis = MapAxis.from_energy_bounds(1, 10, 2, unit="TeV") geom = WcsGeom.create(npix=(10, 10), binsz=0.05, axes=[axis]) counts = Map.from_geom(geom, data=np.ones((2, 10, 10))) counts_off = Map.from_geom(geom, data=np.ones((2, 10, 10))) acceptance = Map.from_geom(geom, data=np.ones((2, 10, 10))) acceptance_off = Map.from_geom(geom, data=np.ones((2, 10, 10))) acceptance_off *= 2 dataset = MapDatasetOnOff( counts=counts, counts_off=counts_off, acceptance=acceptance, acceptance_off=acceptance_off, ) image_dataset = dataset.to_image() assert image_dataset.counts.data.shape == (1, 10, 10) assert image_dataset.acceptance_off.data.shape == (1, 10, 10) assert_allclose(image_dataset.acceptance, 2) assert_allclose(image_dataset.acceptance_off, 4) assert_allclose(image_dataset.counts_off, 2) assert image_dataset.name != dataset.name # Try with a safe_mask mask_safe = Map.from_geom(geom, data=np.ones((2, 10, 10), dtype="bool")) mask_safe.data[0] = 0 dataset.mask_safe = mask_safe image_dataset = dataset.to_image() assert_allclose(image_dataset.acceptance, 1) assert_allclose(image_dataset.acceptance_off, 2) assert_allclose(image_dataset.counts_off, 1)
def test_asmooth_mapdatasetonoff(): kernel = Tophat2DKernel scales = ASmoothMapEstimator.get_scales(3, factor=2, kernel=kernel) * 0.1 * u.deg asmooth = ASmoothMapEstimator( kernel=kernel, scales=scales, method="simple", threshold=2.5 ) counts = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="") counts += 2 counts_off = WcsNDMap.create(npix=(50, 50), binsz=0.02, unit="") counts_off += 3 acceptance = 1 acceptance_off = 3 dataset = MapDatasetOnOff( counts=counts, counts_off=counts_off, acceptance=acceptance, acceptance_off=acceptance_off, ) smoothed = asmooth.run(dataset) assert_allclose(smoothed["counts"].data[25, 25], 2) assert_allclose(smoothed["background"].data[25, 25], 1) assert_allclose(smoothed["significance"].data[25, 25], 4.391334)
def test_create_onoff(geom, geom_etrue): # tests empty datasets created migra_axis = MapAxis(nodes=np.linspace(0.0, 3.0, 51), unit="", name="migra") rad_axis = MapAxis(nodes=np.linspace(0.0, 1.0, 51), unit="deg", name="theta") energy_axis = geom.get_axis_by_name("energy") empty_dataset = MapDatasetOnOff.create(geom, energy_axis, migra_axis, rad_axis) assert_allclose(empty_dataset.counts.data.sum(), 0.0) assert_allclose(empty_dataset.counts_off.data.sum(), 0.0) assert_allclose(empty_dataset.acceptance.data.sum(), 0.0) assert_allclose(empty_dataset.acceptance_off.data.sum(), 0.0) assert empty_dataset.psf.psf_map.data.shape == (2, 50, 10, 10) assert empty_dataset.psf.exposure_map.data.shape == (2, 1, 10, 10) assert empty_dataset.edisp.edisp_map.data.shape == (2, 50, 10, 10) assert empty_dataset.edisp.exposure_map.data.shape == (2, 1, 10, 10) assert_allclose(empty_dataset.edisp.edisp_map.data.sum(), 200) assert_allclose(empty_dataset.gti.time_delta, 0.0 * u.s)
def simple_dataset_on_off(): axis = MapAxis.from_energy_bounds(0.1, 10, 1, unit="TeV") geom = WcsGeom.create(npix=50, binsz=0.02, axes=[axis]) dataset = MapDatasetOnOff.create(geom) dataset.mask_safe += 1 dataset.counts += 2 dataset.counts_off += 1 dataset.acceptance += 1 dataset.acceptance_off += 1 return dataset
def test_stack_onoff_cutout(geom_image): # Test stacking of cutouts dataset = MapDatasetOnOff.create(geom_image) gti = GTI.create([0 * u.s], [1 * u.h], reference_time="2010-01-01T00:00:00") dataset.gti = gti geom_cutout = geom_image.cutout(position=geom_image.center_skydir, width=1 * u.deg) dataset_cutout = dataset.create(geom_cutout) dataset.stack(dataset_cutout) assert_allclose(dataset.counts.data.sum(), dataset_cutout.counts.data.sum()) assert_allclose(dataset.counts_off.data.sum(), dataset_cutout.counts_off.data.sum()) assert_allclose(dataset.alpha.data.sum(), dataset_cutout.alpha.data.sum()) assert_allclose(dataset.exposure.data.sum(), dataset_cutout.exposure.data.sum())
def get_map_dataset_onoff(images, **kwargs): """Returns a MapDatasetOnOff""" mask_geom = images["counts"].geom mask_data = np.ones(images["counts"].data.shape, dtype=bool) mask_safe = Map.from_geom(mask_geom, data=mask_data) return MapDatasetOnOff(models=None, counts=images["counts"], counts_off=images["counts_off"], acceptance=images["acceptance"], acceptance_off=images["acceptance_off"], exposure=images["exposure"], psf=None, edisp=None, mask_fit=None, mask_safe=mask_safe, **kwargs)
def test_map_dataset_onoff_fits_io(images, tmp_path): dataset = get_map_dataset_onoff(images) gti = GTI.create([0 * u.s], [1 * u.h], reference_time="2010-01-01T00:00:00") dataset.gti = gti hdulist = dataset.to_hdulist() actual = [hdu.name for hdu in hdulist] desired = [ "PRIMARY", "COUNTS", "EXPOSURE", "MASK_SAFE", "GTI", "COUNTS_OFF", "ACCEPTANCE", "ACCEPTANCE_OFF", ] assert actual == desired dataset.write(tmp_path / "test.fits") dataset_new = MapDatasetOnOff.read(tmp_path / "test.fits") assert dataset_new.models is None assert dataset_new.mask.dtype == bool assert_allclose(dataset.counts.data, dataset_new.counts.data) assert_allclose(dataset.counts_off.data, dataset_new.counts_off.data) assert_allclose(dataset.acceptance.data, dataset_new.acceptance.data) assert_allclose(dataset.acceptance_off.data, dataset_new.acceptance_off.data) assert_allclose(dataset.exposure.data, dataset_new.exposure.data) assert_allclose(dataset.mask_safe, dataset_new.mask_safe) assert dataset.counts.geom == dataset_new.counts.geom assert dataset.exposure.geom == dataset_new.exposure.geom assert_allclose(dataset.gti.time_sum.to_value("s"), dataset_new.gti.time_sum.to_value("s"))
def test_stack_onoff(images, geom_image): dataset = get_map_dataset_onoff(images) stacked = dataset.copy() stacked.stack(dataset) assert_allclose(stacked.counts.data.sum(), 2 * dataset.counts.data.sum()) assert_allclose(stacked.counts_off.data.sum(), 2 * dataset.counts_off.data.sum()) assert_allclose(stacked.acceptance.data.sum(), dataset.data_shape[0] * dataset.data_shape[1]) assert_allclose( np.nansum(stacked.acceptance_off.data), np.nansum(dataset.counts_off.data / (dataset.counts_off.data * dataset.alpha.data)), ) assert_allclose(stacked.exposure.data, 2.0 * dataset.exposure.data) # Test stacking of cutouts dataset = MapDatasetOnOff.create(geom_image) gti = GTI.create([0 * u.s], [1 * u.h], reference_time="2010-01-01T00:00:00") dataset.gti = gti geom_cutout = geom_image.cutout(position=geom_image.center_skydir, width=1 * u.deg) dataset_cutout = dataset.create(geom_cutout) dataset.stack(dataset_cutout) assert_allclose(dataset.counts.data.sum(), dataset_cutout.counts.data.sum()) assert_allclose(dataset.counts_off.data.sum(), dataset_cutout.counts_off.data.sum()) assert_allclose(dataset.alpha.data.sum(), dataset_cutout.alpha.data.sum()) assert_allclose(dataset.exposure.data.sum(), dataset_cutout.exposure.data.sum())