Debian Codegraph @

petl/test/transform/test_reductions.py

raw
1 from __future__ import absolute_import, print_function, division
2
3
4 import operator
5
6
7 from collections import OrderedDict
8 from petl.test.helpers import ieq
9 from petl.util import strjoin
10 from petl.transform.reductions import rowreduce, aggregate, \
11 mergeduplicates, Conflict, fold
12
13
14 def test_rowreduce():
15
16 table1 = (('foo', 'bar'),
17 ('a', 3),
18 ('a', 7),
19 ('b', 2),
20 ('b', 1),
21 ('b', 9),
22 ('c', 4))
23
24 def sumbar(key, rows):
25 return [key, sum(row[1] for row in rows)]
26
27 table2 = rowreduce(table1, key='foo', reducer=sumbar,
28 header=['foo', 'barsum'])
29 expect2 = (('foo', 'barsum'),
30 ('a', 10),
31 ('b', 12),
32 ('c', 4))
33 ieq(expect2, table2)
34
35
36 def test_rowreduce_fieldnameaccess():
37
38 table1 = (('foo', 'bar'),
39 ('a', 3),
40 ('a', 7),
41 ('b', 2),
42 ('b', 1),
43 ('b', 9),
44 ('c', 4))
45
46 def sumbar(key, records):
47 return [key, sum([rec['bar'] for rec in records])]
48
49 table2 = rowreduce(table1, key='foo', reducer=sumbar,
50 header=['foo', 'barsum'])
51 expect2 = (('foo', 'barsum'),
52 ('a', 10),
53 ('b', 12),
54 ('c', 4))
55 ieq(expect2, table2)
56
57
58 def test_rowreduce_more():
59
60 table1 = (('foo', 'bar'),
61 ('aa', 3),
62 ('aa', 7),
63 ('bb', 2),
64 ('bb', 1),
65 ('bb', 9),
66 ('cc', 4))
67
68 def sumbar(key, records):
69 return [key, sum(rec['bar'] for rec in records)]
70
71 table2 = rowreduce(table1, key='foo', reducer=sumbar,
72 header=['foo', 'barsum'])
73 expect2 = (('foo', 'barsum'),
74 ('aa', 10),
75 ('bb', 12),
76 ('cc', 4))
77 ieq(expect2, table2)
78
79
80 def test_rowreduce_empty():
81 table = (('foo', 'bar'),)
82 expect = (('foo', 'bar'),)
83 reducer = lambda key, rows: (key, [r[0] for r in rows])
84 actual = rowreduce(table, key='foo', reducer=reducer,
85 header=('foo', 'bar'))
86 ieq(expect, actual)
87
88
89 def test_aggregate_simple():
90
91 table1 = (('foo', 'bar', 'baz'),
92 ('a', 3, True),
93 ('a', 7, False),
94 ('b', 2, True),
95 ('b', 2, False),
96 ('b', 9, False),
97 ('c', 4, True))
98
99 # simplest signature - aggregate whole rows
100 table2 = aggregate(table1, 'foo', len)
101 expect2 = (('foo', 'value'),
102 ('a', 2),
103 ('b', 3),
104 ('c', 1))
105 ieq(expect2, table2)
106 ieq(expect2, table2)
107
108 # next simplest signature - aggregate single field
109 table3 = aggregate(table1, 'foo', sum, 'bar')
110 expect3 = (('foo', 'value'),
111 ('a', 10),
112 ('b', 13),
113 ('c', 4))
114 ieq(expect3, table3)
115 ieq(expect3, table3)
116
117 # alternative signature for simple aggregation
118 table4 = aggregate(table1, key=('foo', 'bar'), aggregation=list,
119 value=('bar', 'baz'))
120 expect4 = (('foo', 'bar', 'value'),
121 ('a', 3, [(3, True)]),
122 ('a', 7, [(7, False)]),
123 ('b', 2, [(2, True), (2, False)]),
124 ('b', 9, [(9, False)]),
125 ('c', 4, [(4, True)]))
126 ieq(expect4, table4)
127 ieq(expect4, table4)
128
129 table5 = aggregate(table1, 'foo', len, field='nrows')
130 expect5 = (('foo', 'nrows'),
131 ('a', 2),
132 ('b', 3),
133 ('c', 1))
134 ieq(expect5, table5)
135 ieq(expect5, table5)
136
137
138 def test_aggregate_simple_key_is_None():
139
140 table1 = (('foo', 'bar', 'baz'),
141 ('a', 3, True),
142 ('a', 7, False),
143 ('b', 2, True),
144 ('b', 2, False),
145 ('b', 9, False),
146 ('c', 4, True))
147
148 # simplest signature - aggregate whole rows
149 table2 = aggregate(table1, None, len)
150 expect2 = (('value',), (6,))
151 ieq(expect2, table2)
152 ieq(expect2, table2)
153
154 # next simplest signature - aggregate single field
155 table3 = aggregate(table1, None, sum, 'bar')
156 expect3 = (('value',), (27,))
157 ieq(expect3, table3)
158 ieq(expect3, table3)
159
160 # alternative signature for simple aggregation
161 table4 = aggregate(table1, key=None, aggregation=list,
162 value=('bar', 'baz'))
163 expect4 = (('value',),
164 ( [(3, True), (7, False),
165 (2, True), (2, False), (9, False),
166 (4, True)],),)
167
168 ieq(expect4, table4)
169 ieq(expect4, table4)
170
171 table5 = aggregate(table1, None, len, field='nrows')
172 expect5 = (('nrows',), (6,))
173 ieq(expect5, table5)
174 ieq(expect5, table5)
175
176 def test_aggregate_multifield():
177
178 table1 = (('foo', 'bar'),
179 ('a', 3),
180 ('a', 7),
181 ('b', 2),
182 ('b', 1),
183 ('b', 9),
184 ('c', 4))
185
186 # dict arg
187
188 aggregators = OrderedDict()
189 aggregators['count'] = len
190 aggregators['minbar'] = 'bar', min
191 aggregators['maxbar'] = 'bar', max
192 aggregators['sumbar'] = 'bar', sum
193 aggregators['listbar'] = 'bar', list
194 aggregators['bars'] = 'bar', strjoin(', ')
195
196 table2 = aggregate(table1, 'foo', aggregators)
197 expect2 = (('foo', 'count', 'minbar', 'maxbar', 'sumbar', 'listbar',
198 'bars'),
199 ('a', 2, 3, 7, 10, [3, 7], '3, 7'),
200 ('b', 3, 1, 9, 12, [2, 1, 9], '2, 1, 9'),
201 ('c', 1, 4, 4, 4, [4], '4'))
202 ieq(expect2, table2)
203 ieq(expect2, table2) # check can iterate twice
204
205 # use suffix notation
206
207 table3 = aggregate(table1, 'foo')
208 table3['count'] = len
209 table3['minbar'] = 'bar', min
210 table3['maxbar'] = 'bar', max
211 table3['sumbar'] = 'bar', sum
212 table3['listbar'] = 'bar' # default aggregation is list
213 table3['bars'] = 'bar', strjoin(', ')
214 ieq(expect2, table3)
215
216 # list arg
217
218 aggregators = [('count', len),
219 ('minbar', 'bar', min),
220 ('maxbar', 'bar', max),
221 ('sumbar', 'bar', sum),
222 ('listbar', 'bar', list),
223 ('bars', 'bar', strjoin(', '))]
224
225 table4 = aggregate(table1, 'foo', aggregators)
226 ieq(expect2, table4)
227 ieq(expect2, table4) # check can iterate twice
228
229 def test_aggregate_multifield_key_is_None():
230
231 table1 = (('foo', 'bar'),
232 ('a', 3),
233 ('a', 7),
234 ('b', 2),
235 ('b', 1),
236 ('b', 9),
237 ('c', 4))
238
239 # dict arg
240 aggregators = OrderedDict()
241 aggregators['count'] = len
242 aggregators['minbar'] = 'bar', min
243 aggregators['maxbar'] = 'bar', max
244 aggregators['sumbar'] = 'bar', sum
245 aggregators['listbar'] = 'bar', list
246 aggregators['bars'] = 'bar', strjoin(', ')
247
248 table2 = aggregate(table1, None, aggregators)
249 expect2 = (('count', 'minbar', 'maxbar', 'sumbar', 'listbar',
250 'bars'),
251 (6, 1, 9, 26, [3, 7, 2, 1, 9, 4], '3, 7, 2, 1, 9, 4'))
252 ieq(expect2, table2)
253 ieq(expect2, table2) # check can iterate twice
254
255 # use suffix notation
256 table3 = aggregate(table1, None)
257 table3['count'] = len
258 table3['minbar'] = 'bar', min
259 table3['maxbar'] = 'bar', max
260 table3['sumbar'] = 'bar', sum
261 table3['listbar'] = 'bar' # default aggregation is list
262 table3['bars'] = 'bar', strjoin(', ')
263 ieq(expect2, table3)
264
265 # list arg
266 aggregators = [('count', len),
267 ('minbar', 'bar', min),
268 ('maxbar', 'bar', max),
269 ('sumbar', 'bar', sum),
270 ('listbar', 'bar', list),
271 ('bars', 'bar', strjoin(', '))]
272
273 table4 = aggregate(table1, None, aggregators)
274 ieq(expect2, table4)
275 ieq(expect2, table4) # check can iterate twice
276
277 def test_aggregate_more():
278
279 table1 = (('foo', 'bar'),
280 ('aa', 3),
281 ('aa', 7),
282 ('bb', 2),
283 ('bb', 1),
284 ('bb', 9),
285 ('cc', 4),
286 ('dd', 3))
287
288 aggregators = OrderedDict()
289 aggregators['minbar'] = 'bar', min
290 aggregators['maxbar'] = 'bar', max
291 aggregators['sumbar'] = 'bar', sum
292 aggregators['listbar'] = 'bar' # default aggregation is list
293 aggregators['bars'] = 'bar', strjoin(', ')
294
295 table2 = aggregate(table1, 'foo', aggregators)
296 expect2 = (('foo', 'minbar', 'maxbar', 'sumbar', 'listbar', 'bars'),
297 ('aa', 3, 7, 10, [3, 7], '3, 7'),
298 ('bb', 1, 9, 12, [2, 1, 9], '2, 1, 9'),
299 ('cc', 4, 4, 4, [4], '4'),
300 ('dd', 3, 3, 3, [3], '3'))
301 ieq(expect2, table2)
302 ieq(expect2, table2) # check can iterate twice
303
304 table3 = aggregate(table1, 'foo')
305 table3['minbar'] = 'bar', min
306 table3['maxbar'] = 'bar', max
307 table3['sumbar'] = 'bar', sum
308 table3['listbar'] = 'bar' # default aggregation is list
309 table3['bars'] = 'bar', strjoin(', ')
310 ieq(expect2, table3)
311
312 def test_aggregate_more_key_is_None():
313
314 table1 = (('foo', 'bar'),
315 ('aa', 3),
316 ('aa', 7),
317 ('bb', 2),
318 ('bb', 1),
319 ('bb', 9),
320 ('cc', 4),
321 ('dd', 3))
322
323 aggregators = OrderedDict()
324 aggregators['minbar'] = 'bar', min
325 aggregators['maxbar'] = 'bar', max
326 aggregators['sumbar'] = 'bar', sum
327 aggregators['listbar'] = 'bar' # default aggregation is list
328 aggregators['bars'] = 'bar', strjoin(', ')
329
330 table2 = aggregate(table1, None, aggregators)
331 expect2 = (('minbar', 'maxbar', 'sumbar', 'listbar', 'bars'),
332 (1, 9, 29,
333 [3, 7, 2, 1, 9, 4, 3],
334 '3, 7, 2, 1, 9, 4, 3'))
335 ieq(expect2, table2)
336 ieq(expect2, table2) # check can iterate twice
337
338 table3 = aggregate(table1, None)
339 table3['minbar'] = 'bar', min
340 table3['maxbar'] = 'bar', max
341 table3['sumbar'] = 'bar', sum
342 table3['listbar'] = 'bar' # default aggregation is list
343 table3['bars'] = 'bar', strjoin(', ')
344 ieq(expect2, table3)
345
346 def test_aggregate_multiple_source_fields():
347
348 table = (('foo', 'bar', 'baz'),
349 ('a', 3, True),
350 ('a', 7, False),
351 ('b', 2, True),
352 ('b', 2, False),
353 ('b', 9, False),
354 ('c', 4, True))
355
356 expect = (('foo', 'bar', 'value'),
357 ('a', 3, [(3, True)]),
358 ('a', 7, [(7, False)]),
359 ('b', 2, [(2, True), (2, False)]),
360 ('b', 9, [(9, False)]),
361 ('c', 4, [(4, True)]))
362
363 actual = aggregate(table, ('foo', 'bar'), list, ('bar', 'baz'))
364 ieq(expect, actual)
365 ieq(expect, actual)
366
367 actual = aggregate(table, key=('foo', 'bar'), aggregation=list,
368 value=('bar', 'baz'))
369 ieq(expect, actual)
370 ieq(expect, actual)
371
372 actual = aggregate(table, key=('foo', 'bar'))
373 actual['value'] = ('bar', 'baz'), list
374 ieq(expect, actual)
375 ieq(expect, actual)
376
377 def test_aggregate_multiple_source_fields_key_is_None():
378
379 table = (('foo', 'bar', 'baz'),
380 ('a', 3, True),
381 ('a', 7, False),
382 ('b', 2, True),
383 ('b', 2, False),
384 ('b', 9, False),
385 ('c', 4, True))
386
387 expect = (('value',),
388 ( [(3, True), (7, False),
389 (2, True), (2, False), (9, False),
390 (4, True)],),)
391
392 actual = aggregate(table, None, list, ('bar', 'baz'))
393 ieq(expect, actual)
394 ieq(expect, actual)
395
396 actual = aggregate(table, key=None, aggregation=list,
397 value=('bar', 'baz'))
398 ieq(expect, actual)
399 ieq(expect, actual)
400
401 actual = aggregate(table, key=None)
402 actual['value'] = ('bar', 'baz'), list
403 ieq(expect, actual)
404 ieq(expect, actual)
405
406
407 def test_aggregate_empty():
408
409 table = (('foo', 'bar'),)
410
411 aggregators = OrderedDict()
412 aggregators['minbar'] = 'bar', min
413 aggregators['maxbar'] = 'bar', max
414 aggregators['sumbar'] = 'bar', sum
415
416 actual = aggregate(table, 'foo', aggregators)
417 expect = (('foo', 'minbar', 'maxbar', 'sumbar'),)
418 ieq(expect, actual)
419
420 def test_aggregate_empty_key_is_None():
421
422 table = (('foo', 'bar'),)
423
424 aggregators = OrderedDict()
425 aggregators['minbar'] = 'bar', min
426 aggregators['maxbar'] = 'bar', max
427 aggregators['sumbar'] = 'bar', sum
428
429 actual = aggregate(table, None, aggregators)
430 expect = (('minbar', 'maxbar', 'sumbar'),)
431 ieq(expect, actual)
432
433 def test_mergeduplicates():
434
435 table = (('foo', 'bar', 'baz'),
436 ('A', 1, 2),
437 ('B', '2', None),
438 ('D', 'xyz', 9.4),
439 ('B', None, u'7.8', True),
440 ('E', None, 42.),
441 ('D', 'xyz', 12.3),
442 ('A', 2, None))
443
444 # value overrides missing
445 result = mergeduplicates(table, 'foo', missing=None)
446 expectation = (('foo', 'bar', 'baz'),
447 ('A', Conflict([1, 2]), 2),
448 ('B', '2', u'7.8'),
449 ('D', 'xyz', Conflict([9.4, 12.3])),
450 ('E', None, 42.))
451 ieq(expectation, result)
452
453
454 def test_mergeduplicates_empty():
455 table = (('foo', 'bar'),)
456 expect = (('foo', 'bar'),)
457 actual = mergeduplicates(table, key='foo')
458 ieq(expect, actual)
459
460
461 def test_mergeduplicates_shortrows():
462 table = [['foo', 'bar', 'baz'],
463 ['a', 1, True],
464 ['b', 2, True],
465 ['b', 3]]
466 actual = mergeduplicates(table, 'foo')
467 expect = [('foo', 'bar', 'baz'),
468 ('a', 1, True),
469 ('b', Conflict([2, 3]), True)]
470 ieq(expect, actual)
471
472
473 def test_mergeduplicates_compoundkey():
474 table = [['foo', 'bar', 'baz'],
475 ['a', 1, True],
476 ['a', 1, True],
477 ['a', 2, False],
478 ['a', 2, None],
479 ['c', 3, True],
480 ['c', 3, False],
481 ]
482 actual = mergeduplicates(table, key=('foo', 'bar'))
483 expect = [('foo', 'bar', 'baz'),
484 ('a', 1, True),
485 ('a', 2, False),
486 ('c', 3, Conflict([True, False]))]
487 ieq(expect, actual)
488
489
490 def test_fold():
491
492 t1 = (('id', 'count'), (1, 3), (1, 5), (2, 4), (2, 8))
493 t2 = fold(t1, 'id', operator.add, 'count', presorted=True)
494 expect = (('key', 'value'), (1, 8), (2, 12))
495 ieq(expect, t2)
496 ieq(expect, t2)