From 07eaa4ca538b042b352ec98b4ef1348a7d610ad7 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 5 Oct 2017 01:20:32 -0700 Subject: [PATCH 1/3] 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 --- lib/matplotlib/image.py | 15 +++++++++++++-- lib/matplotlib/tests/test_image.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 6b8477f63018..a18930e480ef 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -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 + try: + a_min = a_min.astype(scaled_dtype) + except AttributeError: + a_min = 0 + try: + a_max = a_max.astype(scaled_dtype) + except AttributeError: + a_min = 1 # 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 diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 15c148f17cdb..0a16e97848f6 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -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() From 8c3ad29e99bd9558b3e1eab4f191181d9af75878 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Wed, 11 Oct 2017 15:02:03 -1000 Subject: [PATCH 2/3] Handle min, max of possibly all-masked array --- lib/matplotlib/image.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index a18930e480ef..bf6bbf245af1 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -368,21 +368,14 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, scaled_dtype = A.dtype else: 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) - a_max = np.ma.max(A) - - # we need these try/except blocks to handle - # fully-masked/invalid input - try: + + a_min = A.min() + if a_min is np.ma.masked: + a_min, a_max = 0, 1 # all masked, so values don't matter + else: a_min = a_min.astype(scaled_dtype) - except AttributeError: - a_min = 0 - try: - a_max = a_max.astype(scaled_dtype) - except AttributeError: - a_min = 1 + a_max = A.max().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 From 63d3fb0baae3c28aad030dcb85a9ce2f81a0e321 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Wed, 11 Oct 2017 19:58:03 -1000 Subject: [PATCH 3/3] Remove trailing whitespace that the github editor so helpfully added --- lib/matplotlib/image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index bf6bbf245af1..5cd5c3f1026f 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -368,14 +368,14 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, scaled_dtype = A.dtype else: scaled_dtype = np.float32 - + a_min = A.min() if a_min is np.ma.masked: a_min, a_max = 0, 1 # all masked, so values don't matter else: a_min = a_min.astype(scaled_dtype) a_max = A.max().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