Skip to content

Commit 1994306

Browse files
committed
feat : add more tests
- plot/test: remove None options - test: add dedicated test for uproot/hist with various flow scheme - test: add type check (numpy/hist with flow=False) [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent d21f64d commit 1994306

File tree

6 files changed

+93
-22
lines changed

6 files changed

+93
-22
lines changed

src/mplhep/plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from collections import OrderedDict, namedtuple
55
from typing import TYPE_CHECKING, Any, Union
66

7+
import hist as Hist
78
import matplotlib as mpl
89
import matplotlib.pyplot as plt
910
import numpy as np
10-
import hist as Hist
1111
from matplotlib.offsetbox import AnchoredText
1212
from matplotlib.transforms import Bbox
1313
from mpl_toolkits.axes_grid1 import axes_size, make_axes_locatable
@@ -132,7 +132,7 @@ def histplot(
132132
Attempts to draw x-axis ticks coinciding with bin boundaries if feasible.
133133
ax : matplotlib.axes.Axes, optional
134134
Axes object (if None, last one is fetched or one is created)
135-
flow : str, optional {None, "show", "sum", "hint"}
135+
flow : str, optional { "show", "sum", "hint"}
136136
Whether plot the under/overflow bin. If "show", add additional under/overflow bin. If "sum", add the under/overflow bin content to first/last bin.
137137
**kwargs :
138138
Keyword arguments passed to underlying matplotlib functions -
@@ -597,7 +597,7 @@ def hist2dplot(
597597
Colorbar maximum.
598598
ax : matplotlib.axes.Axes, optional
599599
Axes object (if None, last one is fetched or one is created)
600-
flow : str, optional {None, "show", "sum","hint"}
600+
flow : str, optional {"show", "sum","hint"}
601601
Whether plot the under/overflow bin. If "show", add additional under/overflow bin. If "sum", add the under/overflow bin content to first/last bin. "hint" would highlight the bins with under/overflow contents
602602
**kwargs :
603603
Keyword arguments passed to underlying matplotlib function - pcolormesh.
-6.79 KB
Loading
42.6 KB
Loading
17.1 KB
Loading
42.1 KB
Loading

tests/test_basic.py

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,37 +115,108 @@ def test_histplot_density():
115115
def test_histplot_flow():
116116
np.random.seed(0)
117117
h = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
118+
h.fill(np.random.normal(10, 3, 400))
119+
fig, axs = plt.subplots(1, 3, sharey=True, figsize=(15, 5))
120+
axs = axs.flatten()
118121

122+
hep.histplot(h, ax=axs[0], flow="hint")
123+
hep.histplot(h, ax=axs[1], flow="show")
124+
hep.histplot(h, ax=axs[2], flow="sum")
125+
126+
axs[0].set_title("Default(hint)", fontsize=18)
127+
axs[1].set_title("Show", fontsize=18)
128+
axs[2].set_title("Sum", fontsize=18)
129+
return fig
130+
131+
132+
@pytest.mark.mpl_image_compare(style="default")
133+
def test_histplot_hist_flow():
134+
np.random.seed(0)
135+
entries = np.random.normal(10, 3, 400)
136+
h = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
119137
h2 = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
120138
h3 = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
121-
122-
one_side = np.random.normal(10, 3, 400)
123-
one_side = one_side[one_side > 5]
124-
no_flow = np.random.normal(10, 3, 400)
125-
no_flow = no_flow[(no_flow < 15) & (no_flow > 5)]
126-
h.fill(np.random.normal(10, 3, 400))
127-
h2.fill(one_side)
128-
h3.fill(no_flow)
129-
fig, axs = plt.subplots(1, 3, sharey=True, figsize=(15, 5))
139+
h4 = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
140+
141+
h.fill(entries)
142+
h2.fill(entries[entries < 15])
143+
h3.fill(entries[entries > 5])
144+
h4.fill(entries[(entries > 5) & (entries < 15)])
145+
import uproot
146+
147+
f = uproot.recreate("flow_th1.root")
148+
f["h"] = h
149+
f["h2"] = h2
150+
f["h3"] = h3
151+
f["h4"] = h4
152+
fig, axs = plt.subplots(2, 2, sharey=True, figsize=(10, 10))
130153
axs = axs.flatten()
131-
hep.histplot(h3, ax=axs[2], flow="hint")
154+
155+
hep.histplot(h, ax=axs[0], flow="show")
156+
hep.histplot(h2, ax=axs[1], flow="show")
132157
hep.histplot(h3, ax=axs[2], flow="show")
133-
hep.histplot(h3, ax=axs[2], flow="sum")
134-
hep.histplot(h2, ax=axs[1], flow="hint")
158+
hep.histplot(h4, ax=axs[3], flow="show")
159+
160+
axs[0].set_title("Two-side overflow", fontsize=18)
161+
axs[1].set_title("Left-side overflow", fontsize=18)
162+
axs[2].set_title("Right-side overflow", fontsize=18)
163+
axs[3].set_title("No overflow", fontsize=18)
164+
fig.subplots_adjust(hspace=0.2, wspace=0.2)
165+
axs[0].legend()
166+
return fig
167+
168+
169+
@pytest.mark.mpl_image_compare(style="default")
170+
def test_histplot_uproot_flow():
171+
np.random.seed(0)
172+
entries = np.random.normal(10, 3, 400)
173+
import uproot
174+
175+
f = uproot.open("flow_th1.root")
176+
h = f["h"]
177+
h2 = f["h2"]
178+
h3 = f["h3"]
179+
h4 = f["h4"]
180+
181+
fig, axs = plt.subplots(2, 2, sharey=True, figsize=(10, 10))
182+
axs = axs.flatten()
183+
184+
hep.histplot(h, ax=axs[0], flow="show")
135185
hep.histplot(h2, ax=axs[1], flow="show")
136-
hep.histplot(h2, ax=axs[1], flow="sum")
137-
hep.histplot(h, ax=axs[0], flow="hint", label="hint")
138-
hep.histplot(h, ax=axs[0], flow="show", label="show")
139-
hep.histplot(h, ax=axs[0], flow="sum", label="sum")
186+
hep.histplot(h3, ax=axs[2], flow="show")
187+
hep.histplot(h4, ax=axs[3], flow="show")
140188

141189
axs[0].set_title("Two-side overflow", fontsize=18)
142-
axs[1].set_title("One-side overflow", fontsize=18)
143-
axs[2].set_title("No overflow", fontsize=18)
144-
fig.subplots_adjust(hspace=0.1, wspace=0.1)
190+
axs[1].set_title("Left-side overflow", fontsize=18)
191+
axs[2].set_title("Right-side overflow", fontsize=18)
192+
axs[3].set_title("No overflow", fontsize=18)
193+
fig.subplots_adjust(hspace=0.2, wspace=0.2)
145194
axs[0].legend()
146195
return fig
147196

148197

198+
@pytest.mark.mpl_image_compare(style="default")
199+
def test_histplot_type_flow():
200+
np.random.seed(0)
201+
entries = np.random.normal(10, 3, 400)
202+
203+
histh = hist.Hist(
204+
hist.axis.Regular(20, 5, 15, name="x", flow=False), hist.storage.Weight()
205+
)
206+
nph, bins = np.histogram(entries, bins=20, range=(5, 15))
207+
histh.fill(entries)
208+
209+
fig, axs = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(10, 5))
210+
axs = axs.flatten()
211+
212+
hep.histplot(histh, ax=axs[0], flow="hint")
213+
hep.histplot(nph, bins, ax=axs[1], flow="hint")
214+
215+
axs[0].set_title("hist, noflow bin", fontsize=18)
216+
axs[1].set_title("numpy hist", fontsize=18)
217+
return fig
218+
219+
149220
@pytest.mark.mpl_image_compare(style="default", remove_text=True)
150221
def test_histplot_multiple():
151222
np.random.seed(0)

0 commit comments

Comments
 (0)