Skip to content

Commit cf862b7

Browse files
committed
fix: test catchup
1 parent 828e9cd commit cf862b7

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/mplhep/comparison_plotters.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,28 @@ def comparison(
339339
# Create a new histogram with the same structure
340340

341341
# Use enhanced plottable histogram for flow comparison
342+
comparison_variances = (
343+
lower_uncertainties**2 if np.any(lower_uncertainties) else None
344+
)
345+
346+
# Create flow-extended edges to match the comparison values
347+
final_bins = h2_plottable.edges_1d() # Use the same method as in plot.py
348+
_flow_bin_size = max(
349+
0.05 * (final_bins[-1] - final_bins[0]), np.mean(np.diff(final_bins))
350+
)
351+
352+
# Check if underflow/overflow bins exist and extend edges accordingly
353+
flow_edges = np.copy(final_bins)
354+
h2_flow_values = h2_for_comparison.values(flow=True)
355+
if h2_flow_values[0] > 0: # Underflow exists
356+
flow_edges = np.insert(flow_edges, 0, flow_edges[0] - _flow_bin_size)
357+
if h2_flow_values[-1] > 0: # Overflow exists
358+
flow_edges = np.append(flow_edges, flow_edges[-1] + _flow_bin_size)
359+
342360
comparison_plottable = EnhancedPlottableHistogram(
343361
comparison_values,
344-
edges=h2_plottable.axes[0].edges,
345-
variances=comparison_variances if np.any(comparison_variances) else None,
362+
edges=flow_edges, # Use flow-extended edges
363+
variances=comparison_variances,
346364
kind=h2_plottable.kind,
347365
)
348366
# Regular comparison without flow bins

tests/test_mock.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ def mock_matplotlib(mocker):
3131
_get_lines.get_next_color.return_value = "next-color"
3232
ax._get_lines = _get_lines
3333

34+
# Mock shared axes methods for flow parameter support
35+
mock_shared_axes = mocker.Mock()
36+
mock_shared_axes.get_siblings.return_value = [
37+
ax
38+
] # Return list containing current axis
39+
ax.get_shared_x_axes.return_value = mock_shared_axes
40+
41+
# Mock position method
42+
mock_position = mocker.Mock()
43+
mock_position.x0 = 0.1 # Arbitrary x-position value
44+
ax.get_position.return_value = mock_position
45+
3446
mpl = mocker.patch("matplotlib.pyplot", autospec=True)
3547
mocker.patch("matplotlib.pyplot.subplots", return_value=(fig, ax))
3648

@@ -44,7 +56,7 @@ def test_simple(mock_matplotlib):
4456
bins = [0, 1, 2, 3]
4557
mh.histplot(h, bins, yerr=True, label="X", ax=ax)
4658

47-
assert len(ax.mock_calls) == 13
59+
assert len(ax.mock_calls) == 17 # Updated count due to shared axes functionality
4860

4961
ax.stairs.assert_called_once_with(
5062
values=approx([1.0, 3.0, 2.0]),
@@ -89,15 +101,15 @@ def test_histplot_real(mock_matplotlib):
89101
mh.histplot([a, b, c], bins=bins, ax=ax, yerr=True, label=["MC1", "MC2", "Data"])
90102
ax.legend()
91103
ax.set_title("Raw")
92-
assert len(ax.mock_calls) == 27
104+
assert len(ax.mock_calls) == 31 # Updated count due to shared axes functionality
93105

94106
ax.reset_mock()
95107

96108
mh.histplot([a, b], bins=bins, ax=ax, stack=True, label=["MC1", "MC2"])
97109
mh.histplot([c], bins=bins, ax=ax, yerr=True, histtype="errorbar", label="Data")
98110
ax.legend()
99111
ax.set_title("Data/MC")
100-
assert len(ax.mock_calls) == 20
112+
assert len(ax.mock_calls) == 28 # Updated count due to shared axes functionality
101113
ax.reset_mock()
102114

103115
mh.histplot(
@@ -114,7 +126,7 @@ def test_histplot_real(mock_matplotlib):
114126
)
115127
ax.legend()
116128
ax.set_title("Data/MC binwnorm")
117-
assert len(ax.mock_calls) == 20
129+
assert len(ax.mock_calls) == 28 # Updated count due to shared axes functionality
118130
ax.reset_mock()
119131

120132
mh.histplot(
@@ -131,4 +143,4 @@ def test_histplot_real(mock_matplotlib):
131143
)
132144
ax.legend()
133145
ax.set_title("Data/MC Density")
134-
assert len(ax.mock_calls) == 20
146+
assert len(ax.mock_calls) == 28 # Updated count due to shared axes functionality

0 commit comments

Comments
 (0)