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

Fix stretching integer data #153

Merged
merged 4 commits into from
Nov 24, 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
13 changes: 13 additions & 0 deletions trollimage/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,19 @@ def test_linear_stretch_does_not_affect_alpha(self, dtype):

np.testing.assert_allclose(img.data.values, res, atol=1.e-6)

@pytest.mark.parametrize("dtype", (np.uint8, np.uint16, int))
def test_linear_stretch_integer(self, dtype):
"""Test linear stretch with integer data."""
arr = np.arange(75, dtype=dtype).reshape(5, 5, 3)
arr[4, 4, :] = 255
data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'],
coords={'bands': ['R', 'G', 'B']})
img = xrimage.XRImage(data)
img.stretch_linear()

assert img.data.values.min() == pytest.approx(-0.0015614156835530892)
assert img.data.values.max() == pytest.approx(1.0960743801652901)

@pytest.mark.parametrize("dtype", (np.float32, np.float64, float))
def test_histogram_stretch(self, dtype):
"""Test histogram stretching."""
Expand Down
6 changes: 5 additions & 1 deletion trollimage/xrimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ def crude_stretch(self, min_stretch=None, max_stretch=None):

attrs = self.data.attrs
offset = -min_stretch * scale_factor

self.data = np.multiply(self.data, scale_factor, dtype=scale_factor.dtype) + offset
self.data.attrs = attrs
self.data.attrs.setdefault('enhancement_history', []).append({'scale': scale_factor,
Expand All @@ -1095,8 +1096,11 @@ def _check_stretch_value(self, val, kind='min'):
if isinstance(val, (list, tuple)):
val = self.xrify_tuples(val)

dtype = self.data.dtype
if np.issubdtype(dtype, np.integer):
dtype = np.dtype(np.float32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be float64 if the input type is int32? the mantissa in float32 in 24bits, so it can't represent the full spectrum of values from int32...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handling is adjusted in d35a5ac

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And fixed in 034d659 . The previous attempt entered the wrong branch.

try:
val = val.astype(self.data.dtype)
val = val.astype(dtype)
except AttributeError:
val = self.data.dtype.type(val)

Expand Down
Loading