1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """The R1 and R2 exponential relaxation curve fitting optimisation functions."""
24
25
26 from numpy import average
27 from re import search
28
29
30 from dep_check import C_module_exp_fn
31 from lib.errors import RelaxError, RelaxLenError
32 from specific_analyses.relax_fit.parameters import assemble_param_vector, assemble_scaling_matrix
33
34
35 if C_module_exp_fn:
36 from target_functions.relax_fit import setup, func, back_calc_I
37
38
84
85
87 """Wrapper function for the C module, for converting numpy arrays.
88
89 @param params: The parameter array from the minimisation code.
90 @type params: numpy array
91 @return: The function value generated by the C module.
92 @rtype: float
93 """
94
95
96 chi2 = func(params.tolist())
97
98
99 return chi2
100
101
103 """Wrapper function for the C module, for converting numpy arrays.
104
105 The currently does nothing.
106 """
107
108
110 """Wrapper function for the C module, for converting numpy arrays.
111
112 The currently does nothing.
113 """
114
115
116 -def grid_search_setup(spin=None, param_vector=None, lower=None, upper=None, inc=None, scaling_matrix=None):
117 """The grid search setup function.
118
119 @keyword spin: The spin data container.
120 @type spin: SpinContainer instance
121 @keyword param_vector: The parameter vector.
122 @type param_vector: numpy array
123 @keyword lower: The lower bounds of the grid search which must be equal to the
124 number of parameters in the model. This optional argument is
125 only used when doing a grid search.
126 @type lower: array of numbers
127 @keyword upper: The upper bounds of the grid search which must be equal to the
128 number of parameters in the model. This optional argument is
129 only used when doing a grid search.
130 @type upper: array of numbers
131 @keyword inc: The increments for each dimension of the space for the grid
132 search. The number of elements in the array must equal to the
133 number of parameters in the model. This argument is only used
134 when doing a grid search.
135 @type inc: array of int
136 @keyword scaling_matrix: The scaling matrix.
137 @type scaling_matrix: numpy diagonal matrix
138 @return: A tuple of the grid size and the minimisation options. For the
139 minimisation options, the first dimension corresponds to the
140 model parameter. The second dimension is a list of the number
141 of increments, the lower bound, and upper bound.
142 @rtype: (int, list of lists [int, float, float])
143 """
144
145
146 n = len(param_vector)
147
148
149 if n == 0:
150 raise RelaxError("Cannot run a grid search on a model with zero parameters.")
151
152
153 if lower != None and len(lower) != n:
154 raise RelaxLenError('lower bounds', n)
155
156
157 if upper != None and len(upper) != n:
158 raise RelaxLenError('upper bounds', n)
159
160
161 if isinstance(inc, list) and len(inc) != n:
162 raise RelaxLenError('increment', n)
163 elif isinstance(inc, int):
164 inc = [inc]*n
165
166
167 if not lower:
168
169 lower = []
170 upper = []
171
172
173 for i in range(n):
174
175 if spin.params[i] == 'rx':
176 lower.append(0.0)
177 upper.append(20.0)
178
179
180 elif search('^i', spin.params[i]):
181
182 min_time = min(cdp.relax_times.values())
183 for key in list(cdp.relax_times.keys()):
184 if cdp.relax_times[key] == min_time:
185 id = key
186 break
187
188
189 lower.append(0.0)
190 upper.append(average(spin.peak_intensity[id]))
191
192
193 lower_new = []
194 upper_new = []
195 for i in range(n):
196 lower_new.append(lower[i] / scaling_matrix[i, i])
197 upper_new.append(upper[i] / scaling_matrix[i, i])
198
199
200 return inc, lower_new, upper_new
201