1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for performing Monte Carlo simulations for error analysis."""
24
25
26 from copy import deepcopy
27 from math import sqrt
28 from numpy import ndarray
29 from random import gauss
30
31
32 from generic_fns.mol_res_spin import exists_mol_res_spin_data
33 from generic_fns import pipes
34 from relax_errors import RelaxError, RelaxNoSequenceError
35 from specific_fns.setup import get_specific_fn
36
37
39 """Function for creating simulation data.
40
41 @keyword method: The type of Monte Carlo simulation to perform.
42 @type method: str
43 """
44
45
46 pipes.test()
47
48
49 if not hasattr(cdp, 'sim_state'):
50 raise RelaxError("Monte Carlo simulations have not been set up.")
51
52
53 valid_methods = ['back_calc', 'direct']
54 if method not in valid_methods:
55 raise RelaxError("The simulation creation method " + repr(method) + " is not valid.")
56
57
58 base_data_loop = get_specific_fn('base_data_loop', cdp.pipe_type)
59 if method == 'back_calc':
60 create_mc_data = get_specific_fn('create_mc_data', cdp.pipe_type)
61 return_data = get_specific_fn('return_data', cdp.pipe_type)
62 return_error = get_specific_fn('return_error', cdp.pipe_type)
63 pack_sim_data = get_specific_fn('pack_sim_data', cdp.pipe_type)
64
65
66 for data_index in base_data_loop():
67
68 if method == 'back_calc':
69 data = create_mc_data(data_index)
70
71
72 else:
73 data = return_data(data_index)
74
75
76 error = return_error(data_index)
77
78
79 if isinstance(data, list) or isinstance(data, ndarray):
80
81 random = []
82 for j in range(cdp.sim_number):
83
84 random.append([])
85 for k in range(len(data)):
86
87 if data[k] == None or error[k] == None:
88 random[j].append(None)
89 continue
90
91
92 random[j].append(gauss(data[k], error[k]))
93
94
95 if isinstance(data, dict):
96
97 random = []
98 for j in range(cdp.sim_number):
99
100 random.append({})
101 for id in data.keys():
102
103 if data[id] == None or error[id] == None:
104 random[j][id] = None
105 continue
106
107
108 random[j][id] = gauss(data[id], error[id])
109
110
111 pack_sim_data(data_index, random)
112
113
115 """Function for calculating errors from the Monte Carlo simulations.
116
117 The standard deviation formula used to calculate the errors is the square root of the
118 bias-corrected variance, given by the formula::
119
120 __________________________
121 / 1
122 sd = / ----- * sum({Xi - Xav}^2)
123 \/ n - 1
124
125 where
126 - n is the total number of simulations.
127 - Xi is the parameter value for simulation i.
128 - Xav is the mean parameter value for all simulations.
129
130
131 @keyword prune: The amount to prune the upper and lower tails the distribution. If set to
132 0.0, no simulations will be pruned. If set to 1.0, all simulations will be
133 pruned. This argument should be set 0.0 to avoid meaningless statistics.
134 @type prune: float
135 """
136
137
138 pipes.test()
139
140
141 if not hasattr(cdp, 'sim_state'):
142 raise RelaxError("Monte Carlo simulations have not been set up.")
143
144
145 model_loop = get_specific_fn('model_loop', cdp.pipe_type)
146 if prune > 0.0:
147 return_sim_chi2 = get_specific_fn('return_sim_chi2', cdp.pipe_type)
148 return_selected_sim = get_specific_fn('return_selected_sim', cdp.pipe_type)
149 return_sim_param = get_specific_fn('return_sim_param', cdp.pipe_type)
150 set_error = get_specific_fn('set_error', cdp.pipe_type)
151
152
153 for model_info in model_loop():
154
155 select_sim = return_selected_sim(model_info)
156
157
158 indices_to_skip = []
159
160
161 if prune > 0.0:
162
163 chi2_array = return_sim_chi2(model_info)
164
165
166 n = len(chi2_array)
167
168
169 chi2_sorted = sorted(deepcopy(chi2_array))
170
171
172 num = int(float(n) * 0.5 * prune)
173
174
175 for i in range(num):
176 indices_to_skip.append(chi2_array.index(chi2_sorted[i]))
177
178
179 for i in range(n-num, n):
180 indices_to_skip.append(chi2_array.index(chi2_sorted[i]))
181
182
183 index = 0
184 while True:
185
186 param_array = return_sim_param(model_info, index)
187
188
189 if param_array == None:
190 break
191
192
193 if param_array[0] != None:
194
195 n = 0
196 for i in range(len(param_array)):
197
198 if not select_sim[i]:
199 continue
200
201
202 if i in indices_to_skip:
203 continue
204
205
206 n = n + 1
207
208
209 Xsum = 0.0
210 for i in range(len(param_array)):
211
212 if not select_sim[i]:
213 continue
214
215
216 if i in indices_to_skip:
217 continue
218
219
220 Xsum = Xsum + param_array[i]
221
222
223 if n == 0:
224 Xav = 0.0
225 else:
226 Xav = Xsum / float(n)
227
228
229 sd = 0.0
230 for i in range(len(param_array)):
231
232 if not select_sim[i]:
233 continue
234
235
236 if i in indices_to_skip:
237 continue
238
239
240 sd = sd + (param_array[i] - Xav)**2
241
242
243 if n <= 1:
244 sd = 0.0
245 else:
246 sd = sqrt(sd / (float(n) - 1.0))
247
248
249 else:
250 sd = None
251
252
253 set_error(model_info, index, sd)
254
255
256 index = index + 1
257
258
259 cdp.sim_state = False
260
261
263 """Set the initial simulation parameter values."""
264
265
266 pipes.test()
267
268
269 if not hasattr(cdp, 'sim_state'):
270 raise RelaxError("Monte Carlo simulations have not been set up.")
271
272
273 init_sim_values = get_specific_fn('init_sim_values', cdp.pipe_type)
274
275
276 init_sim_values()
277
278
280 """Turn simulations off."""
281
282
283 pipes.test()
284
285
286 if not hasattr(cdp, 'sim_state'):
287 raise RelaxError("Monte Carlo simulations have not been set up.")
288
289
290 cdp.sim_state = False
291
292
294 """Turn simulations on."""
295
296
297 pipes.test()
298
299
300 if not hasattr(cdp, 'sim_state'):
301 raise RelaxError("Monte Carlo simulations have not been set up.")
302
303
304 cdp.sim_state = True
305
306
308 """Set the select flag of all simulations of all models to one.
309
310 @keyword number: The number of Monte Carlo simulations to set up.
311 @type number: int
312 @keyword all_select_sim: The selection status of the Monte Carlo simulations. The first
313 dimension of this matrix corresponds to the simulation and the
314 second corresponds to the models.
315 @type all_select_sim: list of lists of bool
316 """
317
318
319 model_loop = get_specific_fn('model_loop', cdp.pipe_type)
320 set_selected_sim = get_specific_fn('set_selected_sim', cdp.pipe_type)
321 skip_function = get_specific_fn('skip_function', cdp.pipe_type)
322
323
324 if all_select_sim == None:
325 select_sim = [True]*number
326
327
328 i = 0
329 for model_info in model_loop():
330
331 if skip_function(model_info):
332 continue
333
334
335 if all_select_sim != None:
336 select_sim = all_select_sim[i]
337
338
339 set_selected_sim(model_info, select_sim)
340
341
342 i += 1
343
344
345 -def setup(number=None, all_select_sim=None):
346 """Function for setting up Monte Carlo simulations.
347
348 @keyword number: The number of Monte Carlo simulations to set up.
349 @type number: int
350 @keyword all_select_sim: The selection status of the Monte Carlo simulations. The first
351 dimension of this matrix corresponds to the simulation and the
352 second corresponds to the instance.
353 @type all_select_sim: list of lists of bool
354 """
355
356
357 pipes.test()
358
359
360 cdp.sim_number = number
361 cdp.sim_state = True
362
363
364 select_all_sims(number=number, all_select_sim=all_select_sim)
365