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 if not run in self.relax.data.run_names:
43 raise RelaxNoRunError, run
44
45
46 if not hasattr(self.relax.data, 'sim_state'):
47 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have not been set up."
48
49
50 if not self.relax.data.res.has_key(run):
51 raise RelaxNoSequenceError, run
52
53
54 valid_methods = ['back_calc', 'direct']
55 if method not in valid_methods:
56 raise RelaxError, "The method " + `method` + " is not valid."
57
58
59 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)]
60
61
62 create_mc_data = self.relax.specific_setup.setup('create_mc_data', function_type)
63 return_data = self.relax.specific_setup.setup('return_data', function_type)
64 return_error = self.relax.specific_setup.setup('return_error', function_type)
65 pack_sim_data = self.relax.specific_setup.setup('pack_sim_data', function_type)
66
67
68 for i in xrange(len(self.relax.data.res[run])):
69
70 if not self.relax.data.res[run][i].select:
71 continue
72
73
74 if method == 'back_calc':
75 data = create_mc_data(run, i)
76
77
78 else:
79 data = return_data(run, i)
80
81
82 error = return_error(run, i)
83
84
85 random = []
86 for j in xrange(self.relax.data.sim_number[run]):
87
88 random.append([])
89 for k in xrange(len(data)):
90
91 if data[k] == None or error[k] == None:
92 random[j].append(None)
93 continue
94
95
96 random[j].append(gauss(data[k], error[k]))
97
98
99 pack_sim_data(run, i, random)
100
101
103 """Function for calculating errors from the Monte Carlo simulations.
104
105 The standard deviation formula used to calculate the errors is the square root of the
106 bias-corrected variance, given by the formula:
107
108 ____________________________
109 / 1
110 sd = / ----- * sum({Xi - Xav}^2)]
111 \/ n - 1
112
113 where:
114 n is the total number of simulations.
115 Xi is the parameter value for simulation i.
116 Xav is the mean parameter value for all simulations.
117 """
118
119
120 if not run in self.relax.data.run_names:
121 raise RelaxNoRunError, run
122
123
124 if not hasattr(self.relax.data, 'sim_state'):
125 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have not been set up."
126
127
128 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)]
129
130
131 count_num_instances = self.relax.specific_setup.setup('num_instances', function_type)
132 if prune > 0.0:
133 return_sim_chi2 = self.relax.specific_setup.setup('return_sim_chi2', function_type)
134 return_sim_param = self.relax.specific_setup.setup('return_sim_param', function_type)
135 set_error = self.relax.specific_setup.setup('set_error', function_type)
136
137
138 num_instances = count_num_instances(run)
139
140
141 for instance in xrange(num_instances):
142
143 indecies_to_skip = []
144
145
146 if prune > 0.0:
147
148 chi2_array = return_sim_chi2(run, instance)
149
150
151 n = len(chi2_array)
152
153
154 chi2_sorted = deepcopy(chi2_array)
155 chi2_sorted.sort()
156
157
158 num = int(float(n) * 0.5 * prune)
159
160
161 for i in xrange(num):
162 indecies_to_skip.append(chi2_array.index(chi2_sorted[i]))
163
164
165 for i in xrange(n-num, n):
166 indecies_to_skip.append(chi2_array.index(chi2_sorted[i]))
167
168
169 index = 0
170 while 1:
171
172 param_array = return_sim_param(run, instance, index)
173
174
175 if param_array == None:
176 break
177
178
179 if param_array[0] != None:
180
181 n = len(param_array)
182
183
184 Xav = 0.0
185 for i in xrange(n):
186
187 if i in indecies_to_skip:
188 continue
189
190
191 Xav = Xav + param_array[i]
192 Xav = Xav / (float(n) - float(len(indecies_to_skip)))
193
194
195 sd = 0.0
196 for i in xrange(n):
197
198 if i in indecies_to_skip:
199 continue
200
201
202 sd = sd + (param_array[i] - Xav)**2
203
204
205 sd = sqrt(sd / (float(n) - float(len(indecies_to_skip)) - 1.0))
206
207
208 else:
209 sd = None
210
211
212 set_error(run, instance, index, sd)
213
214
215 index = index + 1
216
217
219 """Function for setting the initial simulation parameter values."""
220
221
222 if not run in self.relax.data.run_names:
223 raise RelaxNoRunError, run
224
225
226 if not hasattr(self.relax.data, 'sim_state'):
227 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have not been set up."
228
229
230 function_type = self.relax.data.run_types[self.relax.data.run_names.index(run)]
231
232
233 init_sim_values = self.relax.specific_setup.setup('init_sim_values', function_type)
234
235
236 init_sim_values(run)
237
238
239 - def off(self, run=None):
240 """Function for turning simulations off."""
241
242
243 if not run in self.relax.data.run_names:
244 raise RelaxNoRunError, run
245
246
247 if not hasattr(self.relax.data, 'sim_state'):
248 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have not been set up."
249
250
251 self.relax.data.sim_state[run] = 0
252
253
254 - def on(self, run=None):
255 """Function for turning simulations on."""
256
257
258 if not run in self.relax.data.run_names:
259 raise RelaxNoRunError, run
260
261
262 if not hasattr(self.relax.data, 'sim_state'):
263 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have not been set up."
264
265
266 self.relax.data.sim_state[run] = 1
267
268
269 - def setup(self, run=None, number=0):
270 """Function for setting up Monte Carlo simulations."""
271
272
273 if not run in self.relax.data.run_names:
274 raise RelaxNoRunError, run
275
276
277 if hasattr(self.relax.data, 'sim_number') and self.relax.data.sim_number.has_key(run):
278 raise RelaxError, "Monte Carlo simulations for the run " + `run` + " have already been set up."
279
280
281 if not hasattr(self.relax.data, 'sim_number'):
282 self.relax.data.sim_number = {}
283
284
285 self.relax.data.sim_number[run] = number
286
287
288 if not hasattr(self.relax.data, 'sim_state'):
289 self.relax.data.sim_state = {}
290
291
292 self.relax.data.sim_state[run] = 1
293