1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 """The R1 and R2 exponential relaxation curve fitting optimisation functions."""
27
28
29 from numpy import array, float64, ndarray, nan_to_num
30
31
32 from dep_check import C_module_exp_fn
33
34
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
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
60 self.model = model
61
62
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
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
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
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
99 return back_calc_I()
100
101
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
112 if isinstance(params, ndarray):
113 params = params.tolist()
114
115
116 chi2 = func_exp(params)
117
118
119 return nan_to_num(chi2)
120
121
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
132 if isinstance(params, ndarray):
133 params = params.tolist()
134
135
136 chi2 = func_inv(params)
137
138
139 return nan_to_num(chi2)
140
141
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
152 if isinstance(params, ndarray):
153 params = params.tolist()
154
155
156 chi2 = func_sat(params)
157
158
159 return nan_to_num(chi2)
160
161
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
172 if isinstance(params, ndarray):
173 params = params.tolist()
174
175
176 dchi2 = dfunc_exp(params)
177
178
179 return array(dchi2, float64)
180
181
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
192 if isinstance(params, ndarray):
193 params = params.tolist()
194
195
196 dchi2 = dfunc_inv(params)
197
198
199 return array(dchi2, float64)
200
201
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
212 if isinstance(params, ndarray):
213 params = params.tolist()
214
215
216 dchi2 = dfunc_sat(params)
217
218
219 return array(dchi2, float64)
220
221
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
232 if isinstance(params, ndarray):
233 params = params.tolist()
234
235
236 d2chi2 = d2func_exp(params)
237
238
239 return array(d2chi2, float64)
240
241
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
252 if isinstance(params, ndarray):
253 params = params.tolist()
254
255
256 d2chi2 = d2func_inv(params)
257
258
259 return array(d2chi2, float64)
260
261
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
272 if isinstance(params, ndarray):
273 params = params.tolist()
274
275
276 d2chi2 = d2func_sat(params)
277
278
279 return array(d2chi2, float64)
280