test_masked_matrix.py 8.7 KB

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