Fast N-dimensional aggregation functions with Numba
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.rst

Numbagg: Fast N-dimensional aggregation functions with Numba

https://travis-ci.org/shoyer/numbagg.svg?branch=master

Re-implementations of (some) functions found in Bottleneck with Numba and NumPy's generalized ufuncs.

Currently accelerated functions: allnan, anynan, count, nanargmax, nanargmin, nansum, nanmean, nanstd, nanvar, nanmax, nanmin, move_nanmean.

Easy to extend

Numbagg makes it easy to write, in pure Python/NumPy, flexible aggregation functions accelerated by Numba. All the hard work is done by Numba's JIT compiler and NumPy's gufunc machinery (as wrapped by Numba).

For example, here is how we wrote nansum:

import numpy as np
from numbagg.decorators import ndreduce

@ndreduce
def nansum(a):
    asum = 0.0
    for ai in a.flat:
        if not np.isnan(ai):
            asum += ai
    return asum

Advantages over Bottleneck

  • Way less code. Easier to add new functions. No ad-hoc templating system. No Cython!
  • Fast functions still work for >3 dimensions.
  • axis argument handles tuples of integers.
  • ufunc broadcasting lets us supply an array for the window in moving window functions.

The functions in Numbagg are adapted from (and soon to be tested against) Bottleneck's battle-hardened Cython. Still, Numbagg is experimental, and probably not yet ready for production.

Benchmarks

Initial benchmarks are quite encouraging. In many cases, Numbagg/Numba has competitive performance with Bottleneck/Cython:

import numbagg
import numpy as np
import bottleneck

x = np.random.RandomState(42).randn(1000, 1000)
x[x < -1] = np.NaN

# timings with numba=0.15.1-20-gd877602 and bottleneck=0.8.0

In [4]: %timeit numbagg.nanmean(x)
100 loops, best of 3: 2.39 ms per loop

In [5]: %timeit numbagg.nanmean(x, axis=0)
100 loops, best of 3: 9.54 ms per loop

In [6]: %timeit numbagg.nanmean(x, axis=1)
100 loops, best of 3: 2.77 ms per loop

In [7]: %timeit bottleneck.nanmean(x)
100 loops, best of 3: 2.27 ms per loop

In [8]: %timeit bottleneck.nanmean(x, axis=0)
100 loops, best of 3: 9.03 ms per loop

In [9]: %timeit bottleneck.nanmean(x, axis=1)
100 loops, best of 3: 2.3 ms per loop

To see these performance numbers, you'll need to install the dev version of Numba, as Numba's handling of the .flat iterator was sped up considerably in a recent PR.

Philosophy

Numbagg includes somewhat awkward workarounds for features missing from NumPy/Numba:

I hope that the need for most of these will eventually go away. In the meantime, expect Numbagg to be tightly coupled to Numba and NumPy release cycles.

License

MIT. Includes portions of Bottleneck, which is distributed under a Simplified BSD license.