MNT: suppress RuntimeWarning for invalid float comparison in images #7780

Merged
merged 3 commits into from Jan 16, 2017

Projects

None yet

4 participants

@tacaswell
Member

If do not warn for invalid values when looking for over/under pixels
in _ImageBase._make_image.

closes #7746

@tacaswell tacaswell MNT: suppress RuntimeWarning for invalid float comparison in images
If do not warn for invalid values when looking for over/under pixels
in _ImageBase._make_image.

closes #7746
8ff23db
@efiring

The problem could be patched up here, but it seems like the right place to do it is in the norms. Unfortunately, that means all the norms.

lib/matplotlib/image.py
- rgba[..., 2] = A > 1 # over data
+ with np.errstate(invalid='ignore'):
+ rgba[..., 1] = A < 0 # under data
+ rgba[..., 2] = A > 1 # over data
@efiring
efiring Jan 10, 2017 Member

I think this is hiding the real problem. A is the output of a norm, which ensures it is a masked array. Otherwise, the line below would fail. Therefore the problem is that the norm is not running its floating point input through np.ma.masked_invalid or equivalent, so the bad values are not getting masked, resulting in the warnings and in the line below being incorrect, because it is not finding all of the bad data.

@tacaswell
tacaswell Jan 10, 2017 Member

That does not seem to suppress the warning on it's own:

In [81]: np.ma.masked_invalid([[1, 2], [3, np.nan]]) < 3
/home/tcaswell/.virtualenvs/dd35/bin/ipython:1: RuntimeWarning: invalid value encountered in less
  #!/home/tcaswell/mc3/envs/dd35/bin/python
Out[81]: 

masked_array(data =
 [[True True]
 [False --]],
             mask =
 [[False False]
 [False  True]],
       fill_value = True)
@@ -753,5 +754,12 @@ def test_imshow_endianess():
ax2.imshow(Z.astype('>f8'), **kwargs)
+@cleanup
+def test_imshow_no_warn_invalid():
+ with warnings.catch_warnings(record=True) as warns:
@QuLogic
QuLogic Jan 10, 2017 Member

Needs warnings.simplefilter('always') to ensure the warnings get triggered.

@efiring
Member
efiring commented Jan 10, 2017
@tacaswell
Member

@efiring is it worth chasing through old numpy versions to see if this is a regression?

@tacaswell
Member

This appears to be a change from numpy 1.7 -> 1.8

18:09 $ source activate /tmp/lpy_np
(lpy_np) ✔ ~/ 
18:09 $ ipython
Python 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy as np

In [2]: np.ma.masked_invalid([[1, 2], [3, np.nan]]) < 3
Out[2]: 
masked_array(data =
 [[True True]
 [False --]],
             mask =
 [[False False]
 [False  True]],
       fill_value = True)

In [3]:                                                                                                                                                                                                                                                                                                                                                                                                                                 
Do you really want to exit ([y]/n)? y
(lpy_np) ✔ ~/ 
18:09 $ conda list numpy
# packages in environment at /tmp/lpy_np:
#
numpy                     1.7.1                    py34_3    defaults
(lpy_np) ✔ ~/ 
18:09 $ conda install numpy=1.8
Fetching package metadata .......
Solving package specifications: ..........

Package plan for installation in environment /tmp/lpy_np:

The following packages will be UPDATED:

    numpy: 1.7.1-py34_3 defaults --> 1.8.2-py34_1 defaults (soft-link)

Proceed ([y]/n)? y

Unlinking packages ...
[      COMPLETE      ]|###########################################################################################################################################################################################################################################################################################################################################################################################################| 100%
Linking packages ...
[      COMPLETE      ]|###########################################################################################################################################################################################################################################################################################################################################################################################################| 100%
(lpy_np) ✔ ~/ 
18:09 $ ipython
^[[APython 3.4.5 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:47:47) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy as np

In [2]: np.ma.masked_invalid([[1, 2], [3, np.nan]]) < 3
/tmp/lpy_np/bin/ipython:1: RuntimeWarning: invalid value encountered in less
  #!/tmp/lpy_np/bin/python
Out[2]: 
masked_array(data =
 [[True True]
 [False --]],
             mask =
 [[False False]
 [False  True]],
       fill_value = True)

In [3]:                                                                                                                                                                                                                                                                                                                                                                                                                                 
Do you really want to exit ([y]/n)? 
(lpy_np) ✔ ~/ 
18:10 $ conda list numpy
# packages in environment at /tmp/lpy_np:
#
numpy                     1.8.2                    py34_1    defaults
(lpy_np) ✔ ~/ 
18:10 $ 

@naoyak
Contributor
naoyak commented Jan 15, 2017 edited

https://github.com/numpy/numpy/blob/master/doc/release/1.8.0-notes.rst

Runtime warnings when comparing NaN numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comparing ``NaN`` floating point numbers now raises the ``invalid`` runtime
warning. If a ``NaN`` is expected the warning can be ignored using np.errstate.
E.g.::

  with np.errstate(invalid='ignore'):
      operation()
@tacaswell tacaswell closed this Jan 16, 2017
@tacaswell tacaswell reopened this Jan 16, 2017
@efiring efiring merged commit 6e2e1dc into matplotlib:v2.x Jan 16, 2017

1 check was pending

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
@efiring
Member
efiring commented Jan 16, 2017

I think the relevant numpy.ma problem is here: numpy/numpy#4959.

@tacaswell tacaswell deleted the tacaswell:mnt_squelch_imshow_warning branch Jan 16, 2017
@efiring
Member
efiring commented Jan 16, 2017

It turns out that the problem that I thought might arise with imshow does not occur. AxesImage.set_data() is calling cbook.safe_masked_invalid().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment