Skip to content

Commit

Permalink
Fix use of removed array rules in test (#952)
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
HDembinski committed Dec 7, 2023
1 parent ef2e0a7 commit ae1a255
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/iminuit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] != ""


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae1a255

Please sign in to comment.