Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MoAly98 committed Mar 7, 2025
1 parent 5779eab commit 356eada
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/cabinetry/visualize/plot_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ def ranking(
else:
layout = None # pragma: no cover # layout set after figure creation instead

if impacts_method not in ["np_shift", "covariance", "auxdata_shift"]:
raise ValueError(
f"The impacts method {impacts_method} provided is not supported."
+ " Valid options are (np_shift, covariance, auxdata_shift)"
)
if impacts_method == "auxdata_shift":
raise NotImplementedError(
"Plotting impacts computed by shifting auxiliary data is not supported yet."
)

impacts_color_map = {
"np_shift": ["C0", "C5"],
"covariance": ["#2CA02C", "#98DF8A"],
Expand Down Expand Up @@ -207,7 +217,7 @@ def ranking(
y_pos = np.arange(num_pars)[::-1]

pre_up, pre_down = None, None
if impacts_method == "np_shifts":
if impacts_method == "np_shift":
# pre-fit up
pre_up = ax_impact.barh(
y_pos,
Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def test_fit(mock_utils, mock_fit, mock_pulls, mock_corrmat, tmp_path):
np.asarray([[0.8]]),
np.asarray([[1.1]]),
np.asarray([[0.9]]),
impacts_method="np_shift",
),
autospec=True,
)
Expand Down
10 changes: 9 additions & 1 deletion tests/fit/test_fit_results_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ def test_RankingResults():
postfit_up = np.asarray([0.2])
postfit_down = np.asarray([-0.2])
ranking_results = fit.RankingResults(
bestfit, uncertainty, labels, prefit_up, prefit_down, postfit_up, postfit_down
bestfit,
uncertainty,
labels,
prefit_up,
prefit_down,
postfit_up,
postfit_down,
impacts_method="np_shift",
)
assert np.allclose(ranking_results.bestfit, bestfit)
assert np.allclose(ranking_results.uncertainty, uncertainty)
Expand All @@ -39,6 +46,7 @@ def test_RankingResults():
assert np.allclose(ranking_results.prefit_down, prefit_down)
assert np.allclose(ranking_results.postfit_up, postfit_up)
assert np.allclose(ranking_results.postfit_down, postfit_down)
assert ranking_results.impacts_method, "np_shift"


def test_ScanResults():
Expand Down
Binary file added tests/visualize/reference/ranking_covariance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
48 changes: 45 additions & 3 deletions tests/visualize/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,11 @@ def test_ranking(mock_draw):
impact_prefit_down,
impact_postfit_up,
impact_postfit_down,
impacts_method="np_shift",
)
folder_path = "tmp"

figure_path = pathlib.Path(folder_path) / "ranking.pdf"
figure_path = pathlib.Path(folder_path) / "ranking_np_shift.pdf"
bestfit_expected = np.asarray([0.1, 1.2])
uncertainty_expected = np.asarray([0.8, 0.2])
labels_expected = ["modeling", "staterror_a"]
Expand All @@ -530,7 +531,11 @@ def test_ranking(mock_draw):
assert np.allclose(mock_draw.call_args[0][4], impact_prefit_down[::-1])
assert np.allclose(mock_draw.call_args[0][5], impact_postfit_up[::-1])
assert np.allclose(mock_draw.call_args[0][6], impact_postfit_down[::-1])
assert mock_draw.call_args[1] == {"figure_path": figure_path, "close_figure": True}
assert mock_draw.call_args[1] == {
"figure_path": figure_path,
"close_figure": True,
"impacts_method": "np_shift",
}

# maximum parameter amount specified, do not close figure, do not save figure
_ = visualize.ranking(
Expand All @@ -548,7 +553,44 @@ def test_ranking(mock_draw):
assert np.allclose(mock_draw.call_args[0][4], impact_prefit_down[1])
assert np.allclose(mock_draw.call_args[0][5], impact_postfit_up[1])
assert np.allclose(mock_draw.call_args[0][6], impact_postfit_down[1])
assert mock_draw.call_args[1] == {"figure_path": None, "close_figure": False}
assert mock_draw.call_args[1] == {
"figure_path": None,
"close_figure": False,
"impacts_method": "np_shift",
}

# maximum parameter amount specified, do not close figure, do not save figure
# covariance impacts
ranking_results = fit.RankingResults(
bestfit,
uncertainty,
labels,
impact_prefit_up,
impact_prefit_down,
impact_postfit_up,
impact_postfit_down,
impacts_method="covariance",
)
_ = visualize.ranking(
ranking_results,
figure_folder=folder_path,
max_pars=1,
close_figure=False,
save_figure=False,
)
assert mock_draw.call_count == 3
assert np.allclose(mock_draw.call_args[0][0], bestfit_expected[0])
assert np.allclose(mock_draw.call_args[0][1], uncertainty_expected[0])
assert mock_draw.call_args[0][2] == labels_expected[0]
assert np.allclose(mock_draw.call_args[0][3], impact_prefit_up[1])
assert np.allclose(mock_draw.call_args[0][4], impact_prefit_down[1])
assert np.allclose(mock_draw.call_args[0][5], impact_postfit_up[1])
assert np.allclose(mock_draw.call_args[0][6], impact_postfit_down[1])
assert mock_draw.call_args[1] == {
"figure_path": None,
"close_figure": False,
"impacts_method": "covariance",
}


@mock.patch(
Expand Down
70 changes: 68 additions & 2 deletions tests/visualize/test_visualize_plot_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,50 @@ def test_ranking(tmp_path):
impact_postfit_up,
impact_postfit_down,
figure_path=fname,
impacts_method="np_shift",
)
# large tolerance needed here, possibly related to lack of set_tight_layout usage
assert (
compare_images("tests/visualize/reference/ranking.png", str(fname), 50) is None
compare_images("tests/visualize/reference/ranking_np_shift.png", str(fname), 50)
is None
)

# compare figure returned by function
fname = tmp_path / "fig_from_return.png"
fig.savefig(fname)
assert (
compare_images("tests/visualize/reference/ranking.png", str(fname), 50) is None
compare_images("tests/visualize/reference/ranking_np_shift.png", str(fname), 50)
is None
)

fname = tmp_path / "fig_cov.png"
fig = plot_result.ranking(
bestfit,
uncertainty,
labels,
impact_prefit_up,
impact_prefit_down,
impact_postfit_up,
impact_postfit_down,
figure_path=fname,
impacts_method="covariance",
)
# large tolerance needed here, possibly related to lack of set_tight_layout usage
assert (
compare_images(
"tests/visualize/reference/ranking_covariance.png", str(fname), 50
)
is None
)

# compare figure returned by function
fname = tmp_path / "fig_cov_from_return.png"
fig.savefig(fname)
assert (
compare_images(
"tests/visualize/reference/ranking_covariance.png", str(fname), 50
)
is None
)

# do not save figure, but close it
Expand All @@ -116,6 +149,39 @@ def test_ranking(tmp_path):
)
assert mock_close_safe.call_args_list == [((fig, None, True), {})]

with pytest.raises(
ValueError,
match="The impacts method wrong_method provided is not supported."
+ " Valid options are \\(np_shift, covariance, auxdata_shift\\)",
):
plot_result.ranking(
bestfit,
uncertainty,
labels,
impact_prefit_up,
impact_prefit_down,
impact_postfit_up,
impact_postfit_down,
close_figure=True,
impacts_method="wrong_method",
)
with pytest.raises(
NotImplementedError,
match="Plotting impacts computed by shifting auxiliary data is not "
+ "supported yet.",
):
plot_result.ranking(
bestfit,
uncertainty,
labels,
impact_prefit_up,
impact_prefit_down,
impact_postfit_up,
impact_postfit_down,
close_figure=True,
impacts_method="auxdata_shift",
)

plt.close("all")


Expand Down

0 comments on commit 356eada

Please sign in to comment.