diff options
Diffstat (limited to 'numpy/ma/tests/test_subclassing.py')
-rw-r--r-- | numpy/ma/tests/test_subclassing.py | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index 19058fec2..c2c9b8ec9 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -11,23 +11,26 @@ from __future__ import division, absolute_import, print_function __author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)" __version__ = '1.0' __revision__ = "$Revision: 3473 $" -__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $' +__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $' import numpy as np from numpy.testing import * from numpy.ma.testutils import * from numpy.ma.core import * + class SubArray(np.ndarray): - """Defines a generic np.ndarray subclass, that stores some metadata - in the dictionary `info`.""" + # Defines a generic np.ndarray subclass, that stores some metadata + # in the dictionary `info`. def __new__(cls,arr,info={}): x = np.asanyarray(arr).view(cls) x.info = info return x + def __array_finalize__(self, obj): self.info = getattr(obj, 'info', {}) return + def __add__(self, other): result = np.ndarray.__add__(self, other) result.info.update({'added':result.info.pop('added', 0)+1}) @@ -35,16 +38,20 @@ class SubArray(np.ndarray): subarray = SubArray + class MSubArray(SubArray, MaskedArray): + def __new__(cls, data, info={}, mask=nomask): subarr = SubArray(data, info) _data = MaskedArray.__new__(cls, data=subarr, mask=mask) _data.info = subarr.info return _data + def __array_finalize__(self, obj): MaskedArray.__array_finalize__(self, obj) SubArray.__array_finalize__(self, obj) return + def _get_series(self): _view = self.view(MaskedArray) _view._sharedmask = False @@ -53,15 +60,19 @@ class MSubArray(SubArray, MaskedArray): msubarray = MSubArray + class MMatrix(MaskedArray, np.matrix,): + def __new__(cls, data, mask=nomask): mat = np.matrix(data) _data = MaskedArray.__new__(cls, data=mat, mask=mask) return _data + def __array_finalize__(self, obj): np.matrix.__array_finalize__(self, obj) MaskedArray.__array_finalize__(self, obj) return + def _get_series(self): _view = self.view(MaskedArray) _view._sharedmask = False @@ -70,8 +81,9 @@ class MMatrix(MaskedArray, np.matrix,): mmatrix = MMatrix + class TestSubclassing(TestCase): - """Test suite for masked subclasses of ndarray.""" + # Test suite for masked subclasses of ndarray. def setUp(self): x = np.arange(5) @@ -79,7 +91,7 @@ class TestSubclassing(TestCase): self.data = (x, mx) def test_data_subclassing(self): - "Tests whether the subclass is kept." + # Tests whether the subclass is kept. x = np.arange(5) m = [0, 0, 1, 0, 0] xsub = SubArray(x) @@ -89,19 +101,19 @@ class TestSubclassing(TestCase): self.assertTrue(isinstance(xmsub._data, SubArray)) def test_maskedarray_subclassing(self): - "Tests subclassing MaskedArray" + # Tests subclassing MaskedArray (x, mx) = self.data self.assertTrue(isinstance(mx._data, np.matrix)) def test_masked_unary_operations(self): - "Tests masked_unary_operation" + # Tests masked_unary_operation (x, mx) = self.data with np.errstate(divide='ignore'): self.assertTrue(isinstance(log(mx), mmatrix)) assert_equal(log(x), np.log(x)) def test_masked_binary_operations(self): - "Tests masked_binary_operation" + # Tests masked_binary_operation (x, mx) = self.data # Result should be a mmatrix self.assertTrue(isinstance(add(mx, mx), mmatrix)) @@ -114,7 +126,7 @@ class TestSubclassing(TestCase): self.assertTrue(isinstance(hypot(mx, x), mmatrix)) def test_masked_binary_operations2(self): - "Tests domained_masked_binary_operation" + # Tests domained_masked_binary_operation (x, mx) = self.data xmx = masked_array(mx.data.__array__(), mask=mx.mask) self.assertTrue(isinstance(divide(mx, mx), mmatrix)) @@ -149,7 +161,7 @@ class TestSubclassing(TestCase): assert_equal(mxsub.info, xsub.info) def test_subclasspreservation(self): - "Checks that masked_array(...,subok=True) preserves the class." + # Checks that masked_array(...,subok=True) preserves the class. x = np.arange(5) m = [0, 0, 1, 0, 0] xinfo = [(i, j) for (i, j) in zip(x, m)] @@ -176,6 +188,6 @@ class TestSubclassing(TestCase): assert_equal(mxsub._mask, m) -################################################################################ +############################################################################### if __name__ == '__main__': run_module_suite() |