Skip to content

Commit 5a751d4

Browse files
authored
Merge pull request #22 from 3ptscience/feat/removeEqualNan
Serialize doesn't use equal_nan from numpy 1.10
2 parents bbf063f + ae17965 commit 5a751d4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

properties/array.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ def serialize(self, data):
4343
"""Convert the array data to a serialized binary format"""
4444
if isinstance(data.flatten()[0], np.floating):
4545
use_dtype = '<f4'
46-
assert np.allclose(data.astype(use_dtype), data, equal_nan=True), \
46+
nan_mask = ~np.isnan(data)
47+
assert np.allclose(
48+
data.astype(use_dtype)[nan_mask], data[nan_mask]), \
4749
'Converting the type should not screw things up.'
4850
elif isinstance(data.flatten()[0], np.integer):
4951
use_dtype = '<i4'
@@ -98,15 +100,25 @@ def validator(self, instance, proposed):
98100
proposed = np.array(proposed)
99101
if (proposed.dtype.kind == 'i' and
100102
len(set(self.dtype).intersection(six.integer_types)) == 0):
101-
raise ValueError('{}: Array type must be int'.format(self.name))
103+
raise ValueError(
104+
'{name}: Array type must be {type}'.format(
105+
name=self.name,
106+
type=', '.join([str(t) for t in self.dtype])
107+
)
108+
)
102109
if proposed.dtype.kind == 'f' and float not in self.dtype:
103-
raise ValueError('{}: Array type must be '
104-
'float'.format(self.name))
110+
raise ValueError(
111+
'{name}: Array type must be {type}'.format(
112+
name=self.name,
113+
type=', '.join([str(t) for t in self.dtype])
114+
)
115+
)
105116
if len(self.shape) != proposed.ndim:
106117
raise ValueError(
107118
'{}: Array must have {:d} dimensions ''(shape: {})'.format(
108119
self.name, len(self.shape), self.shape
109-
))
120+
)
121+
)
110122
for i, s in enumerate(self.shape):
111123
if s != '*' and proposed.shape[i] != s:
112124
raise ValueError('{}: Array dimension {:d} must be '

tests/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ def test_array(self):
6565
assert isinstance(arrays.int_matrix, np.ndarray)
6666
assert arrays.int_matrix.dtype.kind == 'i'
6767

68+
def test_nan_array(self):
69+
arrays = MyClass()
70+
self.assertRaises(ValueError,
71+
lambda: setattr(arrays, 'int_array',
72+
[np.nan, 0, 2]))
73+
arrays.float_array = [np.nan, 0., 1]
74+
dat = arrays._properties['float_array'].serialize(arrays.float_array)
75+
6876
def test_array_init(self):
6977
def f(shape, dtype):
7078
class MyBadClass(properties.PropertyClass):

0 commit comments

Comments
 (0)