Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the original dtype of the data when stretching #150

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 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
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
Loading