optimize_leastsq : error calling the function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

optimize_leastsq : error calling the function

Delphine t
Hello everyone,
I'm having trouble using the leastsq optimisation function, could anyone help me ?

I have a list of drillings, each one whith its own parameters. All this parameters are concatenated in the array called VARIABLES
the dictionnary dico_taille_variable associate to each drillings the size of the its vetcor "parameters" in order to deconcatenate the VARIABLES array and apply a function calculating the residuals of each drilling in the function residuals_global that I'm trying to optimize.

Residual_global calls the function residuals which is in a Class Drilling and has this aspect :

    def residuals(self,vars) :
        m=self.model(vars)
        f=interpolate.interp1d(self.depth, self.age)
        resi_age=(f(self.icemarkers_depth)-self.icemarkers_age)/self.icemarkers_sigma
        k=interpolate.interp1d(self.depth, self.gage)
        resi_gage=(k(self.gasmarkers_depth)-self.gasmarkers_age)/self.gasmarkers_sigma
        l=interpolate.interp1d(self.depth, self.Ddepth)
        resi_Ddepth=(l(self.Ddepth_depth)-self.Ddepth_Ddepth)/self.Ddepth_sigma
        resi_corr_tau=self.corr_tau
        resi_corr_a=self.corr_a
        resi_corr_LIDIE=self.corr_LIDIE
       
        self.retresi=np.concatenate((resi_age,resi_gage, resi_Ddepth, resi_corr_tau, resi_corr_a, resi_corr_LIDIE))
        return self.retresi

_______The function to optimize_____
def residuals_global(VARIABLES):
    index=0
    vecteur=[]
    RESIDUALS=np.array([])
    for key in dico_taille_variable:
        t=dico_taille_variable[key]
        vecteur=VARIABLES[index+0:index+t]
        D.residuals(vecteur)
        RESIDUALS=np.concatenate((RESIDUALS, D.retresi))
        index=index+t        
    print 'RESIDUALS', RESIDUALS
    return RESIDUALS

_______The call_________
def optimisation() :
    vars2, hess,infodict,mesg,ier=leastsq(residuals_global, VARIABLES, full_output=1)
    print mesg
    print 'vars',vars2
    print 'hess',hess
    return vars2,hess

optimisation()

_______The result_______
error Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    176             else:
    177                 filename = fname
--> 178             __builtin__.execfile(filename, *where)
/home/dtardif/Téléchargements/python/Main.py in <module>()
     64     return vars2,hess
     65
---> 66 optimisation()
     67
     68 D.sigma(hess, vars2)
/home/dtardif/Téléchargements/python/Main.py in optimisation()
     58
     59 def optimisation() :
---> 60     vars2, hess,infodict,mesg,ier=leastsq(residuals_global, VARIABLES, full_output=1)
     61     print mesg
     62     print 'vars',vars2
/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    281             maxfev = 200*(n + 1)
    282         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 283                 gtol, maxfev, epsfcn, factor, diag)
    284     else:
    285         if col_deriv:
error: Error occurred while calling the Python function named residuals_global
_______________________________


I hope I put all the data needed to understand my problem
Any idea ?

Thank's !
Reply | Threaded
Open this post in threaded view
|

Re: optimize_leastsq : error calling the function

Matt Newville
Hi,

I didn't see any other responses to your question, so I'll try.

Though the error messages you're getting aren't very helpful, I
believe the basic issue is that you're expecting several global
variables to be accessible in your objective function
residual_global().  I would recommend that you instead add
dico_taille_variable as a keyword argument to residual_global, and
pass that in with
   scipy.optimize_leastsq(residual_global, VARIABLES,
args=(dico_taille_variable,))

Also, in your objective function it wasn't clear how 'D.residuals()'
and D.retresi was supposed to be found.  I think you may want to pass
in a list or dictionary of the individual instances of your  Drilling
class.

Hope that helps,
--Matt
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user