Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comparison function as histogram operation #762

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 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 Down Expand Up @@ -337,6 +338,20 @@ def ndim(self) -> int:
"""
return self._hist.rank()

def compare(self, hist2) -> bool:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def compare(self, hist2) -> bool:
def compare(self, hist2: "Histogram") -> 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]:
Expand Down