Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Clip RGB data to valid range for imshow#10220
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
`Axes.imshow` clips RGB values to the valid range | ||
------------------------------------------------- | ||
When `Axes.imshow` is passed an RGB or RGBA value with out-of-range | ||
values, it now logs a warning and clips them to the valid range. | ||
The old behaviour, wrapping back in to the range, often hid outliers | ||
and made interpreting RGB images unreliable. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -386,6 +386,7 @@ Yu Feng, | ||
Yunfei Yang, | ||
Yuri D'Elia, | ||
Yuval Langer, | ||
Zac Hatfield-Dodds, | ||
Zach Pincus, | ||
Zair Mubashar, | ||
alex, | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
`Axes.imshow` clips RGB values to the valid range | ||
------------------------------------------------- | ||
When `Axes.imshow` is passed an RGB or RGBA value with out-of-range | ||
values, it now logs a warning and clips them to the valid range. | ||
The old behaviour, wrapping back in to the range, often hid outliers | ||
and made interpreting RGB images unreliable. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -13,6 +13,7 @@ | ||
from math import ceil | ||
import os | ||
import logging | ||
import numpy as np | ||
@@ -34,6 +35,8 @@ | ||
from matplotlib.transforms import (Affine2D, BboxBase, Bbox, BboxTransform, | ||
IdentityTransform, TransformedBbox) | ||
_log = logging.getLogger(__name__) | ||
# map interpolation strings to module constants | ||
_interpd_ = { | ||
'none': _image.NEAREST, # fall back to nearest when not supported | ||
@@ -623,6 +626,23 @@ def set_data(self, A): | ||
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]): | ||
raise TypeError("Invalid dimensions for image data") | ||
if self._A.ndim == 3: | ||
# If the input data has values outside the valid range (after | ||
# normalisation), we issue a warning and then clip X to the bounds | ||
# - otherwise casting wraps extreme values, hiding outliers and | ||
# making reliable interpretation impossible. | ||
high = 255 if np.issubdtype(self._A.dtype, np.integer) else 1 | ||
if self._A.min() < 0 or high < self._A.max(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I wonder if we should pad this by an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We have to clip to exactly [0 .. 1], and for consistency I would prefer to always warn if clipping. If users feel spammed, they can either fix their data or filter the logging output. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think the suggestion might have been to silently clip if (a-vmax)/(vmin - vmax) - 1 < 1e-10 or something that would just represent bit noise that would never affect the interpretation of the data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yes, that's the question - I just think the answer is "No, we shouldn't". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Agree that we shouldn't. | ||
_log.warning( | ||
'Clipping input data to the valid range for imshow with ' | ||
'RGB data ([0..1] for floats or [0..255] for integers).' | ||
) | ||
self._A = np.clip(self._A, 0, high) | ||
# Cast unsupported integer types to uint8 | ||
if self._A.dtype != np.uint8 and np.issubdtype(self._A.dtype, | ||
np.integer): | ||
self._A = self._A.astype(np.uint8) | ||
self._imcache = None | ||
self._rgbacache = None | ||
self.stale = True | ||