Repository: gammapy/gammapy

Issue:
`upsample()` / `downsample()` and other inconsistencies across class hierarchies
**Gammapy version**

`master` branch

**Bug description**

I just realized that I had mixed up `upsample()` and `downsample()` in #3552. I had modified `downsample()` instead of `upsample()` in at least one location.

**Expected behavior**

Consistent `upsample()` / `downsample()` signature across class hierarchy.

**Other information**

DeepSource.io actually reports lots of inconsistencies in method signatures in class hierarchies, see for example:
https://deepsource.io/gh/DimitriPapadopoulos/gammapy/issue/PYL-W0221/occurrences

I believe it would be worth going through such warnings, and revisiting the API with consistency across class hierarchies in mind.



Discussion:
Thanks @DimitriPapadopoulos, I agree this would be good clean-up work to do. I just set the v1.0 milestone...

Resolving patch:
diff --git a/gammapy/maps/core.py b/gammapy/maps/core.py
--- a/gammapy/maps/core.py
+++ b/gammapy/maps/core.py
@@ -563,7 +563,7 @@ def downsample(self, factor, preserve_counts=True, axis_name=None):
         pass
 
     @abc.abstractmethod
-    def upsample(self, factor, order=0, preserve_counts=True, axis=None):
+    def upsample(self, factor, order=0, preserve_counts=True, axis_name=None):
         """Upsample the spatial dimension by a given factor.
 
         Parameters
@@ -576,7 +576,7 @@ def upsample(self, factor, order=0, preserve_counts=True, axis=None):
             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 : str
+        axis_name : str
             Which axis to upsample. By default spatial axes are upsampled.
 
 
diff --git a/gammapy/maps/hpx/ndmap.py b/gammapy/maps/hpx/ndmap.py
--- a/gammapy/maps/hpx/ndmap.py
+++ b/gammapy/maps/hpx/ndmap.py
@@ -286,7 +286,13 @@ def crop(self, crop_width):
         map_out.coadd(self)
         return map_out
 
-    def upsample(self, factor, preserve_counts=True):
+    def upsample(self, factor, order=0, preserve_counts=True, axis_name=None):
+        if axis_name:
+            raise NotImplementedError("HpxNDMap.upsample does currently not support upsampling of non-spatial axes.")
+
+        if order !=0:
+            raise ValueError("HpxNDMap.upsample currently only supports nearest upsampling")
+
         geom = self.geom.upsample(factor)
         coords = geom.get_coord()
         data = self.get_by_coord(coords)
@@ -296,7 +302,10 @@ def upsample(self, factor, preserve_counts=True):
 
         return self._init_copy(geom=geom, data=data)
 
-    def downsample(self, factor, preserve_counts=True):
+    def downsample(self, factor, preserve_counts=True, axis_name=None):
+        if axis_name:
+            raise NotImplementedError("HpxNDMap does currently not support upsampling of non-spatial axes.")
+
         geom = self.geom.downsample(factor)
         coords = self.geom.get_coord()
         vals = self.get_by_coord(coords)
diff --git a/gammapy/maps/region/ndmap.py b/gammapy/maps/region/ndmap.py
--- a/gammapy/maps/region/ndmap.py
+++ b/gammapy/maps/region/ndmap.py
@@ -272,10 +272,12 @@ def create(
         return cls(geom=geom, dtype=dtype, unit=unit, meta=meta, data=data)
 
     def downsample(
-        self, factor, preserve_counts=True, axis_name="energy", weights=None
+        self, factor, preserve_counts=True, axis_name=None, weights=None
     ):
         """Downsample the non-spatial dimension by a given factor.
 
+        By default the first axes is downsampled.
+
         Parameters
         ----------
         factor : int
@@ -296,7 +298,7 @@ def downsample(
             Downsampled region map.
         """
         if axis_name is None:
-            return self.copy()
+            axis_name = self.geom.axes[0].name
 
         geom = self.geom.downsample(factor=factor, axis_name=axis_name)
 
@@ -316,13 +318,17 @@ def downsample(
 
         return self._init_copy(geom=geom, data=data)
 
-    def upsample(self, factor, preserve_counts=True, axis_name="energy"):
+    def upsample(self, factor, order=0, preserve_counts=True, axis_name=None):
         """Upsample the non-spatial dimension by a given factor.
 
+        By default the first axes is upsampled.
+
         Parameters
         ----------
         factor : int
             Upsampling factor.
+        order : int
+            Order of the interpolation used for upsampling.
         preserve_counts : bool
             Preserve the integral over each bin.  This should be true
             if the RegionNDMap is an integral quantity (e.g. counts) and false if
@@ -335,6 +341,9 @@ def upsample(self, factor, preserve_counts=True, axis_name="energy"):
         map : `RegionNDMap`
             Upsampled region map.
         """
+        if axis_name is None:
+            axis_name = self.geom.axes[0].name
+
         geom = self.geom.upsample(factor=factor, axis_name=axis_name)
         data = self.interp_by_coord(geom.get_coord())
 


Test patch: