Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 3 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
FIX: handle fully masked data
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 #9280
  • Loading branch information
tacaswell committed Oct 12, 2017
commit 07eaa4ca538b042b352ec98b4ef1348a7d610ad7
15 changes: 13 additions & 2 deletions 15 lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,19 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
scaled_dtype = np.float32
# old versions of numpy do not work with `np.nammin`
# and `np.nanmax` as inputs
a_min = np.ma.min(A).astype(scaled_dtype)
a_max = np.ma.max(A).astype(scaled_dtype)
a_min = np.ma.min(A)
a_max = np.ma.max(A)

# we need these try/except blocks to handle
# fully-masked/invalid input
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

details....

try:
a_min = a_min.astype(scaled_dtype)
except AttributeError:
a_min = 0
try:
a_max = a_max.astype(scaled_dtype)
Copy link
Member

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)

Copy link
Member

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)

Copy link
Member

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)

except AttributeError:
a_min = 1
Copy link
Member

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)

# scale the input data to [.1, .9]. The Agg
# interpolators clip to [0, 1] internally, use a
# smaller input scale to identify which of the
Expand Down
10 changes: 10 additions & 0 deletions 10 lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,13 @@ def test_imshow_deprecated_interd_warn():
with warnings.catch_warnings(record=True) as warns:
getattr(im, k)
assert len(warns) == 1


def test_full_invalid():
x = np.ones((10, 10))
x[:] = np.nan

f, ax = plt.subplots()
ax.imshow(x)

f.canvas.draw()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.