I'm trying to fit a function to a Gaussian. This works fine for valid data (with about 20 function calls, and much faster than scipy.optimize.curve_fit), but for invalid data it returns garbage after 8000 calls. This is slowing down my data processing massively. How can I pass a "maximum_function_evaluations" parameter to the model? I couldn't see anything in the docs. (I've tried adding a callback function and returning True to abort the fit, but this seems to have no effect.)
I think I am using version 0.9.2, but it may be 0.8.3.
(Sorry for previously posting this as an issue; I didn't read the submission guidelines as you might expect. It might be useful to have this Group a bit more prominently linked from the documentation, by the way.)
if i > 1:
return True # abort fit
else:
return None
Fixed it! I upgraded to 0.9.2, and by chance happened to find this page which has some mention of maxfev. The code is:
mod = GaussianModel()
out = mod.fit(y, initCoeffs, x=x, fit_kws={'maxfev': 20})
I was previously testing this using a callback, and returning True when I wanted to abort the fit (as per the docs). This fails when you try and break out of the first iteration:
File "\Documents\Python\imageProcessTest.py", line 190, in <module>
out = mod.fit(ix, initCoeffs, x=xrd, iter_cb=fitCallback)
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\lmfit-0.9.2-py3.4.egg\lmfit\model.py", line 506, in fit
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\lmfit-0.9.2-py3.4.egg\lmfit\model.py", line 712, in fit
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\lmfit-0.9.2-py3.4.egg\lmfit\minimizer.py", line 706, in minimize
File "C:\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\site-packages\lmfit-0.9.2-py3.4.egg\lmfit\minimizer.py", line 567, in leastsq
TypeError: object of type 'numpy.float64' has no len()
My callback function is:
def fitCallback(params, i, resid, *args, **kws):if i > 1:
return True # abort fit
else:
return None
Hm, that seems like it should work. I can't tell what's going on. Do you have a simple example?
import numpy as np
from lmfit.models import GaussianModel
x = np.arange(100)y = 10 * np.exp(-((x - 50) ** 2 / (2 * 5 ** 2))) + np.random.rand(100)
mod = GaussianModel()
initCoeffs = mod.guess(y, x)
if i > 1:
return True # abort fit
else:
return None