def to_image(self, spectrum=None, keepdims=True): """Reduce to a 2-D map after weighing with the associated exposure and a spectrum Parameters ---------- spectrum : `~gammapy.modeling.models.SpectralModel`, optional Spectral model to compute the weights. Default is power-law with spectral index of 2. keepdims : bool, optional If True, the energy axis is kept with one bin. If False, the axis is removed Returns ------- psf_out : `PSFMap` `PSFMap` with the energy axis summed over """ from gammapy.makers.utils import _map_spectrum_weight if spectrum is None: spectrum = PowerLawSpectralModel(index=2.0) exp_weighed = _map_spectrum_weight(self.exposure_map, spectrum) exposure = exp_weighed.sum_over_axes(axes=["energy_true"], keepdims=keepdims) psf_data = exp_weighed.data * self.psf_map.data / exposure.data psf_map = Map.from_geom(geom=self.psf_map.geom, data=psf_data, unit="sr-1") psf = psf_map.sum_over_axes(axes=["energy_true"], keepdims=keepdims) return self.__class__(psf_map=psf, exposure_map=exposure)
def test_map_spectrum_weight(): axis = MapAxis.from_edges([0.1, 10, 1000], unit="TeV", name="energy_true") expo_map = WcsNDMap.create(npix=10, binsz=1, axes=[axis], unit="m2 s") expo_map.data += 1 spectrum = ConstantSpectralModel(const="42 cm-2 s-1 TeV-1") weighted_expo = _map_spectrum_weight(expo_map, spectrum) assert weighted_expo.data.shape == (2, 10, 10) assert weighted_expo.unit == "m2 s" assert_allclose(weighted_expo.data.sum(), 100)
def run(self, dataset): """ Run adaptive smoothing on input MapDataset. The latter should have Parameters ---------- dataset : `~gammapy.cube.MapDataset` or `~gammapy.cube.MapDatasetOnOff` the input dataset (with one bin in energy at most) Returns ------- images : dict of `~gammapy.maps.WcsNDMap` Smoothed images; keys are: * 'counts' * 'background' * 'flux' (optional) * 'scales' * 'significance'. """ # Check dimensionality if len(dataset.data_shape) == 3: if dataset.data_shape[0] != 1: raise ValueError( "ASmoothMapEstimator.run() requires a dataset with 1 energy bin at most." ) counts = dataset.counts.sum_over_axes(keepdims=False) background = dataset.npred() if isinstance(dataset, MapDatasetOnOff): background += dataset.background background = background.sum_over_axes(keepdims=False) if dataset.exposure is not None: exposure = _map_spectrum_weight(dataset.exposure, self.spectrum) exposure = exposure.sum_over_axes(keepdims=False) else: exposure = None return self.estimate_maps(counts, background, exposure)