summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2018-04-26 17:39:58 -0400
committerMarten van Kerkwijk <mhvk@astro.utoronto.ca>2018-04-27 12:04:03 -0400
commitf18ebf68cfdf7aa816e42b1d358ad416b31a0ec1 (patch)
tree2bdd7ba0c93159650ab574489d9fea93d377721b
parentdadfb545e1cc8d873b999d82d331c0c58f11901f (diff)
downloadnumpy-f18ebf68cfdf7aa816e42b1d358ad416b31a0ec1.tar.gz
MAINT: move all masked array matrix tests to matrixlib.
Further progress in isolating matrix in preparation of its deprecation. There is one place left with an explicit reference to matrix (in MaskedArray.count), which is to be solved later.
-rw-r--r--numpy/ma/core.py16
-rw-r--r--numpy/ma/extras.py11
-rw-r--r--numpy/ma/tests/test_core.py129
-rw-r--r--numpy/ma/tests/test_extras.py3
-rw-r--r--numpy/ma/tests/test_subclassing.py45
-rw-r--r--numpy/matrixlib/tests/test_masked_matrix.py211
6 files changed, 249 insertions, 166 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 91cf8ed0f..fb28fa8e5 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -3089,7 +3089,7 @@ class MaskedArray(ndarray):
returned object (this is equivalent to setting the ``type``
parameter).
type : Python type, optional
- Type of the returned view, e.g., ndarray or matrix. Again, the
+ Type of the returned view, either ndarray or a subclass. The
default None results in type preservation.
Notes
@@ -3673,14 +3673,14 @@ class MaskedArray(ndarray):
>>> type(x.filled())
<type 'numpy.ndarray'>
- Subclassing is preserved. This means that if the data part of the masked
- array is a matrix, `filled` returns a matrix:
-
- >>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]])
- >>> x.filled()
- matrix([[ 1, 999999],
- [999999, 4]])
+ Subclassing is preserved. This means that if, e.g., the data part of
+ the masked array is a recarray, `filled` returns a recarray:
+ >>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray)
+ >>> m = np.ma.array(x, mask=[(True, False), (False, True)])
+ >>> m.filled()
+ rec.array([(999999, 2), ( -3, 999999)],
+ dtype=[('f0', '<i8'), ('f1', '<i8')])
"""
m = self._mask
if m is nomask:
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index 8272dced9..da35217d1 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -1465,9 +1465,14 @@ class MAxisConcatenator(AxisConcatenator):
"""
concatenate = staticmethod(concatenate)
- @staticmethod
- def makemat(arr):
- return array(arr.data.view(np.matrix), mask=arr.mask)
+ @classmethod
+ def makemat(cls, arr):
+ # There used to be a view as np.matrix here, but we may eventually
+ # deprecate that class. In preparation, we use the unmasked version
+ # to construct the matrix (with copy=False for backwards compatibility
+ # with the .view)
+ data = super(MAxisConcatenator, cls).makemat(arr.data, copy=False)
+ return array(data, mask=arr.mask)
def __getitem__(self, key):
# matrix builder syntax, like 'a, b; c, d'
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 9caf38b56..63703f6cd 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -335,49 +335,6 @@ class TestMaskedArray(object):
assert_equal(s1, s2)
assert_(x1[1:1].shape == (0,))
- def test_matrix_indexing(self):
- # Tests conversions and indexing
- x1 = np.matrix([[1, 2, 3], [4, 3, 2]])
- x2 = array(x1, mask=[[1, 0, 0], [0, 1, 0]])
- x3 = array(x1, mask=[[0, 1, 0], [1, 0, 0]])
- x4 = array(x1)
- # test conversion to strings
- str(x2) # raises?
- repr(x2) # raises?
- # tests of indexing
- assert_(type(x2[1, 0]) is type(x1[1, 0]))
- assert_(x1[1, 0] == x2[1, 0])
- assert_(x2[1, 1] is masked)
- assert_equal(x1[0, 2], x2[0, 2])
- assert_equal(x1[0, 1:], x2[0, 1:])
- assert_equal(x1[:, 2], x2[:, 2])
- assert_equal(x1[:], x2[:])
- assert_equal(x1[1:], x3[1:])
- x1[0, 2] = 9
- x2[0, 2] = 9
- assert_equal(x1, x2)
- x1[0, 1:] = 99
- x2[0, 1:] = 99
- assert_equal(x1, x2)
- x2[0, 1] = masked
- assert_equal(x1, x2)
- x2[0, 1:] = masked
- assert_equal(x1, x2)
- x2[0, :] = x1[0, :]
- x2[0, 1] = masked
- assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]])))
- x3[1, :] = masked_array([1, 2, 3], [1, 1, 0])
- assert_(allequal(getmask(x3)[1], array([1, 1, 0])))
- assert_(allequal(getmask(x3[1]), array([1, 1, 0])))
- x4[1, :] = masked_array([1, 2, 3], [1, 1, 0])
- assert_(allequal(getmask(x4[1]), array([1, 1, 0])))
- assert_(allequal(x4[1], array([1, 2, 3])))
- x1 = np.matrix(np.arange(5) * 1.0)
- x2 = masked_values(x1, 3.0)
- assert_equal(x1, x2)
- assert_(allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask))
- assert_equal(3.0, x2.fill_value)
-
@suppress_copy_mask_on_assignment
def test_copy(self):
# Tests of some subtle points of copying and sizing.
@@ -611,11 +568,13 @@ class TestMaskedArray(object):
def test_pickling_subbaseclass(self):
# Test pickling w/ a subclass of ndarray
- a = array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
+ x = np.array([(1.0, 2), (3.0, 4)],
+ dtype=[('x', float), ('y', int)]).view(np.recarray)
+ a = masked_array(x, mask=[(True, False), (False, True)])
a_pickled = pickle.loads(a.dumps())
assert_equal(a_pickled._mask, a._mask)
assert_equal(a_pickled, a)
- assert_(isinstance(a_pickled._data, np.matrix))
+ assert_(isinstance(a_pickled._data, np.recarray))
def test_pickling_maskedconstant(self):
# Test pickling MaskedConstant
@@ -1448,16 +1407,6 @@ class TestMaskedArrayArithmetic(object):
assert_(result is output)
assert_(output[0] is masked)
- def test_count_mean_with_matrix(self):
- m = np.ma.array(np.matrix([[1,2],[3,4]]), mask=np.zeros((2,2)))
-
- assert_equal(m.count(axis=0).shape, (1,2))
- assert_equal(m.count(axis=1).shape, (2,1))
-
- #make sure broadcasting inside mean and var work
- assert_equal(m.mean(axis=0), [[2., 3.]])
- assert_equal(m.mean(axis=1), [[1.5], [3.5]])
-
def test_eq_on_structured(self):
# Test the equality of structured arrays
ndtype = [('A', int), ('B', int)]
@@ -1740,23 +1689,6 @@ class TestMaskedArrayAttributes(object):
def test_flat(self):
# Test that flat can return all types of items [#4585, #4615]
- # test simple access
- test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
- assert_equal(test.flat[1], 2)
- assert_equal(test.flat[2], masked)
- assert_(np.all(test.flat[0:2] == test[0, 0:2]))
- # Test flat on masked_matrices
- test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
- test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
- control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
- assert_equal(test, control)
- # Test setting
- test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
- testflat = test.flat
- testflat[:] = testflat[[2, 1, 0]]
- assert_equal(test, control)
- testflat[0] = 9
- assert_equal(test[0, 0], 9)
# test 2-D record array
# ... on structured array w/ masked records
x = array([[(1, 1.1, 'one'), (2, 2.2, 'two'), (3, 3.3, 'thr')],
@@ -1784,12 +1716,6 @@ class TestMaskedArrayAttributes(object):
if i >= x.shape[-1]:
i = 0
j += 1
- # test that matrices keep the correct shape (#4615)
- a = masked_array(np.matrix(np.eye(2)), mask=0)
- b = a.flat
- b01 = b[:2]
- assert_equal(b01.data, array([[1., 0.]]))
- assert_equal(b01.mask, array([[False, False]]))
def test_assign_dtype(self):
# check that the mask's dtype is updated when dtype is changed
@@ -2893,32 +2819,6 @@ class TestMaskedArrayMethods(object):
assert_equal(mxsmall.any(0), [True, True, False])
assert_equal(mxsmall.any(1), [True, True, False])
- def test_allany_onmatrices(self):
- x = np.array([[0.13, 0.26, 0.90],
- [0.28, 0.33, 0.63],
- [0.31, 0.87, 0.70]])
- X = np.matrix(x)
- m = np.array([[True, False, False],
- [False, False, False],
- [True, True, False]], dtype=np.bool_)
- mX = masked_array(X, mask=m)
- mXbig = (mX > 0.5)
- mXsmall = (mX < 0.5)
-
- assert_(not mXbig.all())
- assert_(mXbig.any())
- assert_equal(mXbig.all(0), np.matrix([False, False, True]))
- assert_equal(mXbig.all(1), np.matrix([False, False, True]).T)
- assert_equal(mXbig.any(0), np.matrix([False, False, True]))
- assert_equal(mXbig.any(1), np.matrix([True, True, True]).T)
-
- assert_(not mXsmall.all())
- assert_(mXsmall.any())
- assert_equal(mXsmall.all(0), np.matrix([True, True, False]))
- assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T)
- assert_equal(mXsmall.any(0), np.matrix([True, True, False]))
- assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T)
-
def test_allany_oddities(self):
# Some fun with all and any
store = empty((), dtype=bool)
@@ -3017,14 +2917,6 @@ class TestMaskedArrayMethods(object):
b = a.compressed()
assert_equal(b, [2, 3, 4])
- a = array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0])
- b = a.compressed()
- assert_equal(b, a)
- assert_(isinstance(b, np.matrix))
- a[0, 0] = masked
- b = a.compressed()
- assert_equal(b, [[2, 3, 4]])
-
def test_empty(self):
# Tests empty/like
datatype = [('a', int), ('b', float), ('c', '|S8')]
@@ -3139,10 +3031,6 @@ class TestMaskedArrayMethods(object):
a = array([0, 0], mask=[1, 1])
aravel = a.ravel()
assert_equal(aravel._mask.shape, a.shape)
- a = array(np.matrix([1, 2, 3, 4, 5]), mask=[[0, 1, 0, 0, 0]])
- aravel = a.ravel()
- assert_equal(aravel.shape, (1, 5))
- assert_equal(aravel._mask.shape, a.shape)
# Checks that small_mask is preserved
a = array([1, 2, 3, 4], mask=[0, 0, 0, 0], shrink=False)
assert_equal(a.ravel()._mask, [0, 0, 0, 0])
@@ -4607,10 +4495,6 @@ class TestMaskedFields(object):
assert_equal(test, data)
assert_equal(test.mask, controlmask.reshape(-1, 2))
- test = a.view((float, 2), np.matrix)
- assert_equal(test, data)
- assert_(isinstance(test, np.matrix))
-
def test_getitem(self):
ndtype = [('a', float), ('b', float)]
a = array(list(zip(np.random.rand(10), np.arange(10))), dtype=ndtype)
@@ -4794,11 +4678,12 @@ class TestMaskedView(object):
def test_view_to_dtype_and_type(self):
(data, a, controlmask) = self.data
- test = a.view((float, 2), np.matrix)
+ test = a.view((float, 2), np.recarray)
assert_equal(test, data)
- assert_(isinstance(test, np.matrix))
+ assert_(isinstance(test, np.recarray))
assert_(not isinstance(test, MaskedArray))
+
class TestOptionalArgs(object):
def test_ndarrayfuncs(self):
# test axis arg behaves the same as ndarray (including multiple axes)
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py
index a7a32b628..2d5e30b2c 100644
--- a/numpy/ma/tests/test_extras.py
+++ b/numpy/ma/tests/test_extras.py
@@ -311,6 +311,9 @@ class TestConcatenator(object):
assert_raises(np.ma.MAError, lambda: mr_['1, 2; 3, 4'])
def test_matrix(self):
+ # Test consistency with unmasked version. If we ever deprecate
+ # matrix, this test should either still pass, or both actual and
+ # expected should fail to be build.
actual = mr_['r', 1, 2, 3]
expected = np.ma.array(np.r_['r', 1, 2, 3])
assert_array_equal(actual, expected)
diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py
index b61a46278..f8ab52bb9 100644
--- a/numpy/ma/tests/test_subclassing.py
+++ b/numpy/ma/tests/test_subclassing.py
@@ -75,27 +75,6 @@ class MSubArray(SubArray, MaskedArray):
msubarray = MSubArray
-class MMatrix(MaskedArray, np.matrix,):
-
- def __new__(cls, data, mask=nomask):
- mat = np.matrix(data)
- _data = MaskedArray.__new__(cls, data=mat, mask=mask)
- return _data
-
- def __array_finalize__(self, obj):
- np.matrix.__array_finalize__(self, obj)
- MaskedArray.__array_finalize__(self, obj)
- return
-
- def _get_series(self):
- _view = self.view(MaskedArray)
- _view._sharedmask = False
- return _view
- _series = property(fget=_get_series)
-
-mmatrix = MMatrix
-
-
# Also a subclass that overrides __str__, __repr__ and __setitem__, disallowing
# setting to non-class values (and thus np.ma.core.masked_print_option)
# and overrides __array_wrap__, updating the info dict, to check that this
@@ -180,7 +159,7 @@ class TestSubclassing(object):
def setup(self):
x = np.arange(5, dtype='float')
- mx = mmatrix(x, mask=[0, 1, 0, 0, 0])
+ mx = msubarray(x, mask=[0, 1, 0, 0, 0])
self.data = (x, mx)
def test_data_subclassing(self):
@@ -196,34 +175,34 @@ class TestSubclassing(object):
def test_maskedarray_subclassing(self):
# Tests subclassing MaskedArray
(x, mx) = self.data
- assert_(isinstance(mx._data, np.matrix))
+ assert_(isinstance(mx._data, subarray))
def test_masked_unary_operations(self):
# Tests masked_unary_operation
(x, mx) = self.data
with np.errstate(divide='ignore'):
- assert_(isinstance(log(mx), mmatrix))
+ assert_(isinstance(log(mx), msubarray))
assert_equal(log(x), np.log(x))
def test_masked_binary_operations(self):
# Tests masked_binary_operation
(x, mx) = self.data
- # Result should be a mmatrix
- assert_(isinstance(add(mx, mx), mmatrix))
- assert_(isinstance(add(mx, x), mmatrix))
+ # Result should be a msubarray
+ assert_(isinstance(add(mx, mx), msubarray))
+ assert_(isinstance(add(mx, x), msubarray))
# Result should work
assert_equal(add(mx, x), mx+x)
- assert_(isinstance(add(mx, mx)._data, np.matrix))
- assert_(isinstance(add.outer(mx, mx), mmatrix))
- assert_(isinstance(hypot(mx, mx), mmatrix))
- assert_(isinstance(hypot(mx, x), mmatrix))
+ assert_(isinstance(add(mx, mx)._data, subarray))
+ assert_(isinstance(add.outer(mx, mx), msubarray))
+ assert_(isinstance(hypot(mx, mx), msubarray))
+ assert_(isinstance(hypot(mx, x), msubarray))
def test_masked_binary_operations2(self):
# Tests domained_masked_binary_operation
(x, mx) = self.data
xmx = masked_array(mx.data.__array__(), mask=mx.mask)
- assert_(isinstance(divide(mx, mx), mmatrix))
- assert_(isinstance(divide(mx, x), mmatrix))
+ assert_(isinstance(divide(mx, mx), msubarray))
+ assert_(isinstance(divide(mx, x), msubarray))
assert_equal(divide(mx, mx), divide(xmx, xmx))
def test_attributepropagation(self):
diff --git a/numpy/matrixlib/tests/test_masked_matrix.py b/numpy/matrixlib/tests/test_masked_matrix.py
new file mode 100644
index 000000000..80d1cacca
--- /dev/null
+++ b/numpy/matrixlib/tests/test_masked_matrix.py
@@ -0,0 +1,211 @@
+from __future__ import division, absolute_import, print_function
+
+import pickle
+
+import numpy as np
+from numpy.ma.testutils import assert_, assert_equal
+from numpy.ma.core import (masked_array, masked_values, masked, allequal,
+ MaskType, getmask, MaskedArray, nomask,
+ log, add, hypot, divide)
+
+
+class MMatrix(MaskedArray, np.matrix,):
+
+ def __new__(cls, data, mask=nomask):
+ mat = np.matrix(data)
+ _data = MaskedArray.__new__(cls, data=mat, mask=mask)
+ return _data
+
+ def __array_finalize__(self, obj):
+ np.matrix.__array_finalize__(self, obj)
+ MaskedArray.__array_finalize__(self, obj)
+ return
+
+ def _get_series(self):
+ _view = self.view(MaskedArray)
+ _view._sharedmask = False
+ return _view
+ _series = property(fget=_get_series)
+
+
+class TestMaskedMatrix(object):
+ def test_matrix_indexing(self):
+ # Tests conversions and indexing
+ x1 = np.matrix([[1, 2, 3], [4, 3, 2]])
+ x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]])
+ x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]])
+ x4 = masked_array(x1)
+ # test conversion to strings
+ str(x2) # raises?
+ repr(x2) # raises?
+ # tests of indexing
+ assert_(type(x2[1, 0]) is type(x1[1, 0]))
+ assert_(x1[1, 0] == x2[1, 0])
+ assert_(x2[1, 1] is masked)
+ assert_equal(x1[0, 2], x2[0, 2])
+ assert_equal(x1[0, 1:], x2[0, 1:])
+ assert_equal(x1[:, 2], x2[:, 2])
+ assert_equal(x1[:], x2[:])
+ assert_equal(x1[1:], x3[1:])
+ x1[0, 2] = 9
+ x2[0, 2] = 9
+ assert_equal(x1, x2)
+ x1[0, 1:] = 99
+ x2[0, 1:] = 99
+ assert_equal(x1, x2)
+ x2[0, 1] = masked
+ assert_equal(x1, x2)
+ x2[0, 1:] = masked
+ assert_equal(x1, x2)
+ x2[0, :] = x1[0, :]
+ x2[0, 1] = masked
+ assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]])))
+ x3[1, :] = masked_array([1, 2, 3], [1, 1, 0])
+ assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0])))
+ assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0])))
+ x4[1, :] = masked_array([1, 2, 3], [1, 1, 0])
+ assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0])))
+ assert_(allequal(x4[1], masked_array([1, 2, 3])))
+ x1 = np.matrix(np.arange(5) * 1.0)
+ x2 = masked_values(x1, 3.0)
+ assert_equal(x1, x2)
+ assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType),
+ x2.mask))
+ assert_equal(3.0, x2.fill_value)
+
+ def test_pickling_subbaseclass(self):
+ # Test pickling w/ a subclass of ndarray
+ a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
+ a_pickled = pickle.loads(a.dumps())
+ assert_equal(a_pickled._mask, a._mask)
+ assert_equal(a_pickled, a)
+ assert_(isinstance(a_pickled._data, np.matrix))
+
+ def test_count_mean_with_matrix(self):
+ m = masked_array(np.matrix([[1, 2], [3, 4]]), mask=np.zeros((2, 2)))
+
+ assert_equal(m.count(axis=0).shape, (1, 2))
+ assert_equal(m.count(axis=1).shape, (2, 1))
+
+ # Make sure broadcasting inside mean and var work
+ assert_equal(m.mean(axis=0), [[2., 3.]])
+ assert_equal(m.mean(axis=1), [[1.5], [3.5]])
+
+ def test_flat(self):
+ # Test that flat can return items even for matrices [#4585, #4615]
+ # test simple access
+ test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
+ assert_equal(test.flat[1], 2)
+ assert_equal(test.flat[2], masked)
+ assert_(np.all(test.flat[0:2] == test[0, 0:2]))
+ # Test flat on masked_matrices
+ test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
+ test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
+ control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
+ assert_equal(test, control)
+ # Test setting
+ test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
+ testflat = test.flat
+ testflat[:] = testflat[[2, 1, 0]]
+ assert_equal(test, control)
+ testflat[0] = 9
+ # test that matrices keep the correct shape (#4615)
+ a = masked_array(np.matrix(np.eye(2)), mask=0)
+ b = a.flat
+ b01 = b[:2]
+ assert_equal(b01.data, np.array([[1., 0.]]))
+ assert_equal(b01.mask, np.array([[False, False]]))
+
+ def test_allany_onmatrices(self):
+ x = np.array([[0.13, 0.26, 0.90],
+ [0.28, 0.33, 0.63],
+ [0.31, 0.87, 0.70]])
+ X = np.matrix(x)
+ m = np.array([[True, False, False],
+ [False, False, False],
+ [True, True, False]], dtype=np.bool_)
+ mX = masked_array(X, mask=m)
+ mXbig = (mX > 0.5)
+ mXsmall = (mX < 0.5)
+
+ assert_(not mXbig.all())
+ assert_(mXbig.any())
+ assert_equal(mXbig.all(0), np.matrix([False, False, True]))
+ assert_equal(mXbig.all(1), np.matrix([False, False, True]).T)
+ assert_equal(mXbig.any(0), np.matrix([False, False, True]))
+ assert_equal(mXbig.any(1), np.matrix([True, True, True]).T)
+
+ assert_(not mXsmall.all())
+ assert_(mXsmall.any())
+ assert_equal(mXsmall.all(0), np.matrix([True, True, False]))
+ assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T)
+ assert_equal(mXsmall.any(0), np.matrix([True, True, False]))
+ assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T)
+
+ def test_compressed(self):
+ a = masked_array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0])
+ b = a.compressed()
+ assert_equal(b, a)
+ assert_(isinstance(b, np.matrix))
+ a[0, 0] = masked
+ b = a.compressed()
+ assert_equal(b, [[2, 3, 4]])
+
+ def test_ravel(self):
+ a = masked_array(np.matrix([1, 2, 3, 4, 5]), mask=[[0, 1, 0, 0, 0]])
+ aravel = a.ravel()
+ assert_equal(aravel.shape, (1, 5))
+ assert_equal(aravel._mask.shape, a.shape)
+
+ def test_view(self):
+ # Test view w/ flexible dtype
+ iterator = list(zip(np.arange(10), np.random.rand(10)))
+ data = np.array(iterator)
+ a = masked_array(iterator, dtype=[('a', float), ('b', float)])
+ a.mask[0] = (1, 0)
+ test = a.view((float, 2), np.matrix)
+ assert_equal(test, data)
+ assert_(isinstance(test, np.matrix))
+ assert_(not isinstance(test, MaskedArray))
+
+
+class TestSubclassing(object):
+ # Test suite for masked subclasses of ndarray.
+
+ def setup(self):
+ x = np.arange(5, dtype='float')
+ mx = MMatrix(x, mask=[0, 1, 0, 0, 0])
+ self.data = (x, mx)
+
+ def test_maskedarray_subclassing(self):
+ # Tests subclassing MaskedArray
+ (x, mx) = self.data
+ assert_(isinstance(mx._data, np.matrix))
+
+ def test_masked_unary_operations(self):
+ # Tests masked_unary_operation
+ (x, mx) = self.data
+ with np.errstate(divide='ignore'):
+ assert_(isinstance(log(mx), MMatrix))
+ assert_equal(log(x), np.log(x))
+
+ def test_masked_binary_operations(self):
+ # Tests masked_binary_operation
+ (x, mx) = self.data
+ # Result should be a MMatrix
+ assert_(isinstance(add(mx, mx), MMatrix))
+ assert_(isinstance(add(mx, x), MMatrix))
+ # Result should work
+ assert_equal(add(mx, x), mx+x)
+ assert_(isinstance(add(mx, mx)._data, np.matrix))
+ assert_(isinstance(add.outer(mx, mx), MMatrix))
+ assert_(isinstance(hypot(mx, mx), MMatrix))
+ assert_(isinstance(hypot(mx, x), MMatrix))
+
+ def test_masked_binary_operations2(self):
+ # Tests domained_masked_binary_operation
+ (x, mx) = self.data
+ xmx = masked_array(mx.data.__array__(), mask=mx.mask)
+ assert_(isinstance(divide(mx, mx), MMatrix))
+ assert_(isinstance(divide(mx, x), MMatrix))
+ assert_equal(divide(mx, mx), divide(xmx, xmx))