Package target_functions :: Module relax_fit_wrapper
[hide private]
[frames] | no frames]

Source Code for Module target_functions.relax_fit_wrapper

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2014 Edward d'Auvergne                                   # 
  4  # Copyright (C) 2006 Chris MacRaild                                           # 
  5  # Copyright (C) 2008 Sebastien Morin                                          # 
  6  # Copyright (C) 2014 Troels E. Linnet                                         # 
  7  #                                                                             # 
  8  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  9  #                                                                             # 
 10  # This program is free software: you can redistribute it and/or modify        # 
 11  # it under the terms of the GNU General Public License as published by        # 
 12  # the Free Software Foundation, either version 3 of the License, or           # 
 13  # (at your option) any later version.                                         # 
 14  #                                                                             # 
 15  # This program is distributed in the hope that it will be useful,             # 
 16  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 18  # GNU General Public License for more details.                                # 
 19  #                                                                             # 
 20  # You should have received a copy of the GNU General Public License           # 
 21  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 22  #                                                                             # 
 23  ############################################################################### 
 24   
 25  # Module docstring. 
 26  """The R1 and R2 exponential relaxation curve fitting optimisation functions.""" 
 27   
 28  # Python module imports. 
 29  from numpy import array, float64, ndarray, nan_to_num 
 30   
 31  # relax module imports. 
 32  from dep_check import C_module_exp_fn 
 33   
 34  # C modules. 
 35  if C_module_exp_fn: 
 36      from target_functions.relax_fit import back_calc_I, d2func_exp, d2func_inv, d2func_sat, dfunc_exp, dfunc_inv, dfunc_sat, func_exp, func_inv, func_sat, jacobian_chi2_exp, jacobian_chi2_inv, jacobian_chi2_sat, jacobian_exp, jacobian_inv, jacobian_sat, setup  
 37   
 38   
