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 from specific_analyses.relax_fit.parameters import assemble_param_vector
32
33
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
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
59 self.model = model
60
61
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
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
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
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
98 return back_calc_I()
99
100
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
111 if isinstance(params, ndarray):
112 params = params.tolist()
113
114
115 chi2 = func_exp(params)
116
117
118 return nan_to_num(chi2)
119
120
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
131 if isinstance(params, ndarray):
132 params = params.tolist()
133
134
135 chi2 = func_inv(params)
136
137
138 return nan_to_num(chi2)
139
140
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
151 if isinstance(params, ndarray):
152 params = params.tolist()
153
154
155 chi2 = func_sat(params)
156
157
158 return nan_to_num(chi2)
159
160
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
171 if isinstance(params, ndarray):
172 params = params.tolist()
173
174
175 dchi2 = dfunc_exp(params)
176
177
178 return array(dchi2, float64)
179
180
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
191 if isinstance(params, ndarray):
192 params = params.tolist()
193
194
195 dchi2 = dfunc_inv(params)
196
197
198 return array(dchi2, float64)
199
200
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
211 if isinstance(params, ndarray):
212 params = params.tolist()
213
214
215 dchi2 = dfunc_sat(params)
216
217
218 return array(dchi2, float64)
219
220
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
231 if isinstance(params, ndarray):
232 params = params.tolist()
233
234
235 d2chi2 = d2func_exp(params)
236
237
238 return array(d2chi2, float64)
239
240
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
251 if isinstance(params, ndarray):
252 params = params.tolist()
253
254
255 d2chi2 = d2func_inv(params)
256
257
258 return array(d2chi2, float64)
259
260
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
271 if isinstance(params, ndarray):
272 params = params.tolist()
273
274
275 d2chi2 = d2func_sat(params)
276
277
278 return array(d2chi2, float64)
279