summaryrefslogtreecommitdiff
path: root/numpy/ma/tests/test_subclassing.py
blob: 146ea305197c2fbd579d293a5137aa8106039604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# pylint: disable-msg=W0611, W0612, W0511,R0201
"""Tests suite for MaskedArray & subclassing.

:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
:version: $Id: test_subclassing.py 3473 2007-10-29 15:18:13Z jarrod.millman $
"""
__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) $'

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`."""
    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})
        return result

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
        return _view
    _series = property(fget=_get_series)

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
        return _view
    _series = property(fget=_get_series)

mmatrix = MMatrix

class TestSubclassing(TestCase):
    """Test suite for masked subclasses of ndarray."""

    def setUp(self):
        x = np.arange(5)
        mx = mmatrix(x, mask=[0, 1, 0, 0, 0])
        self.data = (x, mx)

    def test_data_subclassing(self):
        "Tests whether the subclass is kept."
        x = np.arange(5)
        m = [0,0,1,0,0]
        xsub = SubArray(x)
        xmsub = masked_array(xsub, mask=m)
        self.assertTrue(isinstance(xmsub, MaskedArray))
        assert_equal(xmsub._data, xsub)
        self.assertTrue(isinstance(xmsub._data, SubArray))

    def test_maskedarray_subclassing(self):
        "Tests subclassing MaskedArray"
        (x, mx) = self.data
        self.assertTrue(isinstance(mx._data, np.matrix))

    def test_masked_unary_operations(self):
        "Tests masked_unary_operation"
        (x, mx) = self.data
        self.assertTrue(isinstance(log(mx), mmatrix))
        assert_equal(log(x), np.log(x))

    def test_masked_binary_operations(self):
        "Tests masked_binary_operation"
        (x, mx) = self.data
        # Result should be a mmatrix
        self.assertTrue(isinstance(add(mx,mx), mmatrix))
        self.assertTrue(isinstance(add(mx,x), mmatrix))
        # Result should work
        assert_equal(add(mx,x), mx+x)
        self.assertTrue(isinstance(add(mx,mx)._data, np.matrix))
        self.assertTrue(isinstance(add.outer(mx,mx), mmatrix))
        self.assertTrue(isinstance(hypot(mx,mx), mmatrix))
        self.assertTrue(isinstance(hypot(mx,x), mmatrix))

    def test_masked_binary_operations(self):
        "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))
        self.assertTrue(isinstance(divide(mx,x), mmatrix))
        assert_equal(divide(mx, mx), divide(xmx, xmx))

    def test_attributepropagation(self):
        x = array(arange(5), mask=[0]+[1]*4)
        my = masked_array(subarray(x))
        ym = msubarray(x)
        #
        z = (my+1)
        self.assertTrue(isinstance(z,MaskedArray))
        self.assertTrue(not isinstance(z, MSubArray))
        self.assertTrue(isinstance(z._data, SubArray))
        assert_equal(z._data.info, {})
        #
        z = (ym+1)
        self.assertTrue(isinstance(z, MaskedArray))
        self.assertTrue(isinstance(z, MSubArray))
        self.assertTrue(isinstance(z._data, SubArray))
        self.assertTrue(z._data.info['added'] > 0)
        #
        ym._set_mask([1,0,0,0,1])
        assert_equal(ym._mask, [1,0,0,0,1])
        ym._series._set_mask([0,0,0,0,1])
        assert_equal(ym._mask, [0,0,0,0,1])
        #
        xsub = subarray(x, info={'name':'x'})
        mxsub = masked_array(xsub)
        self.assertTrue(hasattr(mxsub, 'info'))
        assert_equal(mxsub.info, xsub.info)

    def test_subclasspreservation(self):
        "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)]
        xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
        #
        mxsub = masked_array(xsub, subok=False)
        self.assertTrue(not isinstance(mxsub, MSubArray))
        self.assertTrue(isinstance(mxsub, MaskedArray))
        assert_equal(mxsub._mask, m)
        #
        mxsub = asarray(xsub)
        self.assertTrue(not isinstance(mxsub, MSubArray))
        self.assertTrue(isinstance(mxsub, MaskedArray))
        assert_equal(mxsub._mask, m)
        #
        mxsub = masked_array(xsub, subok=True)
        self.assertTrue(isinstance(mxsub, MSubArray))
        assert_equal(mxsub.info, xsub.info)
        assert_equal(mxsub._mask, xsub._mask)
        #
        mxsub = asanyarray(xsub)
        self.assertTrue(isinstance(mxsub, MSubArray))
        assert_equal(mxsub.info, xsub.info)
        assert_equal(mxsub._mask, m)


################################################################################
if __name__ == '__main__':
    run_module_suite()