1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """The R1 and R2 exponential relaxation curve fitting optimisation functions."""
25
26
27 from numpy import array, float64, ndarray, nan_to_num
28
29
30 from dep_check import C_module_exp_fn
31
32
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
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
58 self.model = model
59
60
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
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
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
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
97 return back_calc_I()
98
99
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
110 if isinstance(params, ndarray):
111 params = params.tolist()
112
113
114 chi2 = func_exp(params)
115
116
117 return nan_to_num(chi2)
118
119
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
130 if isinstance(params, ndarray):
131 params = params.tolist()
132
133
134 chi2 = func_inv(params)
135
136
137 return nan_to_num(chi2)
138
139
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
150 if isinstance(params, ndarray):
151 params = params.tolist()
152
153
154 chi2 = func_sat(params)
155
156
157 return nan_to_num(chi2)
158
159
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
170 if isinstance(params, ndarray):
171 params = params.tolist()
172
173
174 dchi2 = dfunc_exp(params)
175
176
177 return array(dchi2, float64)
178
179
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
190 if isinstance(params, ndarray):
191 params = params.tolist()
192
193
194 dchi2 = dfunc_inv(params)
195
196
197 return array(dchi2, float64)
198
199
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
210 if isinstance(params, ndarray):
211 params = params.tolist()
212
213
214 dchi2 = dfunc_sat(params)
215
216
217 return array(dchi2, float64)
218
219
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
230 if isinstance(params, ndarray):
231 params = params.tolist()
232
233
234 d2chi2 = d2func_exp(params)
235
236
237 return array(d2chi2, float64)
238
239
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
250 if isinstance(params, ndarray):
251 params = params.tolist()
252
253
254 d2chi2 = d2func_inv(params)
255
256
257 return array(d2chi2, float64)
258
259
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
270 if isinstance(params, ndarray):
271 params = params.tolist()
272
273
274 d2chi2 = d2func_sat(params)
275
276
277 return array(d2chi2, float64)
278