Skip to content

Commit b540e14

Browse files
committed
fix a test that requires scipy and raise error if invalid value is used for use_pdf
1 parent 1027417 commit b540e14

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/iminuit/cost.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,14 @@ def __init__(self, n, xe, model, verbose, grad, use_pdf):
15021502
self._pred_impl = self._pred_approximate
15031503
elif use_pdf == "numerical":
15041504
self._pred_impl = self._pred_numerical
1505-
else:
1505+
elif use_pdf == "":
15061506
self._pred_impl = self._pred_cdf
1507+
else:
1508+
msg = (
1509+
f"use_pdf={use_pdf} is not understood, "
1510+
"allowed values are 'approximate' and 'numerical'"
1511+
)
1512+
raise ValueError(msg)
15071513

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

tests/test_cost.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,9 @@ def test_BinnedNLL_pickle():
753753

754754
@pytest.mark.parametrize("use_pdf", ["approximate", "numerical"])
755755
def test_BinnedNLL_with_pdf(use_pdf):
756+
if use_pdf == "numerical":
757+
pytest.importorskip("scipy")
758+
756759
xe = np.array([0, 0.1, 0.2, 0.3])
757760
n = [1, 2, 3]
758761
c = BinnedNLL(n, xe, norm_pdf, use_pdf=use_pdf)
@@ -762,6 +765,11 @@ def test_BinnedNLL_with_pdf(use_pdf):
762765
assert_allclose(c.prediction(par), ref, rtol=1e-3)
763766

764767

768+
def test_BinnedNLL_use_pdf_bad_value():
769+
with pytest.raises(ValueError):
770+
BinnedNLL([1, 2], [1, 2, 3], norm_pdf, use_pdf="foo")
771+
772+
765773
@pytest.mark.parametrize("use_pdf", ["approximate", "numerical"])
766774
def test_BinnedNLL_with_pdf_3D(use_pdf):
767775
xe = (np.array([0, 0.1]), np.array([0.1, 0.3, 0.4]), np.array([0, 0.1, 0.2, 0.3]))

0 commit comments

Comments
 (0)