test_extras.py 73 KB

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