test_old_ma.py 32 KB

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