diff options
author | sasha <sasha@localhost> | 2006-01-04 18:29:28 +0000 |
---|---|---|
committer | sasha <sasha@localhost> | 2006-01-04 18:29:28 +0000 |
commit | 6d9c86cde543110e1b5937e2b217eb1374ee9adf (patch) | |
tree | 79b3dfb18d84f4a1ddbe1e3f00609b08832f0a85 | |
parent | 2581fe8666f62f00cb9af309010d61a43af91761 (diff) | |
download | numpy-6d9c86cde543110e1b5937e2b217eb1374ee9adf.tar.gz |
Fixed sf bugs 1395698 and 1395688
Note that this patch changes the return type of a[i] and
the conversion to bool for consistency with the scipy core
rules.
-rw-r--r-- | scipy/base/ma.py | 88 | ||||
-rw-r--r-- | scipy/base/tests/test_ma.py | 15 |
2 files changed, 56 insertions, 47 deletions
diff --git a/scipy/base/ma.py b/scipy/base/ma.py index 245351349..725a2cf91 100644 --- a/scipy/base/ma.py +++ b/scipy/base/ma.py @@ -301,21 +301,16 @@ class masked_unary_operation: d1 = filled(a, self.fill) if self.domain is not None: m = mask_or(m, self.domain(d1)) - if m is None: - result = self.f(d1, *args, **kwargs) - if type(result) is ndarray: - return masked_array (result) - else: - return result - else: - dx = masked_array(d1, m) - result = self.f(filled(dx, self.fill), *args, **kwargs) - if type(result) is ndarray: - return masked_array(result, m) - elif m[...]: - return masked + result = self.f(d1, *args, **kwargs) + if m is not None: + try: + shape = result.shape + except AttributeError: + pass else: - return result + if m.shape != shape: + m = mask_or(getmaskarray(a), getmaskarray(b)) + return masked_array(result, m) def __str__ (self): return "Masked version of " + str(self.f) @@ -353,21 +348,17 @@ class domained_binary_operation: d2 = where(t, self.filly, d2) mb = mask_or(mb, t) m = mask_or(ma, mb) - if m is None: - result = self.f(d1, d2) - if type(result) is ndarray: - return masked_array(result) + result = self.f(d1, d2) + if m is not None: + try: + shape = result.shape + except AttributeError: + pass else: - return result - result = self.f(d1, d2) - if type(result) is ndarray: - if m.shape != result.shape: - m = mask_or(getmaskarray(a), getmaskarray(b)) - return masked_array(result, m) - elif m[...]: - return masked - else: - return result + if m.shape != shape: + m = mask_or(getmaskarray(a), getmaskarray(b)) + return masked_array(result, m) + def __str__ (self): return "Masked version of " + str(self.f) @@ -384,25 +375,18 @@ class masked_binary_operation: def __call__ (self, a, b, *args, **kwargs): "Execute the call behavior." m = mask_or(getmask(a), getmask(b)) - if m is None: - d1 = filled(a, self.fillx) - d2 = filled(b, self.filly) - result = self.f(d1, d2, *args, **kwargs) - if type(result) is ndarray: - return masked_array(result) - else: - return result d1 = filled(a, self.fillx) d2 = filled(b, self.filly) result = self.f(d1, d2, *args, **kwargs) - if type(result) is ndarray: - if m.shape != result.shape: - m = mask_or(getmaskarray(a), getmaskarray(b)) - return masked_array(result, m) - elif m[...]: - return masked - else: - return result + if m is not None: + try: + shape = result.shape + except AttributeError: + pass + else: + if m.shape != shape: + m = mask_or(getmaskarray(a), getmaskarray(b)) + return masked_array(result, m) def reduce (self, target, axis=0): """Reduce target along the given axis with this function.""" @@ -834,6 +818,18 @@ array(data = %(data)s, self._shared_mask = False self._mask[i:j] = m + def __nonzero__(self): + """returns true if any element is non-zero or masked + + """ + # XXX: This changes bool conversion logic from MA. + # XXX: In MA bool(a) == len(a) != 0, but in scipy + # XXX: scalars do not have len + m = self._mask + d = self._data + return bool(m is not None and m.any() + or d is not None and d.any()) + def __len__ (self): """Return length of first dimension. This is weird but Python's slicing behavior depends on it.""" @@ -1906,7 +1902,7 @@ class _minimum_operation: else: return amin(ac.raw_data()) else: - return where(less(a, b), a, b)[...] + return where(less(a, b), a, b) def reduce (self, target, axis=0): """Reduce target along the given axis.""" @@ -1955,7 +1951,7 @@ class _maximum_operation: else: return amax(ac.raw_data()) else: - return where(greater(a, b), a, b)[...] + return where(greater(a, b), a, b) def reduce (self, target, axis=0): """Reduce target along the given axis.""" diff --git a/scipy/base/tests/test_ma.py b/scipy/base/tests/test_ma.py index 884a4a277..f4e4a7bca 100644 --- a/scipy/base/tests/test_ma.py +++ b/scipy/base/tests/test_ma.py @@ -581,7 +581,20 @@ class test_ma(ScipyTestCase): self.assertEqual(1.0, float(array([[1]]))) self.failUnlessRaises(ValueError, float, array([1,1])) self.failUnlessRaises(MAError, float, array([1],mask=[1])) - + self.failUnless(bool(array([0,1]))) + self.failUnless(bool(array([0,0],mask=[0,1]))) + self.failIf(bool(array([0,0]))) + self.failIf(bool(array([0,0],mask=[0,0]))) + + def check_testScalarArithmetic(self): + xm = array(0, mask=1) + self.failUnless((1/array(0)).mask) + self.failUnless((1 + xm).mask) + self.failUnless((-xm).mask) + self.failUnless((-xm).mask) + self.failUnless(maximum(xm, xm).mask) + self.failUnless(minimum(xm, xm).mask) + def timingTest(): for f in [testf, testinplace]: for n in [1000,10000,50000]: |