Skip to content

Commit

Permalink
Correct init_vals (#821)
Browse files Browse the repository at this point in the history
* have result.init_vals be external (user) values, add and use result._init_vals_internal for initial internal values

* remove deprecated keywords for dual_annealing

* avoid pytest deprecation warnings in NIST Strd tests
  • Loading branch information
newville committed Nov 8, 2022
1 parent b58c139 commit 60eedb0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
16 changes: 9 additions & 7 deletions lmfit/minimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ def prepare_fit(self, params=None):
# and which are defined expressions.
result.var_names = [] # note that this *does* belong to self...
result.init_vals = []
result._init_vals_internal = []
result.params.update_constraints()
result.nfev = 0
result.call_kws = {}
Expand All @@ -725,7 +726,8 @@ def prepare_fit(self, params=None):
par.vary = False
if par.vary:
result.var_names.append(name)
result.init_vals.append(par.setup_bounds())
result._init_vals_internal.append(par.setup_bounds())
result.init_vals.append(par.value)

par.init_value = par.value
if par.name is None:
Expand Down Expand Up @@ -953,11 +955,12 @@ def scalar_minimize(self, method='Nelder-Mead', params=None, max_nfev=None,
"""
result = self.prepare_fit(params=params)
result.method = method
variables = result.init_vals
variables = result._init_vals_internal
params = result.params

self.set_max_nfev(max_nfev, 2000*(result.nvarys+1))
fmin_kws = dict(method=method, options={'maxiter': 2*self.max_nfev})
# fmin_kws = dict(method=method, options={'maxfun': 2*self.max_nfev})
fmin_kws.update(self.kws)

if 'maxiter' in kws:
Expand Down Expand Up @@ -1661,7 +1664,7 @@ def leastsq(self, params=None, max_nfev=None, **kws):
result = self.prepare_fit(params=params)
result.method = 'leastsq'
result.nfev -= 2 # correct for "pre-fit" initialization/checks
variables = result.init_vals
variables = result._init_vals_internal

# note we set the max number of function evaluations here, and send twice that
# value to the solver so it essentially never stops on its own
Expand Down Expand Up @@ -1779,7 +1782,7 @@ def basinhopping(self, params=None, max_nfev=None, **kws):
basinhopping_kws.update(self.kws)
basinhopping_kws.update(kws)

x0 = result.init_vals
x0 = result._init_vals_internal
result.call_kws = basinhopping_kws
try:
ret = scipy_basinhopping(self.penalty, x0, **basinhopping_kws)
Expand Down Expand Up @@ -2072,7 +2075,7 @@ def ampgo(self, params=None, max_nfev=None, **kws):
ampgo_kws.update(self.kws)
ampgo_kws.update(kws)

values = result.init_vals
values = result._init_vals_internal
result.method = f"ampgo, with {ampgo_kws['local']} as local solver"
result.call_kws = ampgo_kws
try:
Expand Down Expand Up @@ -2212,8 +2215,7 @@ def dual_annealing(self, params=None, max_nfev=None, **kws):
result.method = 'dual_annealing'
self.set_max_nfev(max_nfev, 200000*(result.nvarys+1))

da_kws = dict(maxiter=1000, local_search_options={},
initial_temp=5230.0, restart_temp_ratio=2e-05,
da_kws = dict(initial_temp=5230.0, restart_temp_ratio=2e-05,
visit=2.62, accept=-5.0, maxfun=2*self.max_nfev,
seed=None, no_local_search=False, callback=None,
x0=None)
Expand Down
57 changes: 28 additions & 29 deletions tests/test_NIST_Strd.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def build_usage():
--------
-m name of fitting method. One of:
leastsq, nelder, powell, lbfgsb, bfgs,
tnc, cobyla, slsqp, cg, newto-cg
tnc, cobyla, slsqp, cg, newton-cg
leastsq (Levenberg-Marquardt) is the default
"""
return usage
Expand Down Expand Up @@ -183,115 +183,114 @@ def RunNIST_Model(model):
out1 = NIST_Dataset(model, start='start1', plot=False, verbose=False)
out2 = NIST_Dataset(model, start='start2', plot=False, verbose=False)
assert(out1 or out2)
return out1 or out2


def test_Bennett5():
return RunNIST_Model('Bennett5')
RunNIST_Model('Bennett5')


def test_BoxBOD():
return RunNIST_Model('BoxBOD')
RunNIST_Model('BoxBOD')


def test_Chwirut1():
return RunNIST_Model('Chwirut1')
RunNIST_Model('Chwirut1')


def test_Chwirut2():
return RunNIST_Model('Chwirut2')
RunNIST_Model('Chwirut2')


def test_DanWood():
return RunNIST_Model('DanWood')
RunNIST_Model('DanWood')


def test_ENSO():
return RunNIST_Model('ENSO')
RunNIST_Model('ENSO')


def test_Eckerle4():
return RunNIST_Model('Eckerle4')
RunNIST_Model('Eckerle4')


def test_Gauss1():
return RunNIST_Model('Gauss1')
RunNIST_Model('Gauss1')


def test_Gauss2():
return RunNIST_Model('Gauss2')
RunNIST_Model('Gauss2')


def test_Gauss3():
return RunNIST_Model('Gauss3')
RunNIST_Model('Gauss3')


def test_Hahn1():
return RunNIST_Model('Hahn1')
RunNIST_Model('Hahn1')


def test_Kirby2():
return RunNIST_Model('Kirby2')
RunNIST_Model('Kirby2')


def test_Lanczos1():
return RunNIST_Model('Lanczos1')
RunNIST_Model('Lanczos1')


def test_Lanczos2():
return RunNIST_Model('Lanczos2')
RunNIST_Model('Lanczos2')


def test_Lanczos3():
return RunNIST_Model('Lanczos3')
RunNIST_Model('Lanczos3')


def test_MGH09():
return RunNIST_Model('MGH09')
RunNIST_Model('MGH09')


def test_MGH10():
return RunNIST_Model('MGH10')
RunNIST_Model('MGH10')


def test_MGH17():
return RunNIST_Model('MGH17')
RunNIST_Model('MGH17')


def test_Misra1a():
return RunNIST_Model('Misra1a')
RunNIST_Model('Misra1a')


def test_Misra1b():
return RunNIST_Model('Misra1b')
RunNIST_Model('Misra1b')


def test_Misra1c():
return RunNIST_Model('Misra1c')
RunNIST_Model('Misra1c')


def test_Misra1d():
return RunNIST_Model('Misra1d')
RunNIST_Model('Misra1d')


def test_Nelson():
return RunNIST_Model('Nelson')
RunNIST_Model('Nelson')


def test_Rat42():
return RunNIST_Model('Rat42')
RunNIST_Model('Rat42')


def test_Rat43():
return RunNIST_Model('Rat43')
RunNIST_Model('Rat43')


def test_Roszman1():
return RunNIST_Model('Roszman1')
RunNIST_Model('Roszman1')


def test_Thurber():
return RunNIST_Model('Thurber')
RunNIST_Model('Thurber')


if __name__ == '__main__':
Expand Down

0 comments on commit 60eedb0

Please sign in to comment.