Boost-histogram
Boost-histogram is a high-performance Python library providing bindings for the C++14 Boost.Histogram library, designed for fast, multi-dimensional generalized histogramming. It treats histograms as first-class objects, enabling easy filling, manipulation, slicing, and projection. The library is currently at version 1.7.2 and maintains an active release cadence with frequent updates and new features, often driven by the Scikit-HEP project.
Warnings
- breaking Python version support has significantly changed across recent versions. Python 3.7 support was removed in `1.5.0`, Python 3.8 support was dropped after `1.5.2` (meaning `1.6.0` and later do not support 3.8), and Python 3.10+ is now required as of `1.7.0`.
- deprecated The internal `_storage_type` attribute changed from a `PendingDeprecationWarning` to a full `DeprecationWarning`.
- gotcha Filling integer-based axes with floating-point arrays (or single floats) is now explicitly disallowed to prevent rounding issues around 0.
- breaking Serialization behavior and format have seen several fixes and changes across versions, particularly around metadata and complex storages. This could lead to issues when loading histograms saved with older versions or in different environments.
- gotcha With the introduction of `MultiCell` storage and generic histogram types, advanced type checking might require updating type hints. Histograms are now generic, meaning `Hist` may need to be `Hist[Any]` in strict type-checked code.
Install
-
pip install boost-histogram -
conda install -c conda-forge boost-histogram
Imports
- boost_histogram
import boost_histogram as bh
- Histogram
bh.Histogram(...)
- axis.Regular
bh.axis.Regular(...)
Quickstart
import boost_histogram as bh
import numpy as np
# Create a 1D histogram with 10 bins from 0 to 1
hist = bh.Histogram(bh.axis.Regular(10, 0, 1))
# Fill the histogram with data
data = np.random.rand(1000)
hist.fill(data)
# Access histogram values (counts)
counts = hist.view()
print(f"Histogram counts: {counts[:5]}...")
# Access axis centers
centers = hist.axes[0].centers
print(f"Axis centers: {centers[:5]}...")