Skip to content

Commit

Permalink
fix a test that requires scipy and raise error if invalid value is us…
Browse files Browse the repository at this point in the history
…ed for use_pdf
  • Loading branch information
HDembinski committed Jan 31, 2024
1 parent 1027417 commit b540e14
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/iminuit/cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,8 +1502,14 @@ def __init__(self, n, xe, model, verbose, grad, use_pdf):
self._pred_impl = self._pred_approximate
elif use_pdf == "numerical":
self._pred_impl = self._pred_numerical
else:
elif use_pdf == "":
self._pred_impl = self._pred_cdf
else:
msg = (
f"use_pdf={use_pdf} is not understood, "
"allowed values are 'approximate' and 'numerical'"
)
raise ValueError(msg)

super().__init__(_model_parameters(model), n, xe, verbose)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ def test_BinnedNLL_pickle():

@pytest.mark.parametrize("use_pdf", ["approximate", "numerical"])
def test_BinnedNLL_with_pdf(use_pdf):
if use_pdf == "numerical":
pytest.importorskip("scipy")

xe = np.array([0, 0.1, 0.2, 0.3])
n = [1, 2, 3]
c = BinnedNLL(n, xe, norm_pdf, use_pdf=use_pdf)
Expand All @@ -762,6 +765,11 @@ def test_BinnedNLL_with_pdf(use_pdf):
assert_allclose(c.prediction(par), ref, rtol=1e-3)


def test_BinnedNLL_use_pdf_bad_value():
with pytest.raises(ValueError):
BinnedNLL([1, 2], [1, 2, 3], norm_pdf, use_pdf="foo")


@pytest.mark.parametrize("use_pdf", ["approximate", "numerical"])
def test_BinnedNLL_with_pdf_3D(use_pdf):
xe = (np.array([0, 0.1]), np.array([0.1, 0.3, 0.4]), np.array([0, 0.1, 0.2, 0.3]))
Expand Down

0 comments on commit b540e14

Please sign in to comment.