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
132 pipes.test()
133
134
135 if not hasattr(cdp, 'sim_state'):
136 raise RelaxError("Monte Carlo simulations have not been set up.")
137
138
139 model_loop = get_specific_fn('model_loop', cdp.pipe_type)
140 return_selected_sim = get_specific_fn('return_selected_sim', cdp.pipe_type)
141 return_sim_param = get_specific_fn('return_sim_param', cdp.pipe_type)
142 set_error = get_specific_fn('set_error', cdp.pipe_type)
143
144
145 for model_info in model_loop():
146
147 select_sim = return_selected_sim(model_info)
148
149
150 index = 0
151 while True:
152
153 param_array = return_sim_param(model_info, index)
154
155
156 if param_array == None:
157 break
158
159
160 if param_array[0] != None:
161
162 n = 0
163 for i in range(len(param_array)):
164
165 if not select_sim[i]:
166 continue
167
168
169 n = n + 1
170
171
172 Xsum = 0.0
173 for i in range(len(param_array)):
174
175 if not select_sim[i]:
176 continue
177
178
179 Xsum = Xsum + param_array[i]
180
181
182 if n == 0:
183 Xav = 0.0
184 else:
185 Xav = Xsum / float(n)
186
187
188 sd = 0.0
189 for i in range(len(param_array)):
190
191 if not select_sim[i]:
192 continue
193
194
195 sd = sd + (param_array[i] - Xav)**2
196
197
198 if n <= 1:
199 sd = 0.0
200 else:
201 sd = sqrt(sd / (float(n) - 1.0))
202
203
204 else:
205 sd = None
206
207
208 set_error(model_info, index, sd)
209
210
211 index = index + 1
212
213
214 cdp.sim_state = False
215
216
218 """Set the initial simulation parameter values."""
219
220
221 pipes.test()
222
223
224 if not hasattr(cdp, 'sim_state'):
225 raise RelaxError("Monte Carlo simulations have not been set up.")
226
227
228 init_sim_values = get_specific_fn('init_sim_values', cdp.pipe_type)
229
230
231 init_sim_values()
232
233
235 """Turn simulations off."""
236
237
238 pipes.test()
239
240
241 if not hasattr(cdp, 'sim_state'):
242 raise RelaxError("Monte Carlo simulations have not been set up.")
243
244
245 cdp.sim_state = False
246
247
249 """Turn simulations on."""
250
251
252 pipes.test()
253
254
255 if not hasattr(cdp, 'sim_state'):
256 raise RelaxError("Monte Carlo simulations have not been set up.")
257
258
259 cdp.sim_state = True
260
261
263 """Set the select flag of all simulations of all models to one.
264
265 @keyword number: The number of Monte Carlo simulations to set up.
266 @type number: int
267 @keyword all_select_sim: The selection status of the Monte Carlo simulations. The first
268 dimension of this matrix corresponds to the simulation and the
269 second corresponds to the models.
270 @type all_select_sim: list of lists of bool
271 """
272
273
274 model_loop = get_specific_fn('model_loop', cdp.pipe_type)
275 set_selected_sim = get_specific_fn('set_selected_sim', cdp.pipe_type)
276 skip_function = get_specific_fn('skip_function', cdp.pipe_type)
277
278
279 if all_select_sim == None:
280 select_sim = [True]*number
281
282
283 i = 0
284 for model_info in model_loop():
285
286 if skip_function(model_info):
287 continue
288
289
290 if all_select_sim != None:
291 select_sim = all_select_sim[i]
292
293
294 set_selected_sim(model_info, select_sim)
295
296
297 i += 1
298
299
300 -def setup(number=None, all_select_sim=None):
301 """Function for setting up Monte Carlo simulations.
302
303 @keyword number: The number of Monte Carlo simulations to set up.
304 @type number: int
305 @keyword all_select_sim: The selection status of the Monte Carlo simulations. The first
306 dimension of this matrix corresponds to the simulation and the
307 second corresponds to the instance.
308 @type all_select_sim: list of lists of bool
309 """
310
311
312 pipes.test()
313
314
315 cdp.sim_number = number
316 cdp.sim_state = True
317
318
319 select_all_sims(number=number, all_select_sim=all_select_sim)
320