diff options
author | pierregm <pierregm@localhost> | 2008-09-11 19:54:31 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-09-11 19:54:31 +0000 |
commit | 91391ca79005517318884cb0fb6433e2acda8e6b (patch) | |
tree | 8fe426354e8cb17bc777c313c032af87c40c9b84 /numpy | |
parent | d14ca836da11243abb042ab12b1e887f4bfa2d37 (diff) | |
download | numpy-91391ca79005517318884cb0fb6433e2acda8e6b.tar.gz |
* fixed view for MaskedArrays w/ flexible dtype
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 34 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 28 |
2 files changed, 58 insertions, 4 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 6d7dc5ddd..e76f8787b 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1257,9 +1257,9 @@ class MaskedArray(ndarray): copy = True # Careful, cls might not always be MaskedArray... if not isinstance(data, cls) or not subok: - _data = _data.view(cls) + _data = ndarray.view(_data, cls) else: - _data = _data.view(type(data)) + _data = ndarray.view(_data, type(data)) # Backwards compatibility w/ numpy.core.ma ....... if hasattr(data,'_mask') and not isinstance(data, ndarray): _data._mask = data._mask @@ -1374,7 +1374,15 @@ class MaskedArray(ndarray): """ # Get main attributes ......... self._update_from(obj) - self._mask = getattr(obj, '_mask', nomask) + if isinstance(obj, ndarray): + odtype = obj.dtype + if odtype.names: + _mask = getattr(obj, '_mask', make_mask_none(obj.shape, odtype)) + else: + _mask = getattr(obj, '_mask', nomask) + else: + _mask = nomask + self._mask = _mask # Finalize the mask ........... if self._mask is not nomask: self._mask.shape = self.shape @@ -1425,6 +1433,24 @@ class MaskedArray(ndarray): #.... return result #............................................. + def view(self, dtype=None, type=None): + if dtype is not None: + if type is None: + args = (dtype,) + else: + args = (dtype, type) + elif type is None: + args = () + else: + args = (type,) + output = ndarray.view(self, *args) + if (getattr(output,'_mask', nomask) is not nomask): + mdtype = make_mask_descr(output.dtype) + output._mask = self._mask.view(mdtype, ndarray) + output._mask.shape = output.shape + return output + view.__doc__ = ndarray.view.__doc__ + #............................................. def astype(self, newtype): """Returns a copy of the array cast to newtype.""" newtype = np.dtype(newtype) @@ -1701,7 +1727,7 @@ class MaskedArray(ndarray): underlying data. """ - return self.view(self._baseclass) + return ndarray.view(self, self._baseclass) _data = property(fget=_get_data) data = property(fget=_get_data) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 78fe1a223..411fca516 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -2342,6 +2342,34 @@ class TestMaskedFields(TestCase): assert_equal(getmaskarray(test), np.array([(1, 1) , (1, 1), (1, 1)], dtype=[('a', '|b1'), ('b', '|b1')])) + # + def test_view(self): + "Test view w/ flexible dtype" + iterator = zip(np.arange(10), np.random.rand(10)) + data = np.array(iterator) + a = array(iterator, dtype=[('a',float),('b',float)]) + a.mask[0] = (1,0) + controlmask = np.array([1]+19*[0], dtype=bool) + # + test = a.view(float) + assert_equal(test, data.ravel()) + assert_equal(test.mask, controlmask) + # + test = a.view((float,2)) + assert_equal(test, data) + assert_equal(test.mask, controlmask.reshape(-1,2)) + # + test = a.view([('A',float),('B',float)]) + assert_equal(test.mask.dtype.names, ('A', 'B')) + assert_equal(test['A'], a['a']) + assert_equal(test['B'], a['b']) + # + test = a.view(np.ndarray) + assert_equal(test, a._data) + # + test = a.view((float,2), np.matrix) + assert_equal(test, data) + assert(isinstance(test, np.matrix)) ############################################################################### |