Skip to main content

Fast N-dimensional aggregation functions with Numba

Project description

Numbagg: Fast N-dimensional aggregation functions with Numba

GitHub Workflow CI Status PyPI Version

Fast, flexible N-dimensional array functions written with Numba and NumPy's generalized ufuncs.

Currently accelerated functions:

  • Array functions: allnan, anynan, count, nanargmax, nanargmin, nanmax, nanmean, nanstd, nanvar, nanmin, nansum
  • Moving window functions: move_exp_nanmean, move_mean, move_sum

Note: Only functions listed here (exposed in Numbagg's top level namespace) are supported as part of Numbagg's public API.

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

You are welcome to experiment with Numbagg's decorator functions, but these are not public APIs (yet): we reserve the right to change them at any time.

We'd rather get your pull requests to add new functions into Numbagg directly!

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.

Most of the functions in Numbagg (including our test suite) are adapted from Bottleneck's battle-hardened implementations. Still, Numbagg is experimental, and probably not yet ready for production.

Benchmarks

Initial benchmarks are quite encouraging. Numbagg/Numba has comparable (slightly better) performance than Bottleneck's hand-written C:

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.41.0 and bottleneck=1.2.1

In [2]: %timeit numbagg.nanmean(x)
1.8 ms ± 92.3 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [3]: %timeit numbagg.nanmean(x, axis=0)
3.63 ms ± 136 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [4]: %timeit numbagg.nanmean(x, axis=1)
1.81 ms ± 41 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [5]: %timeit bottleneck.nanmean(x)
2.22 ms ± 119 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [6]: %timeit bottleneck.nanmean(x, axis=0)
4.45 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [7]: %timeit bottleneck.nanmean(x, axis=1)
2.19 ms ± 13.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Our approach

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

3-clause BSD. Includes portions of Bottleneck, which is distributed under a Simplified BSD license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numbagg-0.2.2.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

numbagg-0.2.2-py2.py3-none-any.whl (19.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file numbagg-0.2.2.tar.gz.

File metadata

  • Download URL: numbagg-0.2.2.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for numbagg-0.2.2.tar.gz
Algorithm Hash digest
SHA256 58edcc31531a60527f29290ad42f5b734f328a9348bd5ef6cd0a3b1044183181
MD5 f1a9dfe2920089f16f7aece8bcea7c03
BLAKE2b-256 65cd604a349bfd9315798c6a5f315e4e4f19298df63ab53429c3c82806334123

See more details on using hashes here.

File details

Details for the file numbagg-0.2.2-py2.py3-none-any.whl.

File metadata

  • Download URL: numbagg-0.2.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for numbagg-0.2.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f1197762cb3b2525284f903d88955dce3cd53f81be101bcaa0f4453f9a9349c6
MD5 e20ea2cbe3764b5bd1c5c2cbc76c4a4d
BLAKE2b-256 140a9ace279a39a2eb6ca589a4e5f83d3b3d15fb9141fd39cc24fbeff7fa53b4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page