summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2008-06-16 17:29:28 +0000
committerpierregm <pierregm@localhost>2008-06-16 17:29:28 +0000
commit5bb7b22cf9e9cbc55a0894b7d5a47b43460d9c69 (patch)
tree33e56001ce6b4dd4b3795559783ab9a5300d47ff
parent8f851719233d1e1dd47b7dc1595d5fb4e53eb33e (diff)
downloadnumpy-5bb7b22cf9e9cbc55a0894b7d5a47b43460d9c69.tar.gz
core.MaskedArray.__new__
* Force a mask to be created from a list of masked arrays when mask=nomask and keep_mask=True
-rw-r--r--numpy/ma/core.py16
-rw-r--r--numpy/ma/tests/test_core.py12
2 files changed, 24 insertions, 4 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 392e91ec1..7a11e1eb6 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -1225,11 +1225,19 @@ class MaskedArray(ndarray):
# With full version
else:
_data._mask = np.zeros(_data.shape, dtype=mdtype)
- if copy:
- _data._mask = _data._mask.copy()
- _data._sharedmask = False
+ # Check whether we missed something
+ elif isinstance(data, (tuple,list)):
+ mask = np.array([getmaskarray(m) for m in data], dtype=mdtype)
+ # Force shrinking of the mask if needed (and possible)
+ if (mdtype == MaskType) and mask.any():
+ _data._mask = mask
+ _data._sharedmask = False
else:
- _data._sharedmask = True
+ if copy:
+ _data._mask = _data._mask.copy()
+ _data._sharedmask = False
+ else:
+ _data._sharedmask = True
# Case 2. : With a mask in input ........
else:
# Read the mask with the current mdtype
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index fd3395188..4558777dd 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -167,6 +167,18 @@ class TestMaskedArray(NumpyTestCase):
dma_3 = MaskedArray(dma_1, mask=[1,0,0,0]*6)
fail_if_equal(dma_3.mask, dma_1.mask)
+ def test_creation_with_list_of_maskedarrays(self):
+ "Tests creaating a masked array from alist of masked arrays."
+ x = array(np.arange(5), mask=[1,0,0,0,0])
+ data = array((x,x[::-1]))
+ assert_equal(data, [[0,1,2,3,4],[4,3,2,1,0]])
+ assert_equal(data._mask, [[1,0,0,0,0],[0,0,0,0,1]])
+ #
+ x.mask = nomask
+ data = array((x,x[::-1]))
+ assert_equal(data, [[0,1,2,3,4],[4,3,2,1,0]])
+ assert(data.mask is nomask)
+
def test_asarray(self):
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
xm.fill_value = -9999