diff --git a/README.md b/README.md index 1d344293e..905655391 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ histograms can be plotted via any compatible library, such as [mplhep][]. * `.kind`: Either `bh.Kind.COUNT` or `bh.Kind.MEAN`, depending on storage * `.sum(flow=False)`: The total count of all bins * `.project(ax1, ax2, ...)`: Project down to listed axis (numbers). Can also reorder axes. + * `.compare(second_hist)`: Compare the histogram with another histogram * `.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) * `.values(flow=False)`: Get a view on the values (counts or means, depending on storage) diff --git a/src/boost_histogram/_internal/hist.py b/src/boost_histogram/_internal/hist.py index 80d27c4bf..801065166 100644 --- a/src/boost_histogram/_internal/hist.py +++ b/src/boost_histogram/_internal/hist.py @@ -1,6 +1,7 @@ import collections.abc import copy import logging +import re import threading import typing import warnings @@ -337,6 +338,20 @@ def ndim(self) -> int: """ return self._hist.rank() + def compare(self, hist2) -> bool: + if np.allclose(self.view().shape, hist2.view().shape): + if np.allclose(self.view(), hist2.view()): + if np.allclose(self.variances(), hist2.variances()): + if ( + re.search("(?<=storage=).*", str(self.view))[0].split("(")[0] + == re.search("(?<=storage=).*", str(hist2.view))[0].split("(")[ + 0 + ] + ): + if list(self.axes) == list(hist2.axes): + return True + return False + def view( self, flow: bool = False ) -> Union["np.typing.NDArray[Any]", WeightedSumView, WeightedMeanView, MeanView]: