[SciPy-user] Re: SciPy-user Digest, Vol 22, Issue 3

aurelien.gourrier at free.fr aurelien.gourrier at free.fr
Wed Jun 29 08:53:30 CDT 2005


Hi Steve,

I tried to use mpfit as it looks quite good but could get very far... I did the
following (extremely) basic test (have to start somewhere...):

----
import mpfit2
import Numeric
from Numeric import *
from RandomArray import randint

def linear(p,x=None,y=None,fjac=None,err=None):
    line = p[0]+p[1]*x
    status = 0
    return [status, (y-line)]

if __name__ == '__main__':
    #simulated experimental data
    x = Numeric.arange(10.)
    p = [3.,2.]
    y = (p[0] + p[1]*x)+ randint(1,3,(10,))/10.

    #estimated initial params
    p0 = [5.7, 1.2]

    #actual call to mpfit
    fa = {'x':x,'y':y}
    m = mpfit2.mpfit('linear',p0, functkw=fa, parinfo=None)
    print 'status', m.status
    if (m.status <= 0): print 'error message', m.errmsg
    print 'parameters = ',m.params
---

but I get blocked at line 957 of your modified code with the following message

---
Traceback (most recent call last):
  File "D:\Boulot\Software\Python\My_modules\MpfitTest01.py", line 28, in
-toplevel-
    m = mpfit2.mpfit('linear',p0, functkw=fa, parinfo=None)
  File "C:\Program Files\Logiciels\Python24\lib\site-packages\mpfit2.py", line
960, in __init__
    whatever1 = ((limited[:,0] & (xall < limits[:,0])) |
TypeError: list indices must be integers
---

I addded several print lines to try and get a bit further with this and I have :

limited = [0, 0]
limits = [0.0, 0.0]

which means, e.g. that limited[:,0] cannot give a correct output...

Would you (or anyone else) be able to tell me if I am missing something out in
my script ?

Cheers,

Aure



Selon Steve Schmerler <elcorto at gmx.net>:

> Hi,
>
> or you can use mpfit for constrained optimization which is a (quite
> fast) pure Python implementation of MINPACK's LMFIT routine.
>
> http://cars9.uchicago.edu/software/python/index.html -> mpfit.py
>
>
http://www.scipy.org/mailinglists/mailman?fn=scipy-user/2005-February/004104.html
>
>
> I did some little bugfixes in this code (see attachment) and used it for
> some time without problems.
>
> cheers,
> steve
>
>
> Christian Kristukat wrote:
> > aurelien.gourrier at free.fr wrote:
> >
> >> Hi all,
> >>
> >> Can anyone tell me if there is a possibility to use constraints on the
> >> leastsq
> >> method ? I need to do some bounded minimization of gaussians or
> >> lorentzians on
> >> 1-D data and couldn't find any way out so far...
> >
> >
> > One method I'm using to add simple bounds is to map the parameters to
> > some limits before calculating the residuals.
> >
> > def residuals(a):
> >     # mapping +/-inf to +/-1
> >     t = a/(abs(a)+1.0)
> >
> >     # amin/amax are 1D arrays containing the bounds
> >     # map -1/+1 to amin/amax
> >     a = (amin+amax)/2.0+(amax-amin)/2.0*t
> >
> >     # avealuate the function
> >     newy = func(a)
> >
> >     return  y-newy
> >
> > Regards, Christian
> >
> > _______________________________________________
> > SciPy-user mailing list
> > SciPy-user at scipy.net
> > http://www.scipy.net/mailman/listinfo/scipy-user
> >
> >
>




More information about the SciPy-user mailing list