Built-in model - how to limit number of function calls?

205 views
Skip to first unread message

Ben Shepherd

unread,
Feb 11, 2016, 3:15:56 AM2/11/16
to lmfit-py

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.)

Ben Shepherd

unread,
Feb 11, 2016, 4:06:38 AM2/11/16
to lmfit-py
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


This isn't a big problem (there's no reason to break out on the first iteration), but it might trip you up on testing.

Ben

PS. I found the link to the Group on the docs site. Probably just me being an idiot and not finding it the first time.

Matt Newville

unread,
Feb 11, 2016, 1:07:26 PM2/11/16
to Ben Shepherd, lmfit-py
HI Ben,



On Thu, Feb 11, 2016 at 3:06 AM, Ben Shepherd <bjash...@gmail.com> wrote:
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})



Great -- glad that's working for you.  We should probably list the "fit_kws you might want to use" in the docs.
 
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?
 
Thanks,

--Matt


Ben Shepherd

unread,
Feb 12, 2016, 2:15:57 AM2/12/16
to lmfit-py, bjash...@gmail.com, newv...@cars.uchicago.edu
On Thursday, February 11, 2016 at 6:07:26 PM UTC, Matt Newville wrote:
Hm, that seems like it should work.  I can't tell what's going on.  Do you have a simple example?
 

Here's my minimal example. It fails with the above error using Python 3.4.3 and lmfit 0.9.3 (scipy 0.15.1, numpy 1.9.2) on a Windows 7 64-bit machine.

import lmfit

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)

def fitCallback(params, i, resid, *args, **kws):

    if i > 1:

        return True # abort fit

    else:

        return None

out = mod.fit(y, initCoeffs, x=x, iter_cb=fitCallback)
Reply all
Reply to author
Forward
0 new messages