Skip to content

Commit a2c039b

Browse files
committed
fix: remove hasattr(h, plot1d)
[pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci fix:CI
1 parent 574ba6d commit a2c039b

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/mplhep/plot.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,15 @@ def histplot(
186186
continue
187187
elif flow is None:
188188
continue
189-
elif hasattr(h, "plot1d"):
189+
elif (
190+
hasattr(h, "axes")
191+
and hasattr(h.axes[0], "traits")
192+
and hasattr(h.axes[0].traits, "underflow")
193+
):
190194
if h.axes[0].traits.underflow:
191195
underflow = underflow + h.values(flow=True)[0]
192196
if h.axes[0].traits.overflow:
193-
overflow = overflow + h.values(flow=True)[0]
197+
overflow = overflow + h.values(flow=True)[-1]
194198
else:
195199
underflow = underflow + h.values(flow=True)[0]
196200
overflow = overflow + h.values(flow=True)[-1]
@@ -199,10 +203,7 @@ def histplot(
199203
plottables = []
200204
flow_bins = final_bins
201205
for i, h in enumerate(hists):
202-
if hasattr(h, "plot1d"):
203-
value, variance = h.values().copy(), h.variances().copy()
204-
else:
205-
value, variance = h.values(), h.variances()
206+
value, variance = h.values(), h.variances()
206207
if (
207208
hasattr(h, "values")
208209
and "flow" not in inspect.getfullargspec(h.values).args
@@ -213,7 +214,13 @@ def histplot(
213214
flow = None
214215
plottables.append(Plottable(value, edges=final_bins, variances=variance))
215216
# check the original hist as flow bins
216-
elif hasattr(h, "plot1d") and len(h.values(flow=True)) < len(final_bins):
217+
elif (
218+
hasattr(h, "axes")
219+
and hasattr(h.axes[0], "traits")
220+
and hasattr(h.axes[0].traits, "underflow")
221+
and not h.axes[0].traits.underflow
222+
and not h.axes[0].traits.overflow
223+
):
217224
print(f"Warning: you don't have flow bins stored in {h}")
218225
flow = None
219226
plottables.append(Plottable(value, edges=final_bins, variances=variance))
@@ -251,6 +258,7 @@ def histplot(
251258
)
252259
plottables.append(Plottable(value, edges=flow_bins, variances=variance))
253260
elif flow == "sum":
261+
value, variance = h.values().copy(), h.variances().copy()
254262
value[0], value[-1] = (
255263
value[0] + h.values(flow=True)[0],
256264
value[-1] + h.values(flow=True)[-1],
@@ -631,7 +639,7 @@ def hist2dplot(
631639

632640
# TODO: use Histogram everywhere
633641

634-
H = h.values() if not hasattr(h, "plot1d") else h.values().copy()
642+
H = h.values()
635643
xbins, xtick_labels = get_plottable_protocol_bins(h.axes[0])
636644
ybins, ytick_labels = get_plottable_protocol_bins(h.axes[1])
637645
# Show under/overflow bins
@@ -646,9 +654,11 @@ def hist2dplot(
646654
)
647655
flow = None
648656
elif (
649-
hasattr(h, "plot1d")
650-
and len(h.values(flow=True)[0]) < len(xbins)
651-
and len(h.values(flow=True)[1]) < len(ybins)
657+
hasattr(h, "axes")
658+
and hasattr(h.axes[0], "traits")
659+
and hasattr(h.axes[0].traits, "underflow")
660+
and not h.axes[0].traits.underflow
661+
and not h.axes[0].traits.overflow
652662
):
653663
flow = None
654664
print(f"Warning: you don't have flow bins stored in {h}")
@@ -696,6 +706,7 @@ def hist2dplot(
696706
if any(h.values(flow=True)[:, -1] > 0):
697707
H = np.insert(H, (-1), np.full(np.shape(H)[1], np.nan), axis=0)
698708
elif flow == "sum":
709+
H = h.values().copy()
699710
# Sum borders
700711
H[0], H[-1] = (
701712
H[0] + h.values(flow=True)[0, 1:-1],

tests/baseline/test_inputs_bh_cat.png

-191 Bytes
Loading

tests/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_hist2dplot_flow():
310310
axs[0].set_title("Default(hint)", fontsize=18)
311311
axs[1].set_title("Show", fontsize=18)
312312
axs[2].set_title("Sum", fontsize=18)
313-
axs[3].set_title("None", fontsize=18)
313+
axs[3].set_title("None", fontsize=18)
314314
fig.subplots_adjust(hspace=0.1, wspace=0.1)
315315

316316
return fig

0 commit comments

Comments
 (0)