# Licensed under a 3-clause BSD style license - see LICENSE.rstimportloggingimportastropy.unitsasufromastropy.tableimportTablefromregionsimportPointSkyRegionfromgammapy.data.pointingimportPointingModefromgammapy.irfimportEDispKernelMap,PSFMap,RecoPSFMapfromgammapy.mapsimportMapfrom.coreimportMakerfrom.utilsimport(make_counts_rad_max,make_edisp_kernel_map,make_edisp_map,make_map_background_irf,make_map_exposure_true_energy,make_psf_map,)__all__=["MapDatasetMaker"]log=logging.getLogger(__name__)
[docs]classMapDatasetMaker(Maker):"""Make binned maps for a single IACT observation. Parameters ---------- selection : list List of str, selecting which maps to make. Available: 'counts', 'exposure', 'background', 'psf', 'edisp' By default, all maps are made. background_oversampling : int Background evaluation oversampling factor in energy. background_interp_missing_data: bool Interpolate missing values in background 3d map. Default is True, have to be set to True for CTA IRF. background_pad_offset: bool Pad one bin in offset for 2d background map. This avoid extrapolation at edges and use the nearest value. Default is True. Examples -------- This example shows how to run the MapMaker for a single observation >>> from gammapy.data import DataStore >>> from gammapy.datasets import MapDataset >>> from gammapy.maps import WcsGeom, MapAxis >>> from gammapy.makers import MapDatasetMaker >>> #load an observation >>> data_store = DataStore.from_dir("$GAMMAPY_DATA/hess-dl3-dr1") >>> obs = data_store.obs(23523) >>> #prepare the geom >>> energy_axis = MapAxis.from_energy_bounds(1.0, 10.0, 4, unit="TeV") >>> energy_axis_true = MapAxis.from_energy_bounds( 0.5, 20, 10, unit="TeV", name="energy_true") >>> geom = WcsGeom.create( skydir=(83.633, 22.014), binsz=0.02, width=(2, 2), frame="icrs", proj="CAR", axes=[energy_axis], ) >>> #Run the maker >>> empty = MapDataset.create(geom=geom, energy_axis_true=energy_axis_true, name="empty") >>> maker = MapDatasetMaker() >>> dataset = maker.run(empty, obs) >>> print(dataset) MapDataset ---------- <BLANKLINE> Name : empty <BLANKLINE> Total counts : 787 Total background counts : 684.52 Total excess counts : 102.48 <BLANKLINE> Predicted counts : 684.52 Predicted background counts : 684.52 Predicted excess counts : nan <BLANKLINE> Exposure min : 7.01e+07 m2 s Exposure max : 1.10e+09 m2 s <BLANKLINE> Number of total bins : 40000 Number of fit bins : 40000 <BLANKLINE> Fit statistic type : cash Fit statistic value (-2 log(L)) : nan <BLANKLINE> Number of models : 0 Number of parameters : 0 Number of free parameters : 0 """tag="MapDatasetMaker"available_selection=["counts","exposure","background","psf","edisp"]def__init__(self,selection=None,background_oversampling=None,background_interp_missing_data=True,background_pad_offset=True,):self.background_oversampling=background_oversamplingself.background_interp_missing_data=background_interp_missing_dataself.background_pad_offset=background_pad_offsetifselectionisNone:selection=self.available_selectionselection=set(selection)ifnotselection.issubset(self.available_selection):difference=selection.difference(self.available_selection)raiseValueError(f"{difference} is not a valid method.")self.selection=selection
[docs]@staticmethoddefmake_counts(geom,observation):"""Make counts map. **NOTE for 1D analysis:** if the `~gammapy.maps.Geom` is built from a `~regions.CircleSkyRegion`, the latter will be directly used to extract the counts. If instead the `~gammapy.maps.Geom` is built from a `~regions.PointSkyRegion`, the size of the ON region is taken from the `RAD_MAX_2D` table containing energy-dependent theta2 cuts. Parameters ---------- geom : `~gammapy.maps.Geom` Reference map geom. observation : `~gammapy.data.Observation` Observation container. Returns ------- counts : `~gammapy.maps.Map` Counts map. """ifgeom.is_regionandisinstance(geom.region,PointSkyRegion):counts=make_counts_rad_max(geom,observation.rad_max,observation.events)else:counts=Map.from_geom(geom)counts.fill_events(observation.events)returncounts