Chris@87
|
1 from __future__ import division, absolute_import, print_function
|
Chris@87
|
2
|
Chris@87
|
3 import sys
|
Chris@87
|
4 import platform
|
Chris@87
|
5 from decimal import Decimal
|
Chris@87
|
6 import warnings
|
Chris@87
|
7 import itertools
|
Chris@87
|
8 import platform
|
Chris@87
|
9
|
Chris@87
|
10 import numpy as np
|
Chris@87
|
11 from numpy.core import *
|
Chris@87
|
12 from numpy.core import umath
|
Chris@87
|
13 from numpy.random import rand, randint, randn
|
Chris@87
|
14 from numpy.testing import *
|
Chris@87
|
15 from numpy.core.multiarray import dot as dot_
|
Chris@87
|
16
|
Chris@87
|
17
|
Chris@87
|
18 class Vec(object):
|
Chris@87
|
19 def __init__(self,sequence=None):
|
Chris@87
|
20 if sequence is None:
|
Chris@87
|
21 sequence=[]
|
Chris@87
|
22 self.array=array(sequence)
|
Chris@87
|
23 def __add__(self, other):
|
Chris@87
|
24 out=Vec()
|
Chris@87
|
25 out.array=self.array+other.array
|
Chris@87
|
26 return out
|
Chris@87
|
27 def __sub__(self, other):
|
Chris@87
|
28 out=Vec()
|
Chris@87
|
29 out.array=self.array-other.array
|
Chris@87
|
30 return out
|
Chris@87
|
31 def __mul__(self, other): # with scalar
|
Chris@87
|
32 out=Vec(self.array.copy())
|
Chris@87
|
33 out.array*=other
|
Chris@87
|
34 return out
|
Chris@87
|
35 def __rmul__(self, other):
|
Chris@87
|
36 return self*other
|
Chris@87
|
37
|
Chris@87
|
38
|
Chris@87
|
39 class TestDot(TestCase):
|
Chris@87
|
40 def setUp(self):
|
Chris@87
|
41 self.A = rand(10, 8)
|
Chris@87
|
42 self.b1 = rand(8, 1)
|
Chris@87
|
43 self.b2 = rand(8)
|
Chris@87
|
44 self.b3 = rand(1, 8)
|
Chris@87
|
45 self.b4 = rand(10)
|
Chris@87
|
46 self.N = 14
|
Chris@87
|
47
|
Chris@87
|
48 def test_matmat(self):
|
Chris@87
|
49 A = self.A
|
Chris@87
|
50 c1 = dot(A.transpose(), A)
|
Chris@87
|
51 c2 = dot_(A.transpose(), A)
|
Chris@87
|
52 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
53
|
Chris@87
|
54 def test_matvec(self):
|
Chris@87
|
55 A, b1 = self.A, self.b1
|
Chris@87
|
56 c1 = dot(A, b1)
|
Chris@87
|
57 c2 = dot_(A, b1)
|
Chris@87
|
58 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
59
|
Chris@87
|
60 def test_matvec2(self):
|
Chris@87
|
61 A, b2 = self.A, self.b2
|
Chris@87
|
62 c1 = dot(A, b2)
|
Chris@87
|
63 c2 = dot_(A, b2)
|
Chris@87
|
64 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
65
|
Chris@87
|
66 def test_vecmat(self):
|
Chris@87
|
67 A, b4 = self.A, self.b4
|
Chris@87
|
68 c1 = dot(b4, A)
|
Chris@87
|
69 c2 = dot_(b4, A)
|
Chris@87
|
70 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
71
|
Chris@87
|
72 def test_vecmat2(self):
|
Chris@87
|
73 b3, A = self.b3, self.A
|
Chris@87
|
74 c1 = dot(b3, A.transpose())
|
Chris@87
|
75 c2 = dot_(b3, A.transpose())
|
Chris@87
|
76 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
77
|
Chris@87
|
78 def test_vecmat3(self):
|
Chris@87
|
79 A, b4 = self.A, self.b4
|
Chris@87
|
80 c1 = dot(A.transpose(), b4)
|
Chris@87
|
81 c2 = dot_(A.transpose(), b4)
|
Chris@87
|
82 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
83
|
Chris@87
|
84 def test_vecvecouter(self):
|
Chris@87
|
85 b1, b3 = self.b1, self.b3
|
Chris@87
|
86 c1 = dot(b1, b3)
|
Chris@87
|
87 c2 = dot_(b1, b3)
|
Chris@87
|
88 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
89
|
Chris@87
|
90 def test_vecvecinner(self):
|
Chris@87
|
91 b1, b3 = self.b1, self.b3
|
Chris@87
|
92 c1 = dot(b3, b1)
|
Chris@87
|
93 c2 = dot_(b3, b1)
|
Chris@87
|
94 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
95
|
Chris@87
|
96 def test_columnvect1(self):
|
Chris@87
|
97 b1 = ones((3, 1))
|
Chris@87
|
98 b2 = [5.3]
|
Chris@87
|
99 c1 = dot(b1, b2)
|
Chris@87
|
100 c2 = dot_(b1, b2)
|
Chris@87
|
101 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
102
|
Chris@87
|
103 def test_columnvect2(self):
|
Chris@87
|
104 b1 = ones((3, 1)).transpose()
|
Chris@87
|
105 b2 = [6.2]
|
Chris@87
|
106 c1 = dot(b2, b1)
|
Chris@87
|
107 c2 = dot_(b2, b1)
|
Chris@87
|
108 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
109
|
Chris@87
|
110 def test_vecscalar(self):
|
Chris@87
|
111 b1 = rand(1, 1)
|
Chris@87
|
112 b2 = rand(1, 8)
|
Chris@87
|
113 c1 = dot(b1, b2)
|
Chris@87
|
114 c2 = dot_(b1, b2)
|
Chris@87
|
115 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
116
|
Chris@87
|
117 def test_vecscalar2(self):
|
Chris@87
|
118 b1 = rand(8, 1)
|
Chris@87
|
119 b2 = rand(1, 1)
|
Chris@87
|
120 c1 = dot(b1, b2)
|
Chris@87
|
121 c2 = dot_(b1, b2)
|
Chris@87
|
122 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
123
|
Chris@87
|
124 def test_all(self):
|
Chris@87
|
125 dims = [(), (1,), (1, 1)]
|
Chris@87
|
126 for dim1 in dims:
|
Chris@87
|
127 for dim2 in dims:
|
Chris@87
|
128 arg1 = rand(*dim1)
|
Chris@87
|
129 arg2 = rand(*dim2)
|
Chris@87
|
130 c1 = dot(arg1, arg2)
|
Chris@87
|
131 c2 = dot_(arg1, arg2)
|
Chris@87
|
132 assert_(c1.shape == c2.shape)
|
Chris@87
|
133 assert_almost_equal(c1, c2, decimal=self.N)
|
Chris@87
|
134
|
Chris@87
|
135 def test_vecobject(self):
|
Chris@87
|
136 U_non_cont = transpose([[1., 1.], [1., 2.]])
|
Chris@87
|
137 U_cont = ascontiguousarray(U_non_cont)
|
Chris@87
|
138 x = array([Vec([1., 0.]), Vec([0., 1.])])
|
Chris@87
|
139 zeros = array([Vec([0., 0.]), Vec([0., 0.])])
|
Chris@87
|
140 zeros_test = dot(U_cont, x) - dot(U_non_cont, x)
|
Chris@87
|
141 assert_equal(zeros[0].array, zeros_test[0].array)
|
Chris@87
|
142 assert_equal(zeros[1].array, zeros_test[1].array)
|
Chris@87
|
143
|
Chris@87
|
144
|
Chris@87
|
145 class TestResize(TestCase):
|
Chris@87
|
146 def test_copies(self):
|
Chris@87
|
147 A = array([[1, 2], [3, 4]])
|
Chris@87
|
148 Ar1 = array([[1, 2, 3, 4], [1, 2, 3, 4]])
|
Chris@87
|
149 assert_equal(resize(A, (2, 4)), Ar1)
|
Chris@87
|
150
|
Chris@87
|
151 Ar2 = array([[1, 2], [3, 4], [1, 2], [3, 4]])
|
Chris@87
|
152 assert_equal(resize(A, (4, 2)), Ar2)
|
Chris@87
|
153
|
Chris@87
|
154 Ar3 = array([[1, 2, 3], [4, 1, 2], [3, 4, 1], [2, 3, 4]])
|
Chris@87
|
155 assert_equal(resize(A, (4, 3)), Ar3)
|
Chris@87
|
156
|
Chris@87
|
157 def test_zeroresize(self):
|
Chris@87
|
158 A = array([[1, 2], [3, 4]])
|
Chris@87
|
159 Ar = resize(A, (0,))
|
Chris@87
|
160 assert_equal(Ar, array([]))
|
Chris@87
|
161
|
Chris@87
|
162 class TestNonarrayArgs(TestCase):
|
Chris@87
|
163 # check that non-array arguments to functions wrap them in arrays
|
Chris@87
|
164 def test_squeeze(self):
|
Chris@87
|
165 A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]
|
Chris@87
|
166 assert_(squeeze(A).shape == (3, 3))
|
Chris@87
|
167
|
Chris@87
|
168 def test_cumproduct(self):
|
Chris@87
|
169 A = [[1, 2, 3], [4, 5, 6]]
|
Chris@87
|
170 assert_(all(cumproduct(A) == array([1, 2, 6, 24, 120, 720])))
|
Chris@87
|
171
|
Chris@87
|
172 def test_size(self):
|
Chris@87
|
173 A = [[1, 2, 3], [4, 5, 6]]
|
Chris@87
|
174 assert_(size(A) == 6)
|
Chris@87
|
175 assert_(size(A, 0) == 2)
|
Chris@87
|
176 assert_(size(A, 1) == 3)
|
Chris@87
|
177
|
Chris@87
|
178 def test_mean(self):
|
Chris@87
|
179 A = [[1, 2, 3], [4, 5, 6]]
|
Chris@87
|
180 assert_(mean(A) == 3.5)
|
Chris@87
|
181 assert_(all(mean(A, 0) == array([2.5, 3.5, 4.5])))
|
Chris@87
|
182 assert_(all(mean(A, 1) == array([2., 5.])))
|
Chris@87
|
183
|
Chris@87
|
184 with warnings.catch_warnings(record=True) as w:
|
Chris@87
|
185 warnings.filterwarnings('always', '', RuntimeWarning)
|
Chris@87
|
186 assert_(isnan(mean([])))
|
Chris@87
|
187 assert_(w[0].category is RuntimeWarning)
|
Chris@87
|
188
|
Chris@87
|
189 def test_std(self):
|
Chris@87
|
190 A = [[1, 2, 3], [4, 5, 6]]
|
Chris@87
|
191 assert_almost_equal(std(A), 1.707825127659933)
|
Chris@87
|
192 assert_almost_equal(std(A, 0), array([1.5, 1.5, 1.5]))
|
Chris@87
|
193 assert_almost_equal(std(A, 1), array([0.81649658, 0.81649658]))
|
Chris@87
|
194
|
Chris@87
|
195 with warnings.catch_warnings(record=True) as w:
|
Chris@87
|
196 warnings.filterwarnings('always', '', RuntimeWarning)
|
Chris@87
|
197 assert_(isnan(std([])))
|
Chris@87
|
198 assert_(w[0].category is RuntimeWarning)
|
Chris@87
|
199
|
Chris@87
|
200 def test_var(self):
|
Chris@87
|
201 A = [[1, 2, 3], [4, 5, 6]]
|
Chris@87
|
202 assert_almost_equal(var(A), 2.9166666666666665)
|
Chris@87
|
203 assert_almost_equal(var(A, 0), array([2.25, 2.25, 2.25]))
|
Chris@87
|
204 assert_almost_equal(var(A, 1), array([0.66666667, 0.66666667]))
|
Chris@87
|
205
|
Chris@87
|
206 with warnings.catch_warnings(record=True) as w:
|
Chris@87
|
207 warnings.filterwarnings('always', '', RuntimeWarning)
|
Chris@87
|
208 assert_(isnan(var([])))
|
Chris@87
|
209 assert_(w[0].category is RuntimeWarning)
|
Chris@87
|
210
|
Chris@87
|
211
|
Chris@87
|
212 class TestBoolScalar(TestCase):
|
Chris@87
|
213 def test_logical(self):
|
Chris@87
|
214 f = False_
|
Chris@87
|
215 t = True_
|
Chris@87
|
216 s = "xyz"
|
Chris@87
|
217 self.assertTrue((t and s) is s)
|
Chris@87
|
218 self.assertTrue((f and s) is f)
|
Chris@87
|
219
|
Chris@87
|
220 def test_bitwise_or(self):
|
Chris@87
|
221 f = False_
|
Chris@87
|
222 t = True_
|
Chris@87
|
223 self.assertTrue((t | t) is t)
|
Chris@87
|
224 self.assertTrue((f | t) is t)
|
Chris@87
|
225 self.assertTrue((t | f) is t)
|
Chris@87
|
226 self.assertTrue((f | f) is f)
|
Chris@87
|
227
|
Chris@87
|
228 def test_bitwise_and(self):
|
Chris@87
|
229 f = False_
|
Chris@87
|
230 t = True_
|
Chris@87
|
231 self.assertTrue((t & t) is t)
|
Chris@87
|
232 self.assertTrue((f & t) is f)
|
Chris@87
|
233 self.assertTrue((t & f) is f)
|
Chris@87
|
234 self.assertTrue((f & f) is f)
|
Chris@87
|
235
|
Chris@87
|
236 def test_bitwise_xor(self):
|
Chris@87
|
237 f = False_
|
Chris@87
|
238 t = True_
|
Chris@87
|
239 self.assertTrue((t ^ t) is f)
|
Chris@87
|
240 self.assertTrue((f ^ t) is t)
|
Chris@87
|
241 self.assertTrue((t ^ f) is t)
|
Chris@87
|
242 self.assertTrue((f ^ f) is f)
|
Chris@87
|
243
|
Chris@87
|
244
|
Chris@87
|
245 class TestBoolArray(TestCase):
|
Chris@87
|
246 def setUp(self):
|
Chris@87
|
247 # offset for simd tests
|
Chris@87
|
248 self.t = array([True] * 41, dtype=np.bool)[1::]
|
Chris@87
|
249 self.f = array([False] * 41, dtype=np.bool)[1::]
|
Chris@87
|
250 self.o = array([False] * 42, dtype=np.bool)[2::]
|
Chris@87
|
251 self.nm = self.f.copy()
|
Chris@87
|
252 self.im = self.t.copy()
|
Chris@87
|
253 self.nm[3] = True
|
Chris@87
|
254 self.nm[-2] = True
|
Chris@87
|
255 self.im[3] = False
|
Chris@87
|
256 self.im[-2] = False
|
Chris@87
|
257
|
Chris@87
|
258 def test_all_any(self):
|
Chris@87
|
259 self.assertTrue(self.t.all())
|
Chris@87
|
260 self.assertTrue(self.t.any())
|
Chris@87
|
261 self.assertFalse(self.f.all())
|
Chris@87
|
262 self.assertFalse(self.f.any())
|
Chris@87
|
263 self.assertTrue(self.nm.any())
|
Chris@87
|
264 self.assertTrue(self.im.any())
|
Chris@87
|
265 self.assertFalse(self.nm.all())
|
Chris@87
|
266 self.assertFalse(self.im.all())
|
Chris@87
|
267 # check bad element in all positions
|
Chris@87
|
268 for i in range(256 - 7):
|
Chris@87
|
269 d = array([False] * 256, dtype=np.bool)[7::]
|
Chris@87
|
270 d[i] = True
|
Chris@87
|
271 self.assertTrue(np.any(d))
|
Chris@87
|
272 e = array([True] * 256, dtype=np.bool)[7::]
|
Chris@87
|
273 e[i] = False
|
Chris@87
|
274 self.assertFalse(np.all(e))
|
Chris@87
|
275 assert_array_equal(e, ~d)
|
Chris@87
|
276 # big array test for blocked libc loops
|
Chris@87
|
277 for i in list(range(9, 6000, 507)) + [7764, 90021, -10]:
|
Chris@87
|
278 d = array([False] * 100043, dtype=np.bool)
|
Chris@87
|
279 d[i] = True
|
Chris@87
|
280 self.assertTrue(np.any(d), msg="%r" % i)
|
Chris@87
|
281 e = array([True] * 100043, dtype=np.bool)
|
Chris@87
|
282 e[i] = False
|
Chris@87
|
283 self.assertFalse(np.all(e), msg="%r" % i)
|
Chris@87
|
284
|
Chris@87
|
285 def test_logical_not_abs(self):
|
Chris@87
|
286 assert_array_equal(~self.t, self.f)
|
Chris@87
|
287 assert_array_equal(np.abs(~self.t), self.f)
|
Chris@87
|
288 assert_array_equal(np.abs(~self.f), self.t)
|
Chris@87
|
289 assert_array_equal(np.abs(self.f), self.f)
|
Chris@87
|
290 assert_array_equal(~np.abs(self.f), self.t)
|
Chris@87
|
291 assert_array_equal(~np.abs(self.t), self.f)
|
Chris@87
|
292 assert_array_equal(np.abs(~self.nm), self.im)
|
Chris@87
|
293 np.logical_not(self.t, out=self.o)
|
Chris@87
|
294 assert_array_equal(self.o, self.f)
|
Chris@87
|
295 np.abs(self.t, out=self.o)
|
Chris@87
|
296 assert_array_equal(self.o, self.t)
|
Chris@87
|
297
|
Chris@87
|
298 def test_logical_and_or_xor(self):
|
Chris@87
|
299 assert_array_equal(self.t | self.t, self.t)
|
Chris@87
|
300 assert_array_equal(self.f | self.f, self.f)
|
Chris@87
|
301 assert_array_equal(self.t | self.f, self.t)
|
Chris@87
|
302 assert_array_equal(self.f | self.t, self.t)
|
Chris@87
|
303 np.logical_or(self.t, self.t, out=self.o)
|
Chris@87
|
304 assert_array_equal(self.o, self.t)
|
Chris@87
|
305 assert_array_equal(self.t & self.t, self.t)
|
Chris@87
|
306 assert_array_equal(self.f & self.f, self.f)
|
Chris@87
|
307 assert_array_equal(self.t & self.f, self.f)
|
Chris@87
|
308 assert_array_equal(self.f & self.t, self.f)
|
Chris@87
|
309 np.logical_and(self.t, self.t, out=self.o)
|
Chris@87
|
310 assert_array_equal(self.o, self.t)
|
Chris@87
|
311 assert_array_equal(self.t ^ self.t, self.f)
|
Chris@87
|
312 assert_array_equal(self.f ^ self.f, self.f)
|
Chris@87
|
313 assert_array_equal(self.t ^ self.f, self.t)
|
Chris@87
|
314 assert_array_equal(self.f ^ self.t, self.t)
|
Chris@87
|
315 np.logical_xor(self.t, self.t, out=self.o)
|
Chris@87
|
316 assert_array_equal(self.o, self.f)
|
Chris@87
|
317
|
Chris@87
|
318 assert_array_equal(self.nm & self.t, self.nm)
|
Chris@87
|
319 assert_array_equal(self.im & self.f, False)
|
Chris@87
|
320 assert_array_equal(self.nm & True, self.nm)
|
Chris@87
|
321 assert_array_equal(self.im & False, self.f)
|
Chris@87
|
322 assert_array_equal(self.nm | self.t, self.t)
|
Chris@87
|
323 assert_array_equal(self.im | self.f, self.im)
|
Chris@87
|
324 assert_array_equal(self.nm | True, self.t)
|
Chris@87
|
325 assert_array_equal(self.im | False, self.im)
|
Chris@87
|
326 assert_array_equal(self.nm ^ self.t, self.im)
|
Chris@87
|
327 assert_array_equal(self.im ^ self.f, self.im)
|
Chris@87
|
328 assert_array_equal(self.nm ^ True, self.im)
|
Chris@87
|
329 assert_array_equal(self.im ^ False, self.im)
|
Chris@87
|
330
|
Chris@87
|
331
|
Chris@87
|
332 class TestBoolCmp(TestCase):
|
Chris@87
|
333 def setUp(self):
|
Chris@87
|
334 self.f = ones(256, dtype=np.float32)
|
Chris@87
|
335 self.ef = ones(self.f.size, dtype=np.bool)
|
Chris@87
|
336 self.d = ones(128, dtype=np.float64)
|
Chris@87
|
337 self.ed = ones(self.d.size, dtype=np.bool)
|
Chris@87
|
338 # generate values for all permutation of 256bit simd vectors
|
Chris@87
|
339 s = 0
|
Chris@87
|
340 for i in range(32):
|
Chris@87
|
341 self.f[s:s+8] = [i & 2**x for x in range(8)]
|
Chris@87
|
342 self.ef[s:s+8] = [(i & 2**x) != 0 for x in range(8)]
|
Chris@87
|
343 s += 8
|
Chris@87
|
344 s = 0
|
Chris@87
|
345 for i in range(16):
|
Chris@87
|
346 self.d[s:s+4] = [i & 2**x for x in range(4)]
|
Chris@87
|
347 self.ed[s:s+4] = [(i & 2**x) != 0 for x in range(4)]
|
Chris@87
|
348 s += 4
|
Chris@87
|
349
|
Chris@87
|
350 self.nf = self.f.copy()
|
Chris@87
|
351 self.nd = self.d.copy()
|
Chris@87
|
352 self.nf[self.ef] = np.nan
|
Chris@87
|
353 self.nd[self.ed] = np.nan
|
Chris@87
|
354
|
Chris@87
|
355 def test_float(self):
|
Chris@87
|
356 # offset for alignment test
|
Chris@87
|
357 for i in range(4):
|
Chris@87
|
358 assert_array_equal(self.f[i:] > 0, self.ef[i:])
|
Chris@87
|
359 assert_array_equal(self.f[i:] - 1 >= 0, self.ef[i:])
|
Chris@87
|
360 assert_array_equal(self.f[i:] == 0, ~self.ef[i:])
|
Chris@87
|
361 assert_array_equal(-self.f[i:] < 0, self.ef[i:])
|
Chris@87
|
362 assert_array_equal(-self.f[i:] + 1 <= 0, self.ef[i:])
|
Chris@87
|
363 r = self.f[i:] != 0
|
Chris@87
|
364 assert_array_equal(r, self.ef[i:])
|
Chris@87
|
365 r2 = self.f[i:] != np.zeros_like(self.f[i:])
|
Chris@87
|
366 r3 = 0 != self.f[i:]
|
Chris@87
|
367 assert_array_equal(r, r2)
|
Chris@87
|
368 assert_array_equal(r, r3)
|
Chris@87
|
369 # check bool == 0x1
|
Chris@87
|
370 assert_array_equal(r.view(np.int8), r.astype(np.int8))
|
Chris@87
|
371 assert_array_equal(r2.view(np.int8), r2.astype(np.int8))
|
Chris@87
|
372 assert_array_equal(r3.view(np.int8), r3.astype(np.int8))
|
Chris@87
|
373
|
Chris@87
|
374 # isnan on amd64 takes the same codepath
|
Chris@87
|
375 assert_array_equal(np.isnan(self.nf[i:]), self.ef[i:])
|
Chris@87
|
376
|
Chris@87
|
377 def test_double(self):
|
Chris@87
|
378 # offset for alignment test
|
Chris@87
|
379 for i in range(2):
|
Chris@87
|
380 assert_array_equal(self.d[i:] > 0, self.ed[i:])
|
Chris@87
|
381 assert_array_equal(self.d[i:] - 1 >= 0, self.ed[i:])
|
Chris@87
|
382 assert_array_equal(self.d[i:] == 0, ~self.ed[i:])
|
Chris@87
|
383 assert_array_equal(-self.d[i:] < 0, self.ed[i:])
|
Chris@87
|
384 assert_array_equal(-self.d[i:] + 1 <= 0, self.ed[i:])
|
Chris@87
|
385 r = self.d[i:] != 0
|
Chris@87
|
386 assert_array_equal(r, self.ed[i:])
|
Chris@87
|
387 r2 = self.d[i:] != np.zeros_like(self.d[i:])
|
Chris@87
|
388 r3 = 0 != self.d[i:]
|
Chris@87
|
389 assert_array_equal(r, r2)
|
Chris@87
|
390 assert_array_equal(r, r3)
|
Chris@87
|
391 # check bool == 0x1
|
Chris@87
|
392 assert_array_equal(r.view(np.int8), r.astype(np.int8))
|
Chris@87
|
393 assert_array_equal(r2.view(np.int8), r2.astype(np.int8))
|
Chris@87
|
394 assert_array_equal(r3.view(np.int8), r3.astype(np.int8))
|
Chris@87
|
395
|
Chris@87
|
396 # isnan on amd64 takes the same codepath
|
Chris@87
|
397 assert_array_equal(np.isnan(self.nd[i:]), self.ed[i:])
|
Chris@87
|
398
|
Chris@87
|
399
|
Chris@87
|
400 class TestSeterr(TestCase):
|
Chris@87
|
401 def test_default(self):
|
Chris@87
|
402 err = geterr()
|
Chris@87
|
403 self.assertEqual(err, dict(
|
Chris@87
|
404 divide='warn',
|
Chris@87
|
405 invalid='warn',
|
Chris@87
|
406 over='warn',
|
Chris@87
|
407 under='ignore',
|
Chris@87
|
408 ))
|
Chris@87
|
409
|
Chris@87
|
410 def test_set(self):
|
Chris@87
|
411 with np.errstate():
|
Chris@87
|
412 err = seterr()
|
Chris@87
|
413 old = seterr(divide='print')
|
Chris@87
|
414 self.assertTrue(err == old)
|
Chris@87
|
415 new = seterr()
|
Chris@87
|
416 self.assertTrue(new['divide'] == 'print')
|
Chris@87
|
417 seterr(over='raise')
|
Chris@87
|
418 self.assertTrue(geterr()['over'] == 'raise')
|
Chris@87
|
419 self.assertTrue(new['divide'] == 'print')
|
Chris@87
|
420 seterr(**old)
|
Chris@87
|
421 self.assertTrue(geterr() == old)
|
Chris@87
|
422
|
Chris@87
|
423 @dec.skipif(platform.machine() == "armv5tel", "See gh-413.")
|
Chris@87
|
424 def test_divide_err(self):
|
Chris@87
|
425 with errstate(divide='raise'):
|
Chris@87
|
426 try:
|
Chris@87
|
427 array([1.]) / array([0.])
|
Chris@87
|
428 except FloatingPointError:
|
Chris@87
|
429 pass
|
Chris@87
|
430 else:
|
Chris@87
|
431 self.fail()
|
Chris@87
|
432 seterr(divide='ignore')
|
Chris@87
|
433 array([1.]) / array([0.])
|
Chris@87
|
434
|
Chris@87
|
435 def test_errobj(self):
|
Chris@87
|
436 olderrobj = np.geterrobj()
|
Chris@87
|
437 self.called = 0
|
Chris@87
|
438 try:
|
Chris@87
|
439 with warnings.catch_warnings(record=True) as w:
|
Chris@87
|
440 warnings.simplefilter("always")
|
Chris@87
|
441 with errstate(divide='warn'):
|
Chris@87
|
442 np.seterrobj([20000, 1, None])
|
Chris@87
|
443 array([1.]) / array([0.])
|
Chris@87
|
444 self.assertEqual(len(w), 1)
|
Chris@87
|
445
|
Chris@87
|
446 def log_err(*args):
|
Chris@87
|
447 self.called += 1
|
Chris@87
|
448 extobj_err = args
|
Chris@87
|
449 assert (len(extobj_err) == 2)
|
Chris@87
|
450 assert ("divide" in extobj_err[0])
|
Chris@87
|
451
|
Chris@87
|
452 with errstate(divide='ignore'):
|
Chris@87
|
453 np.seterrobj([20000, 3, log_err])
|
Chris@87
|
454 array([1.]) / array([0.])
|
Chris@87
|
455 self.assertEqual(self.called, 1)
|
Chris@87
|
456
|
Chris@87
|
457 np.seterrobj(olderrobj)
|
Chris@87
|
458 with errstate(divide='ignore'):
|
Chris@87
|
459 np.divide(1., 0., extobj=[20000, 3, log_err])
|
Chris@87
|
460 self.assertEqual(self.called, 2)
|
Chris@87
|
461 finally:
|
Chris@87
|
462 np.seterrobj(olderrobj)
|
Chris@87
|
463 del self.called
|
Chris@87
|
464
|
Chris@87
|
465 def test_errobj_noerrmask(self):
|
Chris@87
|
466 # errmask = 0 has a special code path for the default
|
Chris@87
|
467 olderrobj = np.geterrobj()
|
Chris@87
|
468 try:
|
Chris@87
|
469 # set errobj to something non default
|
Chris@87
|
470 np.seterrobj([umath.UFUNC_BUFSIZE_DEFAULT,
|
Chris@87
|
471 umath.ERR_DEFAULT + 1, None])
|
Chris@87
|
472 #call a ufunc
|
Chris@87
|
473 np.isnan(np.array([6]))
|
Chris@87
|
474 # same with the default, lots of times to get rid of possible
|
Chris@87
|
475 # pre-existing stack in the code
|
Chris@87
|
476 for i in range(10000):
|
Chris@87
|
477 np.seterrobj([umath.UFUNC_BUFSIZE_DEFAULT, umath.ERR_DEFAULT,
|
Chris@87
|
478 None])
|
Chris@87
|
479 np.isnan(np.array([6]))
|
Chris@87
|
480 finally:
|
Chris@87
|
481 np.seterrobj(olderrobj)
|
Chris@87
|
482
|
Chris@87
|
483
|
Chris@87
|
484 class TestFloatExceptions(TestCase):
|
Chris@87
|
485 def assert_raises_fpe(self, fpeerr, flop, x, y):
|
Chris@87
|
486 ftype = type(x)
|
Chris@87
|
487 try:
|
Chris@87
|
488 flop(x, y)
|
Chris@87
|
489 assert_(False,
|
Chris@87
|
490 "Type %s did not raise fpe error '%s'." % (ftype, fpeerr))
|
Chris@87
|
491 except FloatingPointError as exc:
|
Chris@87
|
492 assert_(str(exc).find(fpeerr) >= 0,
|
Chris@87
|
493 "Type %s raised wrong fpe error '%s'." % (ftype, exc))
|
Chris@87
|
494
|
Chris@87
|
495 def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
|
Chris@87
|
496 # Check that fpe exception is raised.
|
Chris@87
|
497 #
|
Chris@87
|
498 # Given a floating operation `flop` and two scalar values, check that
|
Chris@87
|
499 # the operation raises the floating point exception specified by
|
Chris@87
|
500 #`fpeerr`. Tests all variants with 0-d array scalars as well.
|
Chris@87
|
501
|
Chris@87
|
502 self.assert_raises_fpe(fpeerr, flop, sc1, sc2);
|
Chris@87
|
503 self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2);
|
Chris@87
|
504 self.assert_raises_fpe(fpeerr, flop, sc1, sc2[()]);
|
Chris@87
|
505 self.assert_raises_fpe(fpeerr, flop, sc1[()], sc2[()]);
|
Chris@87
|
506
|
Chris@87
|
507 @dec.knownfailureif(True, "See ticket #2350")
|
Chris@87
|
508 def test_floating_exceptions(self):
|
Chris@87
|
509 # Test basic arithmetic function errors
|
Chris@87
|
510 with np.errstate(all='raise'):
|
Chris@87
|
511 # Test for all real and complex float types
|
Chris@87
|
512 for typecode in np.typecodes['AllFloat']:
|
Chris@87
|
513 ftype = np.obj2sctype(typecode)
|
Chris@87
|
514 if np.dtype(ftype).kind == 'f':
|
Chris@87
|
515 # Get some extreme values for the type
|
Chris@87
|
516 fi = np.finfo(ftype)
|
Chris@87
|
517 ft_tiny = fi.tiny
|
Chris@87
|
518 ft_max = fi.max
|
Chris@87
|
519 ft_eps = fi.eps
|
Chris@87
|
520 underflow = 'underflow'
|
Chris@87
|
521 divbyzero = 'divide by zero'
|
Chris@87
|
522 else:
|
Chris@87
|
523 # 'c', complex, corresponding real dtype
|
Chris@87
|
524 rtype = type(ftype(0).real)
|
Chris@87
|
525 fi = np.finfo(rtype)
|
Chris@87
|
526 ft_tiny = ftype(fi.tiny)
|
Chris@87
|
527 ft_max = ftype(fi.max)
|
Chris@87
|
528 ft_eps = ftype(fi.eps)
|
Chris@87
|
529 # The complex types raise different exceptions
|
Chris@87
|
530 underflow = ''
|
Chris@87
|
531 divbyzero = ''
|
Chris@87
|
532 overflow = 'overflow'
|
Chris@87
|
533 invalid = 'invalid'
|
Chris@87
|
534
|
Chris@87
|
535 self.assert_raises_fpe(underflow,
|
Chris@87
|
536 lambda a, b:a/b, ft_tiny, ft_max)
|
Chris@87
|
537 self.assert_raises_fpe(underflow,
|
Chris@87
|
538 lambda a, b:a*b, ft_tiny, ft_tiny)
|
Chris@87
|
539 self.assert_raises_fpe(overflow,
|
Chris@87
|
540 lambda a, b:a*b, ft_max, ftype(2))
|
Chris@87
|
541 self.assert_raises_fpe(overflow,
|
Chris@87
|
542 lambda a, b:a/b, ft_max, ftype(0.5))
|
Chris@87
|
543 self.assert_raises_fpe(overflow,
|
Chris@87
|
544 lambda a, b:a+b, ft_max, ft_max*ft_eps)
|
Chris@87
|
545 self.assert_raises_fpe(overflow,
|
Chris@87
|
546 lambda a, b:a-b, -ft_max, ft_max*ft_eps)
|
Chris@87
|
547 self.assert_raises_fpe(overflow,
|
Chris@87
|
548 np.power, ftype(2), ftype(2**fi.nexp))
|
Chris@87
|
549 self.assert_raises_fpe(divbyzero,
|
Chris@87
|
550 lambda a, b:a/b, ftype(1), ftype(0))
|
Chris@87
|
551 self.assert_raises_fpe(invalid,
|
Chris@87
|
552 lambda a, b:a/b, ftype(np.inf), ftype(np.inf))
|
Chris@87
|
553 self.assert_raises_fpe(invalid,
|
Chris@87
|
554 lambda a, b:a/b, ftype(0), ftype(0))
|
Chris@87
|
555 self.assert_raises_fpe(invalid,
|
Chris@87
|
556 lambda a, b:a-b, ftype(np.inf), ftype(np.inf))
|
Chris@87
|
557 self.assert_raises_fpe(invalid,
|
Chris@87
|
558 lambda a, b:a+b, ftype(np.inf), ftype(-np.inf))
|
Chris@87
|
559 self.assert_raises_fpe(invalid,
|
Chris@87
|
560 lambda a, b:a*b, ftype(0), ftype(np.inf))
|
Chris@87
|
561
|
Chris@87
|
562 def test_warnings(self):
|
Chris@87
|
563 # test warning code path
|
Chris@87
|
564 with warnings.catch_warnings(record=True) as w:
|
Chris@87
|
565 warnings.simplefilter("always")
|
Chris@87
|
566 with np.errstate(all="warn"):
|
Chris@87
|
567 np.divide(1, 0.)
|
Chris@87
|
568 self.assertEqual(len(w), 1)
|
Chris@87
|
569 self.assertTrue("divide by zero" in str(w[0].message))
|
Chris@87
|
570 np.array(1e300) * np.array(1e300)
|
Chris@87
|
571 self.assertEqual(len(w), 2)
|
Chris@87
|
572 self.assertTrue("overflow" in str(w[-1].message))
|
Chris@87
|
573 np.array(np.inf) - np.array(np.inf)
|
Chris@87
|
574 self.assertEqual(len(w), 3)
|
Chris@87
|
575 self.assertTrue("invalid value" in str(w[-1].message))
|
Chris@87
|
576 np.array(1e-300) * np.array(1e-300)
|
Chris@87
|
577 self.assertEqual(len(w), 4)
|
Chris@87
|
578 self.assertTrue("underflow" in str(w[-1].message))
|
Chris@87
|
579
|
Chris@87
|
580
|
Chris@87
|
581 class TestTypes(TestCase):
|
Chris@87
|
582 def check_promotion_cases(self, promote_func):
|
Chris@87
|
583 #Tests that the scalars get coerced correctly.
|
Chris@87
|
584 b = np.bool_(0)
|
Chris@87
|
585 i8, i16, i32, i64 = int8(0), int16(0), int32(0), int64(0)
|
Chris@87
|
586 u8, u16, u32, u64 = uint8(0), uint16(0), uint32(0), uint64(0)
|
Chris@87
|
587 f32, f64, fld = float32(0), float64(0), longdouble(0)
|
Chris@87
|
588 c64, c128, cld = complex64(0), complex128(0), clongdouble(0)
|
Chris@87
|
589
|
Chris@87
|
590 # coercion within the same kind
|
Chris@87
|
591 assert_equal(promote_func(i8, i16), np.dtype(int16))
|
Chris@87
|
592 assert_equal(promote_func(i32, i8), np.dtype(int32))
|
Chris@87
|
593 assert_equal(promote_func(i16, i64), np.dtype(int64))
|
Chris@87
|
594 assert_equal(promote_func(u8, u32), np.dtype(uint32))
|
Chris@87
|
595 assert_equal(promote_func(f32, f64), np.dtype(float64))
|
Chris@87
|
596 assert_equal(promote_func(fld, f32), np.dtype(longdouble))
|
Chris@87
|
597 assert_equal(promote_func(f64, fld), np.dtype(longdouble))
|
Chris@87
|
598 assert_equal(promote_func(c128, c64), np.dtype(complex128))
|
Chris@87
|
599 assert_equal(promote_func(cld, c128), np.dtype(clongdouble))
|
Chris@87
|
600 assert_equal(promote_func(c64, fld), np.dtype(clongdouble))
|
Chris@87
|
601
|
Chris@87
|
602 # coercion between kinds
|
Chris@87
|
603 assert_equal(promote_func(b, i32), np.dtype(int32))
|
Chris@87
|
604 assert_equal(promote_func(b, u8), np.dtype(uint8))
|
Chris@87
|
605 assert_equal(promote_func(i8, u8), np.dtype(int16))
|
Chris@87
|
606 assert_equal(promote_func(u8, i32), np.dtype(int32))
|
Chris@87
|
607 assert_equal(promote_func(i64, u32), np.dtype(int64))
|
Chris@87
|
608 assert_equal(promote_func(u64, i32), np.dtype(float64))
|
Chris@87
|
609 assert_equal(promote_func(i32, f32), np.dtype(float64))
|
Chris@87
|
610 assert_equal(promote_func(i64, f32), np.dtype(float64))
|
Chris@87
|
611 assert_equal(promote_func(f32, i16), np.dtype(float32))
|
Chris@87
|
612 assert_equal(promote_func(f32, u32), np.dtype(float64))
|
Chris@87
|
613 assert_equal(promote_func(f32, c64), np.dtype(complex64))
|
Chris@87
|
614 assert_equal(promote_func(c128, f32), np.dtype(complex128))
|
Chris@87
|
615 assert_equal(promote_func(cld, f64), np.dtype(clongdouble))
|
Chris@87
|
616
|
Chris@87
|
617 # coercion between scalars and 1-D arrays
|
Chris@87
|
618 assert_equal(promote_func(array([b]), i8), np.dtype(int8))
|
Chris@87
|
619 assert_equal(promote_func(array([b]), u8), np.dtype(uint8))
|
Chris@87
|
620 assert_equal(promote_func(array([b]), i32), np.dtype(int32))
|
Chris@87
|
621 assert_equal(promote_func(array([b]), u32), np.dtype(uint32))
|
Chris@87
|
622 assert_equal(promote_func(array([i8]), i64), np.dtype(int8))
|
Chris@87
|
623 assert_equal(promote_func(u64, array([i32])), np.dtype(int32))
|
Chris@87
|
624 assert_equal(promote_func(i64, array([u32])), np.dtype(uint32))
|
Chris@87
|
625 assert_equal(promote_func(int32(-1), array([u64])), np.dtype(float64))
|
Chris@87
|
626 assert_equal(promote_func(f64, array([f32])), np.dtype(float32))
|
Chris@87
|
627 assert_equal(promote_func(fld, array([f32])), np.dtype(float32))
|
Chris@87
|
628 assert_equal(promote_func(array([f64]), fld), np.dtype(float64))
|
Chris@87
|
629 assert_equal(promote_func(fld, array([c64])), np.dtype(complex64))
|
Chris@87
|
630 assert_equal(promote_func(c64, array([f64])), np.dtype(complex128))
|
Chris@87
|
631 assert_equal(promote_func(complex64(3j), array([f64])),
|
Chris@87
|
632 np.dtype(complex128))
|
Chris@87
|
633
|
Chris@87
|
634 # coercion between scalars and 1-D arrays, where
|
Chris@87
|
635 # the scalar has greater kind than the array
|
Chris@87
|
636 assert_equal(promote_func(array([b]), f64), np.dtype(float64))
|
Chris@87
|
637 assert_equal(promote_func(array([b]), i64), np.dtype(int64))
|
Chris@87
|
638 assert_equal(promote_func(array([b]), u64), np.dtype(uint64))
|
Chris@87
|
639 assert_equal(promote_func(array([i8]), f64), np.dtype(float64))
|
Chris@87
|
640 assert_equal(promote_func(array([u16]), f64), np.dtype(float64))
|
Chris@87
|
641
|
Chris@87
|
642 # uint and int are treated as the same "kind" for
|
Chris@87
|
643 # the purposes of array-scalar promotion.
|
Chris@87
|
644 assert_equal(promote_func(array([u16]), i32), np.dtype(uint16))
|
Chris@87
|
645
|
Chris@87
|
646 # float and complex are treated as the same "kind" for
|
Chris@87
|
647 # the purposes of array-scalar promotion, so that you can do
|
Chris@87
|
648 # (0j + float32array) to get a complex64 array instead of
|
Chris@87
|
649 # a complex128 array.
|
Chris@87
|
650 assert_equal(promote_func(array([f32]), c128), np.dtype(complex64))
|
Chris@87
|
651
|
Chris@87
|
652 def test_coercion(self):
|
Chris@87
|
653 def res_type(a, b):
|
Chris@87
|
654 return np.add(a, b).dtype
|
Chris@87
|
655 self.check_promotion_cases(res_type)
|
Chris@87
|
656
|
Chris@87
|
657 # Use-case: float/complex scalar * bool/int8 array
|
Chris@87
|
658 # shouldn't narrow the float/complex type
|
Chris@87
|
659 for a in [np.array([True, False]), np.array([-3, 12], dtype=np.int8)]:
|
Chris@87
|
660 b = 1.234 * a
|
Chris@87
|
661 assert_equal(b.dtype, np.dtype('f8'), "array type %s" % a.dtype)
|
Chris@87
|
662 b = np.longdouble(1.234) * a
|
Chris@87
|
663 assert_equal(b.dtype, np.dtype(np.longdouble),
|
Chris@87
|
664 "array type %s" % a.dtype)
|
Chris@87
|
665 b = np.float64(1.234) * a
|
Chris@87
|
666 assert_equal(b.dtype, np.dtype('f8'), "array type %s" % a.dtype)
|
Chris@87
|
667 b = np.float32(1.234) * a
|
Chris@87
|
668 assert_equal(b.dtype, np.dtype('f4'), "array type %s" % a.dtype)
|
Chris@87
|
669 b = np.float16(1.234) * a
|
Chris@87
|
670 assert_equal(b.dtype, np.dtype('f2'), "array type %s" % a.dtype)
|
Chris@87
|
671
|
Chris@87
|
672 b = 1.234j * a
|
Chris@87
|
673 assert_equal(b.dtype, np.dtype('c16'), "array type %s" % a.dtype)
|
Chris@87
|
674 b = np.clongdouble(1.234j) * a
|
Chris@87
|
675 assert_equal(b.dtype, np.dtype(np.clongdouble),
|
Chris@87
|
676 "array type %s" % a.dtype)
|
Chris@87
|
677 b = np.complex128(1.234j) * a
|
Chris@87
|
678 assert_equal(b.dtype, np.dtype('c16'), "array type %s" % a.dtype)
|
Chris@87
|
679 b = np.complex64(1.234j) * a
|
Chris@87
|
680 assert_equal(b.dtype, np.dtype('c8'), "array type %s" % a.dtype)
|
Chris@87
|
681
|
Chris@87
|
682 # The following use-case is problematic, and to resolve its
|
Chris@87
|
683 # tricky side-effects requires more changes.
|
Chris@87
|
684 #
|
Chris@87
|
685 ## Use-case: (1-t)*a, where 't' is a boolean array and 'a' is
|
Chris@87
|
686 ## a float32, shouldn't promote to float64
|
Chris@87
|
687 #a = np.array([1.0, 1.5], dtype=np.float32)
|
Chris@87
|
688 #t = np.array([True, False])
|
Chris@87
|
689 #b = t*a
|
Chris@87
|
690 #assert_equal(b, [1.0, 0.0])
|
Chris@87
|
691 #assert_equal(b.dtype, np.dtype('f4'))
|
Chris@87
|
692 #b = (1-t)*a
|
Chris@87
|
693 #assert_equal(b, [0.0, 1.5])
|
Chris@87
|
694 #assert_equal(b.dtype, np.dtype('f4'))
|
Chris@87
|
695 ## Probably ~t (bitwise negation) is more proper to use here,
|
Chris@87
|
696 ## but this is arguably less intuitive to understand at a glance, and
|
Chris@87
|
697 ## would fail if 't' is actually an integer array instead of boolean:
|
Chris@87
|
698 #b = (~t)*a
|
Chris@87
|
699 #assert_equal(b, [0.0, 1.5])
|
Chris@87
|
700 #assert_equal(b.dtype, np.dtype('f4'))
|
Chris@87
|
701
|
Chris@87
|
702 def test_result_type(self):
|
Chris@87
|
703 self.check_promotion_cases(np.result_type)
|
Chris@87
|
704 assert_(np.result_type(None) == np.dtype(None))
|
Chris@87
|
705
|
Chris@87
|
706 def test_promote_types_endian(self):
|
Chris@87
|
707 # promote_types should always return native-endian types
|
Chris@87
|
708 assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8'))
|
Chris@87
|
709 assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8'))
|
Chris@87
|
710
|
Chris@87
|
711 assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21'))
|
Chris@87
|
712 assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21'))
|
Chris@87
|
713 assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21'))
|
Chris@87
|
714 assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21'))
|
Chris@87
|
715
|
Chris@87
|
716 assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8'))
|
Chris@87
|
717 assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8'))
|
Chris@87
|
718 assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8'))
|
Chris@87
|
719 assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8'))
|
Chris@87
|
720 assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8'))
|
Chris@87
|
721 assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8'))
|
Chris@87
|
722
|
Chris@87
|
723 assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8'))
|
Chris@87
|
724 assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8'))
|
Chris@87
|
725 assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8'))
|
Chris@87
|
726 assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
|
Chris@87
|
727
|
Chris@87
|
728 def test_promote_types_strings(self):
|
Chris@87
|
729 assert_equal(np.promote_types('bool', 'S'), np.dtype('S5'))
|
Chris@87
|
730 assert_equal(np.promote_types('b', 'S'), np.dtype('S4'))
|
Chris@87
|
731 assert_equal(np.promote_types('u1', 'S'), np.dtype('S3'))
|
Chris@87
|
732 assert_equal(np.promote_types('u2', 'S'), np.dtype('S5'))
|
Chris@87
|
733 assert_equal(np.promote_types('u4', 'S'), np.dtype('S10'))
|
Chris@87
|
734 assert_equal(np.promote_types('u8', 'S'), np.dtype('S20'))
|
Chris@87
|
735 assert_equal(np.promote_types('i1', 'S'), np.dtype('S4'))
|
Chris@87
|
736 assert_equal(np.promote_types('i2', 'S'), np.dtype('S6'))
|
Chris@87
|
737 assert_equal(np.promote_types('i4', 'S'), np.dtype('S11'))
|
Chris@87
|
738 assert_equal(np.promote_types('i8', 'S'), np.dtype('S21'))
|
Chris@87
|
739 assert_equal(np.promote_types('bool', 'U'), np.dtype('U5'))
|
Chris@87
|
740 assert_equal(np.promote_types('b', 'U'), np.dtype('U4'))
|
Chris@87
|
741 assert_equal(np.promote_types('u1', 'U'), np.dtype('U3'))
|
Chris@87
|
742 assert_equal(np.promote_types('u2', 'U'), np.dtype('U5'))
|
Chris@87
|
743 assert_equal(np.promote_types('u4', 'U'), np.dtype('U10'))
|
Chris@87
|
744 assert_equal(np.promote_types('u8', 'U'), np.dtype('U20'))
|
Chris@87
|
745 assert_equal(np.promote_types('i1', 'U'), np.dtype('U4'))
|
Chris@87
|
746 assert_equal(np.promote_types('i2', 'U'), np.dtype('U6'))
|
Chris@87
|
747 assert_equal(np.promote_types('i4', 'U'), np.dtype('U11'))
|
Chris@87
|
748 assert_equal(np.promote_types('i8', 'U'), np.dtype('U21'))
|
Chris@87
|
749 assert_equal(np.promote_types('bool', 'S1'), np.dtype('S5'))
|
Chris@87
|
750 assert_equal(np.promote_types('bool', 'S30'), np.dtype('S30'))
|
Chris@87
|
751 assert_equal(np.promote_types('b', 'S1'), np.dtype('S4'))
|
Chris@87
|
752 assert_equal(np.promote_types('b', 'S30'), np.dtype('S30'))
|
Chris@87
|
753 assert_equal(np.promote_types('u1', 'S1'), np.dtype('S3'))
|
Chris@87
|
754 assert_equal(np.promote_types('u1', 'S30'), np.dtype('S30'))
|
Chris@87
|
755 assert_equal(np.promote_types('u2', 'S1'), np.dtype('S5'))
|
Chris@87
|
756 assert_equal(np.promote_types('u2', 'S30'), np.dtype('S30'))
|
Chris@87
|
757 assert_equal(np.promote_types('u4', 'S1'), np.dtype('S10'))
|
Chris@87
|
758 assert_equal(np.promote_types('u4', 'S30'), np.dtype('S30'))
|
Chris@87
|
759 assert_equal(np.promote_types('u8', 'S1'), np.dtype('S20'))
|
Chris@87
|
760 assert_equal(np.promote_types('u8', 'S30'), np.dtype('S30'))
|
Chris@87
|
761
|
Chris@87
|
762 def test_can_cast(self):
|
Chris@87
|
763 assert_(np.can_cast(np.int32, np.int64))
|
Chris@87
|
764 assert_(np.can_cast(np.float64, np.complex))
|
Chris@87
|
765 assert_(not np.can_cast(np.complex, np.float))
|
Chris@87
|
766
|
Chris@87
|
767 assert_(np.can_cast('i8', 'f8'))
|
Chris@87
|
768 assert_(not np.can_cast('i8', 'f4'))
|
Chris@87
|
769 assert_(np.can_cast('i4', 'S11'))
|
Chris@87
|
770
|
Chris@87
|
771 assert_(np.can_cast('i8', 'i8', 'no'))
|
Chris@87
|
772 assert_(not np.can_cast('<i8', '>i8', 'no'))
|
Chris@87
|
773
|
Chris@87
|
774 assert_(np.can_cast('<i8', '>i8', 'equiv'))
|
Chris@87
|
775 assert_(not np.can_cast('<i4', '>i8', 'equiv'))
|
Chris@87
|
776
|
Chris@87
|
777 assert_(np.can_cast('<i4', '>i8', 'safe'))
|
Chris@87
|
778 assert_(not np.can_cast('<i8', '>i4', 'safe'))
|
Chris@87
|
779
|
Chris@87
|
780 assert_(np.can_cast('<i8', '>i4', 'same_kind'))
|
Chris@87
|
781 assert_(not np.can_cast('<i8', '>u4', 'same_kind'))
|
Chris@87
|
782
|
Chris@87
|
783 assert_(np.can_cast('<i8', '>u4', 'unsafe'))
|
Chris@87
|
784
|
Chris@87
|
785 assert_(np.can_cast('bool', 'S5'))
|
Chris@87
|
786 assert_(not np.can_cast('bool', 'S4'))
|
Chris@87
|
787
|
Chris@87
|
788 assert_(np.can_cast('b', 'S4'))
|
Chris@87
|
789 assert_(not np.can_cast('b', 'S3'))
|
Chris@87
|
790
|
Chris@87
|
791 assert_(np.can_cast('u1', 'S3'))
|
Chris@87
|
792 assert_(not np.can_cast('u1', 'S2'))
|
Chris@87
|
793 assert_(np.can_cast('u2', 'S5'))
|
Chris@87
|
794 assert_(not np.can_cast('u2', 'S4'))
|
Chris@87
|
795 assert_(np.can_cast('u4', 'S10'))
|
Chris@87
|
796 assert_(not np.can_cast('u4', 'S9'))
|
Chris@87
|
797 assert_(np.can_cast('u8', 'S20'))
|
Chris@87
|
798 assert_(not np.can_cast('u8', 'S19'))
|
Chris@87
|
799
|
Chris@87
|
800 assert_(np.can_cast('i1', 'S4'))
|
Chris@87
|
801 assert_(not np.can_cast('i1', 'S3'))
|
Chris@87
|
802 assert_(np.can_cast('i2', 'S6'))
|
Chris@87
|
803 assert_(not np.can_cast('i2', 'S5'))
|
Chris@87
|
804 assert_(np.can_cast('i4', 'S11'))
|
Chris@87
|
805 assert_(not np.can_cast('i4', 'S10'))
|
Chris@87
|
806 assert_(np.can_cast('i8', 'S21'))
|
Chris@87
|
807 assert_(not np.can_cast('i8', 'S20'))
|
Chris@87
|
808
|
Chris@87
|
809 assert_(np.can_cast('bool', 'S5'))
|
Chris@87
|
810 assert_(not np.can_cast('bool', 'S4'))
|
Chris@87
|
811
|
Chris@87
|
812 assert_(np.can_cast('b', 'U4'))
|
Chris@87
|
813 assert_(not np.can_cast('b', 'U3'))
|
Chris@87
|
814
|
Chris@87
|
815 assert_(np.can_cast('u1', 'U3'))
|
Chris@87
|
816 assert_(not np.can_cast('u1', 'U2'))
|
Chris@87
|
817 assert_(np.can_cast('u2', 'U5'))
|
Chris@87
|
818 assert_(not np.can_cast('u2', 'U4'))
|
Chris@87
|
819 assert_(np.can_cast('u4', 'U10'))
|
Chris@87
|
820 assert_(not np.can_cast('u4', 'U9'))
|
Chris@87
|
821 assert_(np.can_cast('u8', 'U20'))
|
Chris@87
|
822 assert_(not np.can_cast('u8', 'U19'))
|
Chris@87
|
823
|
Chris@87
|
824 assert_(np.can_cast('i1', 'U4'))
|
Chris@87
|
825 assert_(not np.can_cast('i1', 'U3'))
|
Chris@87
|
826 assert_(np.can_cast('i2', 'U6'))
|
Chris@87
|
827 assert_(not np.can_cast('i2', 'U5'))
|
Chris@87
|
828 assert_(np.can_cast('i4', 'U11'))
|
Chris@87
|
829 assert_(not np.can_cast('i4', 'U10'))
|
Chris@87
|
830 assert_(np.can_cast('i8', 'U21'))
|
Chris@87
|
831 assert_(not np.can_cast('i8', 'U20'))
|
Chris@87
|
832
|
Chris@87
|
833 assert_raises(TypeError, np.can_cast, 'i4', None)
|
Chris@87
|
834 assert_raises(TypeError, np.can_cast, None, 'i4')
|
Chris@87
|
835
|
Chris@87
|
836
|
Chris@87
|
837 # Custom exception class to test exception propagation in fromiter
|
Chris@87
|
838 class NIterError(Exception): pass
|
Chris@87
|
839
|
Chris@87
|
840
|
Chris@87
|
841 class TestFromiter(TestCase):
|
Chris@87
|
842 def makegen(self):
|
Chris@87
|
843 for x in range(24):
|
Chris@87
|
844 yield x**2
|
Chris@87
|
845
|
Chris@87
|
846 def test_types(self):
|
Chris@87
|
847 ai32 = fromiter(self.makegen(), int32)
|
Chris@87
|
848 ai64 = fromiter(self.makegen(), int64)
|
Chris@87
|
849 af = fromiter(self.makegen(), float)
|
Chris@87
|
850 self.assertTrue(ai32.dtype == dtype(int32))
|
Chris@87
|
851 self.assertTrue(ai64.dtype == dtype(int64))
|
Chris@87
|
852 self.assertTrue(af.dtype == dtype(float))
|
Chris@87
|
853
|
Chris@87
|
854 def test_lengths(self):
|
Chris@87
|
855 expected = array(list(self.makegen()))
|
Chris@87
|
856 a = fromiter(self.makegen(), int)
|
Chris@87
|
857 a20 = fromiter(self.makegen(), int, 20)
|
Chris@87
|
858 self.assertTrue(len(a) == len(expected))
|
Chris@87
|
859 self.assertTrue(len(a20) == 20)
|
Chris@87
|
860 self.assertRaises(ValueError, fromiter,
|
Chris@87
|
861 self.makegen(), int, len(expected) + 10)
|
Chris@87
|
862
|
Chris@87
|
863 def test_values(self):
|
Chris@87
|
864 expected = array(list(self.makegen()))
|
Chris@87
|
865 a = fromiter(self.makegen(), int)
|
Chris@87
|
866 a20 = fromiter(self.makegen(), int, 20)
|
Chris@87
|
867 self.assertTrue(alltrue(a == expected, axis=0))
|
Chris@87
|
868 self.assertTrue(alltrue(a20 == expected[:20], axis=0))
|
Chris@87
|
869
|
Chris@87
|
870 def load_data(self, n, eindex):
|
Chris@87
|
871 # Utility method for the issue 2592 tests.
|
Chris@87
|
872 # Raise an exception at the desired index in the iterator.
|
Chris@87
|
873 for e in range(n):
|
Chris@87
|
874 if e == eindex:
|
Chris@87
|
875 raise NIterError('error at index %s' % eindex)
|
Chris@87
|
876 yield e
|
Chris@87
|
877
|
Chris@87
|
878 def test_2592(self):
|
Chris@87
|
879 # Test iteration exceptions are correctly raised.
|
Chris@87
|
880 count, eindex = 10, 5
|
Chris@87
|
881 self.assertRaises(NIterError, np.fromiter,
|
Chris@87
|
882 self.load_data(count, eindex), dtype=int, count=count)
|
Chris@87
|
883
|
Chris@87
|
884 def test_2592_edge(self):
|
Chris@87
|
885 # Test iter. exceptions, edge case (exception at end of iterator).
|
Chris@87
|
886 count = 10
|
Chris@87
|
887 eindex = count-1
|
Chris@87
|
888 self.assertRaises(NIterError, np.fromiter,
|
Chris@87
|
889 self.load_data(count, eindex), dtype=int, count=count)
|
Chris@87
|
890
|
Chris@87
|
891
|
Chris@87
|
892 class TestNonzero(TestCase):
|
Chris@87
|
893 def test_nonzero_trivial(self):
|
Chris@87
|
894 assert_equal(np.count_nonzero(array([])), 0)
|
Chris@87
|
895 assert_equal(np.count_nonzero(array([], dtype='?')), 0)
|
Chris@87
|
896 assert_equal(np.nonzero(array([])), ([],))
|
Chris@87
|
897
|
Chris@87
|
898 assert_equal(np.count_nonzero(array(0)), 0)
|
Chris@87
|
899 assert_equal(np.count_nonzero(array(0, dtype='?')), 0)
|
Chris@87
|
900 assert_equal(np.nonzero(array(0)), ([],))
|
Chris@87
|
901 assert_equal(np.count_nonzero(array(1)), 1)
|
Chris@87
|
902 assert_equal(np.count_nonzero(array(1, dtype='?')), 1)
|
Chris@87
|
903 assert_equal(np.nonzero(array(1)), ([0],))
|
Chris@87
|
904
|
Chris@87
|
905 def test_nonzero_onedim(self):
|
Chris@87
|
906 x = array([1, 0, 2, -1, 0, 0, 8])
|
Chris@87
|
907 assert_equal(np.count_nonzero(x), 4)
|
Chris@87
|
908 assert_equal(np.count_nonzero(x), 4)
|
Chris@87
|
909 assert_equal(np.nonzero(x), ([0, 2, 3, 6],))
|
Chris@87
|
910
|
Chris@87
|
911 x = array([(1, 2), (0, 0), (1, 1), (-1, 3), (0, 7)],
|
Chris@87
|
912 dtype=[('a', 'i4'), ('b', 'i2')])
|
Chris@87
|
913 assert_equal(np.count_nonzero(x['a']), 3)
|
Chris@87
|
914 assert_equal(np.count_nonzero(x['b']), 4)
|
Chris@87
|
915 assert_equal(np.nonzero(x['a']), ([0, 2, 3],))
|
Chris@87
|
916 assert_equal(np.nonzero(x['b']), ([0, 2, 3, 4],))
|
Chris@87
|
917
|
Chris@87
|
918 def test_nonzero_twodim(self):
|
Chris@87
|
919 x = array([[0, 1, 0], [2, 0, 3]])
|
Chris@87
|
920 assert_equal(np.count_nonzero(x), 3)
|
Chris@87
|
921 assert_equal(np.nonzero(x), ([0, 1, 1], [1, 0, 2]))
|
Chris@87
|
922
|
Chris@87
|
923 x = np.eye(3)
|
Chris@87
|
924 assert_equal(np.count_nonzero(x), 3)
|
Chris@87
|
925 assert_equal(np.nonzero(x), ([0, 1, 2], [0, 1, 2]))
|
Chris@87
|
926
|
Chris@87
|
927 x = array([[(0, 1), (0, 0), (1, 11)],
|
Chris@87
|
928 [(1, 1), (1, 0), (0, 0)],
|
Chris@87
|
929 [(0, 0), (1, 5), (0, 1)]], dtype=[('a', 'f4'), ('b', 'u1')])
|
Chris@87
|
930 assert_equal(np.count_nonzero(x['a']), 4)
|
Chris@87
|
931 assert_equal(np.count_nonzero(x['b']), 5)
|
Chris@87
|
932 assert_equal(np.nonzero(x['a']), ([0, 1, 1, 2], [2, 0, 1, 1]))
|
Chris@87
|
933 assert_equal(np.nonzero(x['b']), ([0, 0, 1, 2, 2], [0, 2, 0, 1, 2]))
|
Chris@87
|
934
|
Chris@87
|
935 assert_(not x['a'].T.flags.aligned)
|
Chris@87
|
936 assert_equal(np.count_nonzero(x['a'].T), 4)
|
Chris@87
|
937 assert_equal(np.count_nonzero(x['b'].T), 5)
|
Chris@87
|
938 assert_equal(np.nonzero(x['a'].T), ([0, 1, 1, 2], [1, 1, 2, 0]))
|
Chris@87
|
939 assert_equal(np.nonzero(x['b'].T), ([0, 0, 1, 2, 2], [0, 1, 2, 0, 2]))
|
Chris@87
|
940
|
Chris@87
|
941 def test_sparse(self):
|
Chris@87
|
942 # test special sparse condition boolean code path
|
Chris@87
|
943 for i in range(20):
|
Chris@87
|
944 c = np.zeros(200, dtype=np.bool)
|
Chris@87
|
945 c[i::20] = True
|
Chris@87
|
946 assert_equal(np.nonzero(c)[0], np.arange(i, 200 + i, 20))
|
Chris@87
|
947
|
Chris@87
|
948 c = np.zeros(400, dtype=np.bool)
|
Chris@87
|
949 c[10 + i:20 + i] = True
|
Chris@87
|
950 c[20 + i*2] = True
|
Chris@87
|
951 assert_equal(np.nonzero(c)[0],
|
Chris@87
|
952 np.concatenate((np.arange(10 +i, 20 + i), [20 +i*2])))
|
Chris@87
|
953
|
Chris@87
|
954
|
Chris@87
|
955 class TestIndex(TestCase):
|
Chris@87
|
956 def test_boolean(self):
|
Chris@87
|
957 a = rand(3, 5, 8)
|
Chris@87
|
958 V = rand(5, 8)
|
Chris@87
|
959 g1 = randint(0, 5, size=15)
|
Chris@87
|
960 g2 = randint(0, 8, size=15)
|
Chris@87
|
961 V[g1, g2] = -V[g1, g2]
|
Chris@87
|
962 assert_((array([a[0][V>0], a[1][V>0], a[2][V>0]]) == a[:, V>0]).all())
|
Chris@87
|
963
|
Chris@87
|
964 def test_boolean_edgecase(self):
|
Chris@87
|
965 a = np.array([], dtype='int32')
|
Chris@87
|
966 b = np.array([], dtype='bool')
|
Chris@87
|
967 c = a[b]
|
Chris@87
|
968 assert_equal(c, [])
|
Chris@87
|
969 assert_equal(c.dtype, np.dtype('int32'))
|
Chris@87
|
970
|
Chris@87
|
971
|
Chris@87
|
972 class TestBinaryRepr(TestCase):
|
Chris@87
|
973 def test_zero(self):
|
Chris@87
|
974 assert_equal(binary_repr(0), '0')
|
Chris@87
|
975
|
Chris@87
|
976 def test_large(self):
|
Chris@87
|
977 assert_equal(binary_repr(10736848), '101000111101010011010000')
|
Chris@87
|
978
|
Chris@87
|
979 def test_negative(self):
|
Chris@87
|
980 assert_equal(binary_repr(-1), '-1')
|
Chris@87
|
981 assert_equal(binary_repr(-1, width=8), '11111111')
|
Chris@87
|
982
|
Chris@87
|
983 class TestBaseRepr(TestCase):
|
Chris@87
|
984 def test_base3(self):
|
Chris@87
|
985 assert_equal(base_repr(3**5, 3), '100000')
|
Chris@87
|
986
|
Chris@87
|
987 def test_positive(self):
|
Chris@87
|
988 assert_equal(base_repr(12, 10), '12')
|
Chris@87
|
989 assert_equal(base_repr(12, 10, 4), '000012')
|
Chris@87
|
990 assert_equal(base_repr(12, 4), '30')
|
Chris@87
|
991 assert_equal(base_repr(3731624803700888, 36), '10QR0ROFCEW')
|
Chris@87
|
992
|
Chris@87
|
993 def test_negative(self):
|
Chris@87
|
994 assert_equal(base_repr(-12, 10), '-12')
|
Chris@87
|
995 assert_equal(base_repr(-12, 10, 4), '-000012')
|
Chris@87
|
996 assert_equal(base_repr(-12, 4), '-30')
|
Chris@87
|
997
|
Chris@87
|
998 class TestArrayComparisons(TestCase):
|
Chris@87
|
999 def test_array_equal(self):
|
Chris@87
|
1000 res = array_equal(array([1, 2]), array([1, 2]))
|
Chris@87
|
1001 assert_(res)
|
Chris@87
|
1002 assert_(type(res) is bool)
|
Chris@87
|
1003 res = array_equal(array([1, 2]), array([1, 2, 3]))
|
Chris@87
|
1004 assert_(not res)
|
Chris@87
|
1005 assert_(type(res) is bool)
|
Chris@87
|
1006 res = array_equal(array([1, 2]), array([3, 4]))
|
Chris@87
|
1007 assert_(not res)
|
Chris@87
|
1008 assert_(type(res) is bool)
|
Chris@87
|
1009 res = array_equal(array([1, 2]), array([1, 3]))
|
Chris@87
|
1010 assert_(not res)
|
Chris@87
|
1011 assert_(type(res) is bool)
|
Chris@87
|
1012 res = array_equal(array(['a'], dtype='S1'), array(['a'], dtype='S1'))
|
Chris@87
|
1013 assert_(res)
|
Chris@87
|
1014 assert_(type(res) is bool)
|
Chris@87
|
1015 res = array_equal(array([('a', 1)], dtype='S1,u4'), array([('a', 1)], dtype='S1,u4'))
|
Chris@87
|
1016 assert_(res)
|
Chris@87
|
1017 assert_(type(res) is bool)
|
Chris@87
|
1018
|
Chris@87
|
1019 def test_array_equiv(self):
|
Chris@87
|
1020 res = array_equiv(array([1, 2]), array([1, 2]))
|
Chris@87
|
1021 assert_(res)
|
Chris@87
|
1022 assert_(type(res) is bool)
|
Chris@87
|
1023 res = array_equiv(array([1, 2]), array([1, 2, 3]))
|
Chris@87
|
1024 assert_(not res)
|
Chris@87
|
1025 assert_(type(res) is bool)
|
Chris@87
|
1026 res = array_equiv(array([1, 2]), array([3, 4]))
|
Chris@87
|
1027 assert_(not res)
|
Chris@87
|
1028 assert_(type(res) is bool)
|
Chris@87
|
1029 res = array_equiv(array([1, 2]), array([1, 3]))
|
Chris@87
|
1030 assert_(not res)
|
Chris@87
|
1031 assert_(type(res) is bool)
|
Chris@87
|
1032
|
Chris@87
|
1033 res = array_equiv(array([1, 1]), array([1]))
|
Chris@87
|
1034 assert_(res)
|
Chris@87
|
1035 assert_(type(res) is bool)
|
Chris@87
|
1036 res = array_equiv(array([1, 1]), array([[1], [1]]))
|
Chris@87
|
1037 assert_(res)
|
Chris@87
|
1038 assert_(type(res) is bool)
|
Chris@87
|
1039 res = array_equiv(array([1, 2]), array([2]))
|
Chris@87
|
1040 assert_(not res)
|
Chris@87
|
1041 assert_(type(res) is bool)
|
Chris@87
|
1042 res = array_equiv(array([1, 2]), array([[1], [2]]))
|
Chris@87
|
1043 assert_(not res)
|
Chris@87
|
1044 assert_(type(res) is bool)
|
Chris@87
|
1045 res = array_equiv(array([1, 2]), array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
|
Chris@87
|
1046 assert_(not res)
|
Chris@87
|
1047 assert_(type(res) is bool)
|
Chris@87
|
1048
|
Chris@87
|
1049
|
Chris@87
|
1050 def assert_array_strict_equal(x, y):
|
Chris@87
|
1051 assert_array_equal(x, y)
|
Chris@87
|
1052 # Check flags, 32 bit arches typically don't provide 16 byte alignment
|
Chris@87
|
1053 if ((x.dtype.alignment <= 8 or
|
Chris@87
|
1054 np.intp().dtype.itemsize != 4) and
|
Chris@87
|
1055 sys.platform != 'win32'):
|
Chris@87
|
1056 assert_(x.flags == y.flags)
|
Chris@87
|
1057 else:
|
Chris@87
|
1058 assert_(x.flags.owndata == y.flags.owndata)
|
Chris@87
|
1059 assert_(x.flags.writeable == y.flags.writeable)
|
Chris@87
|
1060 assert_(x.flags.c_contiguous == y.flags.c_contiguous)
|
Chris@87
|
1061 assert_(x.flags.f_contiguous == y.flags.f_contiguous)
|
Chris@87
|
1062 assert_(x.flags.updateifcopy == y.flags.updateifcopy)
|
Chris@87
|
1063 # check endianness
|
Chris@87
|
1064 assert_(x.dtype.isnative == y.dtype.isnative)
|
Chris@87
|
1065
|
Chris@87
|
1066
|
Chris@87
|
1067 class TestClip(TestCase):
|
Chris@87
|
1068 def setUp(self):
|
Chris@87
|
1069 self.nr = 5
|
Chris@87
|
1070 self.nc = 3
|
Chris@87
|
1071
|
Chris@87
|
1072 def fastclip(self, a, m, M, out=None):
|
Chris@87
|
1073 if out is None:
|
Chris@87
|
1074 return a.clip(m, M)
|
Chris@87
|
1075 else:
|
Chris@87
|
1076 return a.clip(m, M, out)
|
Chris@87
|
1077
|
Chris@87
|
1078 def clip(self, a, m, M, out=None):
|
Chris@87
|
1079 # use slow-clip
|
Chris@87
|
1080 selector = less(a, m)+2*greater(a, M)
|
Chris@87
|
1081 return selector.choose((a, m, M), out=out)
|
Chris@87
|
1082
|
Chris@87
|
1083 # Handy functions
|
Chris@87
|
1084 def _generate_data(self, n, m):
|
Chris@87
|
1085 return randn(n, m)
|
Chris@87
|
1086
|
Chris@87
|
1087 def _generate_data_complex(self, n, m):
|
Chris@87
|
1088 return randn(n, m) + 1.j *rand(n, m)
|
Chris@87
|
1089
|
Chris@87
|
1090 def _generate_flt_data(self, n, m):
|
Chris@87
|
1091 return (randn(n, m)).astype(float32)
|
Chris@87
|
1092
|
Chris@87
|
1093 def _neg_byteorder(self, a):
|
Chris@87
|
1094 a = asarray(a)
|
Chris@87
|
1095 if sys.byteorder == 'little':
|
Chris@87
|
1096 a = a.astype(a.dtype.newbyteorder('>'))
|
Chris@87
|
1097 else:
|
Chris@87
|
1098 a = a.astype(a.dtype.newbyteorder('<'))
|
Chris@87
|
1099 return a
|
Chris@87
|
1100
|
Chris@87
|
1101 def _generate_non_native_data(self, n, m):
|
Chris@87
|
1102 data = randn(n, m)
|
Chris@87
|
1103 data = self._neg_byteorder(data)
|
Chris@87
|
1104 assert_(not data.dtype.isnative)
|
Chris@87
|
1105 return data
|
Chris@87
|
1106
|
Chris@87
|
1107 def _generate_int_data(self, n, m):
|
Chris@87
|
1108 return (10 * rand(n, m)).astype(int64)
|
Chris@87
|
1109
|
Chris@87
|
1110 def _generate_int32_data(self, n, m):
|
Chris@87
|
1111 return (10 * rand(n, m)).astype(int32)
|
Chris@87
|
1112
|
Chris@87
|
1113 # Now the real test cases
|
Chris@87
|
1114 def test_simple_double(self):
|
Chris@87
|
1115 #Test native double input with scalar min/max.
|
Chris@87
|
1116 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1117 m = 0.1
|
Chris@87
|
1118 M = 0.6
|
Chris@87
|
1119 ac = self.fastclip(a, m, M)
|
Chris@87
|
1120 act = self.clip(a, m, M)
|
Chris@87
|
1121 assert_array_strict_equal(ac, act)
|
Chris@87
|
1122
|
Chris@87
|
1123 def test_simple_int(self):
|
Chris@87
|
1124 #Test native int input with scalar min/max.
|
Chris@87
|
1125 a = self._generate_int_data(self.nr, self.nc)
|
Chris@87
|
1126 a = a.astype(int)
|
Chris@87
|
1127 m = -2
|
Chris@87
|
1128 M = 4
|
Chris@87
|
1129 ac = self.fastclip(a, m, M)
|
Chris@87
|
1130 act = self.clip(a, m, M)
|
Chris@87
|
1131 assert_array_strict_equal(ac, act)
|
Chris@87
|
1132
|
Chris@87
|
1133 def test_array_double(self):
|
Chris@87
|
1134 #Test native double input with array min/max.
|
Chris@87
|
1135 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1136 m = zeros(a.shape)
|
Chris@87
|
1137 M = m + 0.5
|
Chris@87
|
1138 ac = self.fastclip(a, m, M)
|
Chris@87
|
1139 act = self.clip(a, m, M)
|
Chris@87
|
1140 assert_array_strict_equal(ac, act)
|
Chris@87
|
1141
|
Chris@87
|
1142 def test_simple_nonnative(self):
|
Chris@87
|
1143 #Test non native double input with scalar min/max.
|
Chris@87
|
1144 #Test native double input with non native double scalar min/max.
|
Chris@87
|
1145 a = self._generate_non_native_data(self.nr, self.nc)
|
Chris@87
|
1146 m = -0.5
|
Chris@87
|
1147 M = 0.6
|
Chris@87
|
1148 ac = self.fastclip(a, m, M)
|
Chris@87
|
1149 act = self.clip(a, m, M)
|
Chris@87
|
1150 assert_array_equal(ac, act)
|
Chris@87
|
1151
|
Chris@87
|
1152 #Test native double input with non native double scalar min/max.
|
Chris@87
|
1153 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1154 m = -0.5
|
Chris@87
|
1155 M = self._neg_byteorder(0.6)
|
Chris@87
|
1156 assert_(not M.dtype.isnative)
|
Chris@87
|
1157 ac = self.fastclip(a, m, M)
|
Chris@87
|
1158 act = self.clip(a, m, M)
|
Chris@87
|
1159 assert_array_equal(ac, act)
|
Chris@87
|
1160
|
Chris@87
|
1161 def test_simple_complex(self):
|
Chris@87
|
1162 #Test native complex input with native double scalar min/max.
|
Chris@87
|
1163 #Test native input with complex double scalar min/max.
|
Chris@87
|
1164 a = 3 * self._generate_data_complex(self.nr, self.nc)
|
Chris@87
|
1165 m = -0.5
|
Chris@87
|
1166 M = 1.
|
Chris@87
|
1167 ac = self.fastclip(a, m, M)
|
Chris@87
|
1168 act = self.clip(a, m, M)
|
Chris@87
|
1169 assert_array_strict_equal(ac, act)
|
Chris@87
|
1170
|
Chris@87
|
1171 #Test native input with complex double scalar min/max.
|
Chris@87
|
1172 a = 3 * self._generate_data(self.nr, self.nc)
|
Chris@87
|
1173 m = -0.5 + 1.j
|
Chris@87
|
1174 M = 1. + 2.j
|
Chris@87
|
1175 ac = self.fastclip(a, m, M)
|
Chris@87
|
1176 act = self.clip(a, m, M)
|
Chris@87
|
1177 assert_array_strict_equal(ac, act)
|
Chris@87
|
1178
|
Chris@87
|
1179 def test_clip_non_contig(self):
|
Chris@87
|
1180 #Test clip for non contiguous native input and native scalar min/max.
|
Chris@87
|
1181 a = self._generate_data(self.nr * 2, self.nc * 3)
|
Chris@87
|
1182 a = a[::2, ::3]
|
Chris@87
|
1183 assert_(not a.flags['F_CONTIGUOUS'])
|
Chris@87
|
1184 assert_(not a.flags['C_CONTIGUOUS'])
|
Chris@87
|
1185 ac = self.fastclip(a, -1.6, 1.7)
|
Chris@87
|
1186 act = self.clip(a, -1.6, 1.7)
|
Chris@87
|
1187 assert_array_strict_equal(ac, act)
|
Chris@87
|
1188
|
Chris@87
|
1189 def test_simple_out(self):
|
Chris@87
|
1190 #Test native double input with scalar min/max.
|
Chris@87
|
1191 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1192 m = -0.5
|
Chris@87
|
1193 M = 0.6
|
Chris@87
|
1194 ac = zeros(a.shape)
|
Chris@87
|
1195 act = zeros(a.shape)
|
Chris@87
|
1196 self.fastclip(a, m, M, ac)
|
Chris@87
|
1197 self.clip(a, m, M, act)
|
Chris@87
|
1198 assert_array_strict_equal(ac, act)
|
Chris@87
|
1199
|
Chris@87
|
1200 def test_simple_int32_inout(self):
|
Chris@87
|
1201 #Test native int32 input with double min/max and int32 out.
|
Chris@87
|
1202 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1203 m = float64(0)
|
Chris@87
|
1204 M = float64(2)
|
Chris@87
|
1205 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1206 act = ac.copy()
|
Chris@87
|
1207 self.fastclip(a, m, M, ac)
|
Chris@87
|
1208 self.clip(a, m, M, act)
|
Chris@87
|
1209 assert_array_strict_equal(ac, act)
|
Chris@87
|
1210
|
Chris@87
|
1211 def test_simple_int64_out(self):
|
Chris@87
|
1212 #Test native int32 input with int32 scalar min/max and int64 out.
|
Chris@87
|
1213 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1214 m = int32(-1)
|
Chris@87
|
1215 M = int32(1)
|
Chris@87
|
1216 ac = zeros(a.shape, dtype = int64)
|
Chris@87
|
1217 act = ac.copy()
|
Chris@87
|
1218 self.fastclip(a, m, M, ac)
|
Chris@87
|
1219 self.clip(a, m, M, act)
|
Chris@87
|
1220 assert_array_strict_equal(ac, act)
|
Chris@87
|
1221
|
Chris@87
|
1222 def test_simple_int64_inout(self):
|
Chris@87
|
1223 #Test native int32 input with double array min/max and int32 out.
|
Chris@87
|
1224 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1225 m = zeros(a.shape, float64)
|
Chris@87
|
1226 M = float64(1)
|
Chris@87
|
1227 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1228 act = ac.copy()
|
Chris@87
|
1229 self.fastclip(a, m, M, ac)
|
Chris@87
|
1230 self.clip(a, m, M, act)
|
Chris@87
|
1231 assert_array_strict_equal(ac, act)
|
Chris@87
|
1232
|
Chris@87
|
1233 def test_simple_int32_out(self):
|
Chris@87
|
1234 #Test native double input with scalar min/max and int out.
|
Chris@87
|
1235 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1236 m = -1.0
|
Chris@87
|
1237 M = 2.0
|
Chris@87
|
1238 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1239 act = ac.copy()
|
Chris@87
|
1240 self.fastclip(a, m, M, ac)
|
Chris@87
|
1241 self.clip(a, m, M, act)
|
Chris@87
|
1242 assert_array_strict_equal(ac, act)
|
Chris@87
|
1243
|
Chris@87
|
1244 def test_simple_inplace_01(self):
|
Chris@87
|
1245 #Test native double input with array min/max in-place.
|
Chris@87
|
1246 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1247 ac = a.copy()
|
Chris@87
|
1248 m = zeros(a.shape)
|
Chris@87
|
1249 M = 1.0
|
Chris@87
|
1250 self.fastclip(a, m, M, a)
|
Chris@87
|
1251 self.clip(a, m, M, ac)
|
Chris@87
|
1252 assert_array_strict_equal(a, ac)
|
Chris@87
|
1253
|
Chris@87
|
1254 def test_simple_inplace_02(self):
|
Chris@87
|
1255 #Test native double input with scalar min/max in-place.
|
Chris@87
|
1256 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1257 ac = a.copy()
|
Chris@87
|
1258 m = -0.5
|
Chris@87
|
1259 M = 0.6
|
Chris@87
|
1260 self.fastclip(a, m, M, a)
|
Chris@87
|
1261 self.clip(a, m, M, ac)
|
Chris@87
|
1262 assert_array_strict_equal(a, ac)
|
Chris@87
|
1263
|
Chris@87
|
1264 def test_noncontig_inplace(self):
|
Chris@87
|
1265 #Test non contiguous double input with double scalar min/max in-place.
|
Chris@87
|
1266 a = self._generate_data(self.nr * 2, self.nc * 3)
|
Chris@87
|
1267 a = a[::2, ::3]
|
Chris@87
|
1268 assert_(not a.flags['F_CONTIGUOUS'])
|
Chris@87
|
1269 assert_(not a.flags['C_CONTIGUOUS'])
|
Chris@87
|
1270 ac = a.copy()
|
Chris@87
|
1271 m = -0.5
|
Chris@87
|
1272 M = 0.6
|
Chris@87
|
1273 self.fastclip(a, m, M, a)
|
Chris@87
|
1274 self.clip(a, m, M, ac)
|
Chris@87
|
1275 assert_array_equal(a, ac)
|
Chris@87
|
1276
|
Chris@87
|
1277 def test_type_cast_01(self):
|
Chris@87
|
1278 #Test native double input with scalar min/max.
|
Chris@87
|
1279 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1280 m = -0.5
|
Chris@87
|
1281 M = 0.6
|
Chris@87
|
1282 ac = self.fastclip(a, m, M)
|
Chris@87
|
1283 act = self.clip(a, m, M)
|
Chris@87
|
1284 assert_array_strict_equal(ac, act)
|
Chris@87
|
1285
|
Chris@87
|
1286 def test_type_cast_02(self):
|
Chris@87
|
1287 #Test native int32 input with int32 scalar min/max.
|
Chris@87
|
1288 a = self._generate_int_data(self.nr, self.nc)
|
Chris@87
|
1289 a = a.astype(int32)
|
Chris@87
|
1290 m = -2
|
Chris@87
|
1291 M = 4
|
Chris@87
|
1292 ac = self.fastclip(a, m, M)
|
Chris@87
|
1293 act = self.clip(a, m, M)
|
Chris@87
|
1294 assert_array_strict_equal(ac, act)
|
Chris@87
|
1295
|
Chris@87
|
1296 def test_type_cast_03(self):
|
Chris@87
|
1297 #Test native int32 input with float64 scalar min/max.
|
Chris@87
|
1298 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1299 m = -2
|
Chris@87
|
1300 M = 4
|
Chris@87
|
1301 ac = self.fastclip(a, float64(m), float64(M))
|
Chris@87
|
1302 act = self.clip(a, float64(m), float64(M))
|
Chris@87
|
1303 assert_array_strict_equal(ac, act)
|
Chris@87
|
1304
|
Chris@87
|
1305 def test_type_cast_04(self):
|
Chris@87
|
1306 #Test native int32 input with float32 scalar min/max.
|
Chris@87
|
1307 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1308 m = float32(-2)
|
Chris@87
|
1309 M = float32(4)
|
Chris@87
|
1310 act = self.fastclip(a, m, M)
|
Chris@87
|
1311 ac = self.clip(a, m, M)
|
Chris@87
|
1312 assert_array_strict_equal(ac, act)
|
Chris@87
|
1313
|
Chris@87
|
1314 def test_type_cast_05(self):
|
Chris@87
|
1315 #Test native int32 with double arrays min/max.
|
Chris@87
|
1316 a = self._generate_int_data(self.nr, self.nc)
|
Chris@87
|
1317 m = -0.5
|
Chris@87
|
1318 M = 1.
|
Chris@87
|
1319 ac = self.fastclip(a, m * zeros(a.shape), M)
|
Chris@87
|
1320 act = self.clip(a, m * zeros(a.shape), M)
|
Chris@87
|
1321 assert_array_strict_equal(ac, act)
|
Chris@87
|
1322
|
Chris@87
|
1323 def test_type_cast_06(self):
|
Chris@87
|
1324 #Test native with NON native scalar min/max.
|
Chris@87
|
1325 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1326 m = 0.5
|
Chris@87
|
1327 m_s = self._neg_byteorder(m)
|
Chris@87
|
1328 M = 1.
|
Chris@87
|
1329 act = self.clip(a, m_s, M)
|
Chris@87
|
1330 ac = self.fastclip(a, m_s, M)
|
Chris@87
|
1331 assert_array_strict_equal(ac, act)
|
Chris@87
|
1332
|
Chris@87
|
1333 def test_type_cast_07(self):
|
Chris@87
|
1334 #Test NON native with native array min/max.
|
Chris@87
|
1335 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1336 m = -0.5 * ones(a.shape)
|
Chris@87
|
1337 M = 1.
|
Chris@87
|
1338 a_s = self._neg_byteorder(a)
|
Chris@87
|
1339 assert_(not a_s.dtype.isnative)
|
Chris@87
|
1340 act = a_s.clip(m, M)
|
Chris@87
|
1341 ac = self.fastclip(a_s, m, M)
|
Chris@87
|
1342 assert_array_strict_equal(ac, act)
|
Chris@87
|
1343
|
Chris@87
|
1344 def test_type_cast_08(self):
|
Chris@87
|
1345 #Test NON native with native scalar min/max.
|
Chris@87
|
1346 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1347 m = -0.5
|
Chris@87
|
1348 M = 1.
|
Chris@87
|
1349 a_s = self._neg_byteorder(a)
|
Chris@87
|
1350 assert_(not a_s.dtype.isnative)
|
Chris@87
|
1351 ac = self.fastclip(a_s, m, M)
|
Chris@87
|
1352 act = a_s.clip(m, M)
|
Chris@87
|
1353 assert_array_strict_equal(ac, act)
|
Chris@87
|
1354
|
Chris@87
|
1355 def test_type_cast_09(self):
|
Chris@87
|
1356 #Test native with NON native array min/max.
|
Chris@87
|
1357 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1358 m = -0.5 * ones(a.shape)
|
Chris@87
|
1359 M = 1.
|
Chris@87
|
1360 m_s = self._neg_byteorder(m)
|
Chris@87
|
1361 assert_(not m_s.dtype.isnative)
|
Chris@87
|
1362 ac = self.fastclip(a, m_s, M)
|
Chris@87
|
1363 act = self.clip(a, m_s, M)
|
Chris@87
|
1364 assert_array_strict_equal(ac, act)
|
Chris@87
|
1365
|
Chris@87
|
1366 def test_type_cast_10(self):
|
Chris@87
|
1367 #Test native int32 with float min/max and float out for output argument.
|
Chris@87
|
1368 a = self._generate_int_data(self.nr, self.nc)
|
Chris@87
|
1369 b = zeros(a.shape, dtype = float32)
|
Chris@87
|
1370 m = float32(-0.5)
|
Chris@87
|
1371 M = float32(1)
|
Chris@87
|
1372 act = self.clip(a, m, M, out = b)
|
Chris@87
|
1373 ac = self.fastclip(a, m, M, out = b)
|
Chris@87
|
1374 assert_array_strict_equal(ac, act)
|
Chris@87
|
1375
|
Chris@87
|
1376 def test_type_cast_11(self):
|
Chris@87
|
1377 #Test non native with native scalar, min/max, out non native
|
Chris@87
|
1378 a = self._generate_non_native_data(self.nr, self.nc)
|
Chris@87
|
1379 b = a.copy()
|
Chris@87
|
1380 b = b.astype(b.dtype.newbyteorder('>'))
|
Chris@87
|
1381 bt = b.copy()
|
Chris@87
|
1382 m = -0.5
|
Chris@87
|
1383 M = 1.
|
Chris@87
|
1384 self.fastclip(a, m, M, out = b)
|
Chris@87
|
1385 self.clip(a, m, M, out = bt)
|
Chris@87
|
1386 assert_array_strict_equal(b, bt)
|
Chris@87
|
1387
|
Chris@87
|
1388 def test_type_cast_12(self):
|
Chris@87
|
1389 #Test native int32 input and min/max and float out
|
Chris@87
|
1390 a = self._generate_int_data(self.nr, self.nc)
|
Chris@87
|
1391 b = zeros(a.shape, dtype = float32)
|
Chris@87
|
1392 m = int32(0)
|
Chris@87
|
1393 M = int32(1)
|
Chris@87
|
1394 act = self.clip(a, m, M, out = b)
|
Chris@87
|
1395 ac = self.fastclip(a, m, M, out = b)
|
Chris@87
|
1396 assert_array_strict_equal(ac, act)
|
Chris@87
|
1397
|
Chris@87
|
1398 def test_clip_with_out_simple(self):
|
Chris@87
|
1399 #Test native double input with scalar min/max
|
Chris@87
|
1400 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1401 m = -0.5
|
Chris@87
|
1402 M = 0.6
|
Chris@87
|
1403 ac = zeros(a.shape)
|
Chris@87
|
1404 act = zeros(a.shape)
|
Chris@87
|
1405 self.fastclip(a, m, M, ac)
|
Chris@87
|
1406 self.clip(a, m, M, act)
|
Chris@87
|
1407 assert_array_strict_equal(ac, act)
|
Chris@87
|
1408
|
Chris@87
|
1409 def test_clip_with_out_simple2(self):
|
Chris@87
|
1410 #Test native int32 input with double min/max and int32 out
|
Chris@87
|
1411 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1412 m = float64(0)
|
Chris@87
|
1413 M = float64(2)
|
Chris@87
|
1414 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1415 act = ac.copy()
|
Chris@87
|
1416 self.fastclip(a, m, M, ac)
|
Chris@87
|
1417 self.clip(a, m, M, act)
|
Chris@87
|
1418 assert_array_strict_equal(ac, act)
|
Chris@87
|
1419
|
Chris@87
|
1420 def test_clip_with_out_simple_int32(self):
|
Chris@87
|
1421 #Test native int32 input with int32 scalar min/max and int64 out
|
Chris@87
|
1422 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1423 m = int32(-1)
|
Chris@87
|
1424 M = int32(1)
|
Chris@87
|
1425 ac = zeros(a.shape, dtype = int64)
|
Chris@87
|
1426 act = ac.copy()
|
Chris@87
|
1427 self.fastclip(a, m, M, ac)
|
Chris@87
|
1428 self.clip(a, m, M, act)
|
Chris@87
|
1429 assert_array_strict_equal(ac, act)
|
Chris@87
|
1430
|
Chris@87
|
1431 def test_clip_with_out_array_int32(self):
|
Chris@87
|
1432 #Test native int32 input with double array min/max and int32 out
|
Chris@87
|
1433 a = self._generate_int32_data(self.nr, self.nc)
|
Chris@87
|
1434 m = zeros(a.shape, float64)
|
Chris@87
|
1435 M = float64(1)
|
Chris@87
|
1436 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1437 act = ac.copy()
|
Chris@87
|
1438 self.fastclip(a, m, M, ac)
|
Chris@87
|
1439 self.clip(a, m, M, act)
|
Chris@87
|
1440 assert_array_strict_equal(ac, act)
|
Chris@87
|
1441
|
Chris@87
|
1442 def test_clip_with_out_array_outint32(self):
|
Chris@87
|
1443 #Test native double input with scalar min/max and int out
|
Chris@87
|
1444 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1445 m = -1.0
|
Chris@87
|
1446 M = 2.0
|
Chris@87
|
1447 ac = zeros(a.shape, dtype = int32)
|
Chris@87
|
1448 act = ac.copy()
|
Chris@87
|
1449 self.fastclip(a, m, M, ac)
|
Chris@87
|
1450 self.clip(a, m, M, act)
|
Chris@87
|
1451 assert_array_strict_equal(ac, act)
|
Chris@87
|
1452
|
Chris@87
|
1453 def test_clip_inplace_array(self):
|
Chris@87
|
1454 #Test native double input with array min/max
|
Chris@87
|
1455 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1456 ac = a.copy()
|
Chris@87
|
1457 m = zeros(a.shape)
|
Chris@87
|
1458 M = 1.0
|
Chris@87
|
1459 self.fastclip(a, m, M, a)
|
Chris@87
|
1460 self.clip(a, m, M, ac)
|
Chris@87
|
1461 assert_array_strict_equal(a, ac)
|
Chris@87
|
1462
|
Chris@87
|
1463 def test_clip_inplace_simple(self):
|
Chris@87
|
1464 #Test native double input with scalar min/max
|
Chris@87
|
1465 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1466 ac = a.copy()
|
Chris@87
|
1467 m = -0.5
|
Chris@87
|
1468 M = 0.6
|
Chris@87
|
1469 self.fastclip(a, m, M, a)
|
Chris@87
|
1470 self.clip(a, m, M, ac)
|
Chris@87
|
1471 assert_array_strict_equal(a, ac)
|
Chris@87
|
1472
|
Chris@87
|
1473 def test_clip_func_takes_out(self):
|
Chris@87
|
1474 # Ensure that the clip() function takes an out= argument.
|
Chris@87
|
1475 a = self._generate_data(self.nr, self.nc)
|
Chris@87
|
1476 ac = a.copy()
|
Chris@87
|
1477 m = -0.5
|
Chris@87
|
1478 M = 0.6
|
Chris@87
|
1479 a2 = clip(a, m, M, out=a)
|
Chris@87
|
1480 self.clip(a, m, M, ac)
|
Chris@87
|
1481 assert_array_strict_equal(a2, ac)
|
Chris@87
|
1482 self.assertTrue(a2 is a)
|
Chris@87
|
1483
|
Chris@87
|
1484
|
Chris@87
|
1485 class TestAllclose(object):
|
Chris@87
|
1486 rtol = 1e-5
|
Chris@87
|
1487 atol = 1e-8
|
Chris@87
|
1488
|
Chris@87
|
1489 def setUp(self):
|
Chris@87
|
1490 self.olderr = np.seterr(invalid='ignore')
|
Chris@87
|
1491
|
Chris@87
|
1492 def tearDown(self):
|
Chris@87
|
1493 np.seterr(**self.olderr)
|
Chris@87
|
1494
|
Chris@87
|
1495 def tst_allclose(self, x, y):
|
Chris@87
|
1496 assert_(allclose(x, y), "%s and %s not close" % (x, y))
|
Chris@87
|
1497
|
Chris@87
|
1498 def tst_not_allclose(self, x, y):
|
Chris@87
|
1499 assert_(not allclose(x, y), "%s and %s shouldn't be close" % (x, y))
|
Chris@87
|
1500
|
Chris@87
|
1501 def test_ip_allclose(self):
|
Chris@87
|
1502 #Parametric test factory.
|
Chris@87
|
1503 arr = array([100, 1000])
|
Chris@87
|
1504 aran = arange(125).reshape((5, 5, 5))
|
Chris@87
|
1505
|
Chris@87
|
1506 atol = self.atol
|
Chris@87
|
1507 rtol = self.rtol
|
Chris@87
|
1508
|
Chris@87
|
1509 data = [([1, 0], [1, 0]),
|
Chris@87
|
1510 ([atol], [0]),
|
Chris@87
|
1511 ([1], [1+rtol+atol]),
|
Chris@87
|
1512 (arr, arr + arr*rtol),
|
Chris@87
|
1513 (arr, arr + arr*rtol + atol*2),
|
Chris@87
|
1514 (aran, aran + aran*rtol),
|
Chris@87
|
1515 (inf, inf),
|
Chris@87
|
1516 (inf, [inf])]
|
Chris@87
|
1517
|
Chris@87
|
1518 for (x, y) in data:
|
Chris@87
|
1519 yield (self.tst_allclose, x, y)
|
Chris@87
|
1520
|
Chris@87
|
1521 def test_ip_not_allclose(self):
|
Chris@87
|
1522 #Parametric test factory.
|
Chris@87
|
1523 aran = arange(125).reshape((5, 5, 5))
|
Chris@87
|
1524
|
Chris@87
|
1525 atol = self.atol
|
Chris@87
|
1526 rtol = self.rtol
|
Chris@87
|
1527
|
Chris@87
|
1528 data = [([inf, 0], [1, inf]),
|
Chris@87
|
1529 ([inf, 0], [1, 0]),
|
Chris@87
|
1530 ([inf, inf], [1, inf]),
|
Chris@87
|
1531 ([inf, inf], [1, 0]),
|
Chris@87
|
1532 ([-inf, 0], [inf, 0]),
|
Chris@87
|
1533 ([nan, 0], [nan, 0]),
|
Chris@87
|
1534 ([atol*2], [0]),
|
Chris@87
|
1535 ([1], [1+rtol+atol*2]),
|
Chris@87
|
1536 (aran, aran + aran*atol + atol*2),
|
Chris@87
|
1537 (array([inf, 1]), array([0, inf]))]
|
Chris@87
|
1538
|
Chris@87
|
1539 for (x, y) in data:
|
Chris@87
|
1540 yield (self.tst_not_allclose, x, y)
|
Chris@87
|
1541
|
Chris@87
|
1542 def test_no_parameter_modification(self):
|
Chris@87
|
1543 x = array([inf, 1])
|
Chris@87
|
1544 y = array([0, inf])
|
Chris@87
|
1545 allclose(x, y)
|
Chris@87
|
1546 assert_array_equal(x, array([inf, 1]))
|
Chris@87
|
1547 assert_array_equal(y, array([0, inf]))
|
Chris@87
|
1548
|
Chris@87
|
1549
|
Chris@87
|
1550 def test_min_int(self):
|
Chris@87
|
1551 # Could make problems because of abs(min_int) == min_int
|
Chris@87
|
1552 min_int = np.iinfo(np.int_).min
|
Chris@87
|
1553 a = np.array([min_int], dtype=np.int_)
|
Chris@87
|
1554 assert_(allclose(a, a))
|
Chris@87
|
1555
|
Chris@87
|
1556
|
Chris@87
|
1557 class TestIsclose(object):
|
Chris@87
|
1558 rtol = 1e-5
|
Chris@87
|
1559 atol = 1e-8
|
Chris@87
|
1560
|
Chris@87
|
1561 def setup(self):
|
Chris@87
|
1562 atol = self.atol
|
Chris@87
|
1563 rtol = self.rtol
|
Chris@87
|
1564 arr = array([100, 1000])
|
Chris@87
|
1565 aran = arange(125).reshape((5, 5, 5))
|
Chris@87
|
1566
|
Chris@87
|
1567 self.all_close_tests = [
|
Chris@87
|
1568 ([1, 0], [1, 0]),
|
Chris@87
|
1569 ([atol], [0]),
|
Chris@87
|
1570 ([1], [1 + rtol + atol]),
|
Chris@87
|
1571 (arr, arr + arr*rtol),
|
Chris@87
|
1572 (arr, arr + arr*rtol + atol),
|
Chris@87
|
1573 (aran, aran + aran*rtol),
|
Chris@87
|
1574 (inf, inf),
|
Chris@87
|
1575 (inf, [inf]),
|
Chris@87
|
1576 ([inf, -inf], [inf, -inf]),
|
Chris@87
|
1577 ]
|
Chris@87
|
1578 self.none_close_tests = [
|
Chris@87
|
1579 ([inf, 0], [1, inf]),
|
Chris@87
|
1580 ([inf, -inf], [1, 0]),
|
Chris@87
|
1581 ([inf, inf], [1, -inf]),
|
Chris@87
|
1582 ([inf, inf], [1, 0]),
|
Chris@87
|
1583 ([nan, 0], [nan, -inf]),
|
Chris@87
|
1584 ([atol*2], [0]),
|
Chris@87
|
1585 ([1], [1 + rtol + atol*2]),
|
Chris@87
|
1586 (aran, aran + rtol*1.1*aran + atol*1.1),
|
Chris@87
|
1587 (array([inf, 1]), array([0, inf])),
|
Chris@87
|
1588 ]
|
Chris@87
|
1589 self.some_close_tests = [
|
Chris@87
|
1590 ([inf, 0], [inf, atol*2]),
|
Chris@87
|
1591 ([atol, 1, 1e6*(1 + 2*rtol) + atol], [0, nan, 1e6]),
|
Chris@87
|
1592 (arange(3), [0, 1, 2.1]),
|
Chris@87
|
1593 (nan, [nan, nan, nan]),
|
Chris@87
|
1594 ([0], [atol, inf, -inf, nan]),
|
Chris@87
|
1595 (0, [atol, inf, -inf, nan]),
|
Chris@87
|
1596 ]
|
Chris@87
|
1597 self.some_close_results = [
|
Chris@87
|
1598 [True, False],
|
Chris@87
|
1599 [True, False, False],
|
Chris@87
|
1600 [True, True, False],
|
Chris@87
|
1601 [False, False, False],
|
Chris@87
|
1602 [True, False, False, False],
|
Chris@87
|
1603 [True, False, False, False],
|
Chris@87
|
1604 ]
|
Chris@87
|
1605
|
Chris@87
|
1606 def test_ip_isclose(self):
|
Chris@87
|
1607 self.setup()
|
Chris@87
|
1608 tests = self.some_close_tests
|
Chris@87
|
1609 results = self.some_close_results
|
Chris@87
|
1610 for (x, y), result in zip(tests, results):
|
Chris@87
|
1611 yield (assert_array_equal, isclose(x, y), result)
|
Chris@87
|
1612
|
Chris@87
|
1613 def tst_all_isclose(self, x, y):
|
Chris@87
|
1614 assert_(all(isclose(x, y)), "%s and %s not close" % (x, y))
|
Chris@87
|
1615
|
Chris@87
|
1616 def tst_none_isclose(self, x, y):
|
Chris@87
|
1617 msg = "%s and %s shouldn't be close"
|
Chris@87
|
1618 assert_(not any(isclose(x, y)), msg % (x, y))
|
Chris@87
|
1619
|
Chris@87
|
1620 def tst_isclose_allclose(self, x, y):
|
Chris@87
|
1621 msg = "isclose.all() and allclose aren't same for %s and %s"
|
Chris@87
|
1622 assert_array_equal(isclose(x, y).all(), allclose(x, y), msg % (x, y))
|
Chris@87
|
1623
|
Chris@87
|
1624 def test_ip_all_isclose(self):
|
Chris@87
|
1625 self.setup()
|
Chris@87
|
1626 for (x, y) in self.all_close_tests:
|
Chris@87
|
1627 yield (self.tst_all_isclose, x, y)
|
Chris@87
|
1628
|
Chris@87
|
1629 def test_ip_none_isclose(self):
|
Chris@87
|
1630 self.setup()
|
Chris@87
|
1631 for (x, y) in self.none_close_tests:
|
Chris@87
|
1632 yield (self.tst_none_isclose, x, y)
|
Chris@87
|
1633
|
Chris@87
|
1634 def test_ip_isclose_allclose(self):
|
Chris@87
|
1635 self.setup()
|
Chris@87
|
1636 tests = (self.all_close_tests + self.none_close_tests +
|
Chris@87
|
1637 self.some_close_tests)
|
Chris@87
|
1638 for (x, y) in tests:
|
Chris@87
|
1639 yield (self.tst_isclose_allclose, x, y)
|
Chris@87
|
1640
|
Chris@87
|
1641 def test_equal_nan(self):
|
Chris@87
|
1642 assert_array_equal(isclose(nan, nan, equal_nan=True), [True])
|
Chris@87
|
1643 arr = array([1.0, nan])
|
Chris@87
|
1644 assert_array_equal(isclose(arr, arr, equal_nan=True), [True, True])
|
Chris@87
|
1645
|
Chris@87
|
1646 def test_masked_arrays(self):
|
Chris@87
|
1647 x = np.ma.masked_where([True, True, False], np.arange(3))
|
Chris@87
|
1648 assert_(type(x) is type(isclose(2, x)))
|
Chris@87
|
1649
|
Chris@87
|
1650 x = np.ma.masked_where([True, True, False], [nan, inf, nan])
|
Chris@87
|
1651 assert_(type(x) is type(isclose(inf, x)))
|
Chris@87
|
1652
|
Chris@87
|
1653 x = np.ma.masked_where([True, True, False], [nan, nan, nan])
|
Chris@87
|
1654 y = isclose(nan, x, equal_nan=True)
|
Chris@87
|
1655 assert_(type(x) is type(y))
|
Chris@87
|
1656 # Ensure that the mask isn't modified...
|
Chris@87
|
1657 assert_array_equal([True, True, False], y.mask)
|
Chris@87
|
1658
|
Chris@87
|
1659 x = np.ma.masked_where([True, True, False], [nan, nan, nan])
|
Chris@87
|
1660 y = isclose(x, x, equal_nan=True)
|
Chris@87
|
1661 assert_(type(x) is type(y))
|
Chris@87
|
1662 # Ensure that the mask isn't modified...
|
Chris@87
|
1663 assert_array_equal([True, True, False], y.mask)
|
Chris@87
|
1664
|
Chris@87
|
1665 def test_scalar_return(self):
|
Chris@87
|
1666 assert_(isscalar(isclose(1, 1)))
|
Chris@87
|
1667
|
Chris@87
|
1668 def test_no_parameter_modification(self):
|
Chris@87
|
1669 x = array([inf, 1])
|
Chris@87
|
1670 y = array([0, inf])
|
Chris@87
|
1671 isclose(x, y)
|
Chris@87
|
1672 assert_array_equal(x, array([inf, 1]))
|
Chris@87
|
1673 assert_array_equal(y, array([0, inf]))
|
Chris@87
|
1674
|
Chris@87
|
1675 class TestStdVar(TestCase):
|
Chris@87
|
1676 def setUp(self):
|
Chris@87
|
1677 self.A = array([1, -1, 1, -1])
|
Chris@87
|
1678 self.real_var = 1
|
Chris@87
|
1679
|
Chris@87
|
1680 def test_basic(self):
|
Chris@87
|
1681 assert_almost_equal(var(self.A), self.real_var)
|
Chris@87
|
1682 assert_almost_equal(std(self.A)**2, self.real_var)
|
Chris@87
|
1683
|
Chris@87
|
1684 def test_scalars(self):
|
Chris@87
|
1685 assert_equal(var(1), 0)
|
Chris@87
|
1686 assert_equal(std(1), 0)
|
Chris@87
|
1687
|
Chris@87
|
1688 def test_ddof1(self):
|
Chris@87
|
1689 assert_almost_equal(var(self.A, ddof=1),
|
Chris@87
|
1690 self.real_var*len(self.A)/float(len(self.A)-1))
|
Chris@87
|
1691 assert_almost_equal(std(self.A, ddof=1)**2,
|
Chris@87
|
1692 self.real_var*len(self.A)/float(len(self.A)-1))
|
Chris@87
|
1693
|
Chris@87
|
1694 def test_ddof2(self):
|
Chris@87
|
1695 assert_almost_equal(var(self.A, ddof=2),
|
Chris@87
|
1696 self.real_var*len(self.A)/float(len(self.A)-2))
|
Chris@87
|
1697 assert_almost_equal(std(self.A, ddof=2)**2,
|
Chris@87
|
1698 self.real_var*len(self.A)/float(len(self.A)-2))
|
Chris@87
|
1699
|
Chris@87
|
1700 def test_out_scalar(self):
|
Chris@87
|
1701 d = np.arange(10)
|
Chris@87
|
1702 out = np.array(0.)
|
Chris@87
|
1703 r = np.std(d, out=out)
|
Chris@87
|
1704 assert_(r is out)
|
Chris@87
|
1705 assert_array_equal(r, out)
|
Chris@87
|
1706 r = np.var(d, out=out)
|
Chris@87
|
1707 assert_(r is out)
|
Chris@87
|
1708 assert_array_equal(r, out)
|
Chris@87
|
1709 r = np.mean(d, out=out)
|
Chris@87
|
1710 assert_(r is out)
|
Chris@87
|
1711 assert_array_equal(r, out)
|
Chris@87
|
1712
|
Chris@87
|
1713
|
Chris@87
|
1714 class TestStdVarComplex(TestCase):
|
Chris@87
|
1715 def test_basic(self):
|
Chris@87
|
1716 A = array([1, 1.j, -1, -1.j])
|
Chris@87
|
1717 real_var = 1
|
Chris@87
|
1718 assert_almost_equal(var(A), real_var)
|
Chris@87
|
1719 assert_almost_equal(std(A)**2, real_var)
|
Chris@87
|
1720
|
Chris@87
|
1721 def test_scalars(self):
|
Chris@87
|
1722 assert_equal(var(1j), 0)
|
Chris@87
|
1723 assert_equal(std(1j), 0)
|
Chris@87
|
1724
|
Chris@87
|
1725
|
Chris@87
|
1726 class TestCreationFuncs(TestCase):
|
Chris@87
|
1727 #Test ones, zeros, empty and filled
|
Chris@87
|
1728
|
Chris@87
|
1729 def setUp(self):
|
Chris@87
|
1730 self.dtypes = ('b', 'i', 'u', 'f', 'c', 'S', 'a', 'U', 'V')
|
Chris@87
|
1731 self.orders = {'C': 'c_contiguous', 'F': 'f_contiguous'}
|
Chris@87
|
1732 self.ndims = 10
|
Chris@87
|
1733
|
Chris@87
|
1734 def check_function(self, func, fill_value=None):
|
Chris@87
|
1735 par = (
|
Chris@87
|
1736 (0, 1, 2),
|
Chris@87
|
1737 range(self.ndims),
|
Chris@87
|
1738 self.orders,
|
Chris@87
|
1739 self.dtypes,
|
Chris@87
|
1740 2**np.arange(9)
|
Chris@87
|
1741 )
|
Chris@87
|
1742 fill_kwarg = {}
|
Chris@87
|
1743 if fill_value is not None:
|
Chris@87
|
1744 fill_kwarg = {'fill_value': fill_value}
|
Chris@87
|
1745 with warnings.catch_warnings():
|
Chris@87
|
1746 warnings.simplefilter('ignore', DeprecationWarning)
|
Chris@87
|
1747 for size, ndims, order, type, bytes in itertools.product(*par):
|
Chris@87
|
1748 shape = ndims * [size]
|
Chris@87
|
1749 try:
|
Chris@87
|
1750 dtype = np.dtype('{0}{1}'.format(type, bytes))
|
Chris@87
|
1751 except TypeError: # dtype combination does not exist
|
Chris@87
|
1752 continue
|
Chris@87
|
1753 else:
|
Chris@87
|
1754 # do not fill void type
|
Chris@87
|
1755 if fill_value is not None and type in 'V':
|
Chris@87
|
1756 continue
|
Chris@87
|
1757
|
Chris@87
|
1758 arr = func(shape, order=order, dtype=dtype,
|
Chris@87
|
1759 **fill_kwarg)
|
Chris@87
|
1760
|
Chris@87
|
1761 assert_(arr.dtype == dtype)
|
Chris@87
|
1762 assert_(getattr(arr.flags, self.orders[order]))
|
Chris@87
|
1763
|
Chris@87
|
1764 if fill_value is not None:
|
Chris@87
|
1765 if dtype.str.startswith('|S'):
|
Chris@87
|
1766 val = str(fill_value)
|
Chris@87
|
1767 else:
|
Chris@87
|
1768 val = fill_value
|
Chris@87
|
1769 assert_equal(arr, dtype.type(val))
|
Chris@87
|
1770
|
Chris@87
|
1771 def test_zeros(self):
|
Chris@87
|
1772 self.check_function(np.zeros)
|
Chris@87
|
1773
|
Chris@87
|
1774 def test_ones(self):
|
Chris@87
|
1775 self.check_function(np.zeros)
|
Chris@87
|
1776
|
Chris@87
|
1777 def test_empty(self):
|
Chris@87
|
1778 self.check_function(np.empty)
|
Chris@87
|
1779
|
Chris@87
|
1780 def test_filled(self):
|
Chris@87
|
1781 self.check_function(np.full, 0)
|
Chris@87
|
1782 self.check_function(np.full, 1)
|
Chris@87
|
1783
|
Chris@87
|
1784 def test_for_reference_leak(self):
|
Chris@87
|
1785 # Make sure we have an object for reference
|
Chris@87
|
1786 dim = 1
|
Chris@87
|
1787 beg = sys.getrefcount(dim)
|
Chris@87
|
1788 np.zeros([dim]*10)
|
Chris@87
|
1789 assert_(sys.getrefcount(dim) == beg)
|
Chris@87
|
1790 np.ones([dim]*10)
|
Chris@87
|
1791 assert_(sys.getrefcount(dim) == beg)
|
Chris@87
|
1792 np.empty([dim]*10)
|
Chris@87
|
1793 assert_(sys.getrefcount(dim) == beg)
|
Chris@87
|
1794 np.full([dim]*10, 0)
|
Chris@87
|
1795 assert_(sys.getrefcount(dim) == beg)
|
Chris@87
|
1796
|
Chris@87
|
1797
|
Chris@87
|
1798
|
Chris@87
|
1799 class TestLikeFuncs(TestCase):
|
Chris@87
|
1800 '''Test ones_like, zeros_like, empty_like and full_like'''
|
Chris@87
|
1801
|
Chris@87
|
1802 def setUp(self):
|
Chris@87
|
1803 self.data = [
|
Chris@87
|
1804 # Array scalars
|
Chris@87
|
1805 (array(3.), None),
|
Chris@87
|
1806 (array(3), 'f8'),
|
Chris@87
|
1807 # 1D arrays
|
Chris@87
|
1808 (arange(6, dtype='f4'), None),
|
Chris@87
|
1809 (arange(6), 'c16'),
|
Chris@87
|
1810 # 2D C-layout arrays
|
Chris@87
|
1811 (arange(6).reshape(2, 3), None),
|
Chris@87
|
1812 (arange(6).reshape(3, 2), 'i1'),
|
Chris@87
|
1813 # 2D F-layout arrays
|
Chris@87
|
1814 (arange(6).reshape((2, 3), order='F'), None),
|
Chris@87
|
1815 (arange(6).reshape((3, 2), order='F'), 'i1'),
|
Chris@87
|
1816 # 3D C-layout arrays
|
Chris@87
|
1817 (arange(24).reshape(2, 3, 4), None),
|
Chris@87
|
1818 (arange(24).reshape(4, 3, 2), 'f4'),
|
Chris@87
|
1819 # 3D F-layout arrays
|
Chris@87
|
1820 (arange(24).reshape((2, 3, 4), order='F'), None),
|
Chris@87
|
1821 (arange(24).reshape((4, 3, 2), order='F'), 'f4'),
|
Chris@87
|
1822 # 3D non-C/F-layout arrays
|
Chris@87
|
1823 (arange(24).reshape(2, 3, 4).swapaxes(0, 1), None),
|
Chris@87
|
1824 (arange(24).reshape(4, 3, 2).swapaxes(0, 1), '?'),
|
Chris@87
|
1825 ]
|
Chris@87
|
1826
|
Chris@87
|
1827 def compare_array_value(self, dz, value, fill_value):
|
Chris@87
|
1828 if value is not None:
|
Chris@87
|
1829 if fill_value:
|
Chris@87
|
1830 try:
|
Chris@87
|
1831 z = dz.dtype.type(value)
|
Chris@87
|
1832 except OverflowError:
|
Chris@87
|
1833 pass
|
Chris@87
|
1834 else:
|
Chris@87
|
1835 assert_(all(dz == z))
|
Chris@87
|
1836 else:
|
Chris@87
|
1837 assert_(all(dz == value))
|
Chris@87
|
1838
|
Chris@87
|
1839 def check_like_function(self, like_function, value, fill_value=False):
|
Chris@87
|
1840 if fill_value:
|
Chris@87
|
1841 fill_kwarg = {'fill_value': value}
|
Chris@87
|
1842 else:
|
Chris@87
|
1843 fill_kwarg = {}
|
Chris@87
|
1844 for d, dtype in self.data:
|
Chris@87
|
1845 # default (K) order, dtype
|
Chris@87
|
1846 dz = like_function(d, dtype=dtype, **fill_kwarg)
|
Chris@87
|
1847 assert_equal(dz.shape, d.shape)
|
Chris@87
|
1848 assert_equal(array(dz.strides)*d.dtype.itemsize,
|
Chris@87
|
1849 array(d.strides)*dz.dtype.itemsize)
|
Chris@87
|
1850 assert_equal(d.flags.c_contiguous, dz.flags.c_contiguous)
|
Chris@87
|
1851 assert_equal(d.flags.f_contiguous, dz.flags.f_contiguous)
|
Chris@87
|
1852 if dtype is None:
|
Chris@87
|
1853 assert_equal(dz.dtype, d.dtype)
|
Chris@87
|
1854 else:
|
Chris@87
|
1855 assert_equal(dz.dtype, np.dtype(dtype))
|
Chris@87
|
1856 self.compare_array_value(dz, value, fill_value)
|
Chris@87
|
1857
|
Chris@87
|
1858 # C order, default dtype
|
Chris@87
|
1859 dz = like_function(d, order='C', dtype=dtype, **fill_kwarg)
|
Chris@87
|
1860 assert_equal(dz.shape, d.shape)
|
Chris@87
|
1861 assert_(dz.flags.c_contiguous)
|
Chris@87
|
1862 if dtype is None:
|
Chris@87
|
1863 assert_equal(dz.dtype, d.dtype)
|
Chris@87
|
1864 else:
|
Chris@87
|
1865 assert_equal(dz.dtype, np.dtype(dtype))
|
Chris@87
|
1866 self.compare_array_value(dz, value, fill_value)
|
Chris@87
|
1867
|
Chris@87
|
1868 # F order, default dtype
|
Chris@87
|
1869 dz = like_function(d, order='F', dtype=dtype, **fill_kwarg)
|
Chris@87
|
1870 assert_equal(dz.shape, d.shape)
|
Chris@87
|
1871 assert_(dz.flags.f_contiguous)
|
Chris@87
|
1872 if dtype is None:
|
Chris@87
|
1873 assert_equal(dz.dtype, d.dtype)
|
Chris@87
|
1874 else:
|
Chris@87
|
1875 assert_equal(dz.dtype, np.dtype(dtype))
|
Chris@87
|
1876 self.compare_array_value(dz, value, fill_value)
|
Chris@87
|
1877
|
Chris@87
|
1878 # A order
|
Chris@87
|
1879 dz = like_function(d, order='A', dtype=dtype, **fill_kwarg)
|
Chris@87
|
1880 assert_equal(dz.shape, d.shape)
|
Chris@87
|
1881 if d.flags.f_contiguous:
|
Chris@87
|
1882 assert_(dz.flags.f_contiguous)
|
Chris@87
|
1883 else:
|
Chris@87
|
1884 assert_(dz.flags.c_contiguous)
|
Chris@87
|
1885 if dtype is None:
|
Chris@87
|
1886 assert_equal(dz.dtype, d.dtype)
|
Chris@87
|
1887 else:
|
Chris@87
|
1888 assert_equal(dz.dtype, np.dtype(dtype))
|
Chris@87
|
1889 self.compare_array_value(dz, value, fill_value)
|
Chris@87
|
1890
|
Chris@87
|
1891 # Test the 'subok' parameter
|
Chris@87
|
1892 a = np.matrix([[1, 2], [3, 4]])
|
Chris@87
|
1893
|
Chris@87
|
1894 b = like_function(a, **fill_kwarg)
|
Chris@87
|
1895 assert_(type(b) is np.matrix)
|
Chris@87
|
1896
|
Chris@87
|
1897 b = like_function(a, subok=False, **fill_kwarg)
|
Chris@87
|
1898 assert_(type(b) is not np.matrix)
|
Chris@87
|
1899
|
Chris@87
|
1900 def test_ones_like(self):
|
Chris@87
|
1901 self.check_like_function(np.ones_like, 1)
|
Chris@87
|
1902
|
Chris@87
|
1903 def test_zeros_like(self):
|
Chris@87
|
1904 self.check_like_function(np.zeros_like, 0)
|
Chris@87
|
1905
|
Chris@87
|
1906 def test_empty_like(self):
|
Chris@87
|
1907 self.check_like_function(np.empty_like, None)
|
Chris@87
|
1908
|
Chris@87
|
1909 def test_filled_like(self):
|
Chris@87
|
1910 self.check_like_function(np.full_like, 0, True)
|
Chris@87
|
1911 self.check_like_function(np.full_like, 1, True)
|
Chris@87
|
1912 self.check_like_function(np.full_like, 1000, True)
|
Chris@87
|
1913 self.check_like_function(np.full_like, 123.456, True)
|
Chris@87
|
1914 self.check_like_function(np.full_like, np.inf, True)
|
Chris@87
|
1915
|
Chris@87
|
1916 class _TestCorrelate(TestCase):
|
Chris@87
|
1917 def _setup(self, dt):
|
Chris@87
|
1918 self.x = np.array([1, 2, 3, 4, 5], dtype=dt)
|
Chris@87
|
1919 self.y = np.array([-1, -2, -3], dtype=dt)
|
Chris@87
|
1920 self.z1 = np.array([ -3., -8., -14., -20., -26., -14., -5.], dtype=dt)
|
Chris@87
|
1921 self.z2 = np.array([ -5., -14., -26., -20., -14., -8., -3.], dtype=dt)
|
Chris@87
|
1922
|
Chris@87
|
1923 def test_float(self):
|
Chris@87
|
1924 self._setup(np.float)
|
Chris@87
|
1925 z = np.correlate(self.x, self.y, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1926 assert_array_almost_equal(z, self.z1)
|
Chris@87
|
1927 z = np.correlate(self.y, self.x, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1928 assert_array_almost_equal(z, self.z2)
|
Chris@87
|
1929
|
Chris@87
|
1930 def test_object(self):
|
Chris@87
|
1931 self._setup(Decimal)
|
Chris@87
|
1932 z = np.correlate(self.x, self.y, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1933 assert_array_almost_equal(z, self.z1)
|
Chris@87
|
1934 z = np.correlate(self.y, self.x, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1935 assert_array_almost_equal(z, self.z2)
|
Chris@87
|
1936
|
Chris@87
|
1937 class TestCorrelate(_TestCorrelate):
|
Chris@87
|
1938 old_behavior = True
|
Chris@87
|
1939 def _setup(self, dt):
|
Chris@87
|
1940 # correlate uses an unconventional definition so that correlate(a, b)
|
Chris@87
|
1941 # == correlate(b, a), so force the corresponding outputs to be the same
|
Chris@87
|
1942 # as well
|
Chris@87
|
1943 _TestCorrelate._setup(self, dt)
|
Chris@87
|
1944 self.z2 = self.z1
|
Chris@87
|
1945
|
Chris@87
|
1946 @dec.deprecated()
|
Chris@87
|
1947 def test_complex(self):
|
Chris@87
|
1948 x = np.array([1, 2, 3, 4+1j], dtype=np.complex)
|
Chris@87
|
1949 y = np.array([-1, -2j, 3+1j], dtype=np.complex)
|
Chris@87
|
1950 r_z = np.array([3+1j, 6, 8-1j, 9+1j, -1-8j, -4-1j], dtype=np.complex)
|
Chris@87
|
1951 z = np.correlate(x, y, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1952 assert_array_almost_equal(z, r_z)
|
Chris@87
|
1953
|
Chris@87
|
1954 @dec.deprecated()
|
Chris@87
|
1955 def test_float(self):
|
Chris@87
|
1956 _TestCorrelate.test_float(self)
|
Chris@87
|
1957
|
Chris@87
|
1958 @dec.deprecated()
|
Chris@87
|
1959 def test_object(self):
|
Chris@87
|
1960 _TestCorrelate.test_object(self)
|
Chris@87
|
1961
|
Chris@87
|
1962 class TestCorrelateNew(_TestCorrelate):
|
Chris@87
|
1963 old_behavior = False
|
Chris@87
|
1964 def test_complex(self):
|
Chris@87
|
1965 x = np.array([1, 2, 3, 4+1j], dtype=np.complex)
|
Chris@87
|
1966 y = np.array([-1, -2j, 3+1j], dtype=np.complex)
|
Chris@87
|
1967 r_z = np.array([3-1j, 6, 8+1j, 11+5j, -5+8j, -4-1j], dtype=np.complex)
|
Chris@87
|
1968 #z = np.acorrelate(x, y, 'full')
|
Chris@87
|
1969 #assert_array_almost_equal(z, r_z)
|
Chris@87
|
1970
|
Chris@87
|
1971 r_z = r_z[::-1].conjugate()
|
Chris@87
|
1972 z = np.correlate(y, x, 'full', old_behavior=self.old_behavior)
|
Chris@87
|
1973 assert_array_almost_equal(z, r_z)
|
Chris@87
|
1974
|
Chris@87
|
1975 class TestArgwhere(object):
|
Chris@87
|
1976 def test_2D(self):
|
Chris@87
|
1977 x = np.arange(6).reshape((2, 3))
|
Chris@87
|
1978 assert_array_equal(np.argwhere(x > 1),
|
Chris@87
|
1979 [[0, 2],
|
Chris@87
|
1980 [1, 0],
|
Chris@87
|
1981 [1, 1],
|
Chris@87
|
1982 [1, 2]])
|
Chris@87
|
1983
|
Chris@87
|
1984 def test_list(self):
|
Chris@87
|
1985 assert_equal(np.argwhere([4, 0, 2, 1, 3]), [[0], [2], [3], [4]])
|
Chris@87
|
1986
|
Chris@87
|
1987 class TestStringFunction(object):
|
Chris@87
|
1988 def test_set_string_function(self):
|
Chris@87
|
1989 a = np.array([1])
|
Chris@87
|
1990 np.set_string_function(lambda x: "FOO", repr=True)
|
Chris@87
|
1991 assert_equal(repr(a), "FOO")
|
Chris@87
|
1992 np.set_string_function(None, repr=True)
|
Chris@87
|
1993 assert_equal(repr(a), "array([1])")
|
Chris@87
|
1994
|
Chris@87
|
1995 np.set_string_function(lambda x: "FOO", repr=False)
|
Chris@87
|
1996 assert_equal(str(a), "FOO")
|
Chris@87
|
1997 np.set_string_function(None, repr=False)
|
Chris@87
|
1998 assert_equal(str(a), "[1]")
|
Chris@87
|
1999
|
Chris@87
|
2000 class TestRoll(TestCase):
|
Chris@87
|
2001 def test_roll1d(self):
|
Chris@87
|
2002 x = np.arange(10)
|
Chris@87
|
2003 xr = np.roll(x, 2)
|
Chris@87
|
2004 assert_equal(xr, np.array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7]))
|
Chris@87
|
2005
|
Chris@87
|
2006 def test_roll2d(self):
|
Chris@87
|
2007 x2 = np.reshape(np.arange(10), (2, 5))
|
Chris@87
|
2008 x2r = np.roll(x2, 1)
|
Chris@87
|
2009 assert_equal(x2r, np.array([[9, 0, 1, 2, 3], [4, 5, 6, 7, 8]]))
|
Chris@87
|
2010
|
Chris@87
|
2011 x2r = np.roll(x2, 1, axis=0)
|
Chris@87
|
2012 assert_equal(x2r, np.array([[5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]))
|
Chris@87
|
2013
|
Chris@87
|
2014 x2r = np.roll(x2, 1, axis=1)
|
Chris@87
|
2015 assert_equal(x2r, np.array([[4, 0, 1, 2, 3], [9, 5, 6, 7, 8]]))
|
Chris@87
|
2016
|
Chris@87
|
2017 def test_roll_empty(self):
|
Chris@87
|
2018 x = np.array([])
|
Chris@87
|
2019 assert_equal(np.roll(x, 1), np.array([]))
|
Chris@87
|
2020
|
Chris@87
|
2021 class TestCross(TestCase):
|
Chris@87
|
2022 def test_2x2(self):
|
Chris@87
|
2023 u = [1, 2]
|
Chris@87
|
2024 v = [3, 4]
|
Chris@87
|
2025 z = -2
|
Chris@87
|
2026 cp = np.cross(u, v)
|
Chris@87
|
2027 assert_equal(cp, z)
|
Chris@87
|
2028 cp = np.cross(v, u)
|
Chris@87
|
2029 assert_equal(cp, -z)
|
Chris@87
|
2030
|
Chris@87
|
2031 def test_2x3(self):
|
Chris@87
|
2032 u = [1, 2]
|
Chris@87
|
2033 v = [3, 4, 5]
|
Chris@87
|
2034 z = np.array([10, -5, -2])
|
Chris@87
|
2035 cp = np.cross(u, v)
|
Chris@87
|
2036 assert_equal(cp, z)
|
Chris@87
|
2037 cp = np.cross(v, u)
|
Chris@87
|
2038 assert_equal(cp, -z)
|
Chris@87
|
2039
|
Chris@87
|
2040 def test_3x3(self):
|
Chris@87
|
2041 u = [1, 2, 3]
|
Chris@87
|
2042 v = [4, 5, 6]
|
Chris@87
|
2043 z = np.array([-3, 6, -3])
|
Chris@87
|
2044 cp = cross(u, v)
|
Chris@87
|
2045 assert_equal(cp, z)
|
Chris@87
|
2046 cp = np.cross(v, u)
|
Chris@87
|
2047 assert_equal(cp, -z)
|
Chris@87
|
2048
|
Chris@87
|
2049 def test_broadcasting(self):
|
Chris@87
|
2050 # Ticket #2624 (Trac #2032)
|
Chris@87
|
2051 u = np.tile([1, 2], (11, 1))
|
Chris@87
|
2052 v = np.tile([3, 4], (11, 1))
|
Chris@87
|
2053 z = -2
|
Chris@87
|
2054 assert_equal(np.cross(u, v), z)
|
Chris@87
|
2055 assert_equal(np.cross(v, u), -z)
|
Chris@87
|
2056 assert_equal(np.cross(u, u), 0)
|
Chris@87
|
2057
|
Chris@87
|
2058 u = np.tile([1, 2], (11, 1)).T
|
Chris@87
|
2059 v = np.tile([3, 4, 5], (11, 1))
|
Chris@87
|
2060 z = np.tile([10, -5, -2], (11, 1))
|
Chris@87
|
2061 assert_equal(np.cross(u, v, axisa=0), z)
|
Chris@87
|
2062 assert_equal(np.cross(v, u.T), -z)
|
Chris@87
|
2063 assert_equal(np.cross(v, v), 0)
|
Chris@87
|
2064
|
Chris@87
|
2065 u = np.tile([1, 2, 3], (11, 1)).T
|
Chris@87
|
2066 v = np.tile([3, 4], (11, 1)).T
|
Chris@87
|
2067 z = np.tile([-12, 9, -2], (11, 1))
|
Chris@87
|
2068 assert_equal(np.cross(u, v, axisa=0, axisb=0), z)
|
Chris@87
|
2069 assert_equal(np.cross(v.T, u.T), -z)
|
Chris@87
|
2070 assert_equal(np.cross(u.T, u.T), 0)
|
Chris@87
|
2071
|
Chris@87
|
2072 u = np.tile([1, 2, 3], (5, 1))
|
Chris@87
|
2073 v = np.tile([4, 5, 6], (5, 1)).T
|
Chris@87
|
2074 z = np.tile([-3, 6, -3], (5, 1))
|
Chris@87
|
2075 assert_equal(np.cross(u, v, axisb=0), z)
|
Chris@87
|
2076 assert_equal(np.cross(v.T, u), -z)
|
Chris@87
|
2077 assert_equal(np.cross(u, u), 0)
|
Chris@87
|
2078
|
Chris@87
|
2079 def test_broadcasting_shapes(self):
|
Chris@87
|
2080 u = np.ones((2, 1, 3))
|
Chris@87
|
2081 v = np.ones((5, 3))
|
Chris@87
|
2082 assert_equal(np.cross(u, v).shape, (2, 5, 3))
|
Chris@87
|
2083 u = np.ones((10, 3, 5))
|
Chris@87
|
2084 v = np.ones((2, 5))
|
Chris@87
|
2085 assert_equal(np.cross(u, v, axisa=1, axisb=0).shape, (10, 5, 3))
|
Chris@87
|
2086 assert_raises(ValueError, np.cross, u, v, axisa=1, axisb=2)
|
Chris@87
|
2087 assert_raises(ValueError, np.cross, u, v, axisa=3, axisb=0)
|
Chris@87
|
2088 u = np.ones((10, 3, 5, 7))
|
Chris@87
|
2089 v = np.ones((5, 7, 2))
|
Chris@87
|
2090 assert_equal(np.cross(u, v, axisa=1, axisc=2).shape, (10, 5, 3, 7))
|
Chris@87
|
2091 assert_raises(ValueError, np.cross, u, v, axisa=-5, axisb=2)
|
Chris@87
|
2092 assert_raises(ValueError, np.cross, u, v, axisa=1, axisb=-4)
|
Chris@87
|
2093
|
Chris@87
|
2094 def test_outer_out_param():
|
Chris@87
|
2095 arr1 = np.ones((5,))
|
Chris@87
|
2096 arr2 = np.ones((2,))
|
Chris@87
|
2097 arr3 = np.linspace(-2, 2, 5)
|
Chris@87
|
2098 out1 = np.ndarray(shape=(5,5))
|
Chris@87
|
2099 out2 = np.ndarray(shape=(2, 5))
|
Chris@87
|
2100 res1 = np.outer(arr1, arr3, out1)
|
Chris@87
|
2101 assert_equal(res1, out1)
|
Chris@87
|
2102 assert_equal(np.outer(arr2, arr3, out2), out2)
|
Chris@87
|
2103
|
Chris@87
|
2104 if __name__ == "__main__":
|
Chris@87
|
2105 run_module_suite()
|