diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/ma/core.py | 36 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 12 |
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): |