test_old_ma.py 32 KB

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