diff options
author | pierregm <pierregm@localhost> | 2008-05-05 19:22:01 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-05-05 19:22:01 +0000 |
commit | 91c6c401451df57a41e7715c591faedf79ecb581 (patch) | |
tree | c83cda8f9eab11791dd0d1e4589d38fcc9775666 | |
parent | 974b5f54ab552c27ff39e9ae9c63bdfd38c17fbb (diff) | |
download | numpy-91c6c401451df57a41e7715c591faedf79ecb581.tar.gz |
core : force .compressed() to return a type(_baseclass) object (usually a ndarray)
: fixed a bug in .compressed() when the _baseclass is a matrix
-rw-r--r-- | numpy/ma/core.py | 9 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 20 |
2 files changed, 23 insertions, 6 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index cf4d8be2d..61ea0fbec 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1307,6 +1307,10 @@ class MaskedArray(numeric.ndarray): # msg = "Masked arrays must be filled before they can be used as indices!" # raise IndexError, msg dout = ndarray.__getitem__(self.view(ndarray), indx) + # We could directly use ndarray.__getitem__ on self... + # But then we would have to modify __array_finalize__ to prevent the + # mask of being reshaped if it hasn't been set up properly yet... + # So it's easier to stick to the current version m = self._mask if not getattr(dout,'ndim', False): # Just a scalar............ @@ -1577,10 +1581,9 @@ class MaskedArray(numeric.ndarray): """Return a 1-D array of all the non-masked data. """ - data = ndarray.ravel(self._data).view(type(self)) - data._update_from(self) + data = ndarray.ravel(self._data) if self._mask is not nomask: - data = data[numpy.logical_not(ndarray.ravel(self._mask))] + data = data.compress(numpy.logical_not(ndarray.ravel(self._mask))) return data diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 2ed056237..e86922eca 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1342,6 +1342,13 @@ class TestArrayMethods(NumpyTestCase): # Checs 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]) + # Test that the fill_value is preserved + a.fill_value = -99 + a.shape = (2,2) + ar = a.ravel() + assert_equal(ar._mask, [0,0,0,0]) + assert_equal(ar._data, [1,2,3,4]) + assert_equal(ar.fill_value, -99) def test_reshape(self): "Tests reshape" @@ -1358,11 +1365,18 @@ class TestArrayMethods(NumpyTestCase): a = array([1,2,3,4],mask=[0,0,0,0]) b = a.compressed() assert_equal(b, a) - assert_equal(b._mask, nomask) a[0] = masked b = a.compressed() - assert_equal(b._data, [2,3,4]) - assert_equal(b._mask, nomask) + assert_equal(b, [2,3,4]) + # + a = array(numpy.matrix([1,2,3,4]), mask=[0,0,0,0]) + b = a.compressed() + assert_equal(b,a) + assert(isinstance(b,numpy.matrix)) + a[0,0] = masked + b = a.compressed() + assert_equal(b, [[2,3,4]]) + def test_tolist(self): "Tests to list" |