Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/ma/tests/test_extras.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 | |
2 """Tests suite for MaskedArray. | |
3 Adapted from the original test_ma by Pierre Gerard-Marchant | |
4 | |
5 :author: Pierre Gerard-Marchant | |
6 :contact: pierregm_at_uga_dot_edu | |
7 :version: $Id: test_extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $ | |
8 | |
9 """ | |
10 from __future__ import division, absolute_import, print_function | |
11 | |
12 __author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)" | |
13 __version__ = '1.0' | |
14 __revision__ = "$Revision: 3473 $" | |
15 __date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $' | |
16 | |
17 import numpy as np | |
18 from numpy.testing import TestCase, run_module_suite | |
19 from numpy.ma.testutils import (rand, assert_, assert_array_equal, | |
20 assert_equal, assert_almost_equal) | |
21 from numpy.ma.core import (array, arange, masked, MaskedArray, masked_array, | |
22 getmaskarray, shape, nomask, ones, zeros, count) | |
23 from numpy.ma.extras import ( | |
24 atleast_2d, mr_, dot, polyfit, | |
25 cov, corrcoef, median, average, | |
26 unique, setxor1d, setdiff1d, union1d, intersect1d, in1d, ediff1d, | |
27 apply_over_axes, apply_along_axis, | |
28 compress_rowcols, mask_rowcols, | |
29 clump_masked, clump_unmasked, | |
30 flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges, | |
31 masked_all, masked_all_like) | |
32 | |
33 | |
34 class TestGeneric(TestCase): | |
35 # | |
36 def test_masked_all(self): | |
37 # Tests masked_all | |
38 # Standard dtype | |
39 test = masked_all((2,), dtype=float) | |
40 control = array([1, 1], mask=[1, 1], dtype=float) | |
41 assert_equal(test, control) | |
42 # Flexible dtype | |
43 dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']}) | |
44 test = masked_all((2,), dtype=dt) | |
45 control = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt) | |
46 assert_equal(test, control) | |
47 test = masked_all((2, 2), dtype=dt) | |
48 control = array([[(0, 0), (0, 0)], [(0, 0), (0, 0)]], | |
49 mask=[[(1, 1), (1, 1)], [(1, 1), (1, 1)]], | |
50 dtype=dt) | |
51 assert_equal(test, control) | |
52 # Nested dtype | |
53 dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])]) | |
54 test = masked_all((2,), dtype=dt) | |
55 control = array([(1, (1, 1)), (1, (1, 1))], | |
56 mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt) | |
57 assert_equal(test, control) | |
58 test = masked_all((2,), dtype=dt) | |
59 control = array([(1, (1, 1)), (1, (1, 1))], | |
60 mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt) | |
61 assert_equal(test, control) | |
62 test = masked_all((1, 1), dtype=dt) | |
63 control = array([[(1, (1, 1))]], mask=[[(1, (1, 1))]], dtype=dt) | |
64 assert_equal(test, control) | |
65 | |
66 def test_masked_all_like(self): | |
67 # Tests masked_all | |
68 # Standard dtype | |
69 base = array([1, 2], dtype=float) | |
70 test = masked_all_like(base) | |
71 control = array([1, 1], mask=[1, 1], dtype=float) | |
72 assert_equal(test, control) | |
73 # Flexible dtype | |
74 dt = np.dtype({'names': ['a', 'b'], 'formats': ['f', 'f']}) | |
75 base = array([(0, 0), (0, 0)], mask=[(1, 1), (1, 1)], dtype=dt) | |
76 test = masked_all_like(base) | |
77 control = array([(10, 10), (10, 10)], mask=[(1, 1), (1, 1)], dtype=dt) | |
78 assert_equal(test, control) | |
79 # Nested dtype | |
80 dt = np.dtype([('a', 'f'), ('b', [('ba', 'f'), ('bb', 'f')])]) | |
81 control = array([(1, (1, 1)), (1, (1, 1))], | |
82 mask=[(1, (1, 1)), (1, (1, 1))], dtype=dt) | |
83 test = masked_all_like(control) | |
84 assert_equal(test, control) | |
85 | |
86 def test_clump_masked(self): | |
87 # Test clump_masked | |
88 a = masked_array(np.arange(10)) | |
89 a[[0, 1, 2, 6, 8, 9]] = masked | |
90 # | |
91 test = clump_masked(a) | |
92 control = [slice(0, 3), slice(6, 7), slice(8, 10)] | |
93 assert_equal(test, control) | |
94 | |
95 def test_clump_unmasked(self): | |
96 # Test clump_unmasked | |
97 a = masked_array(np.arange(10)) | |
98 a[[0, 1, 2, 6, 8, 9]] = masked | |
99 test = clump_unmasked(a) | |
100 control = [slice(3, 6), slice(7, 8), ] | |
101 assert_equal(test, control) | |
102 | |
103 def test_flatnotmasked_contiguous(self): | |
104 # Test flatnotmasked_contiguous | |
105 a = arange(10) | |
106 # No mask | |
107 test = flatnotmasked_contiguous(a) | |
108 assert_equal(test, slice(0, a.size)) | |
109 # Some mask | |
110 a[(a < 3) | (a > 8) | (a == 5)] = masked | |
111 test = flatnotmasked_contiguous(a) | |
112 assert_equal(test, [slice(3, 5), slice(6, 9)]) | |
113 # | |
114 a[:] = masked | |
115 test = flatnotmasked_contiguous(a) | |
116 assert_equal(test, None) | |
117 | |
118 | |
119 class TestAverage(TestCase): | |
120 # Several tests of average. Why so many ? Good point... | |
121 def test_testAverage1(self): | |
122 # Test of average. | |
123 ott = array([0., 1., 2., 3.], mask=[True, False, False, False]) | |
124 assert_equal(2.0, average(ott, axis=0)) | |
125 assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.])) | |
126 result, wts = average(ott, weights=[1., 1., 2., 1.], returned=1) | |
127 assert_equal(2.0, result) | |
128 self.assertTrue(wts == 4.0) | |
129 ott[:] = masked | |
130 assert_equal(average(ott, axis=0).mask, [True]) | |
131 ott = array([0., 1., 2., 3.], mask=[True, False, False, False]) | |
132 ott = ott.reshape(2, 2) | |
133 ott[:, 1] = masked | |
134 assert_equal(average(ott, axis=0), [2.0, 0.0]) | |
135 assert_equal(average(ott, axis=1).mask[0], [True]) | |
136 assert_equal([2., 0.], average(ott, axis=0)) | |
137 result, wts = average(ott, axis=0, returned=1) | |
138 assert_equal(wts, [1., 0.]) | |
139 | |
140 def test_testAverage2(self): | |
141 # More tests of average. | |
142 w1 = [0, 1, 1, 1, 1, 0] | |
143 w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]] | |
144 x = arange(6, dtype=np.float_) | |
145 assert_equal(average(x, axis=0), 2.5) | |
146 assert_equal(average(x, axis=0, weights=w1), 2.5) | |
147 y = array([arange(6, dtype=np.float_), 2.0 * arange(6)]) | |
148 assert_equal(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.) | |
149 assert_equal(average(y, axis=0), np.arange(6) * 3. / 2.) | |
150 assert_equal(average(y, axis=1), | |
151 [average(x, axis=0), average(x, axis=0) * 2.0]) | |
152 assert_equal(average(y, None, weights=w2), 20. / 6.) | |
153 assert_equal(average(y, axis=0, weights=w2), | |
154 [0., 1., 2., 3., 4., 10.]) | |
155 assert_equal(average(y, axis=1), | |
156 [average(x, axis=0), average(x, axis=0) * 2.0]) | |
157 m1 = zeros(6) | |
158 m2 = [0, 0, 1, 1, 0, 0] | |
159 m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]] | |
160 m4 = ones(6) | |
161 m5 = [0, 1, 1, 1, 1, 1] | |
162 assert_equal(average(masked_array(x, m1), axis=0), 2.5) | |
163 assert_equal(average(masked_array(x, m2), axis=0), 2.5) | |
164 assert_equal(average(masked_array(x, m4), axis=0).mask, [True]) | |
165 assert_equal(average(masked_array(x, m5), axis=0), 0.0) | |
166 assert_equal(count(average(masked_array(x, m4), axis=0)), 0) | |
167 z = masked_array(y, m3) | |
168 assert_equal(average(z, None), 20. / 6.) | |
169 assert_equal(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5]) | |
170 assert_equal(average(z, axis=1), [2.5, 5.0]) | |
171 assert_equal(average(z, axis=0, weights=w2), | |
172 [0., 1., 99., 99., 4.0, 10.0]) | |
173 | |
174 def test_testAverage3(self): | |
175 # Yet more tests of average! | |
176 a = arange(6) | |
177 b = arange(6) * 3 | |
178 r1, w1 = average([[a, b], [b, a]], axis=1, returned=1) | |
179 assert_equal(shape(r1), shape(w1)) | |
180 assert_equal(r1.shape, w1.shape) | |
181 r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1) | |
182 assert_equal(shape(w2), shape(r2)) | |
183 r2, w2 = average(ones((2, 2, 3)), returned=1) | |
184 assert_equal(shape(w2), shape(r2)) | |
185 r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1) | |
186 assert_equal(shape(w2), shape(r2)) | |
187 a2d = array([[1, 2], [0, 4]], float) | |
188 a2dm = masked_array(a2d, [[False, False], [True, False]]) | |
189 a2da = average(a2d, axis=0) | |
190 assert_equal(a2da, [0.5, 3.0]) | |
191 a2dma = average(a2dm, axis=0) | |
192 assert_equal(a2dma, [1.0, 3.0]) | |
193 a2dma = average(a2dm, axis=None) | |
194 assert_equal(a2dma, 7. / 3.) | |
195 a2dma = average(a2dm, axis=1) | |
196 assert_equal(a2dma, [1.5, 4.0]) | |
197 | |
198 def test_onintegers_with_mask(self): | |
199 # Test average on integers with mask | |
200 a = average(array([1, 2])) | |
201 assert_equal(a, 1.5) | |
202 a = average(array([1, 2, 3, 4], mask=[False, False, True, True])) | |
203 assert_equal(a, 1.5) | |
204 | |
205 def test_complex(self): | |
206 # Test with complex data. | |
207 # (Regression test for https://github.com/numpy/numpy/issues/2684) | |
208 mask = np.array([[0, 0, 0, 1, 0], | |
209 [0, 1, 0, 0, 0]], dtype=bool) | |
210 a = masked_array([[0, 1+2j, 3+4j, 5+6j, 7+8j], | |
211 [9j, 0+1j, 2+3j, 4+5j, 7+7j]], | |
212 mask=mask) | |
213 | |
214 av = average(a) | |
215 expected = np.average(a.compressed()) | |
216 assert_almost_equal(av.real, expected.real) | |
217 assert_almost_equal(av.imag, expected.imag) | |
218 | |
219 av0 = average(a, axis=0) | |
220 expected0 = average(a.real, axis=0) + average(a.imag, axis=0)*1j | |
221 assert_almost_equal(av0.real, expected0.real) | |
222 assert_almost_equal(av0.imag, expected0.imag) | |
223 | |
224 av1 = average(a, axis=1) | |
225 expected1 = average(a.real, axis=1) + average(a.imag, axis=1)*1j | |
226 assert_almost_equal(av1.real, expected1.real) | |
227 assert_almost_equal(av1.imag, expected1.imag) | |
228 | |
229 # Test with the 'weights' argument. | |
230 wts = np.array([[0.5, 1.0, 2.0, 1.0, 0.5], | |
231 [1.0, 1.0, 1.0, 1.0, 1.0]]) | |
232 wav = average(a, weights=wts) | |
233 expected = np.average(a.compressed(), weights=wts[~mask]) | |
234 assert_almost_equal(wav.real, expected.real) | |
235 assert_almost_equal(wav.imag, expected.imag) | |
236 | |
237 wav0 = average(a, weights=wts, axis=0) | |
238 expected0 = (average(a.real, weights=wts, axis=0) + | |
239 average(a.imag, weights=wts, axis=0)*1j) | |
240 assert_almost_equal(wav0.real, expected0.real) | |
241 assert_almost_equal(wav0.imag, expected0.imag) | |
242 | |
243 wav1 = average(a, weights=wts, axis=1) | |
244 expected1 = (average(a.real, weights=wts, axis=1) + | |
245 average(a.imag, weights=wts, axis=1)*1j) | |
246 assert_almost_equal(wav1.real, expected1.real) | |
247 assert_almost_equal(wav1.imag, expected1.imag) | |
248 | |
249 | |
250 class TestConcatenator(TestCase): | |
251 # Tests for mr_, the equivalent of r_ for masked arrays. | |
252 | |
253 def test_1d(self): | |
254 # Tests mr_ on 1D arrays. | |
255 assert_array_equal(mr_[1, 2, 3, 4, 5, 6], array([1, 2, 3, 4, 5, 6])) | |
256 b = ones(5) | |
257 m = [1, 0, 0, 0, 0] | |
258 d = masked_array(b, mask=m) | |
259 c = mr_[d, 0, 0, d] | |
260 self.assertTrue(isinstance(c, MaskedArray)) | |
261 assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1]) | |
262 assert_array_equal(c.mask, mr_[m, 0, 0, m]) | |
263 | |
264 def test_2d(self): | |
265 # Tests mr_ on 2D arrays. | |
266 a_1 = rand(5, 5) | |
267 a_2 = rand(5, 5) | |
268 m_1 = np.round_(rand(5, 5), 0) | |
269 m_2 = np.round_(rand(5, 5), 0) | |
270 b_1 = masked_array(a_1, mask=m_1) | |
271 b_2 = masked_array(a_2, mask=m_2) | |
272 # append columns | |
273 d = mr_['1', b_1, b_2] | |
274 self.assertTrue(d.shape == (5, 10)) | |
275 assert_array_equal(d[:, :5], b_1) | |
276 assert_array_equal(d[:, 5:], b_2) | |
277 assert_array_equal(d.mask, np.r_['1', m_1, m_2]) | |
278 d = mr_[b_1, b_2] | |
279 self.assertTrue(d.shape == (10, 5)) | |
280 assert_array_equal(d[:5,:], b_1) | |
281 assert_array_equal(d[5:,:], b_2) | |
282 assert_array_equal(d.mask, np.r_[m_1, m_2]) | |
283 | |
284 | |
285 class TestNotMasked(TestCase): | |
286 # Tests notmasked_edges and notmasked_contiguous. | |
287 | |
288 def test_edges(self): | |
289 # Tests unmasked_edges | |
290 data = masked_array(np.arange(25).reshape(5, 5), | |
291 mask=[[0, 0, 1, 0, 0], | |
292 [0, 0, 0, 1, 1], | |
293 [1, 1, 0, 0, 0], | |
294 [0, 0, 0, 0, 0], | |
295 [1, 1, 1, 0, 0]],) | |
296 test = notmasked_edges(data, None) | |
297 assert_equal(test, [0, 24]) | |
298 test = notmasked_edges(data, 0) | |
299 assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)]) | |
300 assert_equal(test[1], [(3, 3, 3, 4, 4), (0, 1, 2, 3, 4)]) | |
301 test = notmasked_edges(data, 1) | |
302 assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 2, 0, 3)]) | |
303 assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 2, 4, 4, 4)]) | |
304 # | |
305 test = notmasked_edges(data.data, None) | |
306 assert_equal(test, [0, 24]) | |
307 test = notmasked_edges(data.data, 0) | |
308 assert_equal(test[0], [(0, 0, 0, 0, 0), (0, 1, 2, 3, 4)]) | |
309 assert_equal(test[1], [(4, 4, 4, 4, 4), (0, 1, 2, 3, 4)]) | |
310 test = notmasked_edges(data.data, -1) | |
311 assert_equal(test[0], [(0, 1, 2, 3, 4), (0, 0, 0, 0, 0)]) | |
312 assert_equal(test[1], [(0, 1, 2, 3, 4), (4, 4, 4, 4, 4)]) | |
313 # | |
314 data[-2] = masked | |
315 test = notmasked_edges(data, 0) | |
316 assert_equal(test[0], [(0, 0, 1, 0, 0), (0, 1, 2, 3, 4)]) | |
317 assert_equal(test[1], [(1, 1, 2, 4, 4), (0, 1, 2, 3, 4)]) | |
318 test = notmasked_edges(data, -1) | |
319 assert_equal(test[0], [(0, 1, 2, 4), (0, 0, 2, 3)]) | |
320 assert_equal(test[1], [(0, 1, 2, 4), (4, 2, 4, 4)]) | |
321 | |
322 def test_contiguous(self): | |
323 # Tests notmasked_contiguous | |
324 a = masked_array(np.arange(24).reshape(3, 8), | |
325 mask=[[0, 0, 0, 0, 1, 1, 1, 1], | |
326 [1, 1, 1, 1, 1, 1, 1, 1], | |
327 [0, 0, 0, 0, 0, 0, 1, 0], ]) | |
328 tmp = notmasked_contiguous(a, None) | |
329 assert_equal(tmp[-1], slice(23, 24, None)) | |
330 assert_equal(tmp[-2], slice(16, 22, None)) | |
331 assert_equal(tmp[-3], slice(0, 4, None)) | |
332 # | |
333 tmp = notmasked_contiguous(a, 0) | |
334 self.assertTrue(len(tmp[-1]) == 1) | |
335 self.assertTrue(tmp[-2] is None) | |
336 assert_equal(tmp[-3], tmp[-1]) | |
337 self.assertTrue(len(tmp[0]) == 2) | |
338 # | |
339 tmp = notmasked_contiguous(a, 1) | |
340 assert_equal(tmp[0][-1], slice(0, 4, None)) | |
341 self.assertTrue(tmp[1] is None) | |
342 assert_equal(tmp[2][-1], slice(7, 8, None)) | |
343 assert_equal(tmp[2][-2], slice(0, 6, None)) | |
344 | |
345 | |
346 class Test2DFunctions(TestCase): | |
347 # Tests 2D functions | |
348 def test_compress2d(self): | |
349 # Tests compress2d | |
350 x = array(np.arange(9).reshape(3, 3), | |
351 mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]]) | |
352 assert_equal(compress_rowcols(x), [[4, 5], [7, 8]]) | |
353 assert_equal(compress_rowcols(x, 0), [[3, 4, 5], [6, 7, 8]]) | |
354 assert_equal(compress_rowcols(x, 1), [[1, 2], [4, 5], [7, 8]]) | |
355 x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]]) | |
356 assert_equal(compress_rowcols(x), [[0, 2], [6, 8]]) | |
357 assert_equal(compress_rowcols(x, 0), [[0, 1, 2], [6, 7, 8]]) | |
358 assert_equal(compress_rowcols(x, 1), [[0, 2], [3, 5], [6, 8]]) | |
359 x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]]) | |
360 assert_equal(compress_rowcols(x), [[8]]) | |
361 assert_equal(compress_rowcols(x, 0), [[6, 7, 8]]) | |
362 assert_equal(compress_rowcols(x, 1,), [[2], [5], [8]]) | |
363 x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]) | |
364 assert_equal(compress_rowcols(x).size, 0) | |
365 assert_equal(compress_rowcols(x, 0).size, 0) | |
366 assert_equal(compress_rowcols(x, 1).size, 0) | |
367 | |
368 def test_mask_rowcols(self): | |
369 # Tests mask_rowcols. | |
370 x = array(np.arange(9).reshape(3, 3), | |
371 mask=[[1, 0, 0], [0, 0, 0], [0, 0, 0]]) | |
372 assert_equal(mask_rowcols(x).mask, | |
373 [[1, 1, 1], [1, 0, 0], [1, 0, 0]]) | |
374 assert_equal(mask_rowcols(x, 0).mask, | |
375 [[1, 1, 1], [0, 0, 0], [0, 0, 0]]) | |
376 assert_equal(mask_rowcols(x, 1).mask, | |
377 [[1, 0, 0], [1, 0, 0], [1, 0, 0]]) | |
378 x = array(x._data, mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]]) | |
379 assert_equal(mask_rowcols(x).mask, | |
380 [[0, 1, 0], [1, 1, 1], [0, 1, 0]]) | |
381 assert_equal(mask_rowcols(x, 0).mask, | |
382 [[0, 0, 0], [1, 1, 1], [0, 0, 0]]) | |
383 assert_equal(mask_rowcols(x, 1).mask, | |
384 [[0, 1, 0], [0, 1, 0], [0, 1, 0]]) | |
385 x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 0]]) | |
386 assert_equal(mask_rowcols(x).mask, | |
387 [[1, 1, 1], [1, 1, 1], [1, 1, 0]]) | |
388 assert_equal(mask_rowcols(x, 0).mask, | |
389 [[1, 1, 1], [1, 1, 1], [0, 0, 0]]) | |
390 assert_equal(mask_rowcols(x, 1,).mask, | |
391 [[1, 1, 0], [1, 1, 0], [1, 1, 0]]) | |
392 x = array(x._data, mask=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]) | |
393 self.assertTrue(mask_rowcols(x).all() is masked) | |
394 self.assertTrue(mask_rowcols(x, 0).all() is masked) | |
395 self.assertTrue(mask_rowcols(x, 1).all() is masked) | |
396 self.assertTrue(mask_rowcols(x).mask.all()) | |
397 self.assertTrue(mask_rowcols(x, 0).mask.all()) | |
398 self.assertTrue(mask_rowcols(x, 1).mask.all()) | |
399 | |
400 def test_dot(self): | |
401 # Tests dot product | |
402 n = np.arange(1, 7) | |
403 # | |
404 m = [1, 0, 0, 0, 0, 0] | |
405 a = masked_array(n, mask=m).reshape(2, 3) | |
406 b = masked_array(n, mask=m).reshape(3, 2) | |
407 c = dot(a, b, True) | |
408 assert_equal(c.mask, [[1, 1], [1, 0]]) | |
409 c = dot(b, a, True) | |
410 assert_equal(c.mask, [[1, 1, 1], [1, 0, 0], [1, 0, 0]]) | |
411 c = dot(a, b, False) | |
412 assert_equal(c, np.dot(a.filled(0), b.filled(0))) | |
413 c = dot(b, a, False) | |
414 assert_equal(c, np.dot(b.filled(0), a.filled(0))) | |
415 # | |
416 m = [0, 0, 0, 0, 0, 1] | |
417 a = masked_array(n, mask=m).reshape(2, 3) | |
418 b = masked_array(n, mask=m).reshape(3, 2) | |
419 c = dot(a, b, True) | |
420 assert_equal(c.mask, [[0, 1], [1, 1]]) | |
421 c = dot(b, a, True) | |
422 assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [1, 1, 1]]) | |
423 c = dot(a, b, False) | |
424 assert_equal(c, np.dot(a.filled(0), b.filled(0))) | |
425 assert_equal(c, dot(a, b)) | |
426 c = dot(b, a, False) | |
427 assert_equal(c, np.dot(b.filled(0), a.filled(0))) | |
428 # | |
429 m = [0, 0, 0, 0, 0, 0] | |
430 a = masked_array(n, mask=m).reshape(2, 3) | |
431 b = masked_array(n, mask=m).reshape(3, 2) | |
432 c = dot(a, b) | |
433 assert_equal(c.mask, nomask) | |
434 c = dot(b, a) | |
435 assert_equal(c.mask, nomask) | |
436 # | |
437 a = masked_array(n, mask=[1, 0, 0, 0, 0, 0]).reshape(2, 3) | |
438 b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2) | |
439 c = dot(a, b, True) | |
440 assert_equal(c.mask, [[1, 1], [0, 0]]) | |
441 c = dot(a, b, False) | |
442 assert_equal(c, np.dot(a.filled(0), b.filled(0))) | |
443 c = dot(b, a, True) | |
444 assert_equal(c.mask, [[1, 0, 0], [1, 0, 0], [1, 0, 0]]) | |
445 c = dot(b, a, False) | |
446 assert_equal(c, np.dot(b.filled(0), a.filled(0))) | |
447 # | |
448 a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3) | |
449 b = masked_array(n, mask=[0, 0, 0, 0, 0, 0]).reshape(3, 2) | |
450 c = dot(a, b, True) | |
451 assert_equal(c.mask, [[0, 0], [1, 1]]) | |
452 c = dot(a, b) | |
453 assert_equal(c, np.dot(a.filled(0), b.filled(0))) | |
454 c = dot(b, a, True) | |
455 assert_equal(c.mask, [[0, 0, 1], [0, 0, 1], [0, 0, 1]]) | |
456 c = dot(b, a, False) | |
457 assert_equal(c, np.dot(b.filled(0), a.filled(0))) | |
458 # | |
459 a = masked_array(n, mask=[0, 0, 0, 0, 0, 1]).reshape(2, 3) | |
460 b = masked_array(n, mask=[0, 0, 1, 0, 0, 0]).reshape(3, 2) | |
461 c = dot(a, b, True) | |
462 assert_equal(c.mask, [[1, 0], [1, 1]]) | |
463 c = dot(a, b, False) | |
464 assert_equal(c, np.dot(a.filled(0), b.filled(0))) | |
465 c = dot(b, a, True) | |
466 assert_equal(c.mask, [[0, 0, 1], [1, 1, 1], [0, 0, 1]]) | |
467 c = dot(b, a, False) | |
468 assert_equal(c, np.dot(b.filled(0), a.filled(0))) | |
469 | |
470 | |
471 class TestApplyAlongAxis(TestCase): | |
472 # Tests 2D functions | |
473 def test_3d(self): | |
474 a = arange(12.).reshape(2, 2, 3) | |
475 | |
476 def myfunc(b): | |
477 return b[1] | |
478 | |
479 xa = apply_along_axis(myfunc, 2, a) | |
480 assert_equal(xa, [[1, 4], [7, 10]]) | |
481 | |
482 # Tests kwargs functions | |
483 def test_3d_kwargs(self): | |
484 a = arange(12).reshape(2, 2, 3) | |
485 | |
486 def myfunc(b, offset=0): | |
487 return b[1+offset] | |
488 | |
489 xa = apply_along_axis(myfunc, 2, a, offset=1) | |
490 assert_equal(xa, [[2, 5], [8, 11]]) | |
491 | |
492 | |
493 class TestApplyOverAxes(TestCase): | |
494 # Tests apply_over_axes | |
495 def test_basic(self): | |
496 a = arange(24).reshape(2, 3, 4) | |
497 test = apply_over_axes(np.sum, a, [0, 2]) | |
498 ctrl = np.array([[[60], [92], [124]]]) | |
499 assert_equal(test, ctrl) | |
500 a[(a % 2).astype(np.bool)] = masked | |
501 test = apply_over_axes(np.sum, a, [0, 2]) | |
502 ctrl = np.array([[[28], [44], [60]]]) | |
503 assert_equal(test, ctrl) | |
504 | |
505 | |
506 class TestMedian(TestCase): | |
507 def test_pytype(self): | |
508 r = np.ma.median([[np.inf, np.inf], [np.inf, np.inf]], axis=-1) | |
509 assert_equal(r, np.inf) | |
510 | |
511 def test_2d(self): | |
512 # Tests median w/ 2D | |
513 (n, p) = (101, 30) | |
514 x = masked_array(np.linspace(-1., 1., n),) | |
515 x[:10] = x[-10:] = masked | |
516 z = masked_array(np.empty((n, p), dtype=float)) | |
517 z[:, 0] = x[:] | |
518 idx = np.arange(len(x)) | |
519 for i in range(1, p): | |
520 np.random.shuffle(idx) | |
521 z[:, i] = x[idx] | |
522 assert_equal(median(z[:, 0]), 0) | |
523 assert_equal(median(z), 0) | |
524 assert_equal(median(z, axis=0), np.zeros(p)) | |
525 assert_equal(median(z.T, axis=1), np.zeros(p)) | |
526 | |
527 def test_2d_waxis(self): | |
528 # Tests median w/ 2D arrays and different axis. | |
529 x = masked_array(np.arange(30).reshape(10, 3)) | |
530 x[:3] = x[-3:] = masked | |
531 assert_equal(median(x), 14.5) | |
532 assert_equal(median(x, axis=0), [13.5, 14.5, 15.5]) | |
533 assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0]) | |
534 assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1]) | |
535 | |
536 def test_3d(self): | |
537 # Tests median w/ 3D | |
538 x = np.ma.arange(24).reshape(3, 4, 2) | |
539 x[x % 3 == 0] = masked | |
540 assert_equal(median(x, 0), [[12, 9], [6, 15], [12, 9], [18, 15]]) | |
541 x.shape = (4, 3, 2) | |
542 assert_equal(median(x, 0), [[99, 10], [11, 99], [13, 14]]) | |
543 x = np.ma.arange(24).reshape(4, 3, 2) | |
544 x[x % 5 == 0] = masked | |
545 assert_equal(median(x, 0), [[12, 10], [8, 9], [16, 17]]) | |
546 | |
547 def test_neg_axis(self): | |
548 x = masked_array(np.arange(30).reshape(10, 3)) | |
549 x[:3] = x[-3:] = masked | |
550 assert_equal(median(x, axis=-1), median(x, axis=1)) | |
551 | |
552 def test_out(self): | |
553 x = masked_array(np.arange(30).reshape(10, 3)) | |
554 x[:3] = x[-3:] = masked | |
555 out = masked_array(np.ones(10)) | |
556 r = median(x, axis=1, out=out) | |
557 assert_equal(r, out) | |
558 assert_(type(r) == MaskedArray) | |
559 | |
560 | |
561 class TestCov(TestCase): | |
562 | |
563 def setUp(self): | |
564 self.data = array(np.random.rand(12)) | |
565 | |
566 def test_1d_wo_missing(self): | |
567 # Test cov on 1D variable w/o missing values | |
568 x = self.data | |
569 assert_almost_equal(np.cov(x), cov(x)) | |
570 assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False)) | |
571 assert_almost_equal(np.cov(x, rowvar=False, bias=True), | |
572 cov(x, rowvar=False, bias=True)) | |
573 | |
574 def test_2d_wo_missing(self): | |
575 # Test cov on 1 2D variable w/o missing values | |
576 x = self.data.reshape(3, 4) | |
577 assert_almost_equal(np.cov(x), cov(x)) | |
578 assert_almost_equal(np.cov(x, rowvar=False), cov(x, rowvar=False)) | |
579 assert_almost_equal(np.cov(x, rowvar=False, bias=True), | |
580 cov(x, rowvar=False, bias=True)) | |
581 | |
582 def test_1d_w_missing(self): | |
583 # Test cov 1 1D variable w/missing values | |
584 x = self.data | |
585 x[-1] = masked | |
586 x -= x.mean() | |
587 nx = x.compressed() | |
588 assert_almost_equal(np.cov(nx), cov(x)) | |
589 assert_almost_equal(np.cov(nx, rowvar=False), cov(x, rowvar=False)) | |
590 assert_almost_equal(np.cov(nx, rowvar=False, bias=True), | |
591 cov(x, rowvar=False, bias=True)) | |
592 # | |
593 try: | |
594 cov(x, allow_masked=False) | |
595 except ValueError: | |
596 pass | |
597 # | |
598 # 2 1D variables w/ missing values | |
599 nx = x[1:-1] | |
600 assert_almost_equal(np.cov(nx, nx[::-1]), cov(x, x[::-1])) | |
601 assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False), | |
602 cov(x, x[::-1], rowvar=False)) | |
603 assert_almost_equal(np.cov(nx, nx[::-1], rowvar=False, bias=True), | |
604 cov(x, x[::-1], rowvar=False, bias=True)) | |
605 | |
606 def test_2d_w_missing(self): | |
607 # Test cov on 2D variable w/ missing value | |
608 x = self.data | |
609 x[-1] = masked | |
610 x = x.reshape(3, 4) | |
611 valid = np.logical_not(getmaskarray(x)).astype(int) | |
612 frac = np.dot(valid, valid.T) | |
613 xf = (x - x.mean(1)[:, None]).filled(0) | |
614 assert_almost_equal(cov(x), | |
615 np.cov(xf) * (x.shape[1] - 1) / (frac - 1.)) | |
616 assert_almost_equal(cov(x, bias=True), | |
617 np.cov(xf, bias=True) * x.shape[1] / frac) | |
618 frac = np.dot(valid.T, valid) | |
619 xf = (x - x.mean(0)).filled(0) | |
620 assert_almost_equal(cov(x, rowvar=False), | |
621 (np.cov(xf, rowvar=False) * | |
622 (x.shape[0] - 1) / (frac - 1.))) | |
623 assert_almost_equal(cov(x, rowvar=False, bias=True), | |
624 (np.cov(xf, rowvar=False, bias=True) * | |
625 x.shape[0] / frac)) | |
626 | |
627 | |
628 class TestCorrcoef(TestCase): | |
629 | |
630 def setUp(self): | |
631 self.data = array(np.random.rand(12)) | |
632 | |
633 def test_ddof(self): | |
634 # Test ddof keyword | |
635 x = self.data | |
636 assert_almost_equal(np.corrcoef(x, ddof=0), corrcoef(x, ddof=0)) | |
637 | |
638 def test_1d_wo_missing(self): | |
639 # Test cov on 1D variable w/o missing values | |
640 x = self.data | |
641 assert_almost_equal(np.corrcoef(x), corrcoef(x)) | |
642 assert_almost_equal(np.corrcoef(x, rowvar=False), | |
643 corrcoef(x, rowvar=False)) | |
644 assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True), | |
645 corrcoef(x, rowvar=False, bias=True)) | |
646 | |
647 def test_2d_wo_missing(self): | |
648 # Test corrcoef on 1 2D variable w/o missing values | |
649 x = self.data.reshape(3, 4) | |
650 assert_almost_equal(np.corrcoef(x), corrcoef(x)) | |
651 assert_almost_equal(np.corrcoef(x, rowvar=False), | |
652 corrcoef(x, rowvar=False)) | |
653 assert_almost_equal(np.corrcoef(x, rowvar=False, bias=True), | |
654 corrcoef(x, rowvar=False, bias=True)) | |
655 | |
656 def test_1d_w_missing(self): | |
657 # Test corrcoef 1 1D variable w/missing values | |
658 x = self.data | |
659 x[-1] = masked | |
660 x -= x.mean() | |
661 nx = x.compressed() | |
662 assert_almost_equal(np.corrcoef(nx), corrcoef(x)) | |
663 assert_almost_equal(np.corrcoef(nx, rowvar=False), | |
664 corrcoef(x, rowvar=False)) | |
665 assert_almost_equal(np.corrcoef(nx, rowvar=False, bias=True), | |
666 corrcoef(x, rowvar=False, bias=True)) | |
667 # | |
668 try: | |
669 corrcoef(x, allow_masked=False) | |
670 except ValueError: | |
671 pass | |
672 # | |
673 # 2 1D variables w/ missing values | |
674 nx = x[1:-1] | |
675 assert_almost_equal(np.corrcoef(nx, nx[::-1]), corrcoef(x, x[::-1])) | |
676 assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False), | |
677 corrcoef(x, x[::-1], rowvar=False)) | |
678 assert_almost_equal(np.corrcoef(nx, nx[::-1], rowvar=False, bias=True), | |
679 corrcoef(x, x[::-1], rowvar=False, bias=True)) | |
680 | |
681 def test_2d_w_missing(self): | |
682 # Test corrcoef on 2D variable w/ missing value | |
683 x = self.data | |
684 x[-1] = masked | |
685 x = x.reshape(3, 4) | |
686 | |
687 test = corrcoef(x) | |
688 control = np.corrcoef(x) | |
689 assert_almost_equal(test[:-1, :-1], control[:-1, :-1]) | |
690 | |
691 | |
692 class TestPolynomial(TestCase): | |
693 # | |
694 def test_polyfit(self): | |
695 # Tests polyfit | |
696 # On ndarrays | |
697 x = np.random.rand(10) | |
698 y = np.random.rand(20).reshape(-1, 2) | |
699 assert_almost_equal(polyfit(x, y, 3), np.polyfit(x, y, 3)) | |
700 # ON 1D maskedarrays | |
701 x = x.view(MaskedArray) | |
702 x[0] = masked | |
703 y = y.view(MaskedArray) | |
704 y[0, 0] = y[-1, -1] = masked | |
705 # | |
706 (C, R, K, S, D) = polyfit(x, y[:, 0], 3, full=True) | |
707 (c, r, k, s, d) = np.polyfit(x[1:], y[1:, 0].compressed(), 3, | |
708 full=True) | |
709 for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)): | |
710 assert_almost_equal(a, a_) | |
711 # | |
712 (C, R, K, S, D) = polyfit(x, y[:, -1], 3, full=True) | |
713 (c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1, -1], 3, full=True) | |
714 for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)): | |
715 assert_almost_equal(a, a_) | |
716 # | |
717 (C, R, K, S, D) = polyfit(x, y, 3, full=True) | |
718 (c, r, k, s, d) = np.polyfit(x[1:-1], y[1:-1,:], 3, full=True) | |
719 for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)): | |
720 assert_almost_equal(a, a_) | |
721 # | |
722 w = np.random.rand(10) + 1 | |
723 wo = w.copy() | |
724 xs = x[1:-1] | |
725 ys = y[1:-1] | |
726 ws = w[1:-1] | |
727 (C, R, K, S, D) = polyfit(x, y, 3, full=True, w=w) | |
728 (c, r, k, s, d) = np.polyfit(xs, ys, 3, full=True, w=ws) | |
729 assert_equal(w, wo) | |
730 for (a, a_) in zip((C, R, K, S, D), (c, r, k, s, d)): | |
731 assert_almost_equal(a, a_) | |
732 | |
733 | |
734 class TestArraySetOps(TestCase): | |
735 | |
736 def test_unique_onlist(self): | |
737 # Test unique on list | |
738 data = [1, 1, 1, 2, 2, 3] | |
739 test = unique(data, return_index=True, return_inverse=True) | |
740 self.assertTrue(isinstance(test[0], MaskedArray)) | |
741 assert_equal(test[0], masked_array([1, 2, 3], mask=[0, 0, 0])) | |
742 assert_equal(test[1], [0, 3, 5]) | |
743 assert_equal(test[2], [0, 0, 0, 1, 1, 2]) | |
744 | |
745 def test_unique_onmaskedarray(self): | |
746 # Test unique on masked data w/use_mask=True | |
747 data = masked_array([1, 1, 1, 2, 2, 3], mask=[0, 0, 1, 0, 1, 0]) | |
748 test = unique(data, return_index=True, return_inverse=True) | |
749 assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1])) | |
750 assert_equal(test[1], [0, 3, 5, 2]) | |
751 assert_equal(test[2], [0, 0, 3, 1, 3, 2]) | |
752 # | |
753 data.fill_value = 3 | |
754 data = masked_array(data=[1, 1, 1, 2, 2, 3], | |
755 mask=[0, 0, 1, 0, 1, 0], fill_value=3) | |
756 test = unique(data, return_index=True, return_inverse=True) | |
757 assert_equal(test[0], masked_array([1, 2, 3, -1], mask=[0, 0, 0, 1])) | |
758 assert_equal(test[1], [0, 3, 5, 2]) | |
759 assert_equal(test[2], [0, 0, 3, 1, 3, 2]) | |
760 | |
761 def test_unique_allmasked(self): | |
762 # Test all masked | |
763 data = masked_array([1, 1, 1], mask=True) | |
764 test = unique(data, return_index=True, return_inverse=True) | |
765 assert_equal(test[0], masked_array([1, ], mask=[True])) | |
766 assert_equal(test[1], [0]) | |
767 assert_equal(test[2], [0, 0, 0]) | |
768 # | |
769 # Test masked | |
770 data = masked | |
771 test = unique(data, return_index=True, return_inverse=True) | |
772 assert_equal(test[0], masked_array(masked)) | |
773 assert_equal(test[1], [0]) | |
774 assert_equal(test[2], [0]) | |
775 | |
776 def test_ediff1d(self): | |
777 # Tests mediff1d | |
778 x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1]) | |
779 control = array([1, 1, 1, 4], mask=[1, 0, 0, 1]) | |
780 test = ediff1d(x) | |
781 assert_equal(test, control) | |
782 assert_equal(test.data, control.data) | |
783 assert_equal(test.mask, control.mask) | |
784 | |
785 def test_ediff1d_tobegin(self): | |
786 # Test ediff1d w/ to_begin | |
787 x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1]) | |
788 test = ediff1d(x, to_begin=masked) | |
789 control = array([0, 1, 1, 1, 4], mask=[1, 1, 0, 0, 1]) | |
790 assert_equal(test, control) | |
791 assert_equal(test.data, control.data) | |
792 assert_equal(test.mask, control.mask) | |
793 # | |
794 test = ediff1d(x, to_begin=[1, 2, 3]) | |
795 control = array([1, 2, 3, 1, 1, 1, 4], mask=[0, 0, 0, 1, 0, 0, 1]) | |
796 assert_equal(test, control) | |
797 assert_equal(test.data, control.data) | |
798 assert_equal(test.mask, control.mask) | |
799 | |
800 def test_ediff1d_toend(self): | |
801 # Test ediff1d w/ to_end | |
802 x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1]) | |
803 test = ediff1d(x, to_end=masked) | |
804 control = array([1, 1, 1, 4, 0], mask=[1, 0, 0, 1, 1]) | |
805 assert_equal(test, control) | |
806 assert_equal(test.data, control.data) | |
807 assert_equal(test.mask, control.mask) | |
808 # | |
809 test = ediff1d(x, to_end=[1, 2, 3]) | |
810 control = array([1, 1, 1, 4, 1, 2, 3], mask=[1, 0, 0, 1, 0, 0, 0]) | |
811 assert_equal(test, control) | |
812 assert_equal(test.data, control.data) | |
813 assert_equal(test.mask, control.mask) | |
814 | |
815 def test_ediff1d_tobegin_toend(self): | |
816 # Test ediff1d w/ to_begin and to_end | |
817 x = masked_array(np.arange(5), mask=[1, 0, 0, 0, 1]) | |
818 test = ediff1d(x, to_end=masked, to_begin=masked) | |
819 control = array([0, 1, 1, 1, 4, 0], mask=[1, 1, 0, 0, 1, 1]) | |
820 assert_equal(test, control) | |
821 assert_equal(test.data, control.data) | |
822 assert_equal(test.mask, control.mask) | |
823 # | |
824 test = ediff1d(x, to_end=[1, 2, 3], to_begin=masked) | |
825 control = array([0, 1, 1, 1, 4, 1, 2, 3], | |
826 mask=[1, 1, 0, 0, 1, 0, 0, 0]) | |
827 assert_equal(test, control) | |
828 assert_equal(test.data, control.data) | |
829 assert_equal(test.mask, control.mask) | |
830 | |
831 def test_ediff1d_ndarray(self): | |
832 # Test ediff1d w/ a ndarray | |
833 x = np.arange(5) | |
834 test = ediff1d(x) | |
835 control = array([1, 1, 1, 1], mask=[0, 0, 0, 0]) | |
836 assert_equal(test, control) | |
837 self.assertTrue(isinstance(test, MaskedArray)) | |
838 assert_equal(test.data, control.data) | |
839 assert_equal(test.mask, control.mask) | |
840 # | |
841 test = ediff1d(x, to_end=masked, to_begin=masked) | |
842 control = array([0, 1, 1, 1, 1, 0], mask=[1, 0, 0, 0, 0, 1]) | |
843 self.assertTrue(isinstance(test, MaskedArray)) | |
844 assert_equal(test.data, control.data) | |
845 assert_equal(test.mask, control.mask) | |
846 | |
847 def test_intersect1d(self): | |
848 # Test intersect1d | |
849 x = array([1, 3, 3, 3], mask=[0, 0, 0, 1]) | |
850 y = array([3, 1, 1, 1], mask=[0, 0, 0, 1]) | |
851 test = intersect1d(x, y) | |
852 control = array([1, 3, -1], mask=[0, 0, 1]) | |
853 assert_equal(test, control) | |
854 | |
855 def test_setxor1d(self): | |
856 # Test setxor1d | |
857 a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1]) | |
858 b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1]) | |
859 test = setxor1d(a, b) | |
860 assert_equal(test, array([3, 4, 7])) | |
861 # | |
862 a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1]) | |
863 b = [1, 2, 3, 4, 5] | |
864 test = setxor1d(a, b) | |
865 assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1])) | |
866 # | |
867 a = array([1, 2, 3]) | |
868 b = array([6, 5, 4]) | |
869 test = setxor1d(a, b) | |
870 assert_(isinstance(test, MaskedArray)) | |
871 assert_equal(test, [1, 2, 3, 4, 5, 6]) | |
872 # | |
873 a = array([1, 8, 2, 3], mask=[0, 1, 0, 0]) | |
874 b = array([6, 5, 4, 8], mask=[0, 0, 0, 1]) | |
875 test = setxor1d(a, b) | |
876 assert_(isinstance(test, MaskedArray)) | |
877 assert_equal(test, [1, 2, 3, 4, 5, 6]) | |
878 # | |
879 assert_array_equal([], setxor1d([], [])) | |
880 | |
881 def test_in1d(self): | |
882 # Test in1d | |
883 a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1]) | |
884 b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1]) | |
885 test = in1d(a, b) | |
886 assert_equal(test, [True, True, True, False, True]) | |
887 # | |
888 a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1]) | |
889 b = array([1, 5, -1], mask=[0, 0, 1]) | |
890 test = in1d(a, b) | |
891 assert_equal(test, [True, True, False, True, True]) | |
892 # | |
893 assert_array_equal([], in1d([], [])) | |
894 | |
895 def test_in1d_invert(self): | |
896 # Test in1d's invert parameter | |
897 a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1]) | |
898 b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1]) | |
899 assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) | |
900 | |
901 a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1]) | |
902 b = array([1, 5, -1], mask=[0, 0, 1]) | |
903 assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) | |
904 | |
905 assert_array_equal([], in1d([], [], invert=True)) | |
906 | |
907 def test_union1d(self): | |
908 # Test union1d | |
909 a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1]) | |
910 b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1]) | |
911 test = union1d(a, b) | |
912 control = array([1, 2, 3, 4, 5, 7, -1], mask=[0, 0, 0, 0, 0, 0, 1]) | |
913 assert_equal(test, control) | |
914 # | |
915 assert_array_equal([], union1d([], [])) | |
916 | |
917 def test_setdiff1d(self): | |
918 # Test setdiff1d | |
919 a = array([6, 5, 4, 7, 7, 1, 2, 1], mask=[0, 0, 0, 0, 0, 0, 0, 1]) | |
920 b = array([2, 4, 3, 3, 2, 1, 5]) | |
921 test = setdiff1d(a, b) | |
922 assert_equal(test, array([6, 7, -1], mask=[0, 0, 1])) | |
923 # | |
924 a = arange(10) | |
925 b = arange(8) | |
926 assert_equal(setdiff1d(a, b), array([8, 9])) | |
927 | |
928 def test_setdiff1d_char_array(self): | |
929 # Test setdiff1d_charray | |
930 a = np.array(['a', 'b', 'c']) | |
931 b = np.array(['a', 'b', 's']) | |
932 assert_array_equal(setdiff1d(a, b), np.array(['c'])) | |
933 | |
934 | |
935 class TestShapeBase(TestCase): | |
936 # | |
937 def test_atleast2d(self): | |
938 # Test atleast_2d | |
939 a = masked_array([0, 1, 2], mask=[0, 1, 0]) | |
940 b = atleast_2d(a) | |
941 assert_equal(b.shape, (1, 3)) | |
942 assert_equal(b.mask.shape, b.data.shape) | |
943 assert_equal(a.shape, (3,)) | |
944 assert_equal(a.mask.shape, a.data.shape) | |
945 | |
946 | |
947 ############################################################################### | |
948 #------------------------------------------------------------------------------ | |
949 if __name__ == "__main__": | |
950 run_module_suite() |