Skip to content

[Python][UHI] Extend TH1 equality operator to consider additional properties #19061

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ def _eq(self, other):
isinstance(other, type(self))
and _shape(self) == _shape(other)
and np.array_equal(_values_default(self), _values_default(other))
and self.kind == other.kind
and np.array_equal(_get_sum_of_weights_squared(self), _get_sum_of_weights_squared(other))
and all(a == b for a, b in zip(self.axes, other.axes))
)


Expand Down Expand Up @@ -559,11 +562,12 @@ def _get_sum_of_weights_squared(self) -> np.typing.NDArray[Any]: # noqa: F821
import numpy as np

shape = _shape(self, include_flow_bins=False)
return np.frombuffer(
sumw2_arr = np.frombuffer(
self.GetSumw2().GetArray(),
dtype=self.GetSumw2().GetArray().typecode,
count=self.GetSumw2().GetSize(),
).reshape(shape, order="F")[tuple([slice(1, -1)] * len(shape))]
)
return sumw2_arr[tuple([slice(1, -1)] * len(shape))].reshape(shape, order="F") if sumw2_arr.size > 0 else sumw2_arr


values_func_dict: dict[str, Callable] = {
Expand Down
30 changes: 30 additions & 0 deletions bindings/pyroot/pythonizations/test/uhi_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,36 @@ def test_statistics_slice(self, hist_setup):
assert hist_setup.GetStdDev() == pytest.approx(sliced_hist.GetStdDev(), rel=10e-5)
assert hist_setup.GetMean() == pytest.approx(sliced_hist.GetMean(), rel=10e-5)

def test_equality_operator(self, hist_setup):
if _special_setting(hist_setup) or isinstance(hist_setup, (ROOT.TH1C, ROOT.TH2C, ROOT.TH3C)):
pytest.skip("This feature cannot be tested here")

assert hist_setup == hist_setup[...]

cloned_histograms = {
"content": hist_setup.Clone(),
"error": hist_setup.Clone(),
"weight": hist_setup.Clone(),
"axis": hist_setup.Clone(),
}
assert all(hist_setup == other for other in cloned_histograms.values())

# Change the content of a bin
cloned_histograms["content"].SetBinContent(1, 100)
assert hist_setup != cloned_histograms["content"]

# Change the error of a bin
cloned_histograms["error"].SetBinError(1, 10)
assert hist_setup != cloned_histograms["error"]

# Change the weight of a bin
cloned_histograms["weight"].AddBinContent(1, 100)
assert hist_setup != cloned_histograms["weight"]

# Change the x axis
cloned_histograms["axis"].GetXaxis().Set(10, 1, 5)
assert hist_setup != cloned_histograms["axis"]


if __name__ == "__main__":
pytest.main(args=[__file__])
Loading