##// END OF EJS Templates
Merge pull request #6071 from minrk/serialize-nan...
Thomas Kluyver -
r17149:639fc3ac merge
parent child Browse files
Show More
@@ -1,15 +1,7 b''
1 1 """test serialization tools"""
2 2
3 #-------------------------------------------------------------------------------
4 # Copyright (C) 2011 The IPython Development Team
5 #
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
8 #-------------------------------------------------------------------------------
9
10 #-------------------------------------------------------------------------------
11 # Imports
12 #-------------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
13 5
14 6 import pickle
15 7 from collections import namedtuple
@@ -43,10 +35,15 b' class C(object):'
43 35
44 36 SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
45 37 DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
38
46 39 #-------------------------------------------------------------------------------
47 40 # Tests
48 41 #-------------------------------------------------------------------------------
49 42
43 def new_array(shape, dtype):
44 import numpy
45 return numpy.random.random(shape).astype(dtype)
46
50 47 def test_roundtrip_simple():
51 48 for obj in [
52 49 'hello',
@@ -77,28 +74,13 b' def test_roundtrip_buffered():'
77 74 nt.assert_equal(remainder, [])
78 75 nt.assert_equal(obj, obj2)
79 76
80 def _scrub_nan(A):
81 """scrub nans out of empty arrays
82
83 since nan != nan
84 """
85 import numpy
86 if A.dtype.fields and A.shape:
87 for field in A.dtype.fields.keys():
88 try:
89 A[field][numpy.isnan(A[field])] = 0
90 except (TypeError, NotImplementedError):
91 # e.g. str dtype
92 pass
93
94 77 @dec.skip_without('numpy')
95 78 def test_numpy():
96 79 import numpy
97 80 from numpy.testing.utils import assert_array_equal
98 81 for shape in SHAPES:
99 82 for dtype in DTYPES:
100 A = numpy.empty(shape, dtype=dtype)
101 _scrub_nan(A)
83 A = new_array(shape, dtype=dtype)
102 84 bufs = serialize_object(A)
103 85 B, r = unserialize_object(bufs)
104 86 nt.assert_equal(r, [])
@@ -115,8 +97,7 b' def test_recarray():'
115 97 [('f', float), ('s', '|S10')],
116 98 [('n', int), ('s', '|S1'), ('u', 'uint32')],
117 99 ]:
118 A = numpy.empty(shape, dtype=dtype)
119 _scrub_nan(A)
100 A = new_array(shape, dtype=dtype)
120 101
121 102 bufs = serialize_object(A)
122 103 B, r = unserialize_object(bufs)
@@ -131,8 +112,7 b' def test_numpy_in_seq():'
131 112 from numpy.testing.utils import assert_array_equal
132 113 for shape in SHAPES:
133 114 for dtype in DTYPES:
134 A = numpy.empty(shape, dtype=dtype)
135 _scrub_nan(A)
115 A = new_array(shape, dtype=dtype)
136 116 bufs = serialize_object((A,1,2,b'hello'))
137 117 canned = pickle.loads(bufs[0])
138 118 nt.assert_is_instance(canned[0], CannedArray)
@@ -149,8 +129,7 b' def test_numpy_in_dict():'
149 129 from numpy.testing.utils import assert_array_equal
150 130 for shape in SHAPES:
151 131 for dtype in DTYPES:
152 A = numpy.empty(shape, dtype=dtype)
153 _scrub_nan(A)
132 A = new_array(shape, dtype=dtype)
154 133 bufs = serialize_object(dict(a=A,b=1,c=range(20)))
155 134 canned = pickle.loads(bufs[0])
156 135 nt.assert_is_instance(canned['a'], CannedArray)
General Comments 0
You need to be logged in to leave comments. Login now