Skip to content

Commit

Permalink
Merge b1d8d6f into 6617ff7
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Nov 16, 2023
2 parents 6617ff7 + b1d8d6f commit 16914ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions trollimage/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ def test_gamma(self):

def test_crude_stretch(self):
"""Check crude stretching."""
arr = np.arange(75).reshape(5, 5, 3)
arr = np.arange(75, dtype=np.float32).reshape(5, 5, 3)
data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'],
coords={'bands': ['R', 'G', 'B']})
img = xrimage.XRImage(data)
Expand All @@ -1467,11 +1467,15 @@ def test_crude_stretch(self):
enhs = img.data.attrs['enhancement_history'][0]
scale_expected = np.array([0.01388889, 0.01388889, 0.01388889])
offset_expected = np.array([0., -0.01388889, -0.02777778])
assert img.data.dtype == np.float32
np.testing.assert_allclose(enhs['scale'].values, scale_expected)
np.testing.assert_allclose(enhs['offset'].values, offset_expected)
np.testing.assert_allclose(red, arr[:, :, 0] / 72.)
np.testing.assert_allclose(green, (arr[:, :, 1] - 1.) / (73. - 1.))
np.testing.assert_allclose(blue, (arr[:, :, 2] - 2.) / (74. - 2.))
expected_red = arr[:, :, 0] / 72.
np.testing.assert_allclose(red, expected_red.astype(np.float32), rtol=1e-6)
expected_green = (arr[:, :, 1] - 1.) / (73. - 1.)
np.testing.assert_allclose(green, expected_green.astype(np.float32), rtol=1e-6)
expected_blue = (arr[:, :, 2] - 2.) / (74. - 2.)
np.testing.assert_allclose(blue, expected_blue.astype(np.float32), rtol=1e-6)

arr = np.arange(75).reshape(5, 5, 3).astype(float)
data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'],
Expand Down Expand Up @@ -2325,10 +2329,10 @@ class TestXRImageSaveScaleOffset:
def setup_method(self) -> None:
"""Set up the test case."""
from trollimage import xrimage
data = xr.DataArray(np.arange(25).reshape(5, 5, 1), dims=[
data = xr.DataArray(np.arange(25, dtype=np.float32).reshape(5, 5, 1), dims=[
'y', 'x', 'bands'], coords={'bands': ['L']})
self.img = xrimage.XRImage(data)
rgb_data = xr.DataArray(np.arange(3 * 25).reshape(5, 5, 3), dims=[
rgb_data = xr.DataArray(np.arange(3 * 25, dtype=np.float32).reshape(5, 5, 3), dims=[
'y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']})
self.rgb_img = xrimage.XRImage(rgb_data)

Expand Down
4 changes: 2 additions & 2 deletions trollimage/xrimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,9 @@ def crude_stretch(self, min_stretch=None, max_stretch=None):
delta = (max_stretch - min_stretch)
if isinstance(delta, xr.DataArray):
# fillna if delta is NaN
scale_factor = (1.0 / delta).fillna(0)
scale_factor = (1.0 / delta).fillna(0).astype(self.data.dtype)
else:
scale_factor = 1.0 / delta
scale_factor = self.data.dtype.type(1.0 / delta)
attrs = self.data.attrs
offset = -min_stretch * scale_factor
self.data *= scale_factor
Expand Down

0 comments on commit 16914ef

Please sign in to comment.