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