1 | import time |
---|
2 | from data_util.calcthread import CalcThread |
---|
3 | import sys |
---|
4 | import numpy,math |
---|
5 | |
---|
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 compute(self): |
---|
31 | x = self.x |
---|
32 | y = self.y |
---|
33 | output = numpy.zeros((len(x),len(y))) |
---|
34 | |
---|
35 | self.starttime = time.time() |
---|
36 | lx = len(self.x) |
---|
37 | |
---|
38 | #for i_x in range(int(len(self.x)/2)): |
---|
39 | for i_x in range(len(self.x)): |
---|
40 | if i_x%2==1: |
---|
41 | continue |
---|
42 | |
---|
43 | # Check whether we need to bail out |
---|
44 | self.update(output=output) |
---|
45 | self.isquit() |
---|
46 | |
---|
47 | for i_y in range(len(self.y)): |
---|
48 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
49 | output[i_y][i_x] = value |
---|
50 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
51 | |
---|
52 | if lx%2==1: |
---|
53 | i_x = int(len(self.x)/2) |
---|
54 | for i_y in range(len(self.y)): |
---|
55 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
56 | output[i_y][i_x] = value |
---|
57 | |
---|
58 | #for i_x in range(int(len(self.x)/2)): |
---|
59 | for i_x in range(len(self.x)): |
---|
60 | if not i_x%2==1: |
---|
61 | continue |
---|
62 | |
---|
63 | # Check whether we need to bail out |
---|
64 | self.update(output=output) |
---|
65 | self.isquit() |
---|
66 | |
---|
67 | for i_y in range(len(self.y)): |
---|
68 | value = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
69 | output[i_y][i_x] = value |
---|
70 | #output[lx-i_y-1][lx-i_x-1] = value |
---|
71 | |
---|
72 | elapsed = time.time()-self.starttime |
---|
73 | self.complete(output=output, elapsed=elapsed) |
---|
74 | |
---|
75 | |
---|
76 | class Calc2D(CalcThread): |
---|
77 | """ |
---|
78 | Compute 2D model |
---|
79 | This calculation assumes a 2-fold symmetry of the model |
---|
80 | where points are computed for one half of the detector |
---|
81 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
82 | """ |
---|
83 | |
---|
84 | def __init__(self, x, y, data,model,qmin, qmax,qstep, |
---|
85 | completefn = None, |
---|
86 | updatefn = None, |
---|
87 | yieldtime = 0.01, |
---|
88 | worktime = 0.01 |
---|
89 | ): |
---|
90 | CalcThread.__init__(self,completefn, |
---|
91 | updatefn, |
---|
92 | yieldtime, |
---|
93 | worktime) |
---|
94 | self.qmin= qmin |
---|
95 | self.qmax= qmax |
---|
96 | self.qstep= qstep |
---|
97 | self.x = x |
---|
98 | self.y = y |
---|
99 | self.data= data |
---|
100 | ## the model on to calculate |
---|
101 | self.model = model |
---|
102 | self.starttime = 0 |
---|
103 | |
---|
104 | def compute(self): |
---|
105 | """ |
---|
106 | Compute the data given a model function |
---|
107 | """ |
---|
108 | |
---|
109 | output = numpy.zeros((len(self.x),len(self.y))) |
---|
110 | if self.qmin==None: |
---|
111 | self.qmin = 0 |
---|
112 | if self.qmax== None: |
---|
113 | if self.data !=None: |
---|
114 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
115 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
116 | self.qmax=math.sqrt( newx + newy ) |
---|
117 | |
---|
118 | |
---|
119 | self.starttime = time.time() |
---|
120 | lx = len(self.x) |
---|
121 | for i_x in range(len(self.x)): |
---|
122 | # Check whether we need to bail out |
---|
123 | self.update(output=output ) |
---|
124 | self.isquit() |
---|
125 | |
---|
126 | for i_y in range(int(len(self.y))): |
---|
127 | radius = math.sqrt(self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) |
---|
128 | ## for data ignore the qmax |
---|
129 | if self.data == None: |
---|
130 | if self.qmin <= radius : |
---|
131 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
132 | output[i_y][i_x] =value |
---|
133 | else: |
---|
134 | output[i_y][i_x] =0 |
---|
135 | else: |
---|
136 | if self.qmin <= radius and radius<= self.qmax: |
---|
137 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
138 | output[i_y][i_x] =value |
---|
139 | else: |
---|
140 | output[i_y][i_x] =0 |
---|
141 | |
---|
142 | elapsed = time.time()-self.starttime |
---|
143 | self.complete( image = output, |
---|
144 | data = self.data , |
---|
145 | model = self.model, |
---|
146 | elapsed = elapsed, |
---|
147 | qmin = self.qmin, |
---|
148 | qmax =self.qmax, |
---|
149 | qstep = self.qstep ) |
---|
150 | |
---|
151 | |
---|
152 | class Calc2D_4fold(CalcThread): |
---|
153 | """ |
---|
154 | Compute 2D model |
---|
155 | This calculation assumes a 4-fold symmetry of the model. |
---|
156 | Really is the same calculation time since we have to |
---|
157 | calculate points for 0<phi<pi anyway. |
---|
158 | """ |
---|
159 | |
---|
160 | def __init__(self, x, y, model, |
---|
161 | completefn = None, |
---|
162 | updatefn = None, |
---|
163 | yieldtime = 0.01, |
---|
164 | worktime = 0.01 |
---|
165 | ): |
---|
166 | CalcThread.__init__(self,completefn, |
---|
167 | updatefn, |
---|
168 | yieldtime, |
---|
169 | worktime) |
---|
170 | self.x = x |
---|
171 | self.y = y |
---|
172 | self.model = model |
---|
173 | self.starttime = 0 |
---|
174 | |
---|
175 | def compute(self): |
---|
176 | x = self.x |
---|
177 | y = self.y |
---|
178 | output = numpy.zeros((len(x),len(y))) |
---|
179 | |
---|
180 | self.starttime = time.time() |
---|
181 | lx = len(self.x) |
---|
182 | |
---|
183 | for i_x in range(int(len(self.x)/2)): |
---|
184 | if i_x%2==1: |
---|
185 | continue |
---|
186 | |
---|
187 | # Check whether we need to bail out |
---|
188 | self.update(output=output) |
---|
189 | self.isquit() |
---|
190 | |
---|
191 | for i_y in range(int(len(self.y)/2)): |
---|
192 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
193 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
194 | output[i_y][i_x] = value1 + value2 |
---|
195 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
196 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
197 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
198 | |
---|
199 | if lx%2==1: |
---|
200 | i_x = int(len(self.x)/2) |
---|
201 | for i_y in range(int(len(self.y)/2)): |
---|
202 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
203 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
204 | output[i_y][i_x] = value1 + value2 |
---|
205 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
206 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
207 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
208 | |
---|
209 | for i_x in range(int(len(self.x)/2)): |
---|
210 | if not i_x%2==1: |
---|
211 | continue |
---|
212 | |
---|
213 | # Check whether we need to bail out |
---|
214 | self.update(output=output) |
---|
215 | self.isquit() |
---|
216 | |
---|
217 | for i_y in range(int(len(self.y)/2)): |
---|
218 | value1 = self.model.runXY([self.x[i_x], self.y[i_y]]) |
---|
219 | value2 = self.model.runXY([self.x[i_x], self.y[lx-i_y-1]]) |
---|
220 | output[i_y][i_x] = value1 + value2 |
---|
221 | output[lx-i_y-1][lx-i_x-1] = value1 + value2 |
---|
222 | output[lx-i_y-1][i_x] = value1 + value2 |
---|
223 | output[i_y][lx-i_x-1] = value1 + value2 |
---|
224 | |
---|
225 | elapsed = time.time()-self.starttime |
---|
226 | self.complete(output=output, elapsed=elapsed) |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | class Calc1D(CalcThread): |
---|
231 | """Compute 1D data""" |
---|
232 | |
---|
233 | def __init__(self, x, model, |
---|
234 | data=None, |
---|
235 | qmin=None, |
---|
236 | qmax=None, |
---|
237 | smearer=None, |
---|
238 | completefn = None, |
---|
239 | updatefn = None, |
---|
240 | yieldtime = 0.01, |
---|
241 | worktime = 0.01 |
---|
242 | ): |
---|
243 | CalcThread.__init__(self,completefn, |
---|
244 | updatefn, |
---|
245 | yieldtime, |
---|
246 | worktime) |
---|
247 | self.x = x |
---|
248 | self.data= data |
---|
249 | self.qmin= qmin |
---|
250 | self.qmax= qmax |
---|
251 | self.model = model |
---|
252 | self.smearer= smearer |
---|
253 | self.starttime = 0 |
---|
254 | |
---|
255 | def compute(self): |
---|
256 | """ |
---|
257 | Compute model 1d value given qmin , qmax , x value |
---|
258 | """ |
---|
259 | output = numpy.zeros(len(self.x)) |
---|
260 | |
---|
261 | self.starttime = time.time() |
---|
262 | |
---|
263 | for i_x in range(len(self.x)): |
---|
264 | self.update(x= self.x, output=output ) |
---|
265 | # Check whether we need to bail out |
---|
266 | self.isquit() |
---|
267 | if self.qmin <= self.x[i_x] and self.x[i_x] <= self.qmax: |
---|
268 | value = self.model.run(self.x[i_x]) |
---|
269 | output[i_x] = value |
---|
270 | |
---|
271 | ##smearer the ouput of the plot |
---|
272 | if self.smearer!=None: |
---|
273 | output = self.smearer(output) #Todo: Why always output[0]=0??? |
---|
274 | |
---|
275 | ######Temp. FIX for Qrange w/ smear. #ToDo: Should not pass all the data to 'run' or 'smear'... |
---|
276 | for i_x in range(len(self.x)): |
---|
277 | if self.qmin > self.x[i_x] or self.x[i_x] > self.qmax: |
---|
278 | output[i_x] = None |
---|
279 | |
---|
280 | elapsed = time.time()-self.starttime |
---|
281 | self.complete(x= self.x, y= output, |
---|
282 | elapsed=elapsed, model= self.model, data=self.data) |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | class CalcCommandline: |
---|
287 | def __init__(self, n=20000): |
---|
288 | #print thread.get_ident() |
---|
289 | from sans.models.CylinderModel import CylinderModel |
---|
290 | |
---|
291 | model = CylinderModel() |
---|
292 | |
---|
293 | |
---|
294 | print model.runXY([0.01, 0.02]) |
---|
295 | |
---|
296 | qmax = 0.01 |
---|
297 | qstep = 0.0001 |
---|
298 | self.done = False |
---|
299 | |
---|
300 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
301 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
302 | |
---|
303 | |
---|
304 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
305 | completefn=self.complete, |
---|
306 | updatefn=self.update , |
---|
307 | yieldtime=0.0) |
---|
308 | |
---|
309 | calc_thread_2D.queue() |
---|
310 | calc_thread_2D.ready(2.5) |
---|
311 | |
---|
312 | while not self.done: |
---|
313 | time.sleep(1) |
---|
314 | |
---|
315 | def update(self,output): |
---|
316 | print "update" |
---|
317 | |
---|
318 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
319 | print "complete" |
---|
320 | self.done = True |
---|
321 | |
---|
322 | if __name__ == "__main__": |
---|
323 | CalcCommandline() |
---|
324 | |
---|