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