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-2014 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  from specific_analyses.relax_fit.parameters import assemble_param_vector 
 32   
 33  # C modules. 
 34  if C_module_exp_fn: 
 35      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  
 36   
 37   
38 -class Relax_fit_opt:
39 """The exponential curve-fitting Python to C wrapper target function class.""" 40
41 - def __init__(self, model=None, num_params=None, values=None, errors=None, relax_times=None, scaling_matrix=None):
42 """Set up the target function class and alias the target functions. 43 44 @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. 45 @type model: str 46 @keyword num_params: The number of parameters in the model. 47 @type num_params: int 48 @keyword values: The peak intensities. 49 @type values: list of float 50 @keyword errors: The peak intensity errors. 51 @type errors: list of float 52 @keyword relax_times: The list of relaxation times. 53 @type relax_times: list of float 54 @keyword scaling_matrix: The scaling matrix in a diagonalised list form. 55 @type scaling_matrix: list of float 56 """ 57 58 # Store the args. 59 self.model = model 60 61 # Initialise the C code. 62 setup(num_params=num_params, num_times=len(relax_times), values=values, sd=errors, relax_times=relax_times, scaling_matrix=scaling_matrix) 63 64 # Alias the target functions. 65 if model == 'exp': 66 self.func = self.func_exp 67 self.dfunc = self.dfunc_exp 68 self.d2func = self.d2func_exp 69 elif model == 'inv': 70 self.func = self.func_inv 71 self.dfunc = self.dfunc_inv 72 self.d2func = self.d2func_inv 73 elif model == 'sat': 74 self.func = self.func_sat 75 self.dfunc = self.dfunc_sat 76 self.d2func = self.d2func_sat 77 78 # Alias the Jacobian C functions. 79 if model == 'exp': 80 self.jacobian = jacobian_exp 81 self.jacobian_chi2 = jacobian_chi2_exp 82 elif model == 'inv': 83 self.jacobian = jacobian_inv 84 self.jacobian_chi2 = jacobian_chi2_inv 85 elif model == 'sat': 86 self.jacobian = jacobian_sat 87 self.jacobian_chi2 = jacobian_chi2_sat
88 89
90 - def back_calc_data(self):
91 """Return the back-calculated data from the C code. 92 93 @return: The back-calculated peak intensities. 94 @rtype: list of float 95 """ 96 97 # Return the data. 98 return back_calc_I()
99 100
101 - def func_exp(self, params):
102 """Wrapper function for the C module, for converting numpy arrays. 103 104 @param params: The parameter array from the minimisation code. 105 @type params: numpy array 106 @return: The function value generated by the C module. 107 @rtype: float 108 """ 109 110 # Convert if necessary. 111 if isinstance(params, ndarray): 112 params = params.tolist() 113 114 # Call the C code. 115 chi2 = func_exp(params) 116 117 # Return the chi2 value. 118 return nan_to_num(chi2)
119 120
121 - def func_inv(self, params):
122 """Wrapper function for the C module, for converting numpy arrays. 123 124 @param params: The parameter array from the minimisation code. 125 @type params: numpy array 126 @return: The function value generated by the C module. 127 @rtype: float 128 """ 129 130 # Convert if necessary. 131 if isinstance(params, ndarray): 132 params = params.tolist() 133 134 # Call the C code. 135 chi2 = func_inv(params) 136 137 # Return the chi2 value. 138 return nan_to_num(chi2)
139 140
141 - def func_sat(self, params):
142 """Wrapper function for the C module, for converting numpy arrays. 143 144 @param params: The parameter array from the minimisation code. 145 @type params: numpy array 146 @return: The function value generated by the C module. 147 @rtype: float 148 """ 149 150 # Convert if necessary. 151 if isinstance(params, ndarray): 152 params = params.tolist() 153 154 # Call the C code. 155 chi2 = func_sat(params) 156 157 # Return the chi2 value. 158 return nan_to_num(chi2)
159 160
161 - def dfunc_exp(self, params):
162 """Wrapper function for the C module, for converting numpy arrays. 163 164 @param params: The parameter array from the minimisation code. 165 @type params: numpy array 166 @return: The gradient generated by the C module converted to numpy format. 167 @rtype: numpy float64 array 168 """ 169 170 # Convert if necessary. 171 if isinstance(params, ndarray): 172 params = params.tolist() 173 174 # Call the C code. 175 dchi2 = dfunc_exp(params) 176 177 # Return the chi2 gradient as a numpy array. 178 return array(dchi2, float64)
179 180
181 - def dfunc_inv(self, params):
182 """Wrapper function for the C module, for converting numpy arrays. 183 184 @param params: The parameter array from the minimisation code. 185 @type params: numpy array 186 @return: The gradient generated by the C module converted to numpy format. 187 @rtype: numpy float64 array 188 """ 189 190 # Convert if necessary. 191 if isinstance(params, ndarray): 192 params = params.tolist() 193 194 # Call the C code. 195 dchi2 = dfunc_inv(params) 196 197 # Return the chi2 gradient as a numpy array. 198 return array(dchi2, float64)
199 200
201 - def dfunc_sat(self, params):
202 """Wrapper function for the C module, for converting numpy arrays. 203 204 @param params: The parameter array from the minimisation code. 205 @type params: numpy array 206 @return: The gradient generated by the C module converted to numpy format. 207 @rtype: numpy float64 array 208 """ 209 210 # Convert if necessary. 211 if isinstance(params, ndarray): 212 params = params.tolist() 213 214 # Call the C code. 215 dchi2 = dfunc_sat(params) 216 217 # Return the chi2 gradient as a numpy array. 218 return array(dchi2, float64)
219 220
221 - def d2func_exp(self, params):
222 """Wrapper function for the C module, for converting numpy arrays. 223 224 @param params: The parameter array from the minimisation code. 225 @type params: numpy array 226 @return: The Hessian generated by the C module converted to numpy format. 227 @rtype: numpy float64 rank-2 array 228 """ 229 230 # Convert if necessary. 231 if isinstance(params, ndarray): 232 params = params.tolist() 233 234 # Call the C code. 235 d2chi2 = d2func_exp(params) 236 237 # Return the chi2 Hessian as a numpy array. 238 return array(d2chi2, float64)
239 240
241 - def d2func_inv(self, params):
242 """Wrapper function for the C module, for converting numpy arrays. 243 244 @param params: The parameter array from the minimisation code. 245 @type params: numpy array 246 @return: The Hessian generated by the C module converted to numpy format. 247 @rtype: numpy float64 rank-2 array 248 """ 249 250 # Convert if necessary. 251 if isinstance(params, ndarray): 252 params = params.tolist() 253 254 # Call the C code. 255 d2chi2 = d2func_inv(params) 256 257 # Return the chi2 Hessian as a numpy array. 258 return array(d2chi2, float64)
259 260
261 - def d2func_sat(self, params):
262 """Wrapper function for the C module, for converting numpy arrays. 263 264 @param params: The parameter array from the minimisation code. 265 @type params: numpy array 266 @return: The Hessian generated by the C module converted to numpy format. 267 @rtype: numpy float64 rank-2 array 268 """ 269 270 # Convert if necessary. 271 if isinstance(params, ndarray): 272 params = params.tolist() 273 274 # Call the C code. 275 d2chi2 = d2func_sat(params) 276 277 # Return the chi2 Hessian as a numpy array. 278 return array(d2chi2, float64)
279