test_masked_matrix.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import pickle
  2. import numpy as np
  3. from numpy.ma.core import (
  4. MaskedArray,
  5. MaskType,
  6. add,
  7. allequal,
  8. divide,
  9. getmask,
  10. hypot,
  11. log,
  12. masked,
  13. masked_array,
  14. masked_values,
  15. nomask,
  16. )
  17. from numpy.ma.extras import mr_
  18. from numpy.ma.testutils import assert_, assert_array_equal, assert_equal, assert_raises
  19. class MMatrix(MaskedArray, np.matrix,):
  20. def __new__(cls, data, mask=nomask):
  21. mat = np.matrix(data)
  22. _data = MaskedArray.__new__(cls, data=mat, mask=mask)
  23. return _data
  24. def __array_finalize__(self, obj):
  25. np.matrix.__array_finalize__(self, obj)
  26. MaskedArray.__array_finalize__(self, obj)
  27. @property
  28. def _series(self):
  29. _view = self.view(MaskedArray)
  30. _view._sharedmask = False
  31. return _view
  32. class TestMaskedMatrix:
  33. def test_matrix_indexing(self):
  34. # Tests conversions and indexing
  35. x1 = np.matrix([[1, 2, 3], [4, 3, 2]])
  36. x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]])
  37. x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]])
  38. x4 = masked_array(x1)
  39. # test conversion to strings
  40. str(x2) # raises?
  41. repr(x2) # raises?
  42. # tests of indexing
  43. assert_(type(x2[1, 0]) is type(x1[1, 0]))
  44. assert_(x1[1, 0] == x2[1, 0])
  45. assert_(x2[1, 1] is masked)
  46. assert_equal(x1[0, 2], x2[0, 2])
  47. assert_equal(x1[0, 1:], x2[0, 1:])
  48. assert_equal(x1[:, 2], x2[:, 2])
  49. assert_equal(x1[:], x2[:])
  50. assert_equal(x1[1:], x3[1:])
  51. x1[0, 2] = 9
  52. x2[0, 2] = 9
  53. assert_equal(x1, x2)
  54. x1[0, 1:] = 99
  55. x2[0, 1:] = 99
  56. assert_equal(x1, x2)
  57. x2[0, 1] = masked
  58. assert_equal(x1, x2)
  59. x2[0, 1:] = masked
  60. assert_equal(x1, x2)
  61. x2[0, :] = x1[0, :]
  62. x2[0, 1] = masked
  63. assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]])))
  64. x3[1, :] = masked_array([1, 2, 3], [1, 1, 0])
  65. assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0])))
  66. assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0])))
  67. x4[1, :] = masked_array([1, 2, 3], [1, 1, 0])
  68. assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0])))
  69. assert_(allequal(x4[1], masked_array([1, 2, 3])))
  70. x1 = np.matrix(np.arange(5) * 1.0)
  71. x2 = masked_values(x1, 3.0)
  72. assert_equal(x1, x2)
  73. assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType),
  74. x2.mask))
  75. assert_equal(3.0, x2.fill_value)
  76. def test_pickling_subbaseclass(self):
  77. # Test pickling w/ a subclass of ndarray
  78. a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
  79. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  80. a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
  81. assert_equal(a_pickled._mask, a._mask)
  82. assert_equal(a_pickled, a)
  83. assert_(isinstance(a_pickled._data, np.matrix))
  84. def test_count_mean_with_matrix(self):
  85. m = masked_array(np.matrix([[1, 2], [3, 4]]), mask=np.zeros((2, 2)))
  86. assert_equal(m.count(axis=0).shape, (1, 2))
  87. assert_equal(m.count(axis=1).shape, (2, 1))
  88. # Make sure broadcasting inside mean and var work
  89. assert_equal(m.mean(axis=0), [[2., 3.]])
  90. assert_equal(m.mean(axis=1), [[1.5], [3.5]])
  91. def test_flat(self):
  92. # Test that flat can return items even for matrices [#4585, #4615]
  93. # test simple access
  94. test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
  95. assert_equal(test.flat[1], 2)
  96. assert_equal(test.flat[2], masked)
  97. assert_(np.all(test.flat[0:2] == test[0, 0:2]))
  98. # Test flat on masked_matrices
  99. test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
  100. test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
  101. control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
  102. assert_equal(test, control)
  103. # Test setting
  104. test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
  105. testflat = test.flat
  106. testflat[:] = testflat[np.array([2, 1, 0])]
  107. assert_equal(test, control)
  108. testflat[0] = 9
  109. # test that matrices keep the correct shape (#4615)
  110. a = masked_array(np.matrix(np.eye(2)), mask=0)
  111. b = a.flat
  112. b01 = b[:2]
  113. assert_equal(b01.data, np.array([[1., 0.]]))
  114. assert_equal(b01.mask, np.array([[False, False]]))
  115. def test_allany_onmatrices(self):
  116. x = np.array([[0.13, 0.26, 0.90],
  117. [0.28, 0.33, 0.63],
  118. [0.31, 0.87, 0.70]])
  119. X = np.matrix(x)
  120. m = np.array([[True, False, False],
  121. [False, False, False],
  122. [True, True, False]], dtype=np.bool)
  123. mX = masked_array(X, mask=m)
  124. mXbig = (mX > 0.5)
  125. mXsmall = (mX < 0.5)
  126. assert_(not mXbig.all())
  127. assert_(mXbig.any())
  128. assert_equal(mXbig.all(0), np.matrix([False, False, True]))
  129. assert_equal(mXbig.all(1), np.matrix([False, False, True]).T)
  130. assert_equal(mXbig.any(0), np.matrix([False, False, True]))
  131. assert_equal(mXbig.any(1), np.matrix([True, True, True]).T)
  132. assert_(not mXsmall.all())
  133. assert_(mXsmall.any())
  134. assert_equal(mXsmall.all(0), np.matrix([True, True, False]))
  135. assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T)
  136. assert_equal(mXsmall.any(0), np.matrix([True, True, False]))
  137. assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T)
  138. def test_compressed(self):
  139. a = masked_array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0])
  140. b = a.compressed()
  141. assert_equal(b, a)
  142. assert_(isinstance(b, np.matrix))
  143. a[0, 0] = masked
  144. b = a.compressed()
  145. assert_equal(b, [[2, 3, 4]])
  146. def test_ravel(self):
  147. a = masked_array(np.matrix([1, 2, 3, 4, 5]), mask=[[0, 1, 0, 0, 0]])
  148. aravel = a.ravel()
  149. assert_equal(aravel.shape, (1, 5))
  150. assert_equal(aravel._mask.shape, a.shape)
  151. def test_view(self):
  152. # Test view w/ flexible dtype
  153. iterator = list(zip(np.arange(10), np.random.rand(10)))
  154. data = np.array(iterator)
  155. a = masked_array(iterator, dtype=[('a', float), ('b', float)])
  156. a.mask[0] = (1, 0)
  157. test = a.view((float, 2), np.matrix)
  158. assert_equal(test, data)
  159. assert_(isinstance(test, np.matrix))
  160. assert_(not isinstance(test, MaskedArray))
  161. class TestSubclassing:
  162. # Test suite for masked subclasses of ndarray.
  163. def _create_data(self):
  164. x = np.arange(5, dtype='float')
  165. mx = MMatrix(x, mask=[0, 1, 0, 0, 0])
  166. return x, mx
  167. def test_maskedarray_subclassing(self):
  168. # Tests subclassing MaskedArray
  169. mx = self._create_data()[1]
  170. assert_(isinstance(mx._data, np.matrix))
  171. def test_masked_unary_operations(self):
  172. # Tests masked_unary_operation
  173. x, mx = self._create_data()
  174. with np.errstate(divide='ignore'):
  175. assert_(isinstance(log(mx), MMatrix))
  176. assert_equal(log(x), np.log(x))
  177. def test_masked_binary_operations(self):
  178. # Tests masked_binary_operation
  179. x, mx = self._create_data()
  180. # Result should be a MMatrix
  181. assert_(isinstance(add(mx, mx), MMatrix))
  182. assert_(isinstance(add(mx, x), MMatrix))
  183. # Result should work
  184. assert_equal(add(mx, x), mx + x)
  185. assert_(isinstance(add(mx, mx)._data, np.matrix))
  186. with assert_raises(TypeError):
  187. add.outer(mx, mx)
  188. assert_(isinstance(hypot(mx, mx), MMatrix))
  189. assert_(isinstance(hypot(mx, x), MMatrix))
  190. def test_masked_binary_operations2(self):
  191. # Tests domained_masked_binary_operation
  192. x, mx = self._create_data()
  193. xmx = masked_array(mx.data.__array__(), mask=mx.mask)
  194. assert_(isinstance(divide(mx, mx), MMatrix))
  195. assert_(isinstance(divide(mx, x), MMatrix))
  196. assert_equal(divide(mx, mx), divide(xmx, xmx))
  197. class TestConcatenator:
  198. # Tests for mr_, the equivalent of r_ for masked arrays.
  199. def test_matrix_builder(self):
  200. assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4'])
  201. def test_matrix(self):
  202. # Test consistency with unmasked version. If we ever deprecate
  203. # matrix, this test should either still pass, or both actual and
  204. # expected should fail to be build.
  205. actual = mr_['r', 1, 2, 3]
  206. expected = np.ma.array(np.r_['r', 1, 2, 3])
  207. assert_array_equal(actual, expected)
  208. # outer type is masked array, inner type is matrix
  209. assert_equal(type(actual), type(expected))
  210. assert_equal(type(actual.data), type(expected.data))