Skip to content

Commit

Permalink
fix test that requires scipy and raise error on invalid value for use…
Browse files Browse the repository at this point in the history
…_pdf (#962)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
HDembinski and pre-commit-ci[bot] authored Jan 31, 2024
1 parent 1027417 commit 27cf3ce
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/notebooks/binned_vs_unbinned.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cells": [
"cells": [
{
"cell_type": "markdown",
"metadata": {},
Expand Down
2 changes: 1 addition & 1 deletion doc/notebooks/interactive.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cells": [
"cells": [
{
"attachments": {},
"cell_type": "markdown",
Expand Down
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 27cf3ce

Please sign in to comment.