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