From 575365e3378d14c06eb94fbd9e3e52c763ac0fde Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Thu, 10 Oct 2024 13:15:45 +0300 Subject: [PATCH 1/4] Split invert test, test for dtype also for per-channel invert values --- trollimage/tests/test_image.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/trollimage/tests/test_image.py b/trollimage/tests/test_image.py index 0aaf6a9..99ed23c 100644 --- a/trollimage/tests/test_image.py +++ b/trollimage/tests/test_image.py @@ -1595,8 +1595,8 @@ def test_crude_stretch_integer_data(self, dtype, max_stretch): np.testing.assert_allclose(img.data.values, arr.astype(np.float32) / max_stretch, rtol=1e-6) @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) - def test_invert(self, dtype): - """Check inversion of the image.""" + def test_invert_single_parameter(self, dtype): + """Check inversion of the image for single inversion parameter.""" arr = np.arange(75, dtype=dtype).reshape(5, 5, 3) / 75. data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) @@ -1605,10 +1605,13 @@ def test_invert(self, dtype): img.invert(True) enhs = img.data.attrs['enhancement_history'][0] assert enhs == {'scale': -1, 'offset': 1} - assert img.data.dtype == dtype assert np.allclose(img.data.values, 1 - arr) - data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'], + @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) + def test_invert_parameter_for_each_channel(self, dtype): + """Check inversion of the image for single inversion parameter.""" + arr = np.arange(75, dtype=dtype).reshape(5, 5, 3) / 75. + data = xr.DataArray(arr, dims=['y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) img = xrimage.XRImage(data) @@ -1618,6 +1621,7 @@ def test_invert(self, dtype): scale = xr.DataArray(np.array([-1, 1, -1]), dims=['bands'], coords={'bands': ['R', 'G', 'B']}) np.testing.assert_allclose(img.data.values, (data * scale + offset).values) + assert img.data.dtype == dtype @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) def test_linear_stretch(self, dtype): From 236570ef8dd15b34af54711b6adf26559c40e75f Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Thu, 10 Oct 2024 13:16:49 +0300 Subject: [PATCH 2/4] Prevent data promotion in invert() --- trollimage/xrimage.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/trollimage/xrimage.py b/trollimage/xrimage.py index 6c56b3e..16a8357 100644 --- a/trollimage/xrimage.py +++ b/trollimage/xrimage.py @@ -1132,7 +1132,6 @@ def _check_stretch_value(self, val, kind='min'): if val is None: non_band_dims = tuple(x for x in self.data.dims if x != 'bands') val = getattr(self.data, kind)(dim=non_band_dims) - if isinstance(val, (list, tuple)): val = self.xrify_tuples(val) @@ -1302,7 +1301,7 @@ def invert(self, invert=True): logger.debug("Applying invert with parameters %s", str(invert)) if isinstance(invert, (tuple, list)): invert = self.xrify_tuples(invert) - offset = invert.astype(int) + offset = invert.astype(np.int8) scale = (-1) ** offset elif invert: offset = 1 From 04fd0dc69a14228027ac46b474af7fe911d848ef Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Mon, 14 Oct 2024 12:40:12 +0300 Subject: [PATCH 3/4] Add accidentally deleted dtype test --- trollimage/tests/test_image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/trollimage/tests/test_image.py b/trollimage/tests/test_image.py index 99ed23c..500a99f 100644 --- a/trollimage/tests/test_image.py +++ b/trollimage/tests/test_image.py @@ -1605,6 +1605,7 @@ def test_invert_single_parameter(self, dtype): img.invert(True) enhs = img.data.attrs['enhancement_history'][0] assert enhs == {'scale': -1, 'offset': 1} + assert img.data.dtype == dtype assert np.allclose(img.data.values, 1 - arr) @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) From 2ce875e0d0074fbba680c478249d90ccaa783199 Mon Sep 17 00:00:00 2001 From: Panu Lahtinen Date: Mon, 14 Oct 2024 12:44:24 +0300 Subject: [PATCH 4/4] Use data dtype for offset in invert() --- trollimage/xrimage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trollimage/xrimage.py b/trollimage/xrimage.py index 16a8357..5beea01 100644 --- a/trollimage/xrimage.py +++ b/trollimage/xrimage.py @@ -1301,7 +1301,7 @@ def invert(self, invert=True): logger.debug("Applying invert with parameters %s", str(invert)) if isinstance(invert, (tuple, list)): invert = self.xrify_tuples(invert) - offset = invert.astype(np.int8) + offset = invert.astype(self.data.dtype) scale = (-1) ** offset elif invert: offset = 1