codekingpro/portable-devtools
114k
1import pickle
2from functools import reduce
3
4import pytest
5
6import numpy as np
7import numpy._core.fromnumeric as fromnumeric
8import numpy._core.umath as umath
9from numpy.ma import (
10 MaskedArray,
11 MaskType,
12 absolute,
13 add,
14 all,
15 allclose,
16 allequal,
17 alltrue,
18 arange,
19 arccos,
20 arcsin,
21 arctan,
22 arctan2,
23 array,
24 average,
25 choose,
26 concatenate,
27 conjugate,
28 cos,
29 cosh,
30 count,
31 divide,
32 equal,
33 exp,
34 filled,
35 getmask,
36 greater,
37 greater_equal,
38 inner,
39 isMaskedArray,
40 less,
41 less_equal,
42 log,
43 log10,
44 make_mask,
45 masked,
46 masked_array,
47 masked_equal,
48 masked_greater,
49 masked_greater_equal,
50 masked_inside,
51 masked_less,
52 masked_less_equal,
53 masked_not_equal,
54 masked_outside,
55 masked_print_option,
56 masked_values,
57 masked_where,
58 maximum,
59 minimum,
60 multiply,
61 nomask,
62 nonzero,
63 not_equal,
64 ones,
65 outer,
66 product,
67 put,
68 ravel,
69 repeat,
70 resize,
71 shape,
72 sin,
73 sinh,
74 sometrue,
75 sort,
76 sqrt,
77 subtract,
78 sum,
79 take,
80 tan,
81 tanh,
82 transpose,
83 where,
84 zeros,
85)
86from numpy.testing import assert_, assert_equal, assert_raises
87
88pi = np.pi
89
90
91def eq(v, w, msg=''):
92 result = allclose(v, w)
93 if not result:
94 print(f'Not eq:{msg}\n{v}\n----{w}')
95 return result
96
97
98class TestMa:
99
100 def _create_data(self):
101 x = np.array([1., 1., 1., -2., pi / 2.0, 4., 5., -10., 10., 1., 2., 3.])
102 y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
103 a10 = 10.
104 m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
105 m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
106 xm = array(x, mask=m1)
107 ym = array(y, mask=m2)
108 z = np.array([-.5, 0., .5, .8])
109 zm = array(z, mask=[0, 1, 0, 0])
110 xf = np.where(m1, 1e+20, x)
111 s = x.shape
112 xm.set_fill_value(1e+20)
113 return x, y, a10, m1, m2, xm, ym, z, zm, xf, s
114
115 def test_testBasic1d(self):
116 # Test of basic array creation and properties in 1 dimension.
117 x, _, _, m1, _, xm, _, _, _, xf, s = self._create_data()
118 assert_(not isMaskedArray(x))
119 assert_(isMaskedArray(xm))
120 assert_equal(shape(xm), s)
121 assert_equal(xm.shape, s)
122 assert_equal(xm.dtype, x.dtype)
123 assert_equal(xm.size, reduce(lambda x, y: x * y, s))
124 assert_equal(count(xm), len(m1) - reduce(lambda x, y: x + y, m1))
125 assert_(eq(xm, xf))
126 assert_(eq(filled(xm, 1.e20), xf))
127 assert_(eq(x, xm))
128
129 @pytest.mark.parametrize("s", [(4, 3), (6, 2)])
130 def test_testBasic2d(self, s):
131 # Test of basic array creation and properties in 2 dimensions.
132 x, y, _, m1, _, xm, ym, _, _, xf, s = self._create_data()
133 x.shape = s
134 y.shape = s
135 xm.shape = s
136 ym.shape = s
137 xf.shape = s
138
139 assert_(not isMaskedArray(x))
140 assert_(isMaskedArray(xm))
141 assert_equal(shape(xm), s)
142 assert_equal(xm.shape, s)
143 assert_equal(xm.size, reduce(lambda x, y: x * y, s))
144 assert_equal(count(xm), len(m1) - reduce(lambda x, y: x + y, m1))
145 assert_(eq(xm, xf))
146 assert_(eq(filled(xm, 1.e20), xf))
147 assert_(eq(x, xm))
148
149 def test_testArithmetic(self):
150 # Test of basic arithmetic.
151 x, y, a10, _, _, xm, ym, _, _, xf, s = self._create_data()
152 a2d = array([[1, 2], [0, 4]])
153 a2dm = masked_array(a2d, [[0, 0], [1, 0]])
154 assert_(eq(a2d * a2d, a2d * a2dm))
155 assert_(eq(a2d + a2d, a2d + a2dm))
156 assert_(eq(a2d - a2d, a2d - a2dm))
157 for s in [(12,), (4, 3), (2, 6)]:
158 x = x.reshape(s)
159 y = y.reshape(s)
160 xm = xm.reshape(s)
161 ym = ym.reshape(s)
162 xf = xf.reshape(s)
163 assert_(eq(-x, -xm))
164 assert_(eq(x + y, xm + ym))
165 assert_(eq(x - y, xm - ym))
166 assert_(eq(x * y, xm * ym))
167 with np.errstate(divide='ignore', invalid='ignore'):
168 assert_(eq(x / y, xm / ym))
169 assert_(eq(a10 + y, a10 + ym))
170 assert_(eq(a10 - y, a10 - ym))
171 assert_(eq(a10 * y, a10 * ym))
172 with np.errstate(divide='ignore', invalid='ignore'):
173 assert_(eq(a10 / y, a10 / ym))
174 assert_(eq(x + a10, xm + a10))
175 assert_(eq(x - a10, xm - a10))
176 assert_(eq(x * a10, xm * a10))
177 assert_(eq(x / a10, xm / a10))
178 assert_(eq(x ** 2, xm ** 2))
179 assert_(eq(abs(x) ** 2.5, abs(xm) ** 2.5))
180 assert_(eq(x ** y, xm ** ym))
181 assert_(eq(np.add(x, y), add(xm, ym)))
182 assert_(eq(np.subtract(x, y), subtract(xm, ym)))
183 assert_(eq(np.multiply(x, y), multiply(xm, ym)))
184 with np.errstate(divide='ignore', invalid='ignore'):
185 assert_(eq(np.divide(x, y), divide(xm, ym)))
186
187 def test_testMixedArithmetic(self):
188 na = np.array([1])
189 ma = array([1])
190 assert_(isinstance(na + ma, MaskedArray))
191 assert_(isinstance(ma + na, MaskedArray))
192
193 def test_testUfuncs1(self):
194 # Test various functions such as sin, cos.
195 x, y, _, _, _, xm, ym, z, zm, _, _ = self._create_data()
196 assert_(eq(np.cos(x), cos(xm)))
197 assert_(eq(np.cosh(x), cosh(xm)))
198 assert_(eq(np.sin(x), sin(xm)))
199 assert_(eq(np.sinh(x), sinh(xm)))
200 assert_(eq(np.tan(x), tan(xm)))
201 assert_(eq(np.tanh(x), tanh(xm)))
202 with np.errstate(divide='ignore', invalid='ignore'):
203 assert_(eq(np.sqrt(abs(x)), sqrt(xm)))
204 assert_(eq(np.log(abs(x)), log(xm)))
205 assert_(eq(np.log10(abs(x)), log10(xm)))
206 assert_(eq(np.exp(x), exp(xm)))
207 assert_(eq(np.arcsin(z), arcsin(zm)))
208 assert_(eq(np.arccos(z), arccos(zm)))
209 assert_(eq(np.arctan(z), arctan(zm)))
210 assert_(eq(np.arctan2(x, y), arctan2(xm, ym)))
211 assert_(eq(np.absolute(x), absolute(xm)))
212 assert_(eq(np.equal(x, y), equal(xm, ym)))
213 assert_(eq(np.not_equal(x, y), not_equal(xm, ym)))
214 assert_(eq(np.less(x, y), less(xm, ym)))
215 assert_(eq(np.greater(x, y), greater(xm, ym)))
216 assert_(eq(np.less_equal(x, y), less_equal(xm, ym)))
217 assert_(eq(np.greater_equal(x, y), greater_equal(xm, ym)))
218 assert_(eq(np.conjugate(x), conjugate(xm)))
219 assert_(eq(np.concatenate((x, y)), concatenate((xm, ym))))
220 assert_(eq(np.concatenate((x, y)), concatenate((x, y))))
221 assert_(eq(np.concatenate((x, y)), concatenate((xm, y))))
222 assert_(eq(np.concatenate((x, y, x)), concatenate((x, ym, x))))
223
224 def test_xtestCount(self):
225 # Test count
226 ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
227 assert_(count(ott).dtype.type is np.intp)
228 assert_equal(3, count(ott))
229 assert_equal(1, count(1))
230 assert_(eq(0, array(1, mask=[1])))
231 ott = ott.reshape((2, 2))
232 assert_(count(ott).dtype.type is np.intp)
233 assert_(isinstance(count(ott, 0), np.ndarray))
234 assert_(count(ott).dtype.type is np.intp)
235 assert_(eq(3, count(ott)))
236 assert_(getmask(count(ott, 0)) is nomask)
237 assert_(eq([1, 2], count(ott, 0)))
238
239 def test_testMinMax(self):
240 # Test minimum and maximum.
241 x, _, _, _, _, xm, _, _, _, _, _ = self._create_data()
242 xr = np.ravel(x) # max doesn't work if shaped
243 xmr = ravel(xm)
244
245 # true because of careful selection of data
246 assert_(eq(max(xr), maximum.reduce(xmr)))
247 assert_(eq(min(xr), minimum.reduce(xmr)))
248
249 def test_testAddSumProd(self):
250 # Test add, sum, product.
251 x, y, _, _, _, xm, ym, _, _, _, s = self._create_data()
252 assert_(eq(np.add.reduce(x), add.reduce(x)))
253 assert_(eq(np.add.accumulate(x), add.accumulate(x)))
254 assert_(eq(4, sum(array(4), axis=0)))
255 assert_(eq(4, sum(array(4), axis=0)))
256 assert_(eq(np.sum(x, axis=0), sum(x, axis=0)))
257 assert_(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)))
258 assert_(eq(np.sum(x, 0), sum(x, 0)))
259 assert_(eq(np.prod(x, axis=0), product(x, axis=0)))
260 assert_(eq(np.prod(x, 0), product(x, 0)))
261 assert_(eq(np.prod(filled(xm, 1), axis=0),
262 product(xm, axis=0)))
263 if len(s) > 1:
264 assert_(eq(np.concatenate((x, y), 1),
265 concatenate((xm, ym), 1)))
266 assert_(eq(np.add.reduce(x, 1), add.reduce(x, 1)))
267 assert_(eq(np.sum(x, 1), sum(x, 1)))
268 assert_(eq(np.prod(x, 1), product(x, 1)))
269
270 def test_testCI(self):
271 # Test of conversions and indexing
272 x1 = np.array([1, 2, 4, 3])
273 x2 = array(x1, mask=[1, 0, 0, 0])
274 x3 = array(x1, mask=[0, 1, 0, 1])
275 x4 = array(x1)
276 # test conversion to strings
277 str(x2) # raises?
278 repr(x2) # raises?
279 assert_(eq(np.sort(x1), sort(x2, fill_value=0)))
280 # tests of indexing
281 assert_(type(x2[1]) is type(x1[1]))
282 assert_(x1[1] == x2[1])
283 assert_(x2[0] is masked)
284 assert_(eq(x1[2], x2[2]))
285 assert_(eq(x1[2:5], x2[2:5]))
286 assert_(eq(x1[:], x2[:]))
287 assert_(eq(x1[1:], x3[1:]))
288 x1[2] = 9
289 x2[2] = 9
290 assert_(eq(x1, x2))
291 x1[1:3] = 99
292 x2[1:3] = 99
293 assert_(eq(x1, x2))
294 x2[1] = masked
295 assert_(eq(x1, x2))
296 x2[1:3] = masked
297 assert_(eq(x1, x2))
298 x2[:] = x1
299 x2[1] = masked
300 assert_(allequal(getmask(x2), array([0, 1, 0, 0])))
301 x3[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
302 assert_(allequal(getmask(x3), array([0, 1, 1, 0])))
303 x4[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
304 assert_(allequal(getmask(x4), array([0, 1, 1, 0])))
305 assert_(allequal(x4, array([1, 2, 3, 4])))
306 x1 = np.arange(5) * 1.0
307 x2 = masked_values(x1, 3.0)
308 assert_(eq(x1, x2))
309 assert_(allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask))
310 assert_(eq(3.0, x2.fill_value))
311 x1 = array([1, 'hello', 2, 3], object)
312 x2 = np.array([1, 'hello', 2, 3], object)
313 s1 = x1[1]
314 s2 = x2[1]
315 assert_equal(type(s2), str)
316 assert_equal(type(s1), str)
317 assert_equal(s1, s2)
318 assert_(x1[1:1].shape == (0,))
319
320 def test_testCopySize(self):
321 # Tests of some subtle points of copying and sizing.
322 n = [0, 0, 1, 0, 0]
323 m = make_mask(n)
324 m2 = make_mask(m)
325 assert_(m is m2)
326 m3 = make_mask(m, copy=True)
327 assert_(m is not m3)
328
329 x1 = np.arange(5)
330 y1 = array(x1, mask=m)
331 assert_(y1._data is not x1)
332 assert_(allequal(x1, y1._data))
333 assert_(y1._mask is m)
334
335 y1a = array(y1, copy=0)
336 # For copy=False, one might expect that the array would just
337 # passed on, i.e., that it would be "is" instead of "==".
338 # See gh-4043 for discussion.
339 assert_(y1a._mask.__array_interface__ ==
340 y1._mask.__array_interface__)
341
342 y2 = array(x1, mask=m3, copy=0)
343 assert_(y2._mask is m3)
344 assert_(y2[2] is masked)
345 y2[2] = 9
346 assert_(y2[2] is not masked)
347 assert_(y2._mask is m3)
348 assert_(allequal(y2.mask, 0))
349
350 y2a = array(x1, mask=m, copy=1)
351 assert_(y2a._mask is not m)
352 assert_(y2a[2] is masked)
353 y2a[2] = 9
354 assert_(y2a[2] is not masked)
355 assert_(y2a._mask is not m)
356 assert_(allequal(y2a.mask, 0))
357
358 y3 = array(x1 * 1.0, mask=m)
359 assert_(filled(y3).dtype is (x1 * 1.0).dtype)
360
361 x4 = arange(4)
362 x4[2] = masked
363 y4 = resize(x4, (8,))
364 assert_(eq(concatenate([x4, x4]), y4))
365 assert_(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0]))
366 y5 = repeat(x4, (2, 2, 2, 2), axis=0)
367 assert_(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3]))
368 y6 = repeat(x4, 2, axis=0)
369 assert_(eq(y5, y6))
370
371 def test_testPut(self):
372 # Test of put
373 d = arange(5)
374 n = [0, 0, 0, 1, 1]
375 m = make_mask(n)
376 m2 = m.copy()
377 x = array(d, mask=m)
378 assert_(x[3] is masked)
379 assert_(x[4] is masked)
380 x[[1, 4]] = [10, 40]
381 assert_(x._mask is m)
382 assert_(x[3] is masked)
383 assert_(x[4] is not masked)
384 assert_(eq(x, [0, 10, 2, -1, 40]))
385
386 x = array(d, mask=m2, copy=True)
387 x.put([0, 1, 2], [-1, 100, 200])
388 assert_(x._mask is not m2)
389 assert_(x[3] is masked)
390 assert_(x[4] is masked)
391 assert_(eq(x, [-1, 100, 200, 0, 0]))
392
393 def test_testPut2(self):
394 # Test of put
395 d = arange(5)
396 x = array(d, mask=[0, 0, 0, 0, 0])
397 z = array([10, 40], mask=[1, 0])
398 assert_(x[2] is not masked)
399 assert_(x[3] is not masked)
400 x[2:4] = z
401 assert_(x[2] is masked)
402 assert_(x[3] is not masked)
403 assert_(eq(x, [0, 1, 10, 40, 4]))
404
405 d = arange(5)
406 x = array(d, mask=[0, 0, 0, 0, 0])
407 y = x[2:4]
408 z = array([10, 40], mask=[1, 0])
409 assert_(x[2] is not masked)
410 assert_(x[3] is not masked)
411 y[:] = z
412 assert_(y[0] is masked)
413 assert_(y[1] is not masked)
414 assert_(eq(y, [10, 40]))
415 assert_(x[2] is masked)
416 assert_(x[3] is not masked)
417 assert_(eq(x, [0, 1, 10, 40, 4]))
418
419 def test_testMaPut(self):
420 _, _, _, _, _, _, ym, _, zm, _, _ = self._create_data()
421 m = [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]
422 i = np.nonzero(m)[0]
423 put(ym, i, zm)
424 assert_(all(take(ym, i, axis=0) == zm))
425
426 def test_testOddFeatures(self):
427 # Test of other odd features
428 x = arange(20)
429 x = x.reshape(4, 5)
430 x.flat[5] = 12
431 assert_(x[1, 0] == 12)
432 z = x + 10j * x
433 assert_(eq(z.real, x))
434 assert_(eq(z.imag, 10 * x))
435 assert_(eq((z * conjugate(z)).real, 101 * x * x))
436 z.imag[...] = 0.0
437
438 x = arange(10)
439 x[3] = masked
440 assert_(str(x[3]) == str(masked))
441 c = x >= 8
442 assert_(count(where(c, masked, masked)) == 0)
443 assert_(shape(where(c, masked, masked)) == c.shape)
444 z = where(c, x, masked)
445 assert_(z.dtype is x.dtype)
446 assert_(z[3] is masked)
447 assert_(z[4] is masked)
448 assert_(z[7] is masked)
449 assert_(z[8] is not masked)
450 assert_(z[9] is not masked)
451 assert_(eq(x, z))
452 z = where(c, masked, x)
453 assert_(z.dtype is x.dtype)
454 assert_(z[3] is masked)
455 assert_(z[4] is not masked)
456 assert_(z[7] is not masked)
457 assert_(z[8] is masked)
458 assert_(z[9] is masked)
459 z = masked_where(c, x)
460 assert_(z.dtype is x.dtype)
461 assert_(z[3] is masked)
462 assert_(z[4] is not masked)
463 assert_(z[7] is not masked)
464 assert_(z[8] is masked)
465 assert_(z[9] is masked)
466 assert_(eq(x, z))
467 x = array([1., 2., 3., 4., 5.])
468 c = array([1, 1, 1, 0, 0])
469 x[2] = masked
470 z = where(c, x, -x)
471 assert_(eq(z, [1., 2., 0., -4., -5]))
472 c[0] = masked
473 z = where(c, x, -x)
474 assert_(eq(z, [1., 2., 0., -4., -5]))
475 assert_(z[0] is masked)
476 assert_(z[1] is not masked)
477 assert_(z[2] is masked)
478 assert_(eq(masked_where(greater(x, 2), x), masked_greater(x, 2)))
479 assert_(eq(masked_where(greater_equal(x, 2), x),
480 masked_greater_equal(x, 2)))
481 assert_(eq(masked_where(less(x, 2), x), masked_less(x, 2)))
482 assert_(eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2)))
483 assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
484 assert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2)))
485 assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2)))
486 assert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4]))
487 assert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199]))
488 assert_(eq(masked_inside(array(list(range(5)),
489 mask=[1, 0, 0, 0, 0]), 1, 3).mask,
490 [1, 1, 1, 1, 0]))
491 assert_(eq(masked_outside(array(list(range(5)),
492 mask=[0, 1, 0, 0, 0]), 1, 3).mask,
493 [1, 1, 0, 0, 1]))
494 assert_(eq(masked_equal(array(list(range(5)),
495 mask=[1, 0, 0, 0, 0]), 2).mask,
496 [1, 0, 1, 0, 0]))
497 assert_(eq(masked_not_equal(array([2, 2, 1, 2, 1],
498 mask=[1, 0, 0, 0, 0]), 2).mask,
499 [1, 0, 1, 0, 1]))
500 assert_(eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]),
501 [99, 99, 3, 4, 5]))
502 atest = ones((10, 10, 10), dtype=np.float32)
503 btest = zeros(atest.shape, MaskType)
504 ctest = masked_where(btest, atest)
505 assert_(eq(atest, ctest))
506 z = choose(c, (-x, x))
507 assert_(eq(z, [1., 2., 0., -4., -5]))
508 assert_(z[0] is masked)
509 assert_(z[1] is not masked)
510 assert_(z[2] is masked)
511 x = arange(6)
512 x[5] = masked
513 y = arange(6) * 10
514 y[2] = masked
515 c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0])
516 cm = c.filled(1)
517 z = where(c, x, y)
518 zm = where(cm, x, y)
519 assert_(eq(z, zm))
520 assert_(getmask(zm) is nomask)
521 assert_(eq(zm, [0, 1, 2, 30, 40, 50]))
522 z = where(c, masked, 1)
523 assert_(eq(z, [99, 99, 99, 1, 1, 1]))
524 z = where(c, 1, masked)
525 assert_(eq(z, [99, 1, 1, 99, 99, 99]))
526
527 def test_testMinMax2(self):
528 # Test of minimum, maximum.
529 assert_(eq(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3]))
530 assert_(eq(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9]))
531 x = arange(5)
532 y = arange(5) - 2
533 x[3] = masked
534 y[0] = masked
535 assert_(eq(minimum(x, y), where(less(x, y), x, y)))
536 assert_(eq(maximum(x, y), where(greater(x, y), x, y)))
537 assert_(minimum.reduce(x) == 0)
538 assert_(maximum.reduce(x) == 4)
539
540 def test_testTakeTransposeInnerOuter(self):
541 # Test of take, transpose, inner, outer products
542 x = arange(24)
543 y = np.arange(24)
544 x[5:6] = masked
545 x = x.reshape(2, 3, 4)
546 y = y.reshape(2, 3, 4)
547 assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
548 assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
549 assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
550 inner(x, y)))
551 assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
552 outer(x, y)))
553 y = array(['abc', 1, 'def', 2, 3], object)
554 y[2] = masked
555 t = take(y, [0, 3, 4])
556 assert_(t[0] == 'abc')
557 assert_(t[1] == 2)
558 assert_(t[2] == 3)
559
560 def test_testInplace(self):
561 # Test of inplace operations and rich comparisons
562 y = arange(10)
563
564 x = arange(10)
565 xm = arange(10)
566 xm[2] = masked
567 x += 1
568 assert_(eq(x, y + 1))
569 xm += 1
570 assert_(eq(x, y + 1))
571
572 x = arange(10)
573 xm = arange(10)
574 xm[2] = masked
575 x -= 1
576 assert_(eq(x, y - 1))
577 xm -= 1
578 assert_(eq(xm, y - 1))
579
580 x = arange(10) * 1.0
581 xm = arange(10) * 1.0
582 xm[2] = masked
583 x *= 2.0
584 assert_(eq(x, y * 2))
585 xm *= 2.0
586 assert_(eq(xm, y * 2))
587
588 x = arange(10) * 2
589 xm = arange(10)
590 xm[2] = masked
591 x //= 2
592 assert_(eq(x, y))
593 xm //= 2
594 assert_(eq(x, y))
595
596 x = arange(10) * 1.0
597 xm = arange(10) * 1.0
598 xm[2] = masked
599 x /= 2.0
600 assert_(eq(x, y / 2.0))
601 xm /= arange(10)
602 assert_(eq(xm, ones((10,))))
603
604 x = arange(10).astype(np.float32)
605 xm = arange(10)
606 xm[2] = masked
607 x += 1.
608 assert_(eq(x, y + 1.))
609
610 def test_testPickle(self):
611 # Test of pickling
612 x = arange(12)
613 x[4:10:2] = masked
614 x = x.reshape(4, 3)
615 for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
616 s = pickle.dumps(x, protocol=proto)
617 y = pickle.loads(s)
618 assert_(eq(x, y))
619
620 def test_testMasked(self):
621 # Test of masked element
622 xx = arange(6)
623 xx[1] = masked
624 assert_(str(masked) == '--')
625 assert_(xx[1] is masked)
626 assert_equal(filled(xx[1], 0), 0)
627
628 def test_testAverage1(self):
629 # Test of average.
630 ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
631 assert_(eq(2.0, average(ott, axis=0)))
632 assert_(eq(2.0, average(ott, weights=[1., 1., 2., 1.])))
633 result, wts = average(ott, weights=[1., 1., 2., 1.], returned=True)
634 assert_(eq(2.0, result))
635 assert_(wts == 4.0)
636 ott[:] = masked
637 assert_(average(ott, axis=0) is masked)
638 ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
639 ott = ott.reshape(2, 2)
640 ott[:, 1] = masked
641 assert_(eq(average(ott, axis=0), [2.0, 0.0]))
642 assert_(average(ott, axis=1)[0] is masked)
643 assert_(eq([2., 0.], average(ott, axis=0)))
644 result, wts = average(ott, axis=0, returned=True)
645 assert_(eq(wts, [1., 0.]))
646
647 def test_testAverage2(self):
648 # More tests of average.
649 w1 = [0, 1, 1, 1, 1, 0]
650 w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
651 x = arange(6)
652 assert_(allclose(average(x, axis=0), 2.5))
653 assert_(allclose(average(x, axis=0, weights=w1), 2.5))
654 y = array([arange(6), 2.0 * arange(6)])
655 assert_(allclose(average(y, None),
656 np.add.reduce(np.arange(6)) * 3. / 12.))
657 assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
658 assert_(allclose(average(y, axis=1),
659 [average(x, axis=0), average(x, axis=0) * 2.0]))
660 assert_(allclose(average(y, None, weights=w2), 20. / 6.))
661 assert_(allclose(average(y, axis=0, weights=w2),
662 [0., 1., 2., 3., 4., 10.]))
663 assert_(allclose(average(y, axis=1),
664 [average(x, axis=0), average(x, axis=0) * 2.0]))
665 m1 = zeros(6)
666 m2 = [0, 0, 1, 1, 0, 0]
667 m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
668 m4 = ones(6)
669 m5 = [0, 1, 1, 1, 1, 1]
670 assert_(allclose(average(masked_array(x, m1), axis=0), 2.5))
671 assert_(allclose(average(masked_array(x, m2), axis=0), 2.5))
672 assert_(average(masked_array(x, m4), axis=0) is masked)
673 assert_equal(average(masked_array(x, m5), axis=0), 0.0)
674 assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
675 z = masked_array(y, m3)
676 assert_(allclose(average(z, None), 20. / 6.))
677 assert_(allclose(average(z, axis=0),
678 [0., 1., 99., 99., 4.0, 7.5]))
679 assert_(allclose(average(z, axis=1), [2.5, 5.0]))
680 assert_(allclose(average(z, axis=0, weights=w2),
681 [0., 1., 99., 99., 4.0, 10.0]))
682
683 a = arange(6)
684 b = arange(6) * 3
685 r1, w1 = average([[a, b], [b, a]], axis=1, returned=True)
686 assert_equal(shape(r1), shape(w1))
687 assert_equal(r1.shape, w1.shape)
688 r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=True)
689 assert_equal(shape(w2), shape(r2))
690 r2, w2 = average(ones((2, 2, 3)), returned=True)
691 assert_equal(shape(w2), shape(r2))
692 r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=True)
693 assert_(shape(w2) == shape(r2))
694 a2d = array([[1, 2], [0, 4]], float)
695 a2dm = masked_array(a2d, [[0, 0], [1, 0]])
696 a2da = average(a2d, axis=0)
697 assert_(eq(a2da, [0.5, 3.0]))
698 a2dma = average(a2dm, axis=0)
699 assert_(eq(a2dma, [1.0, 3.0]))
700 a2dma = average(a2dm, axis=None)
701 assert_(eq(a2dma, 7. / 3.))
702 a2dma = average(a2dm, axis=1)
703 assert_(eq(a2dma, [1.5, 4.0]))
704
705 def test_testToPython(self):
706 assert_equal(1, int(array(1)))
707 assert_equal(1.0, float(array(1)))
708 assert_equal(1, int(array([[[1]]])))
709 assert_equal(1.0, float(array([[1]])))
710 assert_raises(TypeError, float, array([1, 1]))
711 assert_raises(ValueError, bool, array([0, 1]))
712 assert_raises(ValueError, bool, array([0, 0], mask=[0, 1]))
713
714 def test_testScalarArithmetic(self):
715 xm = array(0, mask=1)
716 # TODO FIXME: Find out what the following raises a warning in r8247
717 with np.errstate(divide='ignore'):
718 assert_((1 / array(0)).mask)
719 assert_((1 + xm).mask)
720 assert_((-xm).mask)
721 assert_((-xm).mask)
722 assert_(maximum(xm, xm).mask)
723 assert_(minimum(xm, xm).mask)
724 assert_(xm.filled().dtype is xm._data.dtype)
725 x = array(0, mask=0)
726 assert_(x.filled() == x._data)
727 assert_equal(str(xm), str(masked_print_option))
728
729 def test_testArrayMethods(self):
730 a = array([1, 3, 2])
731 assert_(eq(a.any(), a._data.any()))
732 assert_(eq(a.all(), a._data.all()))
733 assert_(eq(a.argmax(), a._data.argmax()))
734 assert_(eq(a.argmin(), a._data.argmin()))
735 assert_(eq(a.choose(0, 1, 2, 3, 4),
736 a._data.choose(0, 1, 2, 3, 4)))
737 assert_(eq(a.compress([1, 0, 1]), a._data.compress([1, 0, 1])))
738 assert_(eq(a.conj(), a._data.conj()))
739 assert_(eq(a.conjugate(), a._data.conjugate()))
740 m = array([[1, 2], [3, 4]])
741 assert_(eq(m.diagonal(), m._data.diagonal()))
742 assert_(eq(a.sum(), a._data.sum()))
743 assert_(eq(a.take([1, 2]), a._data.take([1, 2])))
744 assert_(eq(m.transpose(), m._data.transpose()))
745
746 def test_testArrayAttributes(self):
747 a = array([1, 3, 2])
748 assert_equal(a.ndim, 1)
749
750 def test_testAPI(self):
751 assert_(not [m for m in dir(np.ndarray)
752 if m not in dir(MaskedArray) and
753 not m.startswith('_')])
754
755 def test_testSingleElementSubscript(self):
756 a = array([1, 3, 2])
757 b = array([1, 3, 2], mask=[1, 0, 1])
758 assert_equal(a[0].shape, ())
759 assert_equal(b[0].shape, ())
760 assert_equal(b[1].shape, ())
761
762 def test_assignment_by_condition(self):
763 # Test for gh-18951
764 a = array([1, 2, 3, 4], mask=[1, 0, 1, 0])
765 c = a >= 3
766 a[c] = 5
767 assert_(a[2] is masked)
768
769 def test_assignment_by_condition_2(self):
770 # gh-19721
771 a = masked_array([0, 1], mask=[False, False])
772 b = masked_array([0, 1], mask=[True, True])
773 mask = a < 1
774 b[mask] = a[mask]
775 expected_mask = [False, True]
776 assert_equal(b.mask, expected_mask)
777
778
779class TestUfuncs:
780
781 def _create_data(self):
782 return (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
783 array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
784
785 def test_testUfuncRegression(self):
786 f_invalid_ignore = [
787 'sqrt', 'arctanh', 'arcsin', 'arccos',
788 'arccosh', 'arctanh', 'log', 'log10', 'divide',
789 'true_divide', 'floor_divide', 'remainder', 'fmod']
790 for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
791 'sin', 'cos', 'tan',
792 'arcsin', 'arccos', 'arctan',
793 'sinh', 'cosh', 'tanh',
794 'arcsinh',
795 'arccosh',
796 'arctanh',
797 'absolute', 'fabs', 'negative',
798 'floor', 'ceil',
799 'logical_not',
800 'add', 'subtract', 'multiply',
801 'divide', 'true_divide', 'floor_divide',
802 'remainder', 'fmod', 'hypot', 'arctan2',
803 'equal', 'not_equal', 'less_equal', 'greater_equal',
804 'less', 'greater',
805 'logical_and', 'logical_or', 'logical_xor']:
806 try:
807 uf = getattr(umath, f)
808 except AttributeError:
809 uf = getattr(fromnumeric, f)
810 mf = getattr(np.ma, f)
811 args = self._create_data()[:uf.nin]
812 with np.errstate():
813 if f in f_invalid_ignore:
814 np.seterr(invalid='ignore')
815 if f in ['arctanh', 'log', 'log10']:
816 np.seterr(divide='ignore')
817 ur = uf(*args)
818 mr = mf(*args)
819 assert_(eq(ur.filled(0), mr.filled(0), f))
820 assert_(eqmask(ur.mask, mr.mask))
821
822 def test_reduce(self):
823 a = self._create_data()[0]
824 assert_(not alltrue(a, axis=0))
825 assert_(sometrue(a, axis=0))
826 assert_equal(sum(a[:3], axis=0), 0)
827 assert_equal(product(a, axis=0), 0)
828
829 def test_minmax(self):
830 a = arange(1, 13).reshape(3, 4)
831 amask = masked_where(a < 5, a)
832 assert_equal(amask.max(), a.max())
833 assert_equal(amask.min(), 5)
834 assert_((amask.max(0) == a.max(0)).all())
835 assert_((amask.min(0) == [5, 6, 7, 8]).all())
836 assert_(amask.max(1)[0].mask)
837 assert_(amask.min(1)[0].mask)
838
839 def test_nonzero(self):
840 for t in "?bhilqpBHILQPfdgFDGO":
841 x = array([1, 0, 2, 0], mask=[0, 0, 1, 1])
842 assert_(eq(nonzero(x), [0]))
843
844
845class TestArrayMethods:
846
847 def _create_data(self):
848 x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
849 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
850 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
851 6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
852 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
853 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
854 X = x.reshape(6, 6)
855 XX = x.reshape(3, 2, 2, 3)
856
857 m = np.array([0, 1, 0, 1, 0, 0,
858 1, 0, 1, 1, 0, 1,
859 0, 0, 0, 1, 0, 1,
860 0, 0, 0, 1, 1, 1,
861 1, 0, 0, 1, 0, 0,
862 0, 0, 1, 0, 1, 0])
863 mx = array(data=x, mask=m)
864 mX = array(data=X, mask=m.reshape(X.shape))
865 mXX = array(data=XX, mask=m.reshape(XX.shape))
866
867 return x, X, XX, m, mx, mX, mXX
868
869 def test_trace(self):
870 _, X, _, _, _, mX, _ = self._create_data()
871 mXdiag = mX.diagonal()
872 assert_equal(mX.trace(), mX.diagonal().compressed().sum())
873 assert_(eq(mX.trace(),
874 X.trace() - sum(mXdiag.mask * X.diagonal(),
875 axis=0)))
876
877 def test_clip(self):
878 x, _, _, _, mx, _, _ = self._create_data()
879 clipped = mx.clip(2, 8)
880 assert_(eq(clipped.mask, mx.mask))
881 assert_(eq(clipped._data, x.clip(2, 8)))
882 assert_(eq(clipped._data, mx._data.clip(2, 8)))
883
884 def test_ptp(self):
885 _, X, _, m, mx, mX, _ = self._create_data()
886 n, m = X.shape
887 # print(type(mx), mx.compressed())
888 # raise Exception()
889 assert_equal(mx.ptp(), np.ptp(mx.compressed()))
890 rows = np.zeros(n, np.float64)
891 cols = np.zeros(m, np.float64)
892 for k in range(m):
893 cols[k] = np.ptp(mX[:, k].compressed())
894 for k in range(n):
895 rows[k] = np.ptp(mX[k].compressed())
896 assert_(eq(mX.ptp(0), cols))
897 assert_(eq(mX.ptp(1), rows))
898
899 def test_swapaxes(self):
900 _, _, _, _, _, mX, mXX = self._create_data()
901 mXswapped = mX.swapaxes(0, 1)
902 assert_(eq(mXswapped[-1], mX[:, -1]))
903 mXXswapped = mXX.swapaxes(0, 2)
904 assert_equal(mXXswapped.shape, (2, 2, 3, 3))
905
906 def test_cumprod(self):
907 mX = self._create_data()[5]
908 mXcp = mX.cumprod(0)
909 assert_(eq(mXcp._data, mX.filled(1).cumprod(0)))
910 mXcp = mX.cumprod(1)
911 assert_(eq(mXcp._data, mX.filled(1).cumprod(1)))
912
913 def test_cumsum(self):
914 mX = self._create_data()[5]
915 mXcp = mX.cumsum(0)
916 assert_(eq(mXcp._data, mX.filled(0).cumsum(0)))
917 mXcp = mX.cumsum(1)
918 assert_(eq(mXcp._data, mX.filled(0).cumsum(1)))
919
920 def test_varstd(self):
921 _, X, XX, _, _, mX, mXX = self._create_data()
922 assert_(eq(mX.var(axis=None), mX.compressed().var()))
923 assert_(eq(mX.std(axis=None), mX.compressed().std()))
924 assert_(eq(mXX.var(axis=3).shape, XX.var(axis=3).shape))
925 assert_(eq(mX.var().shape, X.var().shape))
926 (mXvar0, mXvar1) = (mX.var(axis=0), mX.var(axis=1))
927 for k in range(6):
928 assert_(eq(mXvar1[k], mX[k].compressed().var()))
929 assert_(eq(mXvar0[k], mX[:, k].compressed().var()))
930 assert_(eq(np.sqrt(mXvar0[k]),
931 mX[:, k].compressed().std()))
932
933
934def eqmask(m1, m2):
935 if m1 is nomask:
936 return m2 is nomask
937 if m2 is nomask:
938 return m1 is nomask
939 return (m1 == m2).all()
940 