def to_region_nd_map(self, region, func=np.nansum, weights=None, method="nearest"): """Get region ND map in a given region. By default the whole map region is considered. Parameters ---------- region: `~regions.Region` or `~astropy.coordinates.SkyCoord` Region. func : numpy.func Function to reduce the data. Default is np.nansum. For boolean Map, use np.any or np.all. weights : `WcsNDMap` Array to be used as weights. The geometry must be equivalent. method : {"nearest", "linear"} How to interpolate if a position is given. Returns ------- spectrum : `~gammapy.maps.RegionNDMap` Spectrum in the given region. """ from gammapy.maps import RegionGeom, RegionNDMap if isinstance(region, SkyCoord): region = PointSkyRegion(region) if weights is not None: if not self.geom == weights.geom: raise ValueError( "Incompatible spatial geoms between map and weights") geom = RegionGeom(region=region, axes=self.geom.axes) if isinstance(region, PointSkyRegion): coords = geom.get_coord() data = self.interp_by_coord(coords=coords, method=method) if weights is not None: data *= weights.interp_by_coord(coords=coords, method=method) else: cutout = self.cutout(position=geom.center_skydir, width=np.max(geom.width)) if weights is not None: weights_cutout = weights.cutout(position=geom.center_skydir, width=geom.width) cutout.data *= weights_cutout.data mask = cutout.geom.to_image().region_mask([region]).data data = func(cutout.data[..., mask], axis=-1) return RegionNDMap(geom=geom, data=data, unit=self.unit, meta=self.meta.copy())
def test_no_edisp(self): dataset = self.datasets[0].copy() energy = dataset.counts.geom.axes[0].copy(name="energy_true") geom = RegionGeom(region=None, axes=[energy]) data = dataset.aeff.interp_by_coord(geom.get_coord()) dataset.aeff = RegionNDMap.from_geom(geom=geom, data=data, unit="cm2") dataset.edisp = None dataset.models = self.pwl fit = Fit([dataset]) result = fit.run() assert_allclose(result.parameters["index"].value, 2.7961, atol=0.02)