图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

858 lines
32 KiB

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