diff options
author | pierregm <pierregm@localhost> | 2009-02-07 09:19:12 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2009-02-07 09:19:12 +0000 |
commit | c180e36cf4438947de5ba38c90ec30e9538f2f10 (patch) | |
tree | c7ee8855ee4594b8d5f2b7d9a474755373c38424 | |
parent | b9086fdf39941343fde65551b8667e635e05f14f (diff) | |
download | numpy-c180e36cf4438947de5ba38c90ec30e9538f2f10.tar.gz |
MaskedArray.resize : systematically raise a TypeError exception, as a masked array never owns its data
MaskedIterator : fixed to allow .flat on masked matrices
-rw-r--r-- | numpy/ma/core.py | 52 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 11 |
2 files changed, 40 insertions, 23 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 2b50cc55a..90c74d9c1 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1478,9 +1478,8 @@ class MaskedIterator(object): "Define an interator." def __init__(self, ma): self.ma = ma - self.ma1d = ma.ravel() - self.ma_iter = np.asarray(ma).flat - + self.dataiter = ma._data.flat + # if ma._mask is nomask: self.maskiter = None else: @@ -1490,15 +1489,23 @@ class MaskedIterator(object): return self def __getitem__(self, indx): - return self.ma1d.__getitem__(indx) + result = self.dataiter.__getitem__(indx).view(type(self.ma)) + if self.maskiter is not None: + _mask = self.maskiter.__getitem__(indx) + _mask.shape = result.shape + result._mask = _mask + return result ### This won't work is ravel makes a copy def __setitem__(self, index, value): - self.ma1d[index] = value + self.dataiter[index] = getdata(value) + if self.maskiter is not None: + self.maskiter[index] = getmaskarray(value) +# self.ma1d[index] = value def next(self): "Returns the next element of the iterator." - d = self.ma_iter.next() + d = self.dataiter.next() if self.maskiter is not None and self.maskiter.next(): d = masked return d @@ -2707,25 +2714,24 @@ class MaskedArray(ndarray): return result # def resize(self, newshape, refcheck=True, order=False): - """Attempt to modify the size and the shape of the array in place. - - The array must own its own memory and not be referenced by - other arrays. - - Returns - ------- - None. + """ + Change shape and size of array in-place. """ - try: - self._data.resize(newshape, refcheck, order) - if self.mask is not nomask: - self._mask.resize(newshape, refcheck, order) - except ValueError: - raise ValueError("Cannot resize an array that has been referenced " - "or is referencing another array in this way.\n" - "Use the resize function.") - return None + # Note : the 'order' keyword looks broken, let's just drop it +# try: +# ndarray.resize(self, newshape, refcheck=refcheck) +# if self.mask is not nomask: +# self._mask.resize(newshape, refcheck=refcheck) +# except ValueError: +# raise ValueError("Cannot resize an array that has been referenced " +# "or is referencing another array in this way.\n" +# "Use the numpy.ma.resize function.") +# return None + errmsg = "A masked array does not own its data "\ + "and therefore cannot be resized.\n" \ + "Use the numpy.ma.resize function instead." + raise ValueError(errmsg) # def put(self, indices, values, mode='raise'): """ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 9c523429a..f13ff3377 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1122,6 +1122,17 @@ class TestMaskedArrayAttributes(TestCase): a[1] = 1 assert_equal(a._mask, zeros(10)) + def test_flat(self): + "Test flat on masked_matrices" + test = ma.array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1]) + test.flat = ma.array([3, 2, 1], mask=[1, 0, 0]) + control = ma.array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0]) + assert_equal(test, control) + # + test = ma.array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1]) + testflat = test.flat + testflat[:] = testflat[[2, 1, 0]] + assert_equal(test, control) #------------------------------------------------------------------------------ |