Skip to content

Commit 77d627e

Browse files
committed
Check for nan and inf in axes.delete_masked_points().
svn path=/trunk/matplotlib/; revision=5791
1 parent 90e6e43 commit 77d627e

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-07-18 Check for nan and inf in axes.delete_masked_points().
2+
This should help hexbin and scatter deal with nans. - ADS
3+
14
2008-07-17 Added ability to manually select contour label locations.
25
Also added a waitforbuttonpress function. - DMK
36

lib/matplotlib/axes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import division, generators
2-
import math, sys, warnings, datetime, new
2+
import math, sys, warnings, datetime, new, types
33

44
import numpy as np
55
from numpy import ma
@@ -36,6 +36,8 @@ def delete_masked_points(*args):
3636
Find all masked points in a set of arguments, and return
3737
the arguments with only the unmasked points remaining.
3838
39+
This will also delete any points that are not finite (nan or inf).
40+
3941
The overall mask is calculated from any masks that are present.
4042
If a mask is found, any argument that does not have the same
4143
dimensions is left unchanged; therefore the argument list may
@@ -49,9 +51,11 @@ def delete_masked_points(*args):
4951
useful.
5052
"""
5153
masks = [ma.getmaskarray(x) for x in args if hasattr(x, 'mask')]
54+
isfinite = [np.isfinite(x) for x in args]
55+
masks.extend( [~x for x in isfinite if not isinstance(x,types.NotImplementedType)] )
5256
if len(masks) == 0:
5357
return args
54-
mask = reduce(ma.mask_or, masks)
58+
mask = reduce(np.logical_or, masks)
5559
margs = []
5660
for x in args:
5761
if (not is_string_like(x)

unit/axes_unit.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import unittest
2+
import numpy as np
3+
import matplotlib.axes as axes
4+
5+
class TestAxes(unittest.TestCase):
6+
def test_delete_masked_points_arrays(self):
7+
input = ( [1,2,3,np.nan,5],
8+
np.array((1,2,3,4,5)),
9+
)
10+
expected = [np.array((1,2,3,5))]*2
11+
actual = axes.delete_masked_points(*input)
12+
assert np.allclose(actual, expected)
13+
14+
input = ( np.ma.array( [1,2,3,4,5], mask=[False,False,False,True,False] ),
15+
np.array((1,2,3,4,5)),
16+
)
17+
expected = [np.array((1,2,3,5))]*2
18+
actual = axes.delete_masked_points(*input)
19+
assert np.allclose(actual, expected)
20+
21+
input = ( [1,2,3,np.nan,5],
22+
np.ma.array( [1,2,3,4,5], mask=[False,False,False,True,False] ),
23+
np.array((1,2,3,4,5)),
24+
)
25+
expected = [np.array((1,2,3,5))]*3
26+
actual = axes.delete_masked_points(*input)
27+
assert np.allclose(actual, expected)
28+
29+
input = ()
30+
expected = ()
31+
actual = axes.delete_masked_points(*input)
32+
assert np.allclose(actual, expected)
33+
34+
35+
input = ( [1,2,3,np.nan,5],
36+
)
37+
expected = [np.array((1,2,3,5))]*1
38+
actual = axes.delete_masked_points(*input)
39+
assert np.allclose(actual, expected)
40+
41+
input = ( np.array((1,2,3,4,5)),
42+
)
43+
expected = [np.array((1,2,3,4,5))]*1
44+
actual = axes.delete_masked_points(*input)
45+
assert np.allclose(actual, expected)
46+
47+
def test_delete_masked_points_strings(self):
48+
input = ( 'hello',
49+
)
50+
expected = ('hello',)
51+
actual = axes.delete_masked_points(*input)
52+
assert actual == expected
53+
54+
input = ( u'hello',
55+
)
56+
expected = (u'hello',)
57+
actual = axes.delete_masked_points(*input)
58+
assert actual == expected
59+
60+
61+
if __name__=='__main__':
62+
unittest.main()

0 commit comments

Comments
 (0)