-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: handle fully masked data #9285
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
Conversation
probably worth adding in the comment that the actual value doesn't matter. |
lib/matplotlib/image.py
Outdated
try: | ||
a_max = a_max.astype(scaled_dtype) | ||
except AttributeError: | ||
a_min = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simpler?
if np.ma.count(A) == 0:
a_min, a_max = 0, 1 # all masked, so values don't matter
else:
a_min = np.ma.min(A).astype(scaled_dtype)
a_max = np.ma.max(A).astype(scaled_dtype)
lib/matplotlib/image.py
Outdated
a_max = np.ma.max(A) | ||
|
||
# we need these try/except blocks to handle | ||
# fully-masked/invalid input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And delete the 4 lines above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
details....
f5c69f6
to
659c9f3
Compare
Actually now that I think of it it may be slightly more efficient to check whether |
It is always masked because an earlier step in the processing chain makes it so. |
Then it definitely seems better to avoid a redundant full iteration... |
659c9f3
to
aa66aee
Compare
ok, so back to the original version of this PR. |
I would make the test on masked explicit rather than relying on attributeerror (but again no big deal) |
lib/matplotlib/image.py
Outdated
except AttributeError: | ||
a_min = 0 | ||
try: | ||
a_max = a_max.astype(scaled_dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gee, can't leave it alone.
try:
a_min = a_min.astype(scaled_dtype)
except AttributeError:
a_min, a_max = 0, 1
else:
a_max = a_max.astype(scaled_dtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or shorten the whole block. From the top:
try:
a_min = A.min().astype(scaled_dtype)
except AttributeError:
a_min, a_max = 0, 1 # all masked, so values don't matter
else:
a_max = A.max().astype(scaled_dtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And taking @anntzer's suggestion:
a_min = A.min().astype(scaled_dtype)
if a_min is np.ma.masked:
a_min, a_max = 0, 1 # all masked, so values don't matter
else:
a_max = A.max().astype(scaled_dtype)
I took the liberty of directly making a commit with a short form that also addresses @anntzer's suggestion instead of pestering you with yet another code chunk in a comment. The image code is sufficiently long and complicated that it is worthwhile to keep the handling of this special case as concise and readable as possible. |
@efiring take care of the merge conflict too? |
Yup, just did it. Tests are running. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conditional on tests passing
ah the joys of pep8 |
The problem there is the lousy editor available for in-place editing in github. It neither shows nor removes trailing whitespace. I will try again, but it will be a shot in the dark. If it doesn't work I guess I will have to go through the whole pull/modify/commit/push dance. |
As min/max do not make sense for an array with no non-masked values numpy returns a singleton, `np.ma.masked`, which can not be cast to a number. In this case just treat numbers as in range (0, 1) (even though it will just be ignored due to the masking). closes matplotlib#9280
a4cd618
to
63d3fb0
Compare
rebased and force-pushed to remove the merge commit. |
thanks |
As min/max do not make sense for an array with no non-masked values
numpy returns a singleton,
np.ma.masked
, which can not be cast toa number. In this case just treat numbers as in range (0, 1) (even
though it will just be ignored due to the masking).
closes #9280
PR Summary
PR Checklist