summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2009-04-13 11:24:40 +0000
committerpierregm <pierregm@localhost>2009-04-13 11:24:40 +0000
commit430647c0dbdde130f0747c1fa2b1939d33e8f9b6 (patch)
tree1f169859fae972a87927b907fa87b3eaeb558333 /numpy
parentd673c50ccfe30f0b97de2d853e20fdc191a3fe30 (diff)
downloadnumpy-430647c0dbdde130f0747c1fa2b1939d33e8f9b6.tar.gz
* fixed notmasked_edges when no data are masked
Diffstat (limited to 'numpy')
-rw-r--r--numpy/ma/extras.py8
-rw-r--r--numpy/ma/tests/test_extras.py52
2 files changed, 42 insertions, 18 deletions
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index f30f73ab3..1aa43a222 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -1249,8 +1249,8 @@ mr_ = mr_class()
def flatnotmasked_edges(a):
"""
- Find the indices of the first and last not masked values in a
- 1D masked array. If all values are masked, returns None.
+ Find the indices of the first and last valid values in a 1D masked array.
+ If all values are masked, returns None.
"""
m = getmask(a)
@@ -1282,7 +1282,7 @@ def notmasked_edges(a, axis=None):
a = asarray(a)
if axis is None or a.ndim == 1:
return flatnotmasked_edges(a)
- m = getmask(a)
+ m = getmaskarray(a)
idx = array(np.indices(a.shape), mask=np.asarray([m]*a.ndim))
return [tuple([idx[i].min(axis).compressed() for i in range(a.ndim)]),
tuple([idx[i].max(axis).compressed() for i in range(a.ndim)]),]
@@ -1338,7 +1338,7 @@ def notmasked_contiguous(a, axis=None):
result = []
#
other = (axis+1)%2
- idx = [0,0]
+ idx = [0, 0]
idx[axis] = slice(None, None)
#
for i in range(a.shape[other]):
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py
index 5ae7bd72c..3c6de62be 100644
--- a/numpy/ma/tests/test_extras.py
+++ b/numpy/ma/tests/test_extras.py
@@ -183,24 +183,45 @@ class TestConcatenator(TestCase):
assert_array_equal(d[5:,:],b_2)
assert_array_equal(d.mask, np.r_[m_1,m_2])
+
+
class TestNotMasked(TestCase):
"Tests notmasked_edges and notmasked_contiguous."
def test_edges(self):
"Tests unmasked_edges"
- a = masked_array(np.arange(24).reshape(3,8),
- mask=[[0,0,0,0,1,1,1,0],
- [1,1,1,1,1,1,1,1],
- [0,0,0,0,0,0,1,0],])
- #
- assert_equal(notmasked_edges(a, None), [0,23])
- #
- tmp = notmasked_edges(a, 0)
- assert_equal(tmp[0], (array([0,0,0,0,2,2,0]), array([0,1,2,3,4,5,7])))
- assert_equal(tmp[1], (array([2,2,2,2,2,2,2]), array([0,1,2,3,4,5,7])))
- #
- tmp = notmasked_edges(a, 1)
- assert_equal(tmp[0], (array([0,2,]), array([0,0])))
- assert_equal(tmp[1], (array([0,2,]), array([7,7])))
+ data = masked_array(np.arange(25).reshape(5, 5),
+ mask=[[0, 0, 1, 0, 0],
+ [0, 0, 0, 1, 1],
+ [1, 1, 0, 0, 0],
+ [0, 0, 0, 0, 0],
+ [1, 1, 1, 0, 0]],)
+ test = notmasked_edges(data, None)
+ assert_equal(test, [0, 24])
+ test = notmasked_edges(data, 0)
+ assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
+ assert_equal(test[1], [(3, 3, 3, 4, 4), (0, 1, 2, 3, 4)])
+ test = notmasked_edges(data, 1)
+ assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 2, 0, 3)])
+ assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 2, 4, 4, 4)])
+ #
+ test = notmasked_edges(data.data, None)
+ assert_equal(test, [0, -1])
+ test = notmasked_edges(data.data, 0)
+ assert_equal(test[0], [(0, 0, 0, 0, 0), (0, 1, 2, 3, 4)])
+ assert_equal(test[1], [(4, 4, 4, 4, 4), (0, 1, 2, 3, 4)])
+ test = notmasked_edges(data.data, -1)
+ assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 0, 0, 0)])
+ assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 4, 4, 4, 4)])
+ #
+ data[-2] = masked
+ test = notmasked_edges(data, 0)
+ assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)])
+ assert_equal(test[1], [(1, 1, 2, 4, 4), (0, 1, 2, 3, 4)])
+ test = notmasked_edges(data, -1)
+ assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)])
+ assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)])
+
+
def test_contiguous(self):
"Tests notmasked_contiguous"
@@ -225,6 +246,9 @@ class TestNotMasked(TestCase):
assert_equal(tmp[2][-1], slice(7,7,None))
assert_equal(tmp[2][-2], slice(0,5,None))
+
+
+
class Test2DFunctions(TestCase):
"Tests 2D functions"
def test_compress2d(self):