Skip to content

Commit 9e0dad8

Browse files
committed
[Python][UHI] add tests
1 parent b3163a3 commit 9e0dad8

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

bindings/pyroot/pythonizations/test/uhi_plotting.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
from uhi.typing.plottable import PlottableHistogram
99

1010

11-
def _iterate_bins(hist):
12-
dim = hist.GetDimension()
13-
for i in range(1, hist.GetNbinsX() + 1):
14-
for j in range(1, hist.GetNbinsY() + 1) if dim > 1 else [None]:
15-
for k in range(1, hist.GetNbinsZ() + 1) if dim > 2 else [None]:
16-
yield tuple(filter(None, (i, j, k)))
11+
def _iterate_bins(hist, flow=False):
12+
ranges = [
13+
range(0 if flow else 1, hist.GetNbinsX() + (2 if flow else 1)),
14+
range(0 if flow else 1, hist.GetNbinsY() + (2 if flow else 1)) if hist.GetDimension() > 1 else [0],
15+
range(0 if flow else 1, hist.GetNbinsZ() + (2 if flow else 1)) if hist.GetDimension() > 2 else [0],
16+
]
17+
yield from ((i, j, k)[: hist.GetDimension()] for i in ranges[0] for j in ranges[1] for k in ranges[2])
1718

1819

1920
class TestTH1Plotting:
@@ -23,9 +24,15 @@ def test_isinstance_plottablehistogram(self, hist_setup):
2324
def test_histogram_values(self, hist_setup):
2425
values = np.array(
2526
[hist_setup.GetBinContent(*bin_indices) for bin_indices in _iterate_bins(hist_setup)]
26-
).reshape(_shape(hist_setup, include_flow_bins=False))
27+
).reshape(_shape(hist_setup, flow=False))
2728
assert np.array_equal(hist_setup.values(), values)
2829

30+
def test_histogram_values_flow(self, hist_setup):
31+
values_flow = np.array(
32+
[hist_setup.GetBinContent(*bin_indices) for bin_indices in _iterate_bins(hist_setup, flow=True)]
33+
).reshape(_shape(hist_setup, flow=True))
34+
assert np.array_equal(hist_setup.values(flow=True), values_flow)
35+
2936

3037
if __name__ == "__main__":
3138
raise SystemExit(pytest.main(args=[__file__]))

0 commit comments

Comments
 (0)