m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1705 lines
66 KiB

6 months ago
  1. # pylint: disable-msg=W0611, W0612, W0511
  2. """Tests suite for MaskedArray.
  3. Adapted from the original test_ma by Pierre Gerard-Marchant
  4. :author: Pierre Gerard-Marchant
  5. :contact: pierregm_at_uga_dot_edu
  6. :version: $Id: test_extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $
  7. """
  8. import warnings
  9. import itertools
  10. import pytest
  11. import numpy as np
  12. from numpy.testing import (
  13. assert_warns, suppress_warnings
  14. )
  15. from numpy.ma.testutils import (
  16. assert_, assert_array_equal, assert_equal, assert_almost_equal
  17. )
  18. from numpy.ma.core import (
  19. array, arange, masked, MaskedArray, masked_array, getmaskarray, shape,
  20. nomask, ones, zeros, count
  21. )
  22. from numpy.ma.extras import (
  23. atleast_1d, atleast_2d, atleast_3d, mr_, dot, polyfit, cov, corrcoef,
  24. median, average, unique, setxor1d, setdiff1d, union1d, intersect1d, in1d,
  25. ediff1d, apply_over_axes, apply_along_axis, compress_nd, compress_rowcols,
  26. mask_rowcols, clump_masked, clump_unmasked, flatnotmasked_contiguous,
  27. notmasked_contiguous, notmasked_edges, masked_all, masked_all_like, isin,
  28. diagflat, stack, vstack
  29. )
  30. class TestGeneric:
  31. #
  32. def test_masked_all(self):
  33. # Tests masked_all
  34. # Standard dtype
  35. test = masked_all((2,), dtype=float)
  36. control = array([1, 1], mask=[1, 1], dtype=float)
  37. assert_equal(test, control)
  38. # Flexible dtype
  39. dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
  40. test = masked_all((2,), dtype=dt)
  41. control = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
  42. assert_equal(test, control)
  43. test = masked_all((2, 2), dtype=dt)
  44. control = array([[(0, 0), (0, 0)], [(0, 0), (0, 0)]],
  45. mask=[[(1, 1), (1, 1)], [(1, 1), (1, 1)]],
  46. dtype=dt)
  47. assert_equal(test, control)
  48. # Nested dtype
  49. dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
  50. test = masked_all((2,), dtype=dt)
  51. control = array([(1, (1, 1)), (1, (1, 1))],
  52. mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
  53. assert_equal(test, control)
  54. test = masked_all((2,), dtype=dt)
  55. control = array([(1, (1, 1)), (1, (1, 1))],
  56. mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
  57. assert_equal(test, control)
  58. test = masked_all((1, 1), dtype=dt)
  59. control = array([[(1, (1, 1))]], mask=[[(1, (1, 1))]], dtype=dt)
  60. assert_equal(test, control)
  61. def test_masked_all_with_object_nested(self):
  62. # Test masked_all works with nested array with dtype of an 'object'
  63. # refers to issue #15895
  64. my_dtype = np.dtype([('b', ([('c', object)], (1,)))])
  65. masked_arr = np.ma.masked_all((1,), my_dtype)
  66. assert_equal(type(masked_arr['b']), np.ma.core.MaskedArray)
  67. assert_equal(type(masked_arr['b']['c']), np.ma.core.MaskedArray)
  68. assert_equal(len(masked_arr['b']['c']), 1)
  69. assert_equal(masked_arr['b']['c'].shape, (1, 1))
  70. assert_equal(masked_arr['b']['c']._fill_value.shape, ())
  71. def test_masked_all_with_object(self):
  72. # same as above except that the array is not nested
  73. my_dtype = np.dtype([('b', (object, (1,)))])
  74. masked_arr = np.ma.masked_all((1,), my_dtype)
  75. assert_equal(type(masked_arr['b']), np.ma.core.MaskedArray)
  76. assert_equal(len(masked_arr['b']), 1)
  77. assert_equal(masked_arr['b'].shape, (1, 1))
  78. assert_equal(masked_arr['b']._fill_value.shape, ())
  79. def test_masked_all_like(self):
  80. # Tests masked_all
  81. # Standard dtype
  82. base = array([1, 2], dtype=float)
  83. test = masked_all_like(base)
  84. control = array([1, 1], mask=[1, 1], dtype=float)
  85. assert_equal(test, control)
  86. # Flexible dtype
  87. dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']})
  88. base = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt)
  89. test = masked_all_like(base)
  90. control = array([(10, 10), (10, 10)], mask=[(1, 1), (1, 1)], dtype=dt)
  91. assert_equal(test, control)
  92. # Nested dtype
  93. dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])])
  94. control = array([(1, (1, 1)), (1, (1, 1))],
  95. mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt)
  96. test = masked_all_like(control)
  97. assert_equal(test, control)
  98. def check_clump(self, f):
  99. for i in range(1, 7):
  100. for j in range(2**i):
  101. k = np.arange(i, dtype=int)
  102. ja = np.full(i, j, dtype=int)
  103. a = masked_array(2**k)
  104. a.mask = (ja & (2**k)) != 0
  105. s = 0
  106. for sl in f(a):
  107. s += a.data[sl].sum()
  108. if f == clump_unmasked:
  109. assert_equal(a.compressed().sum(), s)
  110. else:
  111. a.mask = ~a.mask
  112. assert_equal(a.compressed().sum(), s)
  113. def test_clump_masked(self):
  114. # Test clump_masked
  115. a = masked_array(np.arange(10))
  116. a[[0, 1, 2, 6, 8, 9]] = masked
  117. #
  118. test = clump_masked(a)
  119. control = [slice(0, 3), slice(6, 7), slice(8, 10)]
  120. assert_equal(test, control)
  121. self.check_clump(clump_masked)
  122. def test_clump_unmasked(self):
  123. # Test clump_unmasked
  124. a = masked_array(np.arange(10))
  125. a[[0, 1, 2, 6, 8, 9]] = masked
  126. test = clump_unmasked(a)
  127. control = [slice(3, 6), slice(7, 8), ]
  128. assert_equal(test, control)
  129. self.check_clump(clump_unmasked)
  130. def test_flatnotmasked_contiguous(self):
  131. # Test flatnotmasked_contiguous
  132. a = arange(10)
  133. # No mask
  134. test = flatnotmasked_contiguous(a)
  135. assert_equal(test, [slice(0, a.size)])
  136. # mask of all false
  137. a.mask = np.zeros(10, dtype=bool)
  138. assert_equal(test, [slice(0, a.size)])
  139. # Some mask
  140. a[(a < 3) | (a > 8) | (a == 5)] = masked
  141. test = flatnotmasked_contiguous(a)
  142. assert_equal(test, [slice(3, 5), slice(6, 9)])
  143. #
  144. a[:] = masked
  145. test = flatnotmasked_contiguous(a)
  146. assert_equal(test, [])
  147. class TestAverage:
  148. # Several tests of average. Why so many ? Good point...
  149. def test_testAverage1(self):
  150. # Test of average.
  151. ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
  152. assert_equal(2.0, average(ott, axis=0))
  153. assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
  154. result, wts = average(ott, weights=[1., 1., 2., 1.], returned=True)
  155. assert_equal(2.0, result)
  156. assert_(wts == 4.0)
  157. ott[:] = masked
  158. assert_equal(average(ott, axis=0).mask, [True])
  159. ott = array([0., 1., 2., 3.], mask=[True, False, False, False])
  160. ott = ott.reshape(2, 2)
  161. ott[:, 1] = masked
  162. assert_equal(average(ott, axis=0), [2.0, 0.0])
  163. assert_equal(average(ott, axis=1).mask[0], [True])
  164. assert_equal([2., 0.], average(ott, axis=0))
  165. result, wts = average(ott, axis=0, returned=True)
  166. assert_equal(wts, [1., 0.])
  167. def test_testAverage2(self):
  168. # More tests of average.
  169. w1 = [0, 1, 1, 1, 1, 0]
  170. w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
  171. x = arange(6, dtype=np.float_)
  172. assert_equal(average(x, axis=0), 2.5)
  173. assert_equal(average(x, axis=0, weights=w1), 2.5)
  174. y = array([arange(6, dtype=np.float_), 2.0 * arange(6)])
  175. assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)
  176. assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.)
  177. assert_equal(average(y, axis=1),
  178. [average(x, axis=0), average(x, axis=0) * 2.0])
  179. assert_equal(average(y, None, weights=w2), 20. / 6.)
  180. assert_equal(average(y, axis=0, weights=w2),
  181. [0., 1., 2., 3., 4., 10.])
  182. assert_equal(average(y, axis=1),
  183. [average(x, axis=0), average(x, axis=0) * 2.0])
  184. m1 = zeros(6)
  185. m2 = [0, 0, 1, 1, 0, 0]
  186. m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
  187. m4 = ones(6)
  188. m5 = [0, 1, 1, 1, 1, 1]
  189. assert_equal(average(masked_array(x, m1), axis=0), 2.5)
  190. assert_equal(average(masked_array(x, m2), axis=0), 2.5)
  191. assert_equal(average(masked_array(x, m4), axis=0).mask, [True])
  192. assert_equal(average(masked_array(x, m5), axis=0), 0.0)
  193. assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
  194. z = masked_array(y, m3)
  195. assert_equal(average(z, None), 20. / 6.)
  196. assert_equal(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])
  197. assert_equal(average(z, axis=1), [2.5, 5.0])
  198. assert_equal(average(z, axis=0, weights=w2),
  199. [0., 1., 99., 99., 4.0, 10.0])
  200. def test_testAverage3(self):
  201. # Yet more tests of average!
  202. a = arange(6)
  203. b = arange(6) * 3
  204. r1, w1 = average([[a, b], [b, a]], axis=1, returned=True)
  205. assert_equal(shape(r1), shape(w1))
  206. assert_equal(r1.shape, w1.shape)
  207. r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=True)
  208. assert_equal(shape(w2), shape(r2))
  209. r2, w2 = average(ones((2, 2, 3)), returned=True)
  210. assert_equal(shape(w2), shape(r2))
  211. r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=True)
  212. assert_equal(shape(w2), shape(r2))
  213. a2d = array([[1, 2], [0, 4]], float)
  214. a2dm = masked_array(a2d, [[False, False], [True, False]])
  215. a2da = average(a2d, axis=0)
  216. assert_equal(a2da, [0.5, 3.0])
  217. a2dma = average(a2dm, axis=0)
  218. assert_equal(a2dma, [1.0, 3.0])
  219. a2dma = average(a2dm, axis=None)
  220. assert_equal(a2dma, 7. / 3.)
  221. a2dma = average(a2dm, axis=1)
  222. assert_equal(a2dma, [1.5, 4.0])
  223. def test_onintegers_with_mask(self):
  224. # Test average on integers with mask
  225. a = average(array([1, 2]))
  226. assert_equal(a, 1.5)
  227. a = average(array([1, 2, 3, 4], mask=[False, False, True, True]))
  228. assert_equal(a, 1.5)
  229. def test_complex(self):
  230. # Test with complex data.
  231. # (Regression test for https://github.com/numpy/numpy/issues/2684)
  232. mask = np.array([[0, 0, 0, 1, 0],
  233. [0, 1, 0, 0, 0]], dtype=bool)
  234. a = masked_array([[0, 1+2j, 3+4j, 5+6j, 7+8j],
  235. [9j, 0+1j, 2+3j, 4+5j, 7+7j]],
  236. mask=mask)
  237. av = average(a)
  238. expected = np.average(a.compressed())
  239. assert_almost_equal(av.real, expected.real)
  240. assert_almost_equal(av.imag, expected.imag)
  241. av0 = average(a, axis=0)
  242. expected0 = average(a.real, axis=0) + average(a.imag, axis=0)*1j
  243. assert_almost_equal(av0.real, expected0.real)
  244. assert_almost_equal(av0.imag, expected0.imag)
  245. av1 = average(a, axis=1)
  246. expected1 = average(a.real, axis=1) + average(a.imag, axis=1)*1j
  247. assert_almost_equal(av1.real, expected1.real)
  248. assert_almost_equal(av1.imag, expected1.imag)
  249. # Test with the 'weights' argument.
  250. wts = np.array([[0.5, 1.0, 2.0, 1.0, 0.5],
  251. [1.0, 1.0, 1.0, 1.0, 1.0]])
  252. wav = average(a, weights=wts)
  253. expected = np.average(a.compressed(), weights=wts[~mask])
  254. assert_almost_equal(wav.real, expected.real)
  255. assert_almost_equal(wav.imag, expected.imag)
  256. wav0 = average(a, weights=wts, axis=0)
  257. expected0 = (average(a.real, weights=wts, axis=0) +
  258. average(a.imag, weights=wts, axis=0)*1j)
  259. assert_almost_equal(wav0.real, expected0.real)
  260. assert_almost_equal(wav0.imag, expected0.imag)
  261. wav1 = average(a, weights=wts, axis=1)
  262. expected1 = (average(a.real, weights=wts, axis=1) +
  263. average(a.imag, weights=wts, axis=1)*1j)
  264. assert_almost_equal(wav1.real, expected1.real)
  265. assert_almost_equal(wav1.imag, expected1.imag)
  266. def test_masked_weights(self):
  267. # Test with masked weights.
  268. # (Regression test for https://github.com/numpy/numpy/issues/10438)
  269. a = np.ma.array(np.arange(9).reshape(3, 3),
  270. mask=[[1, 0, 0], [1, 0, 0], [0, 0, 0]])
  271. weights_unmasked = masked_array([5, 28, 31], mask=False)
  272. weights_masked = masked_array([5, 28, 31], mask=[1, 0, 0])
  273. avg_unmasked = average(a, axis=0,
  274. weights=weights_unmasked, returned=False)
  275. expected_unmasked = np.array([6.0, 5.21875, 6.21875])
  276. assert_almost_equal(avg_unmasked, expected_unmasked)
  277. avg_masked = average(a, axis=0, weights=weights_masked, returned=False)
  278. expected_masked = np.array([6.0, 5.576271186440678, 6.576271186440678])
  279. assert_almost_equal(avg_masked, expected_masked)
  280. class TestConcatenator:
  281. # Tests for mr_, the equivalent of r_ for masked arrays.
  282. def test_1d(self):
  283. # Tests mr_ on 1D arrays.
  284. assert_array_equal(mr_[1, 2, 3, 4, 5, 6], array([1, 2, 3, 4, 5, 6]))
  285. b = ones(5)
  286. m = [1, 0, 0, 0, 0]
  287. d = masked_array(b, mask=m)
  288. c = mr_[d, 0, 0, d]
  289. assert_(isinstance(c, MaskedArray))
  290. assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
  291. assert_array_equal(c.mask, mr_[m, 0, 0, m])
  292. def test_2d(self):
  293. # Tests mr_ on 2D arrays.
  294. a_1 = np.random.rand(5, 5)
  295. a_2 = np.random.rand(5, 5)
  296. m_1 = np.round_(np.random.rand(5, 5), 0)
  297. m_2 = np.round_(np.random.rand(5, 5), 0)
  298. b_1 = masked_array(a_1, mask=m_1)
  299. b_2 = masked_array(a_2, mask=m_2)
  300. # append columns
  301. d = mr_['1', b_1, b_2]
  302. assert_(d.shape == (5, 10))
  303. assert_array_equal(d[:, :5], b_1)
  304. assert_array_equal(d[:, 5:], b_2)
  305. assert_array_equal(d.mask, np.r_['1', m_1, m_2])
  306. d = mr_[b_1, b_2]
  307. assert_(d.shape == (10, 5))
  308. assert_array_equal(d[:5,:], b_1)
  309. assert_array_equal(d[5:,:], b_2)
  310. assert_array_equal(d.mask, np.r_[m_1, m_2])
  311. def test_masked_constant(self):
  312. actual = mr_[np.ma.masked, 1]
  313. assert_equal(actual.mask, [True, False])
  314. assert_equal(actual.data[1], 1)
  315. actual = mr_[[1, 2], np.ma.masked]
  316. assert_equal(actual.mask, [False, False, True])
  317. assert_equal(actual.data[:2], [1, 2])
  318. class TestNotMasked:
  319. # Tests notmasked_edges and notmasked_contiguous.
  320. def test_edges(self):
  321. # Tests unmasked_edges
  322. data = masked_array(np.arange(25).reshape(5, 5),
  323. mask=[[0, 0, 1, 0, 0],
  324. [0, 0, 0, 1, 1],
  325. [1, 1, 0, 0, 0],
  326. [0, 0, 0, 0, 0],
  327. [1, 1, 1, 0, 0]],)
  328. test = notmasked_edges(data, None)
  329. assert_equal(test, [0, 24])
  330. test = notmasked_edges(data, 0)
  331. assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
  332. assert_equal(test[1], [(3, 3, 3, 4, 4), (0, 1, 2, 3, 4)])
  333. test = notmasked_edges(data, 1)
  334. assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 2, 0, 3)])
  335. assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 2, 4, 4, 4)])
  336. #
  337. test = notmasked_edges(data.data, None)
  338. assert_equal(test, [0, 24])
  339. test = notmasked_edges(data.data, 0)
  340. assert_equal(test[0], [(0, 0, 0, 0, 0), (0, 1, 2, 3, 4)])
  341. assert_equal(test[1], [(4, 4, 4, 4, 4), (0, 1, 2, 3, 4)])
  342. test = notmasked_edges(data.data, -1)
  343. assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 0, 0, 0)])
  344. assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 4, 4, 4, 4)])
  345. #
  346. data[-2] = masked
  347. test = notmasked_edges(data, 0)
  348. assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
  349. assert_equal(test[1], [(1, 1, 2, 4, 4), (0, 1, 2, 3, 4)])
  350. test = notmasked_edges(data, -1)
  351. assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)])
  352. assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)])
  353. def test_contiguous(self):
  354. # Tests notmasked_contiguous
  355. a = masked_array(np.arange(24).reshape(3, 8),
  356. mask=[[0, 0, 0, 0, 1, 1, 1, 1],
  357. [1, 1, 1, 1, 1, 1, 1, 1],
  358. [0, 0, 0, 0, 0, 0, 1, 0]])
  359. tmp = notmasked_contiguous(a, None)
  360. assert_equal(tmp, [
  361. slice(0, 4, None),
  362. slice(16, 22, None),
  363. slice(23, 24, None)
  364. ])
  365. tmp = notmasked_contiguous(a, 0)
  366. assert_equal(tmp, [
  367. [slice(0, 1, None), slice(2, 3, None)],
  368. [slice(0, 1, None), slice(2, 3, None)],
  369. [slice(0, 1, None), slice(2, 3, None)],
  370. [slice(0, 1, None), slice(2, 3, None)],
  371. [slice(2, 3, None)],
  372. [slice(2, 3, None)],
  373. [],
  374. [slice(2, 3, None)]
  375. ])
  376. #
  377. tmp = notmasked_contiguous(a, 1)
  378. assert_equal(tmp, [
  379. [slice(0, 4, None)],
  380. [],
  381. [slice(0, 6, None), slice(7, 8, None)]
  382. ])
  383. class TestCompressFunctions:
  384. def test_compress_nd(self):
  385. # Tests compress_nd
  386. x = np.array(list(range(3*4*5))).reshape(3, 4, 5)
  387. m = np.zeros((3,4,5)).astype(bool)
  388. m[1,1,1] = True
  389. x = array(x, mask=m)
  390. # axis=None
  391. a = compress_nd(x)
  392. assert_equal(a, [[[ 0, 2, 3, 4],
  393. [10, 12, 13, 14],
  394. [15, 17, 18, 19]],
  395. [[40, 42, 43, 44],
  396. [50, 52, 53, 54],
  397. [55, 57, 58, 59]]])
  398. # axis=0
  399. a = compress_nd(x, 0)
  400. assert_equal(a, [[[ 0, 1, 2, 3, 4],
  401. [ 5, 6, 7, 8, 9],
  402. [10, 11, 12, 13, 14],
  403. [15, 16, 17, 18, 19]],
  404. [[40, 41, 42, 43, 44],
  405. [45, 46, 47, 48, 49],
  406. [50, 51, 52, 53, 54],
  407. [55, 56, 57, 58, 59]]])
  408. # axis=1
  409. a = compress_nd(x, 1)
  410. assert_equal(a, [[[ 0, 1, 2, 3, 4],
  411. [10, 11, 12, 13, 14],
  412. [15, 16, 17, 18, 19]],
  413. [[20, 21, 22, 23, 24],
  414. [30, 31, 32, 33, 34],
  415. [35, 36, 37, 38, 39]],
  416. [[40, 41, 42, 43, 44],
  417. [50, 51, 52, 53, 54],
  418. [55, 56, 57, 58, 59]]])
  419. a2 = compress_nd(x, (1,))
  420. a3 = compress_nd(x, -2)
  421. a4 = compress_nd(x, (-2,))
  422. assert_equal(a, a2)
  423. assert_equal(a, a3)
  424. assert_equal(a, a4)
  425. # axis=2
  426. a = compress_nd(x, 2)
  427. assert_equal(a, [[[ 0, 2, 3, 4],
  428. [ 5, 7, 8, 9],
  429. [10, 12, 13, 14],
  430. [15, 17, 18, 19]],
  431. [[20, 22, 23, 24],
  432. [25, 27, 28, 29],
  433. [30, 32, 33, 34],
  434. [35, 37, 38, 39]],
  435. [[40, 42, 43, 44],
  436. [45, 47, 48, 49],
  437. [50, 52, 53, 54],
  438. [55, 57, 58, 59]]])
  439. a2 = compress_nd(x, (2,))
  440. a3 = compress_nd(x, -1)
  441. a4 = compress_nd(x, (-1,))
  442. assert_equal(a, a2)
  443. assert_equal(a, a3)
  444. assert_equal(a, a4)
  445. # axis=(0, 1)
  446. a = compress_nd(x, (0, 1))
  447. assert_equal(a, [[[ 0, 1, 2, 3, 4],
  448. [10, 11, 12, 13, 14],
  449. [15, 16, 17, 18, 19]],
  450. [[40, 41, 42, 43, 44],
  451. [50, 51, 52, 53, 54],
  452. [55, 56, 57, 58, 59]]])
  453. a2 = compress_nd(x, (0, -2))
  454. assert_equal(a, a2)
  455. # axis=(1, 2)
  456. a = compress_nd(x, (1, 2))
  457. assert_equal(a, [[[ 0, 2, 3, 4],
  458. [10, 12, 13, 14],
  459. [15, 17, 18, 19]],
  460. [[20, 22, 23, 24],
  461. [30, 32, 33, 34],
  462. [35, 37, 38, 39]],
  463. [[40, 42, 43, 44],
  464. [50, 52, 53, 54],
  465. [55, 57, 58, 59]]])
  466. a2 = compress_nd(x, (-2, 2))
  467. a3 = compress_nd(x, (1, -1))
  468. a4 = compress_nd(x, (-2, -1))
  469. assert_equal(a, a2)
  470. assert_equal(a, a3)
  471. assert_equal(a, a4)
  472. # axis=(0, 2)
  473. a = compress_nd(x, (0, 2))
  474. assert_equal(a, [[[ 0, 2, 3, 4],
  475. [ 5, 7, 8, 9],
  476. [10, 12, 13, 14],
  477. [15, 17, 18, 19]],
  478. [[40, 42, 43, 44],
  479. [45, 47, 48, 49],
  480. [50, 52, 53, 54],
  481. [55, 57, 58, 59]]])
  482. a2 = compress_nd(x, (0, -1))
  483. assert_equal(a, a2)
  484. def test_compress_rowcols(self):
  485. # Tests compress_rowcols
  486. x = array(np.arange(9).reshape(3, 3),
  487. mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
  488. assert_equal(compress_rowcols(x), [[4, 5], [7, 8]])
  489. assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]])
  490. assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]])
  491. x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
  492. assert_equal(compress_rowcols(x), [[0, 2], [6, 8]])
  493. assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]])
  494. assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]])
  495. x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
  496. assert_equal(compress_rowcols(x), [[8]])
  497. assert_equal(compress_rowcols(x, 0), [[6, 7, 8]])
  498. assert_equal(compress_rowcols(x, 1,), [[2], [5], [8]])
  499. x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  500. assert_equal(compress_rowcols(x).size, 0)
  501. assert_equal(compress_rowcols(x, 0).size, 0)
  502. assert_equal(compress_rowcols(x, 1).size, 0)
  503. def test_mask_rowcols(self):
  504. # Tests mask_rowcols.
  505. x = array(np.arange(9).reshape(3, 3),
  506. mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
  507. assert_equal(mask_rowcols(x).mask,
  508. [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
  509. assert_equal(mask_rowcols(x, 0).mask,
  510. [[1, 1, 1], [0, 0, 0], [0, 0, 0]])
  511. assert_equal(mask_rowcols(x, 1).mask,
  512. [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
  513. x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
  514. assert_equal(mask_rowcols(x).mask,
  515. [[0, 1, 0], [1, 1, 1], [0, 1, 0]])
  516. assert_equal(mask_rowcols(x, 0).mask,
  517. [[0, 0, 0], [1, 1, 1], [0, 0, 0]])
  518. assert_equal(mask_rowcols(x, 1).mask,
  519. [[0, 1, 0], [0, 1, 0], [0, 1, 0]])
  520. x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]])
  521. assert_equal(mask_rowcols(x).mask,
  522. [[1, 1, 1], [1, 1, 1], [1, 1, 0]])
  523. assert_equal(mask_rowcols(x, 0).mask,
  524. [[1, 1, 1], [1, 1, 1], [0, 0, 0]])
  525. assert_equal(mask_rowcols(x, 1,).mask,
  526. [[1, 1, 0], [1, 1, 0], [1, 1, 0]])
  527. x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  528. assert_(mask_rowcols(x).all() is masked)
  529. assert_(mask_rowcols(x, 0).all() is masked)
  530. assert_(mask_rowcols(x, 1).all() is masked)
  531. assert_(mask_rowcols(x).mask.all())
  532. assert_(mask_rowcols(x, 0).mask.all())
  533. assert_(mask_rowcols(x, 1).mask.all())
  534. @pytest.mark.parametrize("axis", [None, 0, 1])
  535. @pytest.mark.parametrize(["func", "rowcols_axis"],
  536. [(np.ma.mask_rows, 0), (np.ma.mask_cols, 1)])
  537. def test_mask_row_cols_axis_deprecation(self, axis, func, rowcols_axis):
  538. # Test deprecation of the axis argument to `mask_rows` and `mask_cols`
  539. x = array(np.arange(9).reshape(3, 3),
  540. mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]])
  541. with assert_warns(DeprecationWarning):
  542. res = func(x, axis=axis)
  543. assert_equal(res, mask_rowcols(x, rowcols_axis))
  544. def test_dot(self):
  545. # Tests dot product
  546. n = np.arange(1, 7)
  547. #
  548. m = [1, 0, 0, 0, 0, 0]
  549. a = masked_array(n, mask=m).reshape(2, 3)
  550. b = masked_array(n, mask=m).reshape(3, 2)
  551. c = dot(a, b, strict=True)
  552. assert_equal(c.mask, [[1, 1], [1, 0]])
  553. c = dot(b, a, strict=True)
  554. assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]])
  555. c = dot(a, b, strict=False)
  556. assert_equal(c, np.dot(a.filled(0), b.filled(0)))
  557. c = dot(b, a, strict=False)
  558. assert_equal(c, np.dot(b.filled(0), a.filled(0)))
  559. #
  560. m = [0, 0, 0, 0, 0, 1]
  561. a = masked_array(n, mask=m).reshape(2, 3)
  562. b = masked_array(n, mask=m).reshape(3, 2)
  563. c = dot(a, b, strict=True)
  564. assert_equal(c.mask, [[0, 1], [1, 1]])
  565. c = dot(b, a, strict=True)
  566. assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]])
  567. c = dot(a, b, strict=False)
  568. assert_equal(c, np.dot(a.filled(0), b.filled(0)))
  569. assert_equal(c, dot(a, b))
  570. c = dot(b, a, strict=False)
  571. assert_equal(c, np.dot(b.filled(0), a.filled(0)))
  572. #
  573. m = [0, 0, 0, 0, 0, 0]
  574. a = masked_array(n, mask=m).reshape(2, 3)
  575. b = masked_array(n, mask=m).reshape(3, 2)
  576. c = dot(a, b)
  577. assert_equal(c.mask, nomask)
  578. c = dot(b, a)
  579. assert_equal(c.mask, nomask)
  580. #
  581. a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3)
  582. b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
  583. c = dot(a, b, strict=True)
  584. assert_equal(c.mask, [[1, 1], [0, 0]])
  585. c = dot(a, b, strict=False)
  586. assert_equal(c, np.dot(a.filled(0), b.filled(0)))
  587. c = dot(b, a, strict=True)
  588. assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]])
  589. c = dot(b, a, strict=False)
  590. assert_equal(c, np.dot(b.filled(0), a.filled(0)))
  591. #
  592. a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
  593. b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2)
  594. c = dot(a, b, strict=True)
  595. assert_equal(c.mask, [[0, 0], [1, 1]])
  596. c = dot(a, b)
  597. assert_equal(c, np.dot(a.filled(0), b.filled(0)))
  598. c = dot(b, a, strict=True)
  599. assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]])
  600. c = dot(b, a, strict=False)
  601. assert_equal(c, np.dot(b.filled(0), a.filled(0)))
  602. #
  603. a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3)
  604. b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2)
  605. c = dot(a, b, strict=True)
  606. assert_equal(c.mask, [[1, 0], [1, 1]])
  607. c = dot(a, b, strict=False)
  608. assert_equal(c, np.dot(a.filled(0), b.filled(0)))
  609. c = dot(b, a, strict=True)
  610. assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]])
  611. c = dot(b, a, strict=False)
  612. assert_equal(c, np.dot(b.filled(0), a.filled(0)))
  613. def test_dot_returns_maskedarray(self):
  614. # See gh-6611
  615. a = np.eye(3)
  616. b = array(a)
  617. assert_(type(dot(a, a)) is MaskedArray)
  618. assert_(type(dot(a, b)) is MaskedArray)
  619. assert_(type(dot(b, a)) is MaskedArray)
  620. assert_(type(dot(b, b)) is MaskedArray)
  621. def test_dot_out(self):
  622. a = array(np.eye(3))
  623. out = array(np.zeros((3, 3)))
  624. res = dot(a, a, out=out)
  625. assert_(res is out)
  626. assert_equal(a, res)
  627. class TestApplyAlongAxis:
  628. # Tests 2D functions
  629. def test_3d(self):
  630. a = arange(12.).reshape(2, 2, 3)
  631. def myfunc(b):
  632. return b[1]
  633. xa = apply_along_axis(myfunc, 2, a)
  634. assert_equal(xa, [[1, 4], [7, 10]])
  635. # Tests kwargs functions
  636. def test_3d_kwargs(self):
  637. a = arange(12).reshape(2, 2, 3)
  638. def myfunc(b, offset=0):
  639. return b[1+offset]
  640. xa = apply_along_axis(myfunc, 2, a, offset=1)
  641. assert_equal(xa, [[2, 5], [8, 11]])
  642. class TestApplyOverAxes:
  643. # Tests apply_over_axes
  644. def test_basic(self):
  645. a = arange(24).reshape(2, 3, 4)
  646. test = apply_over_axes(np.sum, a, [0, 2])
  647. ctrl = np.array([[[60], [92], [124]]])
  648. assert_equal(test, ctrl)
  649. a[(a % 2).astype(bool)] = masked
  650. test = apply_over_axes(np.sum, a, [0, 2])
  651. ctrl = np.array([[[28], [44], [60]]])
  652. assert_equal(test, ctrl)
  653. class TestMedian:
  654. def test_pytype(self):
  655. r = np.ma.median([[np.inf, np.inf], [np.inf, np.inf]], axis=-1)
  656. assert_equal(r, np.inf)
  657. def test_inf(self):
  658. # test that even which computes handles inf / x = masked
  659. r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
  660. [np.inf, np.inf]]), axis=-1)
  661. assert_equal(r, np.inf)
  662. r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
  663. [np.inf, np.inf]]), axis=None)
  664. assert_equal(r, np.inf)
  665. # all masked
  666. r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
  667. [np.inf, np.inf]], mask=True),
  668. axis=-1)
  669. assert_equal(r.mask, True)
  670. r = np.ma.median(np.ma.masked_array([[np.inf, np.inf],
  671. [np.inf, np.inf]], mask=True),
  672. axis=None)
  673. assert_equal(r.mask, True)
  674. def test_non_masked(self):
  675. x = np.arange(9)
  676. assert_equal(np.ma.median(x), 4.)
  677. assert_(type(np.ma.median(x)) is not MaskedArray)
  678. x = range(8)
  679. assert_equal(np.ma.median(x), 3.5)
  680. assert_(type(np.ma.median(x)) is not MaskedArray)
  681. x = 5
  682. assert_equal(np.ma.median(x), 5.)
  683. assert_(type(np.ma.median(x)) is not MaskedArray)
  684. # integer
  685. x = np.arange(9 * 8).reshape(9, 8)
  686. assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
  687. assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
  688. assert_(np.ma.median(x, axis=1) is not MaskedArray)
  689. # float
  690. x = np.arange(9 * 8.).reshape(9, 8)
  691. assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
  692. assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
  693. assert_(np.ma.median(x, axis=1) is not MaskedArray)
  694. def test_docstring_examples(self):
  695. "test the examples given in the docstring of ma.median"
  696. x = array(np.arange(8), mask=[0]*4 + [1]*4)
  697. assert_equal(np.ma.median(x), 1.5)
  698. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  699. assert_(type(np.ma.median(x)) is not MaskedArray)
  700. x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
  701. assert_equal(np.ma.median(x), 2.5)
  702. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  703. assert_(type(np.ma.median(x)) is not MaskedArray)
  704. ma_x = np.ma.median(x, axis=-1, overwrite_input=True)
  705. assert_equal(ma_x, [2., 5.])
  706. assert_equal(ma_x.shape, (2,), "shape mismatch")
  707. assert_(type(ma_x) is MaskedArray)
  708. def test_axis_argument_errors(self):
  709. msg = "mask = %s, ndim = %s, axis = %s, overwrite_input = %s"
  710. for ndmin in range(5):
  711. for mask in [False, True]:
  712. x = array(1, ndmin=ndmin, mask=mask)
  713. # Valid axis values should not raise exception
  714. args = itertools.product(range(-ndmin, ndmin), [False, True])
  715. for axis, over in args:
  716. try:
  717. np.ma.median(x, axis=axis, overwrite_input=over)
  718. except Exception:
  719. raise AssertionError(msg % (mask, ndmin, axis, over))
  720. # Invalid axis values should raise exception
  721. args = itertools.product([-(ndmin + 1), ndmin], [False, True])
  722. for axis, over in args:
  723. try:
  724. np.ma.median(x, axis=axis, overwrite_input=over)
  725. except np.AxisError:
  726. pass
  727. else:
  728. raise AssertionError(msg % (mask, ndmin, axis, over))
  729. def test_masked_0d(self):
  730. # Check values
  731. x = array(1, mask=False)
  732. assert_equal(np.ma.median(x), 1)
  733. x = array(1, mask=True)
  734. assert_equal(np.ma.median(x), np.ma.masked)
  735. def test_masked_1d(self):
  736. x = array(np.arange(5), mask=True)
  737. assert_equal(np.ma.median(x), np.ma.masked)
  738. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  739. assert_(type(np.ma.median(x)) is np.ma.core.MaskedConstant)
  740. x = array(np.arange(5), mask=False)
  741. assert_equal(np.ma.median(x), 2.)
  742. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  743. assert_(type(np.ma.median(x)) is not MaskedArray)
  744. x = array(np.arange(5), mask=[0,1,0,0,0])
  745. assert_equal(np.ma.median(x), 2.5)
  746. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  747. assert_(type(np.ma.median(x)) is not MaskedArray)
  748. x = array(np.arange(5), mask=[0,1,1,1,1])
  749. assert_equal(np.ma.median(x), 0.)
  750. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  751. assert_(type(np.ma.median(x)) is not MaskedArray)
  752. # integer
  753. x = array(np.arange(5), mask=[0,1,1,0,0])
  754. assert_equal(np.ma.median(x), 3.)
  755. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  756. assert_(type(np.ma.median(x)) is not MaskedArray)
  757. # float
  758. x = array(np.arange(5.), mask=[0,1,1,0,0])
  759. assert_equal(np.ma.median(x), 3.)
  760. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  761. assert_(type(np.ma.median(x)) is not MaskedArray)
  762. # integer
  763. x = array(np.arange(6), mask=[0,1,1,1,1,0])
  764. assert_equal(np.ma.median(x), 2.5)
  765. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  766. assert_(type(np.ma.median(x)) is not MaskedArray)
  767. # float
  768. x = array(np.arange(6.), mask=[0,1,1,1,1,0])
  769. assert_equal(np.ma.median(x), 2.5)
  770. assert_equal(np.ma.median(x).shape, (), "shape mismatch")
  771. assert_(type(np.ma.median(x)) is not MaskedArray)
  772. def test_1d_shape_consistency(self):
  773. assert_equal(np.ma.median(array([1,2,3],mask=[0,0,0])).shape,
  774. np.ma.median(array([1,2,3],mask=[0,1,0])).shape )
  775. def test_2d(self):
  776. # Tests median w/ 2D
  777. (n, p) = (101, 30)
  778. x = masked_array(np.linspace(-1., 1., n),)
  779. x[:10] = x[-10:] = masked
  780. z = masked_array(np.empty((n, p), dtype=float))
  781. z[:, 0] = x[:]
  782. idx = np.arange(len(x))
  783. for i in range(1, p):
  784. np.random.shuffle(idx)
  785. z[:, i] = x[idx]
  786. assert_equal(median(z[:, 0]), 0)
  787. assert_equal(median(z), 0)
  788. assert_equal(median(z, axis=0), np.zeros(p))
  789. assert_equal(median(z.T, axis=1), np.zeros(p))
  790. def test_2d_waxis(self):
  791. # Tests median w/ 2D arrays and different axis.
  792. x = masked_array(np.arange(30).reshape(10, 3))
  793. x[:3] = x[-3:] = masked
  794. assert_equal(median(x), 14.5)
  795. assert_(type(np.ma.median(x)) is not MaskedArray)
  796. assert_equal(median(x, axis=0), [13.5, 14.5, 15.5])
  797. assert_(type(np.ma.median(x, axis=0)) is MaskedArray)
  798. assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0])
  799. assert_(type(np.ma.median(x, axis=1)) is MaskedArray)
  800. assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1])
  801. def test_3d(self):
  802. # Tests median w/ 3D
  803. x = np.ma.arange(24).reshape(3, 4, 2)
  804. x[x % 3 == 0] = masked
  805. assert_equal(median(x, 0), [[12, 9], [6, 15], [12, 9], [18, 15]])
  806. x.shape = (4, 3, 2)
  807. assert_equal(median(x, 0), [[99, 10], [11, 99], [13, 14]])
  808. x = np.ma.arange(24).reshape(4, 3, 2)
  809. x[x % 5 == 0] = masked
  810. assert_equal(median(x, 0), [[12, 10], [8, 9], [16, 17]])
  811. def test_neg_axis(self):
  812. x = masked_array(np.arange(30).reshape(10, 3))
  813. x[:3] = x[-3:] = masked
  814. assert_equal(median(x, axis=-1), median(x, axis=1))
  815. def test_out_1d(self):
  816. # integer float even odd
  817. for v in (30, 30., 31, 31.):
  818. x = masked_array(np.arange(v))
  819. x[:3] = x[-3:] = masked
  820. out = masked_array(np.ones(()))
  821. r = median(x, out=out)
  822. if v == 30:
  823. assert_equal(out, 14.5)
  824. else:
  825. assert_equal(out, 15.)
  826. assert_(r is out)
  827. assert_(type(r) is MaskedArray)
  828. def test_out(self):
  829. # integer float even odd
  830. for v in (40, 40., 30, 30.):
  831. x = masked_array(np.arange(v).reshape(10, -1))
  832. x[:3] = x[-3:] = masked
  833. out = masked_array(np.ones(10))
  834. r = median(x, axis=1, out=out)
  835. if v == 30:
  836. e = masked_array([0.]*3 + [10, 13, 16, 19] + [0.]*3,
  837. mask=[True] * 3 + [False] * 4 + [True] * 3)
  838. else:
  839. e = masked_array([0.]*3 + [13.5, 17.5, 21.5, 25.5] + [0.]*3,
  840. mask=[True]*3 + [False]*4 + [True]*3)
  841. assert_equal(r, e)
  842. assert_(r is out)
  843. assert_(type(r) is MaskedArray)
  844. def test_single_non_masked_value_on_axis(self):
  845. data = [[1., 0.],
  846. [0., 3.],
  847. [0., 0.]]
  848. masked_arr = np.ma.masked_equal(data, 0)
  849. expected = [1., 3.]
  850. assert_array_equal(np.ma.median(masked_arr, axis=0),
  851. expected)
  852. def test_nan(self):
  853. for mask in (False, np.zeros(6, dtype=bool)):
  854. dm = np.ma.array([[1, np.nan, 3], [1, 2, 3]])
  855. dm.mask = mask
  856. # scalar result
  857. r = np.ma.median(dm, axis=None)
  858. assert_(np.isscalar(r))
  859. assert_array_equal(r, np.nan)
  860. r = np.ma.median(dm.ravel(), axis=0)
  861. assert_(np.isscalar(r))
  862. assert_array_equal(r, np.nan)
  863. r = np.ma.median(dm, axis=0)
  864. assert_equal(type(r), MaskedArray)
  865. assert_array_equal(r, [1, np.nan, 3])
  866. r = np.ma.median(dm, axis=1)
  867. assert_equal(type(r), MaskedArray)
  868. assert_array_equal(r, [np.nan, 2])
  869. r = np.ma.median(dm, axis=-1)
  870. assert_equal(type(r), MaskedArray)
  871. assert_array_equal(r, [np.nan, 2])
  872. dm = np.ma.array([[1, np.nan, 3], [1, 2, 3]])
  873. dm[:, 2] = np.ma.masked
  874. assert_array_equal(np.ma.median(dm, axis=None), np.nan)
  875. assert_array_equal(np.ma.median(dm, axis=0), [1, np.nan, 3])
  876. assert_array_equal(np.ma.median(dm, axis=1), [np.nan, 1.5])
  877. def test_out_nan(self):
  878. o = np.ma.masked_array(np.zeros((4,)))
  879. d = np.ma.masked_array(np.ones((3, 4)))
  880. d[2, 1] = np.nan
  881. d[2, 2] = np.ma.masked
  882. assert_equal(np.ma.median(d, 0, out=o), o)
  883. o = np.ma.masked_array(np.zeros((3,)))
  884. assert_equal(np.ma.median(d, 1, out=o), o)
  885. o = np.ma.masked_array(np.zeros(()))
  886. assert_equal(np.ma.median(d, out=o), o)
  887. def test_nan_behavior(self):
  888. a = np.ma.masked_array(np.arange(24, dtype=float))
  889. a[::3] = np.ma.masked
  890. a[2] = np.nan
  891. assert_array_equal(np.ma.median(a), np.nan)
  892. assert_array_equal(np.ma.median(a, axis=0), np.nan)
  893. a = np.ma.masked_array(np.arange(24, dtype=float).reshape(2, 3, 4))
  894. a.mask = np.arange(a.size) % 2 == 1
  895. aorig = a.copy()
  896. a[1, 2, 3] = np.nan
  897. a[1, 1, 2] = np.nan
  898. # no axis
  899. assert_array_equal(np.ma.median(a), np.nan)
  900. assert_(np.isscalar(np.ma.median(a)))
  901. # axis0
  902. b = np.ma.median(aorig, axis=0)
  903. b[2, 3] = np.nan
  904. b[1, 2] = np.nan
  905. assert_equal(np.ma.median(a, 0), b)
  906. # axis1
  907. b = np.ma.median(aorig, axis=1)
  908. b[1, 3] = np.nan
  909. b[1, 2] = np.nan
  910. assert_equal(np.ma.median(a, 1), b)
  911. # axis02
  912. b = np.ma.median(aorig, axis=(0, 2))
  913. b[1] = np.nan
  914. b[2] = np.nan
  915. assert_equal(np.ma.median(a, (0, 2)), b)
  916. def test_ambigous_fill(self):
  917. # 255 is max value, used as filler for sort
  918. a = np.array([[3, 3, 255], [3, 3, 255]], dtype=np.uint8)
  919. a = np.ma.masked_array(a, mask=a == 3)
  920. assert_array_equal(np.ma.median(a, axis=1), 255)
  921. assert_array_equal(np.ma.median(a, axis=1).mask, False)
  922. assert_array_equal(np.ma.median(a, axis=0), a[0])
  923. assert_array_equal(np.ma.median(a), 255)
  924. def test_special(self):
  925. for inf in [np.inf, -np.inf]:
  926. a = np.array([[inf, np.nan], [np.nan, np.nan]])
  927. a = np.ma.masked_array(a, mask=np.isnan(a))
  928. assert_equal(np.ma.median(a, axis=0), [inf, np.nan])
  929. assert_equal(np.ma.median(a, axis=1), [inf, np.nan])
  930. assert_equal(np.ma.median(a), inf)
  931. a = np.array([[np.nan, np.nan, inf], [np.nan, np.nan, inf]])
  932. a = np.ma.masked_array(a, mask=np.isnan(a))
  933. assert_array_equal(np.ma.median(a, axis=1), inf)
  934. assert_array_equal(np.ma.median(a, axis=1).mask, False)
  935. assert_array_equal(np.ma.median(a, axis=0), a[0])
  936. assert_array_equal(np.ma.median(a), inf)
  937. # no mask
  938. a = np.array([[inf, inf], [inf, inf]])
  939. assert_equal(np.ma.median(a), inf)
  940. assert_equal(np.ma.median(a, axis=0), inf)
  941. assert_equal(np.ma.median(a, axis=1), inf)
  942. a = np.array([[inf, 7, -inf, -9],
  943. [-10, np.nan, np.nan, 5],
  944. [4, np.nan, np.nan, inf]],
  945. dtype=np.float32)
  946. a = np.ma.masked_array(a, mask=np.isnan(a))
  947. if inf > 0:
  948. assert_equal(np.ma.median(a, axis=0), [4., 7., -inf, 5.])
  949. assert_equal(np.ma.median(a), 4.5)
  950. else:
  951. assert_equal(np.ma.median(a, axis=0), [-10., 7., -inf, -9.])
  952. assert_equal(np.ma.median(a), -2.5)
  953. assert_equal(np.ma.median(a, axis=1), [-1., -2.5, inf])
  954. for i in range(0, 10):
  955. for j in range(1, 10):
  956. a = np.array([([np.nan] * i) + ([inf] * j)] * 2)
  957. a = np.ma.masked_array(a, mask=np.isnan(a))
  958. assert_equal(np.ma.median(a), inf)
  959. assert_equal(np.ma.median(a, axis=1), inf)
  960. assert_equal(np.ma.median(a, axis=0),
  961. ([np.nan] * i) + [inf] * j)
  962. def test_empty(self):
  963. # empty arrays
  964. a = np.ma.masked_array(np.array([], dtype=float))
  965. with suppress_warnings() as w:
  966. w.record(RuntimeWarning)
  967. assert_array_equal(np.ma.median(a), np.nan)
  968. assert_(w.log[0].category is RuntimeWarning)
  969. # multiple dimensions
  970. a = np.ma.masked_array(np.array([], dtype=float, ndmin=3))
  971. # no axis
  972. with suppress_warnings() as w:
  973. w.record(RuntimeWarning)
  974. warnings.filterwarnings('always', '', RuntimeWarning)
  975. assert_array_equal(np.ma.median(a), np.nan)
  976. assert_(w.log[0].category is RuntimeWarning)
  977. # axis 0 and 1
  978. b = np.ma.masked_array(np.array([], dtype=float, ndmin=2))
  979. assert_equal(np.ma.median(a, axis=0), b)
  980. assert_equal(np.ma.median(a, axis=1), b)
  981. # axis 2
  982. b = np.ma.masked_array(np.array(np.nan, dtype=float, ndmin=2))
  983. with warnings.catch_warnings(record=True) as w:
  984. warnings.filterwarnings('always', '', RuntimeWarning)
  985. assert_equal(np.ma.median(a, axis=2), b)
  986. assert_(w[0].category is RuntimeWarning)
  987. def test_object(self):
  988. o = np.ma.masked_array(np.arange(7.))
  989. assert_(type(np.ma.median(o.astype(object))), float)
  990. o[2] = np.nan
  991. assert_(type(np.ma.median(o.astype(object))), float)
  992. class TestCov:
  993. def setup(self):
  994. self.data = array(np.random.rand(12))
  995. def test_1d_without_missing(self):
  996. # Test cov on 1D variable w/o missing values
  997. x = self.data
  998. assert_almost_equal(np.cov(x), cov(x))
  999. assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
  1000. assert_almost_equal(np.cov(x, rowvar=False, bias=True),
  1001. cov(x, rowvar=False, bias=True))
  1002. def test_2d_without_missing(self):
  1003. # Test cov on 1 2D variable w/o missing values
  1004. x = self.data.reshape(3, 4)
  1005. assert_almost_equal(np.cov(x), cov(x))
  1006. assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False))
  1007. assert_almost_equal(np.cov(x, rowvar=False, bias=True),
  1008. cov(x, rowvar=False, bias=True))
  1009. def test_1d_with_missing(self):
  1010. # Test cov 1 1D variable w/missing values
  1011. x = self.data
  1012. x[-1] = masked
  1013. x -= x.mean()
  1014. nx = x.compressed()
  1015. assert_almost_equal(np.cov(nx), cov(x))
  1016. assert_almost_equal(np.cov(nx, rowvar=False), cov(x, rowvar=False))
  1017. assert_almost_equal(np.cov(nx, rowvar=False, bias=True),
  1018. cov(x, rowvar=False, bias=True))
  1019. #
  1020. try:
  1021. cov(x, allow_masked=False)
  1022. except ValueError:
  1023. pass
  1024. #
  1025. # 2 1D variables w/ missing values
  1026. nx = x[1:-1]
  1027. assert_almost_equal(np.cov(nx, nx[::-1]), cov(x, x[::-1]))
  1028. assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False),
  1029. cov(x, x[::-1], rowvar=False))
  1030. assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False, bias=True),
  1031. cov(x, x[::-1], rowvar=False, bias=True))
  1032. def test_2d_with_missing(self):
  1033. # Test cov on 2D variable w/ missing value
  1034. x = self.data
  1035. x[-1] = masked
  1036. x = x.reshape(3, 4)
  1037. valid = np.logical_not(getmaskarray(x)).astype(int)
  1038. frac = np.dot(valid, valid.T)
  1039. xf = (x - x.mean(1)[:, None]).filled(0)
  1040. assert_almost_equal(cov(x),
  1041. np.cov(xf) * (x.shape[1] - 1) / (frac - 1.))
  1042. assert_almost_equal(cov(x, bias=True),
  1043. np.cov(xf, bias=True) * x.shape[1] / frac)
  1044. frac = np.dot(valid.T, valid)
  1045. xf = (x - x.mean(0)).filled(0)
  1046. assert_almost_equal(cov(x, rowvar=False),
  1047. (np.cov(xf, rowvar=False) *
  1048. (x.shape[0] - 1) / (frac - 1.)))
  1049. assert_almost_equal(cov(x, rowvar=False, bias=True),
  1050. (np.cov(xf, rowvar=False, bias=True) *
  1051. x.shape[0] / frac))
  1052. class TestCorrcoef:
  1053. def setup(self):
  1054. self.data = array(np.random.rand(12))
  1055. self.data2 = array(np.random.rand(12))
  1056. def test_ddof(self):
  1057. # ddof raises DeprecationWarning
  1058. x, y = self.data, self.data2
  1059. expected = np.corrcoef(x)
  1060. expected2 = np.corrcoef(x, y)
  1061. with suppress_warnings() as sup:
  1062. warnings.simplefilter("always")
  1063. assert_warns(DeprecationWarning, corrcoef, x, ddof=-1)
  1064. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1065. # ddof has no or negligible effect on the function
  1066. assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0))
  1067. assert_almost_equal(corrcoef(x, ddof=-1), expected)
  1068. assert_almost_equal(corrcoef(x, y, ddof=-1), expected2)
  1069. assert_almost_equal(corrcoef(x, ddof=3), expected)
  1070. assert_almost_equal(corrcoef(x, y, ddof=3), expected2)
  1071. def test_bias(self):
  1072. x, y = self.data, self.data2
  1073. expected = np.corrcoef(x)
  1074. # bias raises DeprecationWarning
  1075. with suppress_warnings() as sup:
  1076. warnings.simplefilter("always")
  1077. assert_warns(DeprecationWarning, corrcoef, x, y, True, False)
  1078. assert_warns(DeprecationWarning, corrcoef, x, y, True, True)
  1079. assert_warns(DeprecationWarning, corrcoef, x, bias=False)
  1080. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1081. # bias has no or negligible effect on the function
  1082. assert_almost_equal(corrcoef(x, bias=1), expected)
  1083. def test_1d_without_missing(self):
  1084. # Test cov on 1D variable w/o missing values
  1085. x = self.data
  1086. assert_almost_equal(np.corrcoef(x), corrcoef(x))
  1087. assert_almost_equal(np.corrcoef(x, rowvar=False),
  1088. corrcoef(x, rowvar=False))
  1089. with suppress_warnings() as sup:
  1090. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1091. assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
  1092. corrcoef(x, rowvar=False, bias=True))
  1093. def test_2d_without_missing(self):
  1094. # Test corrcoef on 1 2D variable w/o missing values
  1095. x = self.data.reshape(3, 4)
  1096. assert_almost_equal(np.corrcoef(x), corrcoef(x))
  1097. assert_almost_equal(np.corrcoef(x, rowvar=False),
  1098. corrcoef(x, rowvar=False))
  1099. with suppress_warnings() as sup:
  1100. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1101. assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True),
  1102. corrcoef(x, rowvar=False, bias=True))
  1103. def test_1d_with_missing(self):
  1104. # Test corrcoef 1 1D variable w/missing values
  1105. x = self.data
  1106. x[-1] = masked
  1107. x -= x.mean()
  1108. nx = x.compressed()
  1109. assert_almost_equal(np.corrcoef(nx), corrcoef(x))
  1110. assert_almost_equal(np.corrcoef(nx, rowvar=False),
  1111. corrcoef(x, rowvar=False))
  1112. with suppress_warnings() as sup:
  1113. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1114. assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True),
  1115. corrcoef(x, rowvar=False, bias=True))
  1116. try:
  1117. corrcoef(x, allow_masked=False)
  1118. except ValueError:
  1119. pass
  1120. # 2 1D variables w/ missing values
  1121. nx = x[1:-1]
  1122. assert_almost_equal(np.corrcoef(nx, nx[::-1]), corrcoef(x, x[::-1]))
  1123. assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False),
  1124. corrcoef(x, x[::-1], rowvar=False))
  1125. with suppress_warnings() as sup:
  1126. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1127. # ddof and bias have no or negligible effect on the function
  1128. assert_almost_equal(np.corrcoef(nx, nx[::-1]),
  1129. corrcoef(x, x[::-1], bias=1))
  1130. assert_almost_equal(np.corrcoef(nx, nx[::-1]),
  1131. corrcoef(x, x[::-1], ddof=2))
  1132. def test_2d_with_missing(self):
  1133. # Test corrcoef on 2D variable w/ missing value
  1134. x = self.data
  1135. x[-1] = masked
  1136. x = x.reshape(3, 4)
  1137. test = corrcoef(x)
  1138. control = np.corrcoef(x)
  1139. assert_almost_equal(test[:-1, :-1], control[:-1, :-1])
  1140. with suppress_warnings() as sup:
  1141. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  1142. # ddof and bias have no or negligible effect on the function
  1143. assert_almost_equal(corrcoef(x, ddof=-2)[:-1, :-1],
  1144. control[:-1, :-1])
  1145. assert_almost_equal(corrcoef(x, ddof=3)[:-1, :-1],
  1146. control[:-1, :-1])
  1147. assert_almost_equal(corrcoef(x, bias=1)[:-1, :-1],
  1148. control[:-1, :-1])
  1149. class TestPolynomial:
  1150. #
  1151. def test_polyfit(self):
  1152. # Tests polyfit
  1153. # On ndarrays
  1154. x = np.random.rand(10)
  1155. y = np.random.rand(20).reshape(-1, 2)
  1156. assert_almost_equal(polyfit(x, y, 3), np.polyfit(x, y, 3))
  1157. # ON 1D maskedarrays
  1158. x = x.view(MaskedArray)
  1159. x[0] = masked
  1160. y = y.view(MaskedArray)
  1161. y[0, 0] = y[-1, -1] = masked
  1162. #
  1163. (C, R, K, S, D) = polyfit(x, y[:, 0], 3, full=True)
  1164. (c, r, k, s, d) = np.polyfit(x[1:], y[1:, 0].compressed(), 3,
  1165. full=True)
  1166. for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
  1167. assert_almost_equal(a, a_)
  1168. #
  1169. (C, R, K, S, D) = polyfit(x, y[:, -1], 3, full=True)
  1170. (c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1, -1], 3, full=True)
  1171. for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
  1172. assert_almost_equal(a, a_)
  1173. #
  1174. (C, R, K, S, D) = polyfit(x, y, 3, full=True)
  1175. (c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1,:], 3, full=True)
  1176. for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
  1177. assert_almost_equal(a, a_)
  1178. #
  1179. w = np.random.rand(10) + 1
  1180. wo = w.copy()
  1181. xs = x[1:-1]
  1182. ys = y[1:-1]
  1183. ws = w[1:-1]
  1184. (C, R, K, S, D) = polyfit(x, y, 3, full=True, w=w)
  1185. (c, r, k, s, d) = np.polyfit(xs, ys, 3, full=True, w=ws)
  1186. assert_equal(w, wo)
  1187. for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
  1188. assert_almost_equal(a, a_)
  1189. def test_polyfit_with_masked_NaNs(self):
  1190. x = np.random.rand(10)
  1191. y = np.random.rand(20).reshape(-1, 2)
  1192. x[0] = np.nan
  1193. y[-1,-1] = np.nan
  1194. x = x.view(MaskedArray)
  1195. y = y.view(MaskedArray)
  1196. x[0] = masked
  1197. y[-1,-1] = masked
  1198. (C, R, K, S, D) = polyfit(x, y, 3, full=True)
  1199. (c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1,:], 3, full=True)
  1200. for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)):
  1201. assert_almost_equal(a, a_)
  1202. class TestArraySetOps:
  1203. def test_unique_onlist(self):
  1204. # Test unique on list
  1205. data = [1, 1, 1, 2, 2, 3]
  1206. test = unique(data, return_index=True, return_inverse=True)
  1207. assert_(isinstance(test[0], MaskedArray))
  1208. assert_equal(test[0], masked_array([1, 2, 3], mask=[0, 0, 0]))
  1209. assert_equal(test[1], [0, 3, 5])
  1210. assert_equal(test[2], [0, 0, 0, 1, 1, 2])
  1211. def test_unique_onmaskedarray(self):
  1212. # Test unique on masked data w/use_mask=True
  1213. data = masked_array([1, 1, 1, 2, 2, 3], mask=[0, 0, 1, 0, 1, 0])
  1214. test = unique(data, return_index=True, return_inverse=True)
  1215. assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
  1216. assert_equal(test[1], [0, 3, 5, 2])
  1217. assert_equal(test[2], [0, 0, 3, 1, 3, 2])
  1218. #
  1219. data.fill_value = 3
  1220. data = masked_array(data=[1, 1, 1, 2, 2, 3],
  1221. mask=[0, 0, 1, 0, 1, 0], fill_value=3)
  1222. test = unique(data, return_index=True, return_inverse=True)
  1223. assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1]))
  1224. assert_equal(test[1], [0, 3, 5, 2])
  1225. assert_equal(test[2], [0, 0, 3, 1, 3, 2])
  1226. def test_unique_allmasked(self):
  1227. # Test all masked
  1228. data = masked_array([1, 1, 1], mask=True)
  1229. test = unique(data, return_index=True, return_inverse=True)
  1230. assert_equal(test[0], masked_array([1, ], mask=[True]))
  1231. assert_equal(test[1], [0])
  1232. assert_equal(test[2], [0, 0, 0])
  1233. #
  1234. # Test masked
  1235. data = masked
  1236. test = unique(data, return_index=True, return_inverse=True)
  1237. assert_equal(test[0], masked_array(masked))
  1238. assert_equal(test[1], [0])
  1239. assert_equal(test[2], [0])
  1240. def test_ediff1d(self):
  1241. # Tests mediff1d
  1242. x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
  1243. control = array([1, 1, 1, 4], mask=[1, 0, 0, 1])
  1244. test = ediff1d(x)
  1245. assert_equal(test, control)
  1246. assert_equal(test.filled(0), control.filled(0))
  1247. assert_equal(test.mask, control.mask)
  1248. def test_ediff1d_tobegin(self):
  1249. # Test ediff1d w/ to_begin
  1250. x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
  1251. test = ediff1d(x, to_begin=masked)
  1252. control = array([0, 1, 1, 1, 4], mask=[1, 1, 0, 0, 1])
  1253. assert_equal(test, control)
  1254. assert_equal(test.filled(0), control.filled(0))
  1255. assert_equal(test.mask, control.mask)
  1256. #
  1257. test = ediff1d(x, to_begin=[1, 2, 3])
  1258. control = array([1, 2, 3, 1, 1, 1, 4], mask=[0, 0, 0, 1, 0, 0, 1])
  1259. assert_equal(test, control)
  1260. assert_equal(test.filled(0), control.filled(0))
  1261. assert_equal(test.mask, control.mask)
  1262. def test_ediff1d_toend(self):
  1263. # Test ediff1d w/ to_end
  1264. x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
  1265. test = ediff1d(x, to_end=masked)
  1266. control = array([1, 1, 1, 4, 0], mask=[1, 0, 0, 1, 1])
  1267. assert_equal(test, control)
  1268. assert_equal(test.filled(0), control.filled(0))
  1269. assert_equal(test.mask, control.mask)
  1270. #
  1271. test = ediff1d(x, to_end=[1, 2, 3])
  1272. control = array([1, 1, 1, 4, 1, 2, 3], mask=[1, 0, 0, 1, 0, 0, 0])
  1273. assert_equal(test, control)
  1274. assert_equal(test.filled(0), control.filled(0))
  1275. assert_equal(test.mask, control.mask)
  1276. def test_ediff1d_tobegin_toend(self):
  1277. # Test ediff1d w/ to_begin and to_end
  1278. x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1])
  1279. test = ediff1d(x, to_end=masked, to_begin=masked)
  1280. control = array([0, 1, 1, 1, 4, 0], mask=[1, 1, 0, 0, 1, 1])
  1281. assert_equal(test, control)
  1282. assert_equal(test.filled(0), control.filled(0))
  1283. assert_equal(test.mask, control.mask)
  1284. #
  1285. test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked)
  1286. control = array([0, 1, 1, 1, 4, 1, 2, 3],
  1287. mask=[1, 1, 0, 0, 1, 0, 0, 0])
  1288. assert_equal(test, control)
  1289. assert_equal(test.filled(0), control.filled(0))
  1290. assert_equal(test.mask, control.mask)
  1291. def test_ediff1d_ndarray(self):
  1292. # Test ediff1d w/ a ndarray
  1293. x = np.arange(5)
  1294. test = ediff1d(x)
  1295. control = array([1, 1, 1, 1], mask=[0, 0, 0, 0])
  1296. assert_equal(test, control)
  1297. assert_(isinstance(test, MaskedArray))
  1298. assert_equal(test.filled(0), control.filled(0))
  1299. assert_equal(test.mask, control.mask)
  1300. #
  1301. test = ediff1d(x, to_end=masked, to_begin=masked)
  1302. control = array([0, 1, 1, 1, 1, 0], mask=[1, 0, 0, 0, 0, 1])
  1303. assert_(isinstance(test, MaskedArray))
  1304. assert_equal(test.filled(0), control.filled(0))
  1305. assert_equal(test.mask, control.mask)
  1306. def test_intersect1d(self):
  1307. # Test intersect1d
  1308. x = array([1, 3, 3, 3], mask=[0, 0, 0, 1])
  1309. y = array([3, 1, 1, 1], mask=[0, 0, 0, 1])
  1310. test = intersect1d(x, y)
  1311. control = array([1, 3, -1], mask=[0, 0, 1])
  1312. assert_equal(test, control)
  1313. def test_setxor1d(self):
  1314. # Test setxor1d
  1315. a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
  1316. b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
  1317. test = setxor1d(a, b)
  1318. assert_equal(test, array([3, 4, 7]))
  1319. #
  1320. a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
  1321. b = [1, 2, 3, 4, 5]
  1322. test = setxor1d(a, b)
  1323. assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1]))
  1324. #
  1325. a = array([1, 2, 3])
  1326. b = array([6, 5, 4])
  1327. test = setxor1d(a, b)
  1328. assert_(isinstance(test, MaskedArray))
  1329. assert_equal(test, [1, 2, 3, 4, 5, 6])
  1330. #
  1331. a = array([1, 8, 2, 3], mask=[0, 1, 0, 0])
  1332. b = array([6, 5, 4, 8], mask=[0, 0, 0, 1])
  1333. test = setxor1d(a, b)
  1334. assert_(isinstance(test, MaskedArray))
  1335. assert_equal(test, [1, 2, 3, 4, 5, 6])
  1336. #
  1337. assert_array_equal([], setxor1d([], []))
  1338. def test_isin(self):
  1339. # the tests for in1d cover most of isin's behavior
  1340. # if in1d is removed, would need to change those tests to test
  1341. # isin instead.
  1342. a = np.arange(24).reshape([2, 3, 4])
  1343. mask = np.zeros([2, 3, 4])
  1344. mask[1, 2, 0] = 1
  1345. a = array(a, mask=mask)
  1346. b = array(data=[0, 10, 20, 30, 1, 3, 11, 22, 33],
  1347. mask=[0, 1, 0, 1, 0, 1, 0, 1, 0])
  1348. ec = zeros((2, 3, 4), dtype=bool)
  1349. ec[0, 0, 0] = True
  1350. ec[0, 0, 1] = True
  1351. ec[0, 2, 3] = True
  1352. c = isin(a, b)
  1353. assert_(isinstance(c, MaskedArray))
  1354. assert_array_equal(c, ec)
  1355. #compare results of np.isin to ma.isin
  1356. d = np.isin(a, b[~b.mask]) & ~a.mask
  1357. assert_array_equal(c, d)
  1358. def test_in1d(self):
  1359. # Test in1d
  1360. a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
  1361. b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
  1362. test = in1d(a, b)
  1363. assert_equal(test, [True, True, True, False, True])
  1364. #
  1365. a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
  1366. b = array([1, 5, -1], mask=[0, 0, 1])
  1367. test = in1d(a, b)
  1368. assert_equal(test, [True, True, False, True, True])
  1369. #
  1370. assert_array_equal([], in1d([], []))
  1371. def test_in1d_invert(self):
  1372. # Test in1d's invert parameter
  1373. a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
  1374. b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
  1375. assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
  1376. a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
  1377. b = array([1, 5, -1], mask=[0, 0, 1])
  1378. assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
  1379. assert_array_equal([], in1d([], [], invert=True))
  1380. def test_union1d(self):
  1381. # Test union1d
  1382. a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1])
  1383. b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
  1384. test = union1d(a, b)
  1385. control = array([1, 2, 3, 4, 5, 7, -1], mask=[0, 0, 0, 0, 0, 0, 1])
  1386. assert_equal(test, control)
  1387. # Tests gh-10340, arguments to union1d should be
  1388. # flattened if they are not already 1D
  1389. x = array([[0, 1, 2], [3, 4, 5]], mask=[[0, 0, 0], [0, 0, 1]])
  1390. y = array([0, 1, 2, 3, 4], mask=[0, 0, 0, 0, 1])
  1391. ez = array([0, 1, 2, 3, 4, 5], mask=[0, 0, 0, 0, 0, 1])
  1392. z = union1d(x, y)
  1393. assert_equal(z, ez)
  1394. #
  1395. assert_array_equal([], union1d([], []))
  1396. def test_setdiff1d(self):
  1397. # Test setdiff1d
  1398. a = array([6, 5, 4, 7, 7, 1, 2, 1], mask=[0, 0, 0, 0, 0, 0, 0, 1])
  1399. b = array([2, 4, 3, 3, 2, 1, 5])
  1400. test = setdiff1d(a, b)
  1401. assert_equal(test, array([6, 7, -1], mask=[0, 0, 1]))
  1402. #
  1403. a = arange(10)
  1404. b = arange(8)
  1405. assert_equal(setdiff1d(a, b), array([8, 9]))
  1406. a = array([], np.uint32, mask=[])
  1407. assert_equal(setdiff1d(a, []).dtype, np.uint32)
  1408. def test_setdiff1d_char_array(self):
  1409. # Test setdiff1d_charray
  1410. a = np.array(['a', 'b', 'c'])
  1411. b = np.array(['a', 'b', 's'])
  1412. assert_array_equal(setdiff1d(a, b), np.array(['c']))
  1413. class TestShapeBase:
  1414. def test_atleast_2d(self):
  1415. # Test atleast_2d
  1416. a = masked_array([0, 1, 2], mask=[0, 1, 0])
  1417. b = atleast_2d(a)
  1418. assert_equal(b.shape, (1, 3))
  1419. assert_equal(b.mask.shape, b.data.shape)
  1420. assert_equal(a.shape, (3,))
  1421. assert_equal(a.mask.shape, a.data.shape)
  1422. assert_equal(b.mask.shape, b.data.shape)
  1423. def test_shape_scalar(self):
  1424. # the atleast and diagflat function should work with scalars
  1425. # GitHub issue #3367
  1426. # Additionally, the atleast functions should accept multiple scalars
  1427. # correctly
  1428. b = atleast_1d(1.0)
  1429. assert_equal(b.shape, (1,))
  1430. assert_equal(b.mask.shape, b.shape)
  1431. assert_equal(b.data.shape, b.shape)
  1432. b = atleast_1d(1.0, 2.0)
  1433. for a in b:
  1434. assert_equal(a.shape, (1,))
  1435. assert_equal(a.mask.shape, a.shape)
  1436. assert_equal(a.data.shape, a.shape)
  1437. b = atleast_2d(1.0)
  1438. assert_equal(b.shape, (1, 1))
  1439. assert_equal(b.mask.shape, b.shape)
  1440. assert_equal(b.data.shape, b.shape)
  1441. b = atleast_2d(1.0, 2.0)
  1442. for a in b:
  1443. assert_equal(a.shape, (1, 1))
  1444. assert_equal(a.mask.shape, a.shape)
  1445. assert_equal(a.data.shape, a.shape)
  1446. b = atleast_3d(1.0)
  1447. assert_equal(b.shape, (1, 1, 1))
  1448. assert_equal(b.mask.shape, b.shape)
  1449. assert_equal(b.data.shape, b.shape)
  1450. b = atleast_3d(1.0, 2.0)
  1451. for a in b:
  1452. assert_equal(a.shape, (1, 1, 1))
  1453. assert_equal(a.mask.shape, a.shape)
  1454. assert_equal(a.data.shape, a.shape)
  1455. b = diagflat(1.0)
  1456. assert_equal(b.shape, (1, 1))
  1457. assert_equal(b.mask.shape, b.data.shape)
  1458. class TestStack:
  1459. def test_stack_1d(self):
  1460. a = masked_array([0, 1, 2], mask=[0, 1, 0])
  1461. b = masked_array([9, 8, 7], mask=[1, 0, 0])
  1462. c = stack([a, b], axis=0)
  1463. assert_equal(c.shape, (2, 3))
  1464. assert_array_equal(a.mask, c[0].mask)
  1465. assert_array_equal(b.mask, c[1].mask)
  1466. d = vstack([a, b])
  1467. assert_array_equal(c.data, d.data)
  1468. assert_array_equal(c.mask, d.mask)
  1469. c = stack([a, b], axis=1)
  1470. assert_equal(c.shape, (3, 2))
  1471. assert_array_equal(a.mask, c[:, 0].mask)
  1472. assert_array_equal(b.mask, c[:, 1].mask)
  1473. def test_stack_masks(self):
  1474. a = masked_array([0, 1, 2], mask=True)
  1475. b = masked_array([9, 8, 7], mask=False)
  1476. c = stack([a, b], axis=0)
  1477. assert_equal(c.shape, (2, 3))
  1478. assert_array_equal(a.mask, c[0].mask)
  1479. assert_array_equal(b.mask, c[1].mask)
  1480. d = vstack([a, b])
  1481. assert_array_equal(c.data, d.data)
  1482. assert_array_equal(c.mask, d.mask)
  1483. c = stack([a, b], axis=1)
  1484. assert_equal(c.shape, (3, 2))
  1485. assert_array_equal(a.mask, c[:, 0].mask)
  1486. assert_array_equal(b.mask, c[:, 1].mask)
  1487. def test_stack_nd(self):
  1488. # 2D
  1489. shp = (3, 2)
  1490. d1 = np.random.randint(0, 10, shp)
  1491. d2 = np.random.randint(0, 10, shp)
  1492. m1 = np.random.randint(0, 2, shp).astype(bool)
  1493. m2 = np.random.randint(0, 2, shp).astype(bool)
  1494. a1 = masked_array(d1, mask=m1)
  1495. a2 = masked_array(d2, mask=m2)
  1496. c = stack([a1, a2], axis=0)
  1497. c_shp = (2,) + shp
  1498. assert_equal(c.shape, c_shp)
  1499. assert_array_equal(a1.mask, c[0].mask)
  1500. assert_array_equal(a2.mask, c[1].mask)
  1501. c = stack([a1, a2], axis=-1)
  1502. c_shp = shp + (2,)
  1503. assert_equal(c.shape, c_shp)
  1504. assert_array_equal(a1.mask, c[..., 0].mask)
  1505. assert_array_equal(a2.mask, c[..., 1].mask)
  1506. # 4D
  1507. shp = (3, 2, 4, 5,)
  1508. d1 = np.random.randint(0, 10, shp)
  1509. d2 = np.random.randint(0, 10, shp)
  1510. m1 = np.random.randint(0, 2, shp).astype(bool)
  1511. m2 = np.random.randint(0, 2, shp).astype(bool)
  1512. a1 = masked_array(d1, mask=m1)
  1513. a2 = masked_array(d2, mask=m2)
  1514. c = stack([a1, a2], axis=0)
  1515. c_shp = (2,) + shp
  1516. assert_equal(c.shape, c_shp)
  1517. assert_array_equal(a1.mask, c[0].mask)
  1518. assert_array_equal(a2.mask, c[1].mask)
  1519. c = stack([a1, a2], axis=-1)
  1520. c_shp = shp + (2,)
  1521. assert_equal(c.shape, c_shp)
  1522. assert_array_equal(a1.mask, c[..., 0].mask)
  1523. assert_array_equal(a2.mask, c[..., 1].mask)