Skip to main content

Fast histogramming in Python built on pybind11 and OpenMP.

Project description

pygram11

Documentation Status Actions Status PyPI version Conda Forge Python Version

Simple and fast histogramming in Python accelerated with OpenMP with help from pybind11.

pygram11 provides functions for very fast histogram calculations (and the variance in each bin) in one and two dimensions. The API is very simple; documentation can be found here (you'll also find some benchmarks there).

Installing

From PyPI

Binary wheels are provided for Linux and macOS. They can be installed from PyPI via pip:

pip install pygram11

From conda-forge

For installation via the conda package manager pygram11 is part of conda-forge.

conda install pygram11 -c conda-forge

Please note that on macOS the OpenMP libraries from LLVM (libomp) and Intel (libiomp) may clash if your conda environment includes the Intel Math Kernel Library (MKL) package distributed by Anaconda. You may need to install the nomkl package to prevent the clash (Intel MKL accelerates many linear algebra operations, but does not impact pygram11):

conda install nomkl ## sometimes necessary fix (macOS only)

From Source

You need is a C++14 compiler and OpenMP. If you are using a relatively modern GCC release on Linux then you probably don't have to worry about the OpenMP dependency. If you are on macOS, you can install libomp from Homebrew (pygram11 does compile on Apple Silicon devices with Python version >= 3.9 and libomp installed from Homebrew). With those dependencies met, simply run:

git clone https://github.com/douglasdavis/pygram11.git --recurse-submodules
cd pygram11
pip install .

Or let pip handle the cloning procedure:

pip install git+https://github.com/douglasdavis/pygram11.git@main

Tests are run on Python versions >= 3.7 and binary wheels are provided for those versions.

In Action

A histogram (with fixed bin width) of weighted data in one dimension:

>>> rng = np.random.default_rng(123)
>>> x = rng.standard_normal(10000)
>>> w = rng.uniform(0.8, 1.2, x.shape[0])
>>> h, err = pygram11.histogram(x, bins=40, range=(-4, 4), weights=w)

A histogram with fixed bin width which saves the under and overflow in the first and last bins:

>>> x = rng.standard_normal(1000000)
>>> h, __ = pygram11.histogram(x, bins=20, range=(-3, 3), flow=True)

where we've used __ to catch the None returned when weights are absent. A histogram in two dimensions with variable width bins:

>>> x = rng.standard_normal(1000)
>>> y = rng.standard_normal(1000)
>>> xbins = [-2.0, -1.0, -0.5, 1.5, 2.0, 3.1]
>>> ybins = [-3.0, -1.5, -0.1, 0.8, 2.0, 2.8]
>>> h, err = pygram11.histogram2d(x, y, bins=[xbins, ybins])

Manually controlling OpenMP acceleration with context managers:

>>> with pygram11.omp_disabled():  # disable all thresholds.
...     result, _ = pygram11.histogram(x, bins=10, range=(-3, 3))
...
>>> with pygram11.omp_forced(key="thresholds.var1d"):  # force a single threshold.
...     result, _ = pygram11.histogram(x, bins=[-3, -2, 0, 2, 3])
...

Histogramming multiple weight variations for the same data, then putting the result in a DataFrame (the input pandas DataFrame will be interpreted as a NumPy array):

>>> N = 10000
>>> weights = pd.DataFrame({"weight_a": np.abs(rng.standard_normal(N)),
...                         "weight_b": rng.uniform(0.5, 0.8, N),
...                         "weight_c": rng.uniform(0.0, 1.0, N)})
>>> data = rng.standard_normal(N)
>>> count, err = pygram11.histogram(data, bins=20, range=(-3, 3), weights=weights, flow=True)
>>> count_df = pd.DataFrame(count, columns=weights.columns)
>>> err_df = pd.DataFrame(err, columns=weights.columns)

I also wrote a blog post with some simple examples.

Other Libraries

  • boost-histogram provides Pythonic object oriented histograms.
  • Simple and fast histogramming in Python using the NumPy C API: fast-histogram (no variance or overflow support).
  • To calculate histograms in Python on a GPU, see cupy.histogram.

If there is something you'd like to see in pygram11, please open an issue or pull request.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pygram11-0.12.1.tar.gz (474.7 kB view details)

Uploaded Source

Built Distributions

pygram11-0.12.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (501.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

pygram11-0.12.1-cp310-cp310-macosx_10_9_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pygram11-0.12.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (501.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pygram11-0.12.1-cp39-cp39-macosx_10_9_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pygram11-0.12.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (502.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pygram11-0.12.1-cp38-cp38-macosx_10_9_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

pygram11-0.12.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (476.2 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pygram11-0.12.1-cp37-cp37m-macosx_10_9_x86_64.whl (981.8 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file pygram11-0.12.1.tar.gz.

File metadata

  • Download URL: pygram11-0.12.1.tar.gz
  • Upload date:
  • Size: 474.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pygram11-0.12.1.tar.gz
Algorithm Hash digest
SHA256 5477e476dc3e5c2efb1413f8d73524c43878f691b91649b683dc3b110a6ce5f0
MD5 32f43c7956746439ac39cb2a9ff00388
BLAKE2b-256 9e99d0801be9864948204136ecbb86b102eaa018a8eae60d435dd459bb306a90

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pygram11-0.12.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 3110783b11400860771b64cba80d6a5c89fd3d6cac69577d347093fccfeea568
MD5 85e9cfdfc9709f81b60c9baebb02c92c
BLAKE2b-256 9cccd0a0a0eabbdd7f861455dd344156d8d82aed21c0422cbc8561881d859b0c

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pygram11-0.12.1-cp310-cp310-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.10, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pygram11-0.12.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 16b0702328a6731b0bf7cb460fd95979d742834d89e6f38505b1a3f3ade6f79a
MD5 bff6f246571ee68a6608a737dbed2a0b
BLAKE2b-256 ba3ae204c4f98efe4bf426db39ed53b32aa5356fa6ed618d42f72b8ef6bc59a2

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pygram11-0.12.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 15db334bf3ab234f59885192ba03035e20946284e5fd76c6e54b8060b45cfbe1
MD5 b65a276ef7e44297e03e1a7e93e4b85c
BLAKE2b-256 d8d2ebb03d247b4aef27f3a61a287f1591d1d6f1a7ab861ed5b232c3b6d3f573

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pygram11-0.12.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pygram11-0.12.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bcd312f90e3f39eef1a4bce9154244783b6af919c48614b9f4d7fcb8bf25a24e
MD5 a984b5e677b7d79e378adaf7cd537f51
BLAKE2b-256 4c1d6444887fdac23cadf2eb688d25afd7031552c1d821394fb163760e437f98

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pygram11-0.12.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 10f038067cce3bd1e8d0b88ddcc0f1d79f90455a792e93726690ac73847f4ebc
MD5 d9dffe2901cd0d23e06859ccefd3964f
BLAKE2b-256 8848e14efb1543b6ce21c5d33cad54a982ad6527a9f288e224f10b4a767de3e5

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pygram11-0.12.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pygram11-0.12.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 17d79adbe9e2e05059c7df23ef3cf112184e370b94159c6f5c66823686c81e77
MD5 25c3d6c95b842d2ff3086f7f8020969a
BLAKE2b-256 9d0bb87f65f76f9afbbdf56741ac56521c44b4766d5bc3426919c43fabd85f5c

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pygram11-0.12.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 49793088b5b760ee675c788686aa7ba64ba14d5d3d134e450a976445c08c7d65
MD5 6da9c13432e8428941d9cd3bceac46e7
BLAKE2b-256 37f9e78adeea8db929000483567e685ecffee3ae433a329268dd0d57e57d4743

See more details on using hashes here.

File details

Details for the file pygram11-0.12.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pygram11-0.12.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 981.8 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pygram11-0.12.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3bd99a3dd389d44f7f8f61691eb83df9fa7f088d9c3826d7da8c99395e7ca715
MD5 4dbcd1147d6bd86a799ce8af0390cc03
BLAKE2b-256 7dc308992703a13d44f7eebbcd1f68884df383f9878207b31308902ea8a53626

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