39 -class Relax_fit_opt:
40 """The exponential curve-fitting Python to C wrapper target function class.""" 41
42 - def __init__(self, model=None, num_params=None, values=None, errors=None, relax_times=None, scaling_matrix=None):
43 """Set up the target function class and alias the target functions. 44 45 @keyword model: The exponential curve type. This can be 'exp' for the standard two parameter exponential curve, 'inv' for the inversion recovery experiment, and 'sat' for the saturation recovery experiment. 46 @type model: str 47 @keyword num_params: The number of parameters in the model. 48 @type num_params: int 49 @keyword values: The peak intensities. 50 @type values: list of float 51 @keyword errors: The peak intensity errors. 52 @type errors: list of float 53 @keyword relax_times: The list of relaxation times. 54 @type relax_times: list of float 55 @keyword scaling_matrix: The scaling matrix in a diagonalised list form. 56 @type scaling_matrix: list of float 57 """ 58 59 # Store the args. 60 self.model = model 61 62 # Initialise the C code. 63 setup(num_params=num_params, num_times=len(relax_times), values=values, sd=errors, relax_times=relax_times, scaling_matrix=scaling_matrix) 64 65 # Alias the target functions. 66 if model == 'exp': 67 self.func = self.func_exp 68 self.dfunc = self.dfunc_exp 69 self.d2func = self.d2func_exp 70 elif model == 'inv': 71 self.func = self.func_inv 72 self.dfunc = self.dfunc_inv 73 self.d2func = self.d2func_inv 74 elif model == 'sat': 75 self.func = self.func_sat 76 self.dfunc = self.dfunc_sat 77 self.d2func = self.d2func_sat 78 79 # Alias the Jacobian C functions. 80 if model == 'exp': 81 self.jacobian = jacobian_exp 82 self.jacobian_chi2 = jacobian_chi2_exp 83 elif model == 'inv': 84 self.jacobian = jacobian_inv 85 self.jacobian_chi2 = jacobian_chi2_inv 86 elif model == 'sat': 87 self.jacobian = jacobian_sat 88 self.jacobian_chi2 = jacobian_chi2_sat
89 90
91 - def back_calc_data(self):
92 """Return the back-calculated data from the C code. 93 94 @return: The back-calculated peak intensities. 95 @rtype: list of float 96 """ 97 98 # Return the data. 99 return back_calc_I()
100 101
102 - def func_exp(self, params):
103 """Wrapper function for the C module, for converting numpy arrays. 104 105 @param params: The parameter array from the minimisation code. 106 @type params: numpy array 107 @return: The function value generated by the C module. 108 @rtype: float 109 """ 110 111 # Convert if necessary. 112 if isinstance(params, ndarray): 113 params = params.tolist() 114 115 # Call the C code. 116 chi2 = func_exp(params) 117 118 # Return the chi2 value. 119 return nan_to_num(chi2)
120 121
122 - def func_inv(self, params):
123 """Wrapper function for the C module, for converting numpy arrays. 124 125 @param params: The parameter array from the minimisation code. 126 @type params: numpy array 127 @return: The function value generated by the C module. 128 @rtype: float 129 """ 130 131 # Convert if necessary. 132 if isinstance(params, ndarray): 133 params = params.tolist() 134 135 # Call the C code. 136 chi2 = func_inv(params) 137 138 # Return the chi2 value. 139 return nan_to_num(chi2)
140 141
142 - def func_sat(self, params):
143 """Wrapper function for the C module, for converting numpy arrays. 144 145 @param params: The parameter array from the minimisation code. 146 @type params: numpy array 147 @return: The function value generated by the C module. 148 @rtype: float 149 """ 150 151 # Convert if necessary. 152 if isinstance(params, ndarray): 153 params = params.tolist() 154 155 # Call the C code. 156 chi2 = func_sat(params) 157 158 # Return the chi2 value. 159 return nan_to_num(chi2)
160 161
162 - def dfunc_exp(self, params):
163 """Wrapper function for the C module, for converting numpy arrays. 164 165 @param params: The parameter array from the minimisation code. 166 @type params: numpy array 167 @return: The gradient generated by the C module converted to numpy format. 168 @rtype: numpy float64 array 169 """ 170 171 # Convert if necessary. 172 if isinstance(params, ndarray): 173 params = params.tolist() 174 175 # Call the C code. 176 dchi2 = dfunc_exp(params) 177 178 # Return the chi2 gradient as a numpy array. 179 return array(dchi2, float64)
180 181
182 - def dfunc_inv(self, params):
183 """Wrapper function for the C module, for converting numpy arrays. 184 185 @param params: The parameter array from the minimisation code. 186 @type params: numpy array 187 @return: The gradient generated by the C module converted to numpy format. 188 @rtype: numpy float64 array 189 """ 190 191 # Convert if necessary. 192 if isinstance(params, ndarray): 193 params = params.tolist() 194 195 # Call the C code. 196 dchi2 = dfunc_inv(params) 197 198 # Return the chi2 gradient as a numpy array. 199 return array(dchi2, float64)
200 201
202 - def dfunc_sat(self, params):
203 """Wrapper function for the C module, for converting numpy arrays. 204 205 @param params: The parameter array from the minimisation code. 206 @type params: numpy array 207 @return: The gradient generated by the C module converted to numpy format. 208 @rtype: numpy float64 array 209 """ 210 211 # Convert if necessary. 212 if isinstance(params, ndarray): 213 params = params.tolist() 214 215 # Call the C code. 216 dchi2 = dfunc_sat(params) 217 218 # Return the chi2 gradient as a numpy array. 219 return array(dchi2, float64)
220 221
222 - def d2func_exp(self, params):
223 """Wrapper function for the C module, for converting numpy arrays. 224 225 @param params: The parameter array from the minimisation code. 226 @type params: numpy array 227 @return: The Hessian generated by the C module converted to numpy format. 228 @rtype: numpy float64 rank-2 array 229 """ 230 231 # Convert if necessary. 232 if isinstance(params, ndarray): 233 params = params.tolist() 234 235 # Call the C code. 236 d2chi2 = d2func_exp(params) 237 238 # Return the chi2 Hessian as a numpy array. 239 return array(d2chi2, float64)
240 241
242 - def d2func_inv(self, params):
243 """Wrapper function for the C module, for converting numpy arrays. 244 245 @param params: The parameter array from the minimisation code. 246 @type params: numpy array 247 @return: The Hessian generated by the C module converted to numpy format. 248 @rtype: numpy float64 rank-2 array 249 """ 250 251 # Convert if necessary. 252 if isinstance(params, ndarray): 253 params = params.tolist() 254 255 # Call the C code. 256 d2chi2 = d2func_inv(params) 257 258 # Return the chi2 Hessian as a numpy array. 259 return array(d2chi2, float64)
260 261
262 - def d2func_sat(self, params):
263 """Wrapper function for the C module, for converting numpy arrays. 264 265 @param params: The parameter array from the minimisation code. 266 @type params: numpy array 267 @return: The Hessian generated by the C module converted to numpy format. 268 @rtype: numpy float64 rank-2 array 269 """ 270 271 # Convert if necessary. 272 if isinstance(params, ndarray): 273 params = params.tolist() 274 275 # Call the C code. 276 d2chi2 = d2func_sat(params) 277 278 # Return the chi2 Hessian as a numpy array. 279 return array(d2chi2, float64)
280