summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2009-08-06 16:38:54 +0000
committerpierregm <pierregm@localhost>2009-08-06 16:38:54 +0000
commitd0bba527fa3ade5674c670b6df1d7ad0346603c1 (patch)
treeb94659f8db46956e6f1a2b1fbec0a5f0fbee1a21 /numpy
parent99835f47c42535b20bddb670f09bb99f011eae7f (diff)
downloadnumpy-d0bba527fa3ade5674c670b6df1d7ad0346603c1.tar.gz
* fixed _from_methods to run on ndarrays (bug #1187)
Diffstat (limited to 'numpy')
-rw-r--r--numpy/ma/core.py36
-rw-r--r--numpy/ma/tests/test_core.py12
2 files changed, 35 insertions, 13 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 5f6a7e260..851f753b7 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -4909,19 +4909,31 @@ class _frommethod:
return doc
#
def __call__(self, a, *args, **params):
- if isinstance(a, MaskedArray):
- return getattr(a, self.__name__).__call__(*args, **params)
- #FIXME ----
- #As x is not a MaskedArray, we transform it to a ndarray with asarray
- #... and call the corresponding method.
- #Except that sometimes it doesn't work (try reshape([1,2,3,4],(2,2)))
- #we end up with a "SystemError: NULL result without error in PyObject_Call"
- #A dirty trick is then to call the initial numpy function...
- method = getattr(narray(a, copy=False), self.__name__)
- try:
+ # Get the method from the array (if possible)
+ method_name = self.__name__
+ method = getattr(a, method_name, None)
+ if method is not None:
return method(*args, **params)
- except SystemError:
- return getattr(np,self.__name__).__call__(a, *args, **params)
+ # Still here ? Then a is not a MaskedArray
+ method = getattr(MaskedArray, method_name, None)
+ if method is not None:
+ return method(MaskedArray(a), *args, **params)
+ # Still here ? OK, let's call the corresponding np function
+ method = getattr(np, method_name)
+ return method(a, *args, **params)
+# if isinstance(a, MaskedArray):
+# return getattr(a, self.__name__).__call__(*args, **params)
+# #FIXME ----
+# #As x is not a MaskedArray, we transform it to a ndarray with asarray
+# #... and call the corresponding method.
+# #Except that sometimes it doesn't work (try reshape([1,2,3,4],(2,2)))
+# #we end up with a "SystemError: NULL result without error in PyObject_Call"
+# #A dirty trick is then to call the initial numpy function...
+# method = getattr(narray(a, copy=False), self.__name__)
+# try:
+# return method(*args, **params)
+# except SystemError:
+# return getattr(np,self.__name__).__call__(a, *args, **params)
all = _frommethod('all')
anomalies = anom = _frommethod('anom')
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index ee2af1110..dc37ff4b6 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -2339,7 +2339,7 @@ class TestMaskedArrayMethods(TestCase):
#------------------------------------------------------------------------------
-class TestMaskArrayMathMethod(TestCase):
+class TestMaskedArrayMathMethods(TestCase):
def setUp(self):
"Base data definition."
@@ -2978,6 +2978,16 @@ class TestMaskedArrayFunctions(TestCase):
control = np.array([ 0, 0, 0, 0, 0, 1], dtype=bool)
assert_equal(test, control)
+
+ def test_on_ndarray(self):
+ "Test functions on ndarrays"
+ a = np.array([1, 2, 3, 4])
+ m = array(a, mask=False)
+ test = anom(a)
+ assert_equal(test, m.anom())
+ test = reshape(a, (2, 2))
+ assert_equal(test, m.reshape(2, 2))
+
#------------------------------------------------------------------------------
class TestMaskedFields(TestCase):