Add axis argument to WcsNDMap.upsample() and WcsNDMap.downsample()
@@ -380,26 +380,34 @@ class WcsNDMap(WcsMap):
return map_out
- def upsample(self, factor, order=0, preserve_counts=True):
- geom = self.geom.upsample(factor)
- idx = geom.get_idx()
- pix = (
- (idx[0] - 0.5 * (factor - 1)) / factor,
- (idx[1] - 0.5 * (factor - 1)) / factor,
- ) + idx[2:]
- data = map_coordinates(self.data.T, pix, order=order, mode="nearest")
+ def upsample(self, factor, preserve_counts=True, axis=None):
+ geom = self.geom.upsample(factor, axis=axis)
+ coords = geom.get_coord()
+ data = self.get_by_coord(coords=coords)
+
if preserve_counts:
+ if axis is None:
data /= factor ** 2
+ else:
+ data /= factor
return self._init_copy(geom=geom, data=data)
- def downsample(self, factor, preserve_counts=True):
- geom = self.geom.downsample(factor)
+ def downsample(self, factor, preserve_counts=True, axis=None):
+ geom = self.geom.downsample(factor, axis=axis)
+ if axis is None:
block_size = (factor, factor) + (1,) * len(self.geom.axes)
- data = block_reduce(self.data, block_size[::-1], np.nansum)
+ else:
+ block_size = [1] * self.data.ndim
+ idx = self.geom.get_axis_index_by_name(axis)
+ block_size[-(idx + 1)] = factor
+
+ data = block_reduce(self.data, tuple(block_size[::-1]), np.nansum)
if not preserve_counts:
+ if axis is None:
data /= factor ** 2
-
+ else:
+ data /= factor
return self._init_copy(geom=geom, data=data)
def plot(self, ax=None, fig=None, add_cbar=False, stretch="linear", **kwargs):
