Skip to content

Commit

Permalink
change var name to options, update based on this, add to notes, remov…
Browse files Browse the repository at this point in the history
…e test
  • Loading branch information
lvarriano committed Dec 5, 2024
1 parent 8703298 commit 53d7e3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
27 changes: 15 additions & 12 deletions src/iminuit/minuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,11 @@ def scipy(
the tolerance :attr:`tol` has no effect on SciPy minimizers.
You can specify convergence tolerance and other options for the SciPy minimizers
through the `options` parameter.
through the `options` parameter. Note that providing the SciPy options
`"maxiter"`, `"maxfev"`, and/or `"maxfun"` (depending on the minimizer) takes
precedence over providing a value for `ncall`. If you want to explicitly control
the number of iterations or function evaluations for a particular SciPy minimizer,
you should provide values for all of its relevant options.
"""
try:
from scipy.optimize import (
Expand All @@ -1051,9 +1055,6 @@ def scipy(
if ncall is None:
ncall = self._migrad_maxcall()

if type(options) is not dict:
raise ValueError("options must be a dictionary")

cfree = ~np.array(self.fixed[:], dtype=bool)
cpar = np.array(self.values[:])
no_fixed_parameters = np.all(cfree)
Expand Down Expand Up @@ -1239,24 +1240,26 @@ def __call__(self, par, v):

# attempt to set default number of function evaluations if not provided
# various workarounds for API inconsistencies in scipy.optimize.minimize
added_maxiter = False
if "maxiter" not in options:
scipy_options["maxiter"] = ncall
options["maxiter"] = ncall
added_maxiter = True
if method in (
"Nelder-Mead",
"Powell",
):
if "maxfev" not in options:
scipy_options["maxfev"] = ncall
options["maxfev"] = ncall

if "maxiter" not in options:
del scipy_options["maxiter"]
if added_maxiter:
del options["maxiter"]

if method in ("L-BFGS-B", "TNC"):
if "maxfun" not in options:
scipy_options["maxfun"] = ncall
options["maxfun"] = ncall

if "maxiter" not in options:
del scipy_options["maxiter"]
if added_maxiter:
del options["maxiter"]

if method in ("COBYLA", "SLSQP", "trust-constr") and constraints is None:
constraints = ()
Expand All @@ -1276,7 +1279,7 @@ def __call__(self, par, v):
hess=hess,
hessp=hessp,
constraints=constraints,
options=scipy_options,
options=options,
)
if self.print_level > 0:
print(r)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_scipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,6 @@ def test_bad_constraint():
m.scipy(constraints=[{}])


def test_bad_options():
m = Minuit(fcn, a=1, b=2)
with pytest.raises(ValueError):
m.scipy(options=[])


def test_high_print_level(capsys):
m = Minuit(fcn, a=1, b=2)
m.scipy()
Expand Down

0 comments on commit 53d7e3b

Please sign in to comment.