Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/ma/tests/test_subclassing.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 # pylint: disable-msg=W0611, W0612, W0511,R0201 | |
2 """Tests suite for MaskedArray & subclassing. | |
3 | |
4 :author: Pierre Gerard-Marchant | |
5 :contact: pierregm_at_uga_dot_edu | |
6 :version: $Id: test_subclassing.py 3473 2007-10-29 15:18:13Z jarrod.millman $ | |
7 | |
8 """ | |
9 from __future__ import division, absolute_import, print_function | |
10 | |
11 __author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)" | |
12 __version__ = '1.0' | |
13 __revision__ = "$Revision: 3473 $" | |
14 __date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $' | |
15 | |
16 import numpy as np | |
17 from numpy.testing import * | |
18 from numpy.ma.testutils import * | |
19 from numpy.ma.core import * | |
20 | |
21 | |
22 class SubArray(np.ndarray): | |
23 # Defines a generic np.ndarray subclass, that stores some metadata | |
24 # in the dictionary `info`. | |
25 def __new__(cls,arr,info={}): | |
26 x = np.asanyarray(arr).view(cls) | |
27 x.info = info | |
28 return x | |
29 | |
30 def __array_finalize__(self, obj): | |
31 self.info = getattr(obj, 'info', {}) | |
32 return | |
33 | |
34 def __add__(self, other): | |
35 result = np.ndarray.__add__(self, other) | |
36 result.info.update({'added':result.info.pop('added', 0)+1}) | |
37 return result | |
38 | |
39 subarray = SubArray | |
40 | |
41 | |
42 class MSubArray(SubArray, MaskedArray): | |
43 | |
44 def __new__(cls, data, info={}, mask=nomask): | |
45 subarr = SubArray(data, info) | |
46 _data = MaskedArray.__new__(cls, data=subarr, mask=mask) | |
47 _data.info = subarr.info | |
48 return _data | |
49 | |
50 def __array_finalize__(self, obj): | |
51 MaskedArray.__array_finalize__(self, obj) | |
52 SubArray.__array_finalize__(self, obj) | |
53 return | |
54 | |
55 def _get_series(self): | |
56 _view = self.view(MaskedArray) | |
57 _view._sharedmask = False | |
58 return _view | |
59 _series = property(fget=_get_series) | |
60 | |
61 msubarray = MSubArray | |
62 | |
63 | |
64 class MMatrix(MaskedArray, np.matrix,): | |
65 | |
66 def __new__(cls, data, mask=nomask): | |
67 mat = np.matrix(data) | |
68 _data = MaskedArray.__new__(cls, data=mat, mask=mask) | |
69 return _data | |
70 | |
71 def __array_finalize__(self, obj): | |
72 np.matrix.__array_finalize__(self, obj) | |
73 MaskedArray.__array_finalize__(self, obj) | |
74 return | |
75 | |
76 def _get_series(self): | |
77 _view = self.view(MaskedArray) | |
78 _view._sharedmask = False | |
79 return _view | |
80 _series = property(fget=_get_series) | |
81 | |
82 mmatrix = MMatrix | |
83 | |
84 | |
85 # also a subclass that overrides __str__, __repr__ and __setitem__, disallowing | |
86 # setting to non-class values (and thus np.ma.core.masked_print_option) | |
87 class ComplicatedSubArray(SubArray): | |
88 def __str__(self): | |
89 return 'myprefix {0} mypostfix'.format( | |
90 super(ComplicatedSubArray, self).__str__()) | |
91 | |
92 def __repr__(self): | |
93 # Return a repr that does not start with 'name(' | |
94 return '<{0} {1}>'.format(self.__class__.__name__, self) | |
95 | |
96 def __setitem__(self, item, value): | |
97 # this ensures direct assignment to masked_print_option will fail | |
98 if not isinstance(value, ComplicatedSubArray): | |
99 raise ValueError("Can only set to MySubArray values") | |
100 super(ComplicatedSubArray, self).__setitem__(item, value) | |
101 | |
102 | |
103 class TestSubclassing(TestCase): | |
104 # Test suite for masked subclasses of ndarray. | |
105 | |
106 def setUp(self): | |
107 x = np.arange(5) | |
108 mx = mmatrix(x, mask=[0, 1, 0, 0, 0]) | |
109 self.data = (x, mx) | |
110 | |
111 def test_data_subclassing(self): | |
112 # Tests whether the subclass is kept. | |
113 x = np.arange(5) | |
114 m = [0, 0, 1, 0, 0] | |
115 xsub = SubArray(x) | |
116 xmsub = masked_array(xsub, mask=m) | |
117 self.assertTrue(isinstance(xmsub, MaskedArray)) | |
118 assert_equal(xmsub._data, xsub) | |
119 self.assertTrue(isinstance(xmsub._data, SubArray)) | |
120 | |
121 def test_maskedarray_subclassing(self): | |
122 # Tests subclassing MaskedArray | |
123 (x, mx) = self.data | |
124 self.assertTrue(isinstance(mx._data, np.matrix)) | |
125 | |
126 def test_masked_unary_operations(self): | |
127 # Tests masked_unary_operation | |
128 (x, mx) = self.data | |
129 with np.errstate(divide='ignore'): | |
130 self.assertTrue(isinstance(log(mx), mmatrix)) | |
131 assert_equal(log(x), np.log(x)) | |
132 | |
133 def test_masked_binary_operations(self): | |
134 # Tests masked_binary_operation | |
135 (x, mx) = self.data | |
136 # Result should be a mmatrix | |
137 self.assertTrue(isinstance(add(mx, mx), mmatrix)) | |
138 self.assertTrue(isinstance(add(mx, x), mmatrix)) | |
139 # Result should work | |
140 assert_equal(add(mx, x), mx+x) | |
141 self.assertTrue(isinstance(add(mx, mx)._data, np.matrix)) | |
142 self.assertTrue(isinstance(add.outer(mx, mx), mmatrix)) | |
143 self.assertTrue(isinstance(hypot(mx, mx), mmatrix)) | |
144 self.assertTrue(isinstance(hypot(mx, x), mmatrix)) | |
145 | |
146 def test_masked_binary_operations2(self): | |
147 # Tests domained_masked_binary_operation | |
148 (x, mx) = self.data | |
149 xmx = masked_array(mx.data.__array__(), mask=mx.mask) | |
150 self.assertTrue(isinstance(divide(mx, mx), mmatrix)) | |
151 self.assertTrue(isinstance(divide(mx, x), mmatrix)) | |
152 assert_equal(divide(mx, mx), divide(xmx, xmx)) | |
153 | |
154 def test_attributepropagation(self): | |
155 x = array(arange(5), mask=[0]+[1]*4) | |
156 my = masked_array(subarray(x)) | |
157 ym = msubarray(x) | |
158 # | |
159 z = (my+1) | |
160 self.assertTrue(isinstance(z, MaskedArray)) | |
161 self.assertTrue(not isinstance(z, MSubArray)) | |
162 self.assertTrue(isinstance(z._data, SubArray)) | |
163 assert_equal(z._data.info, {}) | |
164 # | |
165 z = (ym+1) | |
166 self.assertTrue(isinstance(z, MaskedArray)) | |
167 self.assertTrue(isinstance(z, MSubArray)) | |
168 self.assertTrue(isinstance(z._data, SubArray)) | |
169 self.assertTrue(z._data.info['added'] > 0) | |
170 # | |
171 ym._set_mask([1, 0, 0, 0, 1]) | |
172 assert_equal(ym._mask, [1, 0, 0, 0, 1]) | |
173 ym._series._set_mask([0, 0, 0, 0, 1]) | |
174 assert_equal(ym._mask, [0, 0, 0, 0, 1]) | |
175 # | |
176 xsub = subarray(x, info={'name':'x'}) | |
177 mxsub = masked_array(xsub) | |
178 self.assertTrue(hasattr(mxsub, 'info')) | |
179 assert_equal(mxsub.info, xsub.info) | |
180 | |
181 def test_subclasspreservation(self): | |
182 # Checks that masked_array(...,subok=True) preserves the class. | |
183 x = np.arange(5) | |
184 m = [0, 0, 1, 0, 0] | |
185 xinfo = [(i, j) for (i, j) in zip(x, m)] | |
186 xsub = MSubArray(x, mask=m, info={'xsub':xinfo}) | |
187 # | |
188 mxsub = masked_array(xsub, subok=False) | |
189 self.assertTrue(not isinstance(mxsub, MSubArray)) | |
190 self.assertTrue(isinstance(mxsub, MaskedArray)) | |
191 assert_equal(mxsub._mask, m) | |
192 # | |
193 mxsub = asarray(xsub) | |
194 self.assertTrue(not isinstance(mxsub, MSubArray)) | |
195 self.assertTrue(isinstance(mxsub, MaskedArray)) | |
196 assert_equal(mxsub._mask, m) | |
197 # | |
198 mxsub = masked_array(xsub, subok=True) | |
199 self.assertTrue(isinstance(mxsub, MSubArray)) | |
200 assert_equal(mxsub.info, xsub.info) | |
201 assert_equal(mxsub._mask, xsub._mask) | |
202 # | |
203 mxsub = asanyarray(xsub) | |
204 self.assertTrue(isinstance(mxsub, MSubArray)) | |
205 assert_equal(mxsub.info, xsub.info) | |
206 assert_equal(mxsub._mask, m) | |
207 | |
208 def test_subclass_repr(self): | |
209 """test that repr uses the name of the subclass | |
210 and 'array' for np.ndarray""" | |
211 x = np.arange(5) | |
212 mx = masked_array(x, mask=[True, False, True, False, False]) | |
213 self.assertTrue(repr(mx).startswith('masked_array')) | |
214 xsub = SubArray(x) | |
215 mxsub = masked_array(xsub, mask=[True, False, True, False, False]) | |
216 self.assertTrue(repr(mxsub).startswith( | |
217 'masked_{0}(data = [-- 1 -- 3 4]'.format(SubArray.__name__))) | |
218 | |
219 def test_subclass_str(self): | |
220 """test str with subclass that has overridden str, setitem""" | |
221 # first without override | |
222 x = np.arange(5) | |
223 xsub = SubArray(x) | |
224 mxsub = masked_array(xsub, mask=[True, False, True, False, False]) | |
225 self.assertTrue(str(mxsub) == '[-- 1 -- 3 4]') | |
226 | |
227 xcsub = ComplicatedSubArray(x) | |
228 assert_raises(ValueError, xcsub.__setitem__, 0, | |
229 np.ma.core.masked_print_option) | |
230 mxcsub = masked_array(xcsub, mask=[True, False, True, False, False]) | |
231 self.assertTrue(str(mxcsub) == 'myprefix [-- 1 -- 3 4] mypostfix') | |
232 | |
233 | |
234 ############################################################################### | |
235 if __name__ == '__main__': | |
236 run_module_suite() |