Author: tlinnet Date: Fri Aug 29 11:50:23 2014 New Revision: 25423 URL: http://svn.gna.org/viewcvs/relax?rev=25423&view=rev Log: Implemented the direct Jacobian in Python, to be independent of C-code in development fase. task #7822(https://gna.org/task/index.php?7822): Implement user function to estimate R2eff and associated errors for exponential curve fitting. Modified: trunk/specific_analyses/relax_disp/estimate_r2eff.py Modified: trunk/specific_analyses/relax_disp/estimate_r2eff.py URL: http://svn.gna.org/viewcvs/relax/trunk/specific_analyses/relax_disp/estimate_r2eff.py?rev=25423&r1=25422&r2=25423&view=diff ============================================================================== --- trunk/specific_analyses/relax_disp/estimate_r2eff.py (original) +++ trunk/specific_analyses/relax_disp/estimate_r2eff.py Fri Aug 29 11:50:23 2014 @@ -59,6 +59,38 @@ from scipy.optimize import leastsq +def func_exp_grad(params=None, times=None, values=None, errors=None): + """The gradient (Jacobian matrix) of func_exp for Co-variance calculation. + + @param params: The vector of parameter values. + @type params: numpy rank-1 float array + @keyword times: The time points. + @type times: numpy array + @param values: The measured values. + @type values: numpy array + @param errors: The standard deviation of the measured intensity values per time point. + @type errors: numpy array + @return: The Jacobian matrix with 'm' rows of function derivatives per 'n' columns of parameters. + @rtype: numpy array + """ + + # Unpack the parameter values. + r2eff = params[0] + i0 = params[1] + + # Make partial derivative, with respect to r2eff. + d_exp_d_r2eff = -i0 * times * exp(-r2eff * times) + + # Make partial derivative, with respect to i0. + d_exp_d_i0 = exp(-r2eff * times) + + # Define Jacobian as m rows with function derivatives and n columns of parameters. + jacobian_matrix_exp = transpose(array( [d_exp_d_r2eff , d_exp_d_i0] ) ) + + # Return Jacobian matrix. + return jacobian_matrix_exp + + def func_exp_chi2_grad(params=None, times=None, values=None, errors=None): """Return the gradient (Jacobian matrix) of func_exp_chi2() for exponential fit . @@ -168,7 +200,8 @@ weights = ones(errors.shape) else: # Calculate the direct exponential Jacobian matrix from C code. - jacobian_matrix_exp = transpose(asarray( jacobian(params) ) ) + #jacobian_matrix_exp = transpose(asarray( jacobian(params) ) ) + jacobian_matrix_exp = func_exp_grad(params=params, times=times, values=values, errors=errors) weights = 1. / errors**2 # Get the co-variance @@ -527,38 +560,6 @@ # Return return Kw - - - def func_exp_grad(self, params=None, times=None, values=None, errors=None): - """The gradient (Jacobian matrix) of func_exp for Co-variance calculation. - - @param params: The vector of parameter values. - @type params: numpy rank-1 float array - @keyword times: The time points. - @type times: numpy array - @param values: The measured values. - @type values: numpy array - @param errors: The standard deviation of the measured intensity values per time point. - @type errors: numpy array - @return: The Jacobian matrix with 'm' rows of function derivatives per 'n' columns of parameters. - @rtype: numpy array - """ - - # Unpack the parameter values. - r2eff = params[0] - i0 = params[1] - - # Make partial derivative, with respect to r2eff. - d_exp_d_r2eff = -i0 * times * exp(-r2eff * times) - - # Make partial derivative, with respect to i0. - d_exp_d_i0 = exp(-r2eff * times) - - # Define Jacobian as m rows with function derivatives and n columns of parameters. - jacobian_matrix_exp = transpose(array( [d_exp_d_r2eff , d_exp_d_i0] ) ) - - # Return Jacobian matrix. - return jacobian_matrix_exp def func_exp_chi2(self, params=None, times=None, values=None, errors=None): @@ -985,7 +986,7 @@ jacobian_matrix_exp = func_exp_chi2_grad(params=param_vector, times=E.times, values=E.values, errors=E.errors) else: # Use the direct Jacobian. - jacobian_matrix_exp = E.func_exp_grad(params=param_vector, times=E.times, values=E.values, errors=E.errors) + jacobian_matrix_exp = func_exp_grad(params=param_vector, times=E.times, values=E.values, errors=E.errors) # Get the co-variance if E.chi2_jacobian: