def downsample(self, factor, preserve_counts=True, axis_name="energy", weights=None): if axis_name is None: return self.copy() geom = self.geom.downsample(factor=factor, axis_name=axis_name) block_size = [1] * self.data.ndim idx = self.geom.axes.index_data(axis_name) block_size[idx] = factor if weights is None: weights = 1 else: weights = weights.data func = np.nansum if preserve_counts else np.nanmean if self.data.dtype == bool: func = np.all data = block_reduce(self.data * weights, tuple(block_size), func=func) return self._init_copy(geom=geom, data=data)
def downsample(self, factor, preserve_counts=True, axis_name=None, weights=None): if factor == 1 or factor is None: return self geom = self.geom.downsample(factor, axis_name=axis_name) if axis_name is None: block_size = (1, ) * len(self.geom.axes) + (factor, factor) else: block_size = [1] * self.data.ndim idx = self.geom.axes.index_data(axis_name) block_size[idx] = factor func = np.nansum if preserve_counts else np.nanmean if weights is None: weights = 1 else: weights = weights.data data = block_reduce(self.data * weights, tuple(block_size), func=func) return self._init_copy(geom=geom, data=data.astype(self.data.dtype))
def downsample(self, factor, preserve_counts=True, axis=None, weights=None): geom = self.geom.downsample(factor, axis=axis) if axis is None: block_size = (factor, factor) + (1, ) * len(self.geom.axes) else: block_size = [1] * self.data.ndim idx = self.geom.get_axis_index_by_name(axis) block_size[idx + 2] = factor func = np.nansum if preserve_counts else np.nanmean if weights is None: weights = 1 else: weights = weights.data data = block_reduce(self.data * weights, tuple(block_size[::-1]), func=func) return self._init_copy(geom=geom, data=data.astype(self.data.dtype))
def downsample(self, factor, preserve_counts=True, axis="energy"): geom = self.geom.downsample(factor=factor, axis=axis) block_size = [1] * self.data.ndim idx = self.geom.get_axis_index_by_name(axis) block_size[-(idx + 1)] = factor func = np.nansum if preserve_counts else np.nanmean data = block_reduce(self.data, tuple(block_size[::-1]), func=func) return self._init_copy(geom=geom, data=data)
def downsample(self, factor, preserve_counts=True, axis_name="energy"): geom = self.geom.downsample(factor=factor, axis_name=axis_name) block_size = [1] * self.data.ndim idx = self.geom.axes.index_data(axis_name) block_size[idx] = factor func = np.nansum if preserve_counts else np.nanmean data = block_reduce(self.data, tuple(block_size), func=func) return self._init_copy(geom=geom, data=data)
def downsample(self, factor, preserve_counts=True, axis_name="energy", weights=None): """Downsample the non-spatial dimension by a given factor. Parameters ---------- factor : int Downsampling factor. preserve_counts : bool Preserve the integral over each bin. This should be true if the map is an integral quantity (e.g. counts) and false if the map is a differential quantity (e.g. intensity). axis_name : str Which axis to downsample. Default is "energy". weights : `RegionNDMap` Contains the weights to apply to the axis to reduce. Default is just weighs of one. Returns ------- map : `RegionNDMap` Downsampled region map. """ if axis_name is None: return self.copy() geom = self.geom.downsample(factor=factor, axis_name=axis_name) block_size = [1] * self.data.ndim idx = self.geom.axes.index_data(axis_name) block_size[idx] = factor if weights is None: weights = 1 else: weights = weights.data func = np.nansum if preserve_counts else np.nanmean if self.is_mask: func = np.all data = block_reduce(self.data * weights, tuple(block_size), func=func) return self._init_copy(geom=geom, data=data)
def downsample(self, factor): """Downsample spectrum. Parameters ---------- factor : int Downsampling factor. Returns ------- spectrum : `~gammapy.spectrum.CountsSpectrum` Downsampled spectrum. """ from gammapy.extern.skimage import block_reduce data = block_reduce(self.data, block_size=(factor, )) energy = self.energy.downsample(factor).edges return self.__class__(energy_lo=energy[:-1], energy_hi=energy[1:], data=data)