Skip to content
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ histograms can be plotted via any compatible library, such as [mplhep][].
* `/=`: Divide by a scaler (not all storages) (`hist / scalar` supported too)
* `.kind`: Either `bh.Kind.COUNT` or `bh.Kind.MEAN`, depending on storage
* `.sum(flow=False)`: The total count of all bins
* `.compare(second_hist)`: Compare the histogram with another histogram
* `.project(ax1, ax2, ...)`: Project down to listed axis (numbers). Can also reorder axes.
* `.to_numpy(flow=False, view=False)`: Convert to a NumPy style tuple (with or without under/overflow bins)
* `.view(flow=False)`: Get a view on the bin contents (with or without under/overflow bins)
Expand Down
42 changes: 42 additions & 0 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
import copy
import logging
import re
import threading
import typing
import warnings
Expand All @@ -23,6 +24,7 @@
)

import numpy as np
from pytest import approx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use pytest inside the library itself. There are tools for this like np.allclose. Also compare should return True/False, not errors / asserts. Let's move the changes to hist.py into a different PR, as that's not related to using approx in the tests. :)


import boost_histogram
from boost_histogram import _core
Expand Down Expand Up @@ -297,6 +299,46 @@ def ndim(self) -> int:
"""
return self._hist.rank() # type: ignore[no-any-return]

def compare(self, hist2):
if self.view().shape == approx(hist2.view().shape):
if self.view() == approx(hist2.view()):
if self.variances() == approx(hist2.variances()):
if (
re.search("(?<=storage=).*", str(self.view))[0].split("(")[0]
== re.search("(?<=storage=).*", str(hist2.view))[0].split("(")[
0
]
):
if list(map(str, [i for i in self.axes])) == list(
map(str, [i for i in hist2.axes])
):
return True
else:
assert list(map(str, [i for i in self.axes])) == list(
map(str, [i for i in hist2.axes])
), "The axes are not equal."
else:
assert (
re.search("(?<=storage=).*", str(self.view))[0].split("(")[
0
]
== re.search("(?<=storage=).*", str(hist2.view))[0].split(
"("
)[0]
), "The storage type is not equal."
else:
assert self.variances() == approx(
hist2.variances()
), "The histogram contents are not equal."
else:
assert self.view() == approx(
hist2.view()
), "The histogram contents are not equal."
else:
assert self.view().shape == approx(
hist2.view().shape
), "The histogram dimensions are not equal."

def view(
self, flow: bool = False
) -> Union["np.typing.NDArray[Any]", WeightedSumView, WeightedMeanView, MeanView]:
Expand Down
31 changes: 16 additions & 15 deletions tests/test_axes_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest
from pytest import approx

import boost_histogram as bh

Expand Down Expand Up @@ -39,11 +40,11 @@ def test_axes_centers(h):
full_answers = np.mgrid[0.5:10, 0.5:5, 0.5:2]

for i in range(3):
np.testing.assert_allclose(centers.broadcast()[i], full_answers[i])
np.testing.assert_allclose(centers[i], answers[i])
np.testing.assert_allclose(centers.T[i], answers[i].T)
np.testing.assert_allclose(centers.flatten()[i], answers[i].flatten())
np.testing.assert_allclose(h.axes[i].centers, answers[i].ravel())
assert centers.broadcast()[i] == approx(full_answers[i])
assert centers[i] == approx(answers[i])
assert centers.T[i] == approx(answers[i].T)
assert centers.flatten()[i] == approx(answers[i].flatten())
assert h.axes[i].centers == approx(answers[i].ravel())


def test_axes_edges(h):
Expand All @@ -52,11 +53,11 @@ def test_axes_edges(h):
full_answers = np.mgrid[0:11, 0:6, 0:3]

for i in range(3):
np.testing.assert_allclose(edges.broadcast()[i], full_answers[i])
np.testing.assert_allclose(edges[i], answers[i])
np.testing.assert_allclose(edges.T[i], answers[i].T)
np.testing.assert_allclose(edges.ravel()[i], answers[i].ravel())
np.testing.assert_allclose(h.axes[i].edges, answers[i].ravel())
assert edges.broadcast()[i] == approx(full_answers[i])
assert edges[i] == approx(answers[i])
assert edges.T[i] == approx(answers[i].T)
assert edges.ravel()[i] == approx(answers[i].ravel())
assert h.axes[i].edges == approx(answers[i].ravel())


def test_axes_widths(h):
Expand All @@ -65,11 +66,11 @@ def test_axes_widths(h):
full_answers = np.mgrid[1:1:10j, 1:1:5j, 1:1:2j]

for i in range(3):
np.testing.assert_allclose(widths.broadcast()[i], full_answers[i])
np.testing.assert_allclose(widths[i], answers[i])
np.testing.assert_allclose(widths.T[i], answers[i].T)
np.testing.assert_allclose(widths.ravel()[i], answers[i].ravel())
np.testing.assert_allclose(h.axes[i].widths, answers[i].ravel())
assert widths.broadcast()[i] == approx(full_answers[i])
assert widths[i] == approx(answers[i])
assert widths.T[i] == approx(answers[i].T)
assert widths.ravel()[i] == approx(answers[i].ravel())
assert h.axes[i].widths == approx(answers[i].ravel())


def test_axis_misconstuct():
Expand Down
56 changes: 28 additions & 28 deletions tests/test_numpy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def test_histogram1d(a, opt):
h1, e1 = np.histogram(v, **opt)
h2, e2 = bh.numpy.histogram(v, **opt)

np.testing.assert_array_almost_equal(e1, e2)
np.testing.assert_array_equal(h1, h2)
assert e1 == approx(e2)
assert h1 == approx(h2)

opt = copy.deepcopy(opt)
opt["density"] = True

h1, e1 = np.histogram(v, **opt)
h2, e2 = bh.numpy.histogram(v, **opt)

np.testing.assert_array_almost_equal(e1, e2)
np.testing.assert_array_almost_equal(h1, h2)
assert e1 == approx(e2)
assert h1 == approx(h2)


@pytest.mark.parametrize("a", inputs_1d)
Expand All @@ -71,11 +71,11 @@ def test_histogram1d_object(a, opt):
bh_h2 = bh.numpy.histogram(v, **bh_opt)
h2, e2 = bh_h2.to_numpy()

np.testing.assert_array_almost_equal(e1, e2)
np.testing.assert_array_equal(h1, h2)
assert e1 == approx(e2)
assert h1 == approx(h2)

# Ensure reducible
assert bh_h2[:5].values() == pytest.approx(h1[:5])
assert bh_h2[:5].values() == approx(h1[:5])

opt = copy.deepcopy(opt)
opt["density"] = True
Expand All @@ -94,16 +94,16 @@ def test_histogram2d():
h1, e1x, e1y = np.histogram2d(x, y)
h2, e2x, e2y = bh.numpy.histogram2d(x, y)

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert h1 == approx(h2)

h1, e1x, e1y = np.histogram2d(x, y, density=True)
h2, e2x, e2y = bh.numpy.histogram2d(x, y, density=True)

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_almost_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert h1 == approx(h2)


def test_histogram2d_object():
Expand All @@ -114,9 +114,9 @@ def test_histogram2d_object():
bh_h2 = bh.numpy.histogram2d(x, y, histogram=bh.Histogram)
h2, e2x, e2y = bh_h2.to_numpy()

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert h1 == approx(h2)

with pytest.raises(KeyError):
bh.numpy.histogram2d(x, y, density=True, histogram=bh.Histogram)
Expand All @@ -130,18 +130,18 @@ def test_histogramdd():
h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z])
h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z])

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_almost_equal(e1z, e2z)
np.testing.assert_array_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert e1z == approx(e2z)
assert h1 == approx(h2)

h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z], density=True)
h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z], density=True)

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_almost_equal(e1z, e2z)
np.testing.assert_array_almost_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert e1z == approx(e2z)
assert h1 == approx(h2)


def test_histogramdd_object():
Expand All @@ -153,10 +153,10 @@ def test_histogramdd_object():
bh_h2 = bh.numpy.histogramdd([x, y, z], histogram=bh.Histogram)
h2, (e2x, e2y, e2z) = bh_h2.to_numpy(dd=True)

np.testing.assert_array_almost_equal(e1x, e2x)
np.testing.assert_array_almost_equal(e1y, e2y)
np.testing.assert_array_almost_equal(e1z, e2z)
np.testing.assert_array_equal(h1, h2)
assert e1x == approx(e2x)
assert e1y == approx(e2y)
assert e1z == approx(e2z)
assert h1 == approx(h2)

with pytest.raises(KeyError):
bh.numpy.histogramdd([x, y, z], density=True, histogram=bh.Histogram)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np
import pytest
from numpy.testing import assert_almost_equal, assert_array_equal
from pytest import approx

import boost_histogram as bh

Expand Down Expand Up @@ -86,7 +86,7 @@ def test_axes(axis, args, opts, copy_fn):
orig = axis(*args, **opts)
new = copy_fn(orig)
assert new == orig
np.testing.assert_array_equal(new.centers, orig.centers)
assert new.centers == approx(orig.centers)


@pytest.mark.parametrize("axis,args,opts", axes_creations)
Expand All @@ -97,7 +97,7 @@ def test_metadata_str(axis, args, opts, copy_fn):
assert new.metadata == orig.metadata
new.metadata = orig.metadata
assert new == orig
np.testing.assert_array_equal(new.centers, orig.centers)
assert new.centers == approx(orig.centers)


# Special test: Deepcopy should change metadata id, copy should not
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_storage(benchmark, copy_fn, storage, extra):
hist.fill(x, weight=np.arange(2 * n + 4) + 1, sample=np.arange(2 * n + 4) + 1)

new = benchmark(copy_fn, hist)
assert_array_equal(hist.view(True), new.view(True))
assert hist.view(True) == approx(new.view(True))
assert new == hist


Expand Down Expand Up @@ -213,8 +213,8 @@ def test_pickle_transforms(mod, copy_fn):
ax3 = bh.axis.Regular(100, 1, 100, transform=bh.axis.transform.log)

assert ax1 == ax2
assert_array_equal(ax1.centers, ax2.centers)
assert_almost_equal(ax2.centers, ax3.centers, decimal=10)
assert ax1.centers == approx(ax2.centers)
assert ax2.centers == approx(ax3.centers)


def test_hist_axes_reference(copy_fn):
Expand Down