Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/tests/test_arraysetops.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 """Test functions for 1D array set operations. | |
2 | |
3 """ | |
4 from __future__ import division, absolute_import, print_function | |
5 | |
6 import numpy as np | |
7 from numpy.testing import ( | |
8 run_module_suite, TestCase, assert_array_equal | |
9 ) | |
10 from numpy.lib.arraysetops import ( | |
11 ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d | |
12 ) | |
13 | |
14 | |
15 class TestSetOps(TestCase): | |
16 | |
17 def test_unique(self): | |
18 | |
19 def check_all(a, b, i1, i2, c, dt): | |
20 base_msg = 'check {0} failed for type {1}' | |
21 | |
22 msg = base_msg.format('values', dt) | |
23 v = unique(a) | |
24 assert_array_equal(v, b, msg) | |
25 | |
26 msg = base_msg.format('return_index', dt) | |
27 v, j = unique(a, 1, 0, 0) | |
28 assert_array_equal(v, b, msg) | |
29 assert_array_equal(j, i1, msg) | |
30 | |
31 msg = base_msg.format('return_inverse', dt) | |
32 v, j = unique(a, 0, 1, 0) | |
33 assert_array_equal(v, b, msg) | |
34 assert_array_equal(j, i2, msg) | |
35 | |
36 msg = base_msg.format('return_counts', dt) | |
37 v, j = unique(a, 0, 0, 1) | |
38 assert_array_equal(v, b, msg) | |
39 assert_array_equal(j, c, msg) | |
40 | |
41 msg = base_msg.format('return_index and return_inverse', dt) | |
42 v, j1, j2 = unique(a, 1, 1, 0) | |
43 assert_array_equal(v, b, msg) | |
44 assert_array_equal(j1, i1, msg) | |
45 assert_array_equal(j2, i2, msg) | |
46 | |
47 msg = base_msg.format('return_index and return_counts', dt) | |
48 v, j1, j2 = unique(a, 1, 0, 1) | |
49 assert_array_equal(v, b, msg) | |
50 assert_array_equal(j1, i1, msg) | |
51 assert_array_equal(j2, c, msg) | |
52 | |
53 msg = base_msg.format('return_inverse and return_counts', dt) | |
54 v, j1, j2 = unique(a, 0, 1, 1) | |
55 assert_array_equal(v, b, msg) | |
56 assert_array_equal(j1, i2, msg) | |
57 assert_array_equal(j2, c, msg) | |
58 | |
59 msg = base_msg.format(('return_index, return_inverse ' | |
60 'and return_counts'), dt) | |
61 v, j1, j2, j3 = unique(a, 1, 1, 1) | |
62 assert_array_equal(v, b, msg) | |
63 assert_array_equal(j1, i1, msg) | |
64 assert_array_equal(j2, i2, msg) | |
65 assert_array_equal(j3, c, msg) | |
66 | |
67 a = [5, 7, 1, 2, 1, 5, 7]*10 | |
68 b = [1, 2, 5, 7] | |
69 i1 = [2, 3, 0, 1] | |
70 i2 = [2, 3, 0, 1, 0, 2, 3]*10 | |
71 c = np.multiply([2, 1, 2, 2], 10) | |
72 | |
73 # test for numeric arrays | |
74 types = [] | |
75 types.extend(np.typecodes['AllInteger']) | |
76 types.extend(np.typecodes['AllFloat']) | |
77 types.append('datetime64[D]') | |
78 types.append('timedelta64[D]') | |
79 for dt in types: | |
80 aa = np.array(a, dt) | |
81 bb = np.array(b, dt) | |
82 check_all(aa, bb, i1, i2, c, dt) | |
83 | |
84 # test for object arrays | |
85 dt = 'O' | |
86 aa = np.empty(len(a), dt) | |
87 aa[:] = a | |
88 bb = np.empty(len(b), dt) | |
89 bb[:] = b | |
90 check_all(aa, bb, i1, i2, c, dt) | |
91 | |
92 # test for structured arrays | |
93 dt = [('', 'i'), ('', 'i')] | |
94 aa = np.array(list(zip(a, a)), dt) | |
95 bb = np.array(list(zip(b, b)), dt) | |
96 check_all(aa, bb, i1, i2, c, dt) | |
97 | |
98 # test for ticket #2799 | |
99 aa = [1. + 0.j, 1 - 1.j, 1] | |
100 assert_array_equal(np.unique(aa), [1. - 1.j, 1. + 0.j]) | |
101 | |
102 # test for ticket #4785 | |
103 a = [(1, 2), (1, 2), (2, 3)] | |
104 unq = [1, 2, 3] | |
105 inv = [0, 1, 0, 1, 1, 2] | |
106 a1 = unique(a) | |
107 assert_array_equal(a1, unq) | |
108 a2, a2_inv = unique(a, return_inverse=True) | |
109 assert_array_equal(a2, unq) | |
110 assert_array_equal(a2_inv, inv) | |
111 | |
112 def test_intersect1d(self): | |
113 # unique inputs | |
114 a = np.array([5, 7, 1, 2]) | |
115 b = np.array([2, 4, 3, 1, 5]) | |
116 | |
117 ec = np.array([1, 2, 5]) | |
118 c = intersect1d(a, b, assume_unique=True) | |
119 assert_array_equal(c, ec) | |
120 | |
121 # non-unique inputs | |
122 a = np.array([5, 5, 7, 1, 2]) | |
123 b = np.array([2, 1, 4, 3, 3, 1, 5]) | |
124 | |
125 ed = np.array([1, 2, 5]) | |
126 c = intersect1d(a, b) | |
127 assert_array_equal(c, ed) | |
128 | |
129 assert_array_equal([], intersect1d([], [])) | |
130 | |
131 def test_setxor1d(self): | |
132 a = np.array([5, 7, 1, 2]) | |
133 b = np.array([2, 4, 3, 1, 5]) | |
134 | |
135 ec = np.array([3, 4, 7]) | |
136 c = setxor1d(a, b) | |
137 assert_array_equal(c, ec) | |
138 | |
139 a = np.array([1, 2, 3]) | |
140 b = np.array([6, 5, 4]) | |
141 | |
142 ec = np.array([1, 2, 3, 4, 5, 6]) | |
143 c = setxor1d(a, b) | |
144 assert_array_equal(c, ec) | |
145 | |
146 a = np.array([1, 8, 2, 3]) | |
147 b = np.array([6, 5, 4, 8]) | |
148 | |
149 ec = np.array([1, 2, 3, 4, 5, 6]) | |
150 c = setxor1d(a, b) | |
151 assert_array_equal(c, ec) | |
152 | |
153 assert_array_equal([], setxor1d([], [])) | |
154 | |
155 def test_ediff1d(self): | |
156 zero_elem = np.array([]) | |
157 one_elem = np.array([1]) | |
158 two_elem = np.array([1, 2]) | |
159 | |
160 assert_array_equal([], ediff1d(zero_elem)) | |
161 assert_array_equal([0], ediff1d(zero_elem, to_begin=0)) | |
162 assert_array_equal([0], ediff1d(zero_elem, to_end=0)) | |
163 assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0)) | |
164 assert_array_equal([], ediff1d(one_elem)) | |
165 assert_array_equal([1], ediff1d(two_elem)) | |
166 | |
167 def test_in1d(self): | |
168 # we use two different sizes for the b array here to test the | |
169 # two different paths in in1d(). | |
170 for mult in (1, 10): | |
171 # One check without np.array, to make sure lists are handled correct | |
172 a = [5, 7, 1, 2] | |
173 b = [2, 4, 3, 1, 5] * mult | |
174 ec = np.array([True, False, True, True]) | |
175 c = in1d(a, b, assume_unique=True) | |
176 assert_array_equal(c, ec) | |
177 | |
178 a[0] = 8 | |
179 ec = np.array([False, False, True, True]) | |
180 c = in1d(a, b, assume_unique=True) | |
181 assert_array_equal(c, ec) | |
182 | |
183 a[0], a[3] = 4, 8 | |
184 ec = np.array([True, False, True, False]) | |
185 c = in1d(a, b, assume_unique=True) | |
186 assert_array_equal(c, ec) | |
187 | |
188 a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) | |
189 b = [2, 3, 4] * mult | |
190 ec = [False, True, False, True, True, True, True, True, True, False, | |
191 True, False, False, False] | |
192 c = in1d(a, b) | |
193 assert_array_equal(c, ec) | |
194 | |
195 b = b + [5, 5, 4] * mult | |
196 ec = [True, True, True, True, True, True, True, True, True, True, | |
197 True, False, True, True] | |
198 c = in1d(a, b) | |
199 assert_array_equal(c, ec) | |
200 | |
201 a = np.array([5, 7, 1, 2]) | |
202 b = np.array([2, 4, 3, 1, 5] * mult) | |
203 ec = np.array([True, False, True, True]) | |
204 c = in1d(a, b) | |
205 assert_array_equal(c, ec) | |
206 | |
207 a = np.array([5, 7, 1, 1, 2]) | |
208 b = np.array([2, 4, 3, 3, 1, 5] * mult) | |
209 ec = np.array([True, False, True, True, True]) | |
210 c = in1d(a, b) | |
211 assert_array_equal(c, ec) | |
212 | |
213 a = np.array([5, 5]) | |
214 b = np.array([2, 2] * mult) | |
215 ec = np.array([False, False]) | |
216 c = in1d(a, b) | |
217 assert_array_equal(c, ec) | |
218 | |
219 a = np.array([5]) | |
220 b = np.array([2]) | |
221 ec = np.array([False]) | |
222 c = in1d(a, b) | |
223 assert_array_equal(c, ec) | |
224 | |
225 assert_array_equal(in1d([], []), []) | |
226 | |
227 def test_in1d_char_array(self): | |
228 a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b']) | |
229 b = np.array(['a', 'c']) | |
230 | |
231 ec = np.array([True, False, True, False, False, True, False, False]) | |
232 c = in1d(a, b) | |
233 | |
234 assert_array_equal(c, ec) | |
235 | |
236 def test_in1d_invert(self): | |
237 "Test in1d's invert parameter" | |
238 # We use two different sizes for the b array here to test the | |
239 # two different paths in in1d(). | |
240 for mult in (1, 10): | |
241 a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5]) | |
242 b = [2, 3, 4] * mult | |
243 assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True)) | |
244 | |
245 def test_in1d_ravel(self): | |
246 # Test that in1d ravels its input arrays. This is not documented | |
247 # behavior however. The test is to ensure consistentency. | |
248 a = np.arange(6).reshape(2, 3) | |
249 b = np.arange(3, 9).reshape(3, 2) | |
250 long_b = np.arange(3, 63).reshape(30, 2) | |
251 ec = np.array([False, False, False, True, True, True]) | |
252 | |
253 assert_array_equal(in1d(a, b, assume_unique=True), ec) | |
254 assert_array_equal(in1d(a, b, assume_unique=False), ec) | |
255 assert_array_equal(in1d(a, long_b, assume_unique=True), ec) | |
256 assert_array_equal(in1d(a, long_b, assume_unique=False), ec) | |
257 | |
258 def test_union1d(self): | |
259 a = np.array([5, 4, 7, 1, 2]) | |
260 b = np.array([2, 4, 3, 3, 2, 1, 5]) | |
261 | |
262 ec = np.array([1, 2, 3, 4, 5, 7]) | |
263 c = union1d(a, b) | |
264 assert_array_equal(c, ec) | |
265 | |
266 assert_array_equal([], union1d([], [])) | |
267 | |
268 def test_setdiff1d(self): | |
269 a = np.array([6, 5, 4, 7, 1, 2, 7, 4]) | |
270 b = np.array([2, 4, 3, 3, 2, 1, 5]) | |
271 | |
272 ec = np.array([6, 7]) | |
273 c = setdiff1d(a, b) | |
274 assert_array_equal(c, ec) | |
275 | |
276 a = np.arange(21) | |
277 b = np.arange(19) | |
278 ec = np.array([19, 20]) | |
279 c = setdiff1d(a, b) | |
280 assert_array_equal(c, ec) | |
281 | |
282 assert_array_equal([], setdiff1d([], [])) | |
283 | |
284 def test_setdiff1d_char_array(self): | |
285 a = np.array(['a', 'b', 'c']) | |
286 b = np.array(['a', 'b', 's']) | |
287 assert_array_equal(setdiff1d(a, b), np.array(['c'])) | |
288 | |
289 def test_manyways(self): | |
290 a = np.array([5, 7, 1, 2, 8]) | |
291 b = np.array([9, 8, 2, 4, 3, 1, 5]) | |
292 | |
293 c1 = setxor1d(a, b) | |
294 aux1 = intersect1d(a, b) | |
295 aux2 = union1d(a, b) | |
296 c2 = setdiff1d(aux2, aux1) | |
297 assert_array_equal(c1, c2) | |
298 | |
299 | |
300 if __name__ == "__main__": | |
301 run_module_suite() |