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