From 86c520115154ac44acf7c599c7d01b5989116209 Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Thu, 7 Dec 2023 13:03:58 +0100 Subject: [PATCH] Fix use of removed array rules in test (#952) This fixes the failing test and two performance warnings raised in the tests. Like @henryiii suggested, these changes were needed in one particular test which relied on broadcasting rules (in this case they were narrowing rules) that have been removed from numpy. Previously, it was possible to do this ``` a = np.zeroes(2) b = np.ones(1) a[0] += b # add sequence of length 1 to scalar ``` --- src/iminuit/util.py | 2 +- tests/test_cost.py | 12 ++++++------ tests/test_minimize.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/iminuit/util.py b/src/iminuit/util.py index 8f62ac03a..fa3b1af4b 100644 --- a/src/iminuit/util.py +++ b/src/iminuit/util.py @@ -236,7 +236,7 @@ def _set(self, i: int, arg: UserBound) -> None: state.set_error(i, err) -def _normalize_limit(lim: UserBound) -> Tuple[float, float]: +def _normalize_limit(lim: Optional[Iterable]) -> Tuple[float, float]: if lim is None: return (-np.inf, np.inf) a, b = lim diff --git a/tests/test_cost.py b/tests/test_cost.py index 05a1e4073..b683b6210 100644 --- a/tests/test_cost.py +++ b/tests/test_cost.py @@ -1164,7 +1164,7 @@ def grad2(x, b, a): return g def model3(x, c): - return c + return c * np.ones_like(x) def grad3(x, c): return np.ones((1, len(x))) @@ -1201,7 +1201,7 @@ def grad3(x, c): a = 2 b = 3 ref = np.zeros(2) - ref[0] += lsq1.grad(a) + ref[0] += lsq1.grad(a)[0] ref[[1, 0]] += lsq2.grad(b, a) assert_allclose(lsq12.grad(a, b), ref) @@ -1214,9 +1214,9 @@ def grad3(x, c): a = 2 b = 3 ref = np.zeros(2) - ref[0] += lsq1.grad(a) + ref[0] += lsq1.grad(a)[0] ref[[1, 0]] += lsq2.grad(b, a) - ref[0] += lsq1.grad(a) + ref[0] += lsq1.grad(a)[0] assert_allclose(lsq121.grad(a, b), ref) lsq312 = lsq3 + lsq12 @@ -1229,8 +1229,8 @@ def grad3(x, c): b = 3 c = 4 ref = np.zeros(3) - ref[0] += lsq3.grad(c) - ref[1] += lsq1.grad(a) + ref[0] += lsq3.grad(c)[0] + ref[1] += lsq1.grad(a)[0] ref[[2, 1]] += lsq2.grad(b, a) assert_allclose(lsq312.grad(c, a, b), ref) diff --git a/tests/test_minimize.py b/tests/test_minimize.py index ea5d50e3c..07e51d67c 100644 --- a/tests/test_minimize.py +++ b/tests/test_minimize.py @@ -63,9 +63,9 @@ def rosen(par): def test_disp(capsys): - minimize(lambda x: x**2, 0) + minimize(lambda x: np.sum(x**2), 0) assert capsys.readouterr()[0] == "" - minimize(lambda x: x**2, 0, options={"disp": True}) + minimize(lambda x: np.sum(x**2), 0, options={"disp": True}) assert capsys.readouterr()[0] != "" @@ -115,7 +115,7 @@ class Fcn: def __call__(self, x): self.n += 1 - return x**2 + 1e-2 * (self.n % 3) + return np.sum(x**2 + 1e-2 * (self.n % 3)) r = minimize(Fcn(), [1], options={"maxfun": 100000000}) assert not r.success