1 | import time |
---|
2 | from calcthread import CalcThread |
---|
3 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
4 | import sys |
---|
5 | import wx |
---|
6 | class Calc2D_all(CalcThread): |
---|
7 | """ |
---|
8 | Compute 2D model |
---|
9 | This calculation assumes a 2-fold symmetry of the model |
---|
10 | where points are computed for one half of the detector |
---|
11 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
12 | """ |
---|
13 | |
---|
14 | def __init__(self, x, y, model, |
---|
15 | completefn = None, |
---|
16 | updatefn = None, |
---|
17 | yieldtime = 0.01, |
---|
18 | worktime = 0.01 |
---|
19 | ): |
---|
20 | CalcThread.__init__(self,completefn, |
---|
21 | updatefn, |
---|
22 | yieldtime, |
---|
23 | worktime) |
---|
24 | |
---|
25 | self.x = x |
---|
26 | self.y = y |
---|
27 | self.model = model |
---|
28 | self.starttime = 0 |
---|
29 | |
---|
30 | def isquit(self): |
---|
31 | try: |
---|
32 | CalcThread.isquit(self) |
---|
33 | except KeyboardInterrupt: |
---|
34 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
35 | "Calc %s interrupted" % self.model.name)) |
---|
36 | raise KeyboardInterrupt |
---|
37 | |
---|
38 | def compute(self): |
---|
39 | import numpy |
---|
40 | x = self.x |
---|
41 | y = self.y |
---|
42 | output = numpy.zeros((len(x),len(y))) |
---|
43 | |
---|
44 | self.starttime = time.time() |
---|
45 | lx = len(self.x) |
---|
46 | |
---|
47 | #for i_x in range(int(len(self.x)/2)): |
---|
48 | for i_x in range(len(self.x)): |
---|
49 | if i_x%2==1: |
---|
50 | continue |
---|
51 | |
---|
52 | # Check whether we need to bail out |
---|
53 | self.update(output=output) |
---|
54 | self.isquit() |
---|
55 | |
---|
56 | for i_y in range(len(self.y)): |
---|
57 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
58 | output[i_y][i_x] = value |
---|
59 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
60 | |
---|
61 | if lx%2==1: |
---|
62 | i_x = int(len(self.x)/2) |
---|
63 | for i_y in range(len(self.y)): |
---|
64 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
65 | output[i_y][i_x] = value |
---|
66 | |
---|
67 | #for i_x in range(int(len(self.x)/2)): |
---|
68 | for i_x in range(len(self.x)): |
---|
69 | if not i_x%2==1: |
---|
70 | continue |
---|
71 | |
---|
72 | # Check whether we need to bail out |
---|
73 | self.update(output=output) |
---|
74 | self.isquit() |
---|
75 | |
---|
76 | for i_y in range(len(self.y)): |
---|
77 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
78 | output[i_y][i_x] = value |
---|
79 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
80 | |
---|
81 | elapsed = time.time()-self.starttime |
---|
82 | self.complete(output=output, elapsed=elapsed) |
---|
83 | |
---|
84 | |
---|
85 | class Calc2D(CalcThread): |
---|
86 | """ |
---|
87 | Compute 2D model |
---|
88 | This calculation assumes a 2-fold symmetry of the model |
---|
89 | where points are computed for one half of the detector |
---|
90 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
91 | """ |
---|
92 | |
---|
93 | def __init__(self,parent, x, y, model,qmin, qmax,qstep, |
---|
94 | completefn = None, |
---|
95 | updatefn = None, |
---|
96 | yieldtime = 0.01, |
---|
97 | worktime = 0.01 |
---|
98 | ): |
---|
99 | CalcThread.__init__(self,completefn, |
---|
100 | updatefn, |
---|
101 | yieldtime, |
---|
102 | worktime) |
---|
103 | self.parent =parent |
---|
104 | self.qmin= qmin |
---|
105 | self.qmax=qmax |
---|
106 | self.qstep= qstep |
---|
107 | self.x = x |
---|
108 | self.y = y |
---|
109 | ## the model on to calculate |
---|
110 | self.model = model |
---|
111 | self.starttime = 0 |
---|
112 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
113 | "Start Drawing model ",curr_thread=self,type="start")) |
---|
114 | |
---|
115 | |
---|
116 | def isquit(self): |
---|
117 | try: |
---|
118 | CalcThread.isquit(self) |
---|
119 | except KeyboardInterrupt: |
---|
120 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
121 | "Calc %s interrupted" % self.model.name)) |
---|
122 | |
---|
123 | raise KeyboardInterrupt |
---|
124 | |
---|
125 | |
---|
126 | def update(self, output=None): |
---|
127 | """ |
---|
128 | Post a message if update available |
---|
129 | """ |
---|
130 | wx.PostEvent(self.parent, StatusEvent(status="Plot \ |
---|
131 | #updating ... ",curr_thread=self,type="update")) |
---|
132 | |
---|
133 | |
---|
134 | def compute(self): |
---|
135 | """ |
---|
136 | Compute the data given a model function |
---|
137 | """ |
---|
138 | import numpy |
---|
139 | x = self.x |
---|
140 | y = self.y |
---|
141 | output = numpy.zeros((len(x),len(y))) |
---|
142 | |
---|
143 | center_x=0 |
---|
144 | center_y=0 |
---|
145 | |
---|
146 | self.starttime = time.time() |
---|
147 | |
---|
148 | |
---|
149 | lx = len(self.x) |
---|
150 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
151 | "Computing ",curr_thread=self,type="progress")) |
---|
152 | for i_x in range(len(self.x)): |
---|
153 | # Check whether we need to bail out |
---|
154 | self.update(output=output ) |
---|
155 | self.isquit() |
---|
156 | |
---|
157 | for i_y in range(int(len(self.y))): |
---|
158 | try: |
---|
159 | if (self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) \ |
---|
160 | < self.qmin * self.qmin: |
---|
161 | |
---|
162 | output[i_x] [i_y]=0 |
---|
163 | else: |
---|
164 | value = self.model.runXY([self.x[i_x]-center_x, self.y[i_y]-center_y]) |
---|
165 | output[i_x] [i_y]=value |
---|
166 | |
---|
167 | except: |
---|
168 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
169 | "Error computing %s at [%g,%g]" %(self.model.name, self.x[i_x],self.y[i_y]))) |
---|
170 | pass |
---|
171 | |
---|
172 | elapsed = time.time()-self.starttime |
---|
173 | self.complete( |
---|
174 | output=output, elapsed=elapsed,model= self.model, |
---|
175 | qmin= self.qmin, |
---|
176 | qmax=self.qmax, |
---|
177 | qstep=self.qstep) |
---|
178 | |
---|
179 | class Calc2D_4fold(CalcThread): |
---|
180 | """ |
---|
181 | Compute 2D model |
---|
182 | This calculation assumes a 4-fold symmetry of the model. |
---|
183 | Really is the same calculation time since we have to |
---|
184 | calculate points for 0<phi<pi anyway. |
---|
185 | """ |
---|
186 | |
---|
187 | def __init__(self, x, y, model, |
---|
188 | completefn = None, |
---|
189 | updatefn = None, |
---|
190 | yieldtime = 0.01, |
---|
191 | worktime = 0.01 |
---|
192 | ): |
---|
193 | CalcThread.__init__(self,completefn, |
---|
194 | updatefn, |
---|
195 | yieldtime, |
---|
196 | worktime) |
---|
197 | self.x = x |
---|
198 | self.y = y |
---|
199 | self.model = model |
---|
200 | self.starttime = 0 |
---|
201 | |
---|
202 | def isquit(self): |
---|
203 | try: |
---|
204 | CalcThread.isquit(self) |
---|
205 | except KeyboardInterrupt: |
---|
206 | #printEVT("Calc %s interrupted" % self.model.name) |
---|
207 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
208 | "Calc %s interrupted" % self.model.name)) |
---|
209 | |
---|
210 | raise KeyboardInterrupt |
---|
211 | |
---|
212 | def compute(self): |
---|
213 | import numpy |
---|
214 | x = self.x |
---|
215 | y = self.y |
---|
216 | output = numpy.zeros((len(x),len(y))) |
---|
217 | |
---|
218 | self.starttime = time.time() |
---|
219 | lx = len(self.x) |
---|
220 | |
---|
221 | for i_x in range(int(len(self.x)/2)): |
---|
222 | if i_x%2==1: |
---|
223 | continue |
---|
224 | |
---|
225 | # Check whether we need to bail out |
---|
226 | self.update(output=output) |
---|
227 | self.isquit() |
---|
228 | |
---|
229 | for i_y in range(int(len(self.y)/2)): |
---|
230 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
231 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
232 | output[i_y][i_x] = value1 + value2 |
---|
233 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
234 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
235 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
236 | |
---|
237 | if lx%2==1: |
---|
238 | i_x = int(len(self.x)/2) |
---|
239 | for i_y in range(int(len(self.y)/2)): |
---|
240 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
241 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
242 | output[i_y][i_x] = value1 + value2 |
---|
243 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
244 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
245 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
246 | |
---|
247 | for i_x in range(int(len(self.x)/2)): |
---|
248 | if not i_x%2==1: |
---|
249 | continue |
---|
250 | |
---|
251 | # Check whether we need to bail out |
---|
252 | self.update(output=output) |
---|
253 | self.isquit() |
---|
254 | |
---|
255 | for i_y in range(int(len(self.y)/2)): |
---|
256 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
257 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
258 | output[i_y][i_x] = value1 + value2 |
---|
259 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
260 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
261 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
262 | |
---|
263 | elapsed = time.time()-self.starttime |
---|
264 | self.complete(output=output, elapsed=elapsed) |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | class Calc1D(CalcThread): |
---|
269 | """Compute 1D data""" |
---|
270 | |
---|
271 | def __init__(self, x, model, |
---|
272 | completefn = None, |
---|
273 | updatefn = None, |
---|
274 | yieldtime = 0.01, |
---|
275 | worktime = 0.01 |
---|
276 | ): |
---|
277 | CalcThread.__init__(self,completefn, |
---|
278 | updatefn, |
---|
279 | yieldtime, |
---|
280 | worktime) |
---|
281 | self.x = x |
---|
282 | self.model = model |
---|
283 | self.starttime = 0 |
---|
284 | |
---|
285 | def compute(self): |
---|
286 | import numpy |
---|
287 | x = self.x |
---|
288 | output = numpy.zeros(len(x)) |
---|
289 | |
---|
290 | self.starttime = time.time() |
---|
291 | |
---|
292 | for i_x in range(len(self.x)): |
---|
293 | |
---|
294 | # Check whether we need to bail out |
---|
295 | self.isquit() |
---|
296 | |
---|
297 | try: |
---|
298 | value = self.model.run(self.x[i_x]) |
---|
299 | output[i_x] = value |
---|
300 | except: |
---|
301 | |
---|
302 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
303 | "Error computing %s at %g" %(self.model.name, self.x[i_x]))) |
---|
304 | |
---|
305 | elapsed = time.time()-self.starttime |
---|
306 | self.complete(output=output, elapsed=elapsed) |
---|
307 | |
---|
308 | class CalcCommandline: |
---|
309 | def __init__(self, n=20000): |
---|
310 | #print thread.get_ident() |
---|
311 | from sans.models.CylinderModel import CylinderModel |
---|
312 | from sans.models.DisperseModel import DisperseModel |
---|
313 | import Averager2D |
---|
314 | import pylab |
---|
315 | |
---|
316 | submodel = CylinderModel() |
---|
317 | #model = Averager2D.Averager2D() |
---|
318 | #model.set_model(submodel) |
---|
319 | #model.set_dispersity([['cyl_phi',0.2,10], |
---|
320 | # ['cyl_theta',0.2,10], |
---|
321 | # ['length',10,10],]) |
---|
322 | |
---|
323 | model = DisperseModel(submodel, ['cyl_phi', 'cyl_theta', 'length'], |
---|
324 | [0.2, 0.2, 10.0]) |
---|
325 | model.setParam('n_pts', 10) |
---|
326 | |
---|
327 | print model.runXY([0.01, 0.02]) |
---|
328 | |
---|
329 | qmax = 0.01 |
---|
330 | qstep = 0.0001 |
---|
331 | self.done = False |
---|
332 | |
---|
333 | x = pylab.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
334 | y = pylab.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
335 | |
---|
336 | calc_thread_2D = Calc2D(x, y, model.clone(), |
---|
337 | completefn=self.complete, |
---|
338 | updatefn=self.update, |
---|
339 | yieldtime=0.0) |
---|
340 | |
---|
341 | calc_thread_2D.queue() |
---|
342 | calc_thread_2D.ready(2.5) |
---|
343 | |
---|
344 | while not self.done: |
---|
345 | time.sleep(1) |
---|
346 | |
---|
347 | def update(self,output): |
---|
348 | print "update" |
---|
349 | |
---|
350 | def complete(self,output, elapsed=0.0): |
---|
351 | print "complete" |
---|
352 | self.done = True |
---|
353 | |
---|
354 | if __name__ == "__main__": |
---|
355 | CalcCommandline() |
---|
356 | |
---|