def aggregate_group_loop(*args, **kwargs): """wraps func in lambda which prevents aggregate_numpy from recognising and optimising it. Instead it groups and loops.""" func = kwargs['func'] del kwargs['func'] return aggregate_np(*args, func=lambda x: func(x), **kwargs)
"""wraps func in lambda which prevents aggregate_numpy from recognising and optimising it. Instead it groups and loops.""" func = kwargs['func'] del kwargs['func'] return aggregate_np(*args, func=lambda x: func(x), **kwargs) print "TODO: use more extensive tests in test_accumarray.py" print "" print "-----simple examples----------" test_a = np.array([12.0, 3.2, -15, 88, 12.9]) test_group_idx = np.array([1, 0, 1, 4, 1]) print "test_a: ", test_a print "test_group_idx: ", test_group_idx print "accumarray(test_group_idx, test_a):" print aggregate_np(test_group_idx, test_a) # group vals by idx and sum # array([3.2, 9.9, 0., 0., 88.]) print "accumarray(test_group_idx, test_a, sz=8, func='min', fill_value=np.nan):" print aggregate_np(test_group_idx, test_a, size=8, func='min', fill_value=np.nan) # array([3.2, -15., nan, 88., nan, nan, nan, nan]) print "accumarray(test_group_idx, test_a, sz=5, func=lambda x: ' + '.join(str(xx) for xx in x),fill_value='')" print aggregate_np(test_group_idx, test_a, size=5, func=lambda x: ' + '.join(str(xx) for xx in x), fill_value='')
"""wraps func in lambda which prevents aggregate_numpy from recognising and optimising it. Instead it groups and loops.""" func = kwargs['func'] del kwargs['func'] return aggregate_np(*args, func=lambda x: func(x), **kwargs) print "TODO: use more extensive tests in test_accumarray.py" print "" print "-----simple examples----------" test_a = np.array([12.0, 3.2, -15, 88, 12.9]) test_group_idx = np.array([1, 0, 1, 4, 1 ]) print "test_a: ", test_a print "test_group_idx: ", test_group_idx print "accumarray(test_group_idx, test_a):" print aggregate_np(test_group_idx, test_a) # group vals by idx and sum # array([3.2, 9.9, 0., 0., 88.]) print "accumarray(test_group_idx, test_a, sz=8, func='min', fill_value=np.nan):" print aggregate_np(test_group_idx, test_a, size=8, func='min', fill_value=np.nan) # array([3.2, -15., nan, 88., nan, nan, nan, nan]) print "accumarray(test_group_idx, test_a, sz=5, func=lambda x: ' + '.join(str(xx) for xx in x),fill_value='')" print aggregate_np(test_group_idx, test_a, size=5, func=lambda x: ' + '.join(str(xx) for xx in x), fill_value='') print "" print "---------testing--------------" print "compare against group-and-loop with numpy" testable_funcs = {aliasing[f]: f for f in (np.sum, np.prod, np.any, np.all, np.min, np.max, np.std, np.var, np.mean)} test_group_idx = np.random.randint(0, 1e3, 1e5) test_a = np.random.rand(1e5) * 100 - 50 test_a[test_a > 25] = 0 # for use with bool functions