1 | |
---|
2 | |
---|
3 | import time |
---|
4 | from data_util.calcthread import CalcThread |
---|
5 | import sys |
---|
6 | import numpy,math |
---|
7 | from DataLoader.smearing_2d import Smearer2D |
---|
8 | |
---|
9 | class Calc2D(CalcThread): |
---|
10 | """ |
---|
11 | Compute 2D model |
---|
12 | This calculation assumes a 2-fold symmetry of the model |
---|
13 | where points are computed for one half of the detector |
---|
14 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
15 | """ |
---|
16 | def __init__(self, x, y, data,model,smearer,qmin, qmax,qstep, |
---|
17 | id , |
---|
18 | state=None, |
---|
19 | toggle_mode_on=False, |
---|
20 | completefn = None, |
---|
21 | updatefn = None, |
---|
22 | yieldtime = 0.01, |
---|
23 | worktime = 0.01 |
---|
24 | ): |
---|
25 | CalcThread.__init__(self,completefn, |
---|
26 | updatefn, |
---|
27 | yieldtime, |
---|
28 | worktime) |
---|
29 | self.qmin= qmin |
---|
30 | self.qmax= qmax |
---|
31 | self.qstep= qstep |
---|
32 | self.toggle_mode_on = toggle_mode_on |
---|
33 | self.x = x |
---|
34 | self.y = y |
---|
35 | self.data= data |
---|
36 | self.page_id = id |
---|
37 | self.state = None |
---|
38 | # the model on to calculate |
---|
39 | self.model = model |
---|
40 | self.smearer = smearer#(data=self.data,model=self.model) |
---|
41 | self.starttime = 0 |
---|
42 | |
---|
43 | def compute(self): |
---|
44 | """ |
---|
45 | Compute the data given a model function |
---|
46 | """ |
---|
47 | self.starttime = time.time() |
---|
48 | # Determine appropriate q range |
---|
49 | if self.qmin==None: |
---|
50 | self.qmin = 0 |
---|
51 | if self.qmax== None: |
---|
52 | if self.data !=None: |
---|
53 | newx= math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
54 | newy= math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2) |
---|
55 | self.qmax=math.sqrt( newx + newy ) |
---|
56 | |
---|
57 | if self.data != None: |
---|
58 | self.I_data = self.data.data |
---|
59 | self.qx_data = self.data.qx_data |
---|
60 | self.qy_data = self.data.qy_data |
---|
61 | self.dqx_data = self.data.dqx_data |
---|
62 | self.dqy_data = self.data.dqy_data |
---|
63 | self.mask = self.data.mask |
---|
64 | else: |
---|
65 | xbin = numpy.linspace(start= -1*self.qmax, |
---|
66 | stop= self.qmax, |
---|
67 | num= self.qstep, |
---|
68 | endpoint=True ) |
---|
69 | ybin = numpy.linspace(start= -1*self.qmax, |
---|
70 | stop= self.qmax, |
---|
71 | num= self.qstep, |
---|
72 | endpoint=True ) |
---|
73 | |
---|
74 | new_xbin = numpy.tile(xbin, (len(ybin),1)) |
---|
75 | new_ybin = numpy.tile(ybin, (len(xbin),1)) |
---|
76 | new_ybin = new_ybin.swapaxes(0,1) |
---|
77 | new_xbin = new_xbin.flatten() |
---|
78 | new_ybin = new_ybin.flatten() |
---|
79 | self.qy_data = new_ybin |
---|
80 | self.qx_data = new_xbin |
---|
81 | # fake data |
---|
82 | self.I_data = numpy.ones(len(self.qx_data)) |
---|
83 | |
---|
84 | self.mask = numpy.ones(len(self.qx_data),dtype=bool) |
---|
85 | |
---|
86 | # Define matrix where data will be plotted |
---|
87 | radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data ) |
---|
88 | index_data= (self.qmin<= radius)&(self.mask) |
---|
89 | |
---|
90 | # For theory, qmax is based on 1d qmax |
---|
91 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
92 | index_model = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
93 | index_model = (index_model)&(self.mask) |
---|
94 | index_model = (index_model)&(numpy.isfinite(self.I_data)) |
---|
95 | if self.data ==None: |
---|
96 | # Only qmin value will be consider for the detector |
---|
97 | index_model = index_data |
---|
98 | |
---|
99 | if self.smearer != None: |
---|
100 | # Set smearer w/ data, model and index. |
---|
101 | fn = self.smearer |
---|
102 | fn.set_model(self.model) |
---|
103 | fn.set_index(index_model) |
---|
104 | # Get necessary data from self.data and set the data for smearing |
---|
105 | fn.get_data() |
---|
106 | # Calculate smeared Intensity (by Gaussian averaging): DataLoader/smearing2d/Smearer2D() |
---|
107 | value = fn.get_value() |
---|
108 | |
---|
109 | else: |
---|
110 | # calculation w/o smearing |
---|
111 | value = self.model.evalDistribution([self.qx_data[index_model],self.qy_data[index_model]]) |
---|
112 | |
---|
113 | output = numpy.zeros(len(self.qx_data)) |
---|
114 | |
---|
115 | # output default is None |
---|
116 | # This method is to distinguish between masked point(nan) and data point = 0. |
---|
117 | output = output/output |
---|
118 | # set value for self.mask==True, else still None to Plottools |
---|
119 | output[index_model] = value |
---|
120 | |
---|
121 | elapsed = time.time()-self.starttime |
---|
122 | self.complete(image=output, |
---|
123 | data=self.data, |
---|
124 | id=self.page_id, |
---|
125 | model=self.model, |
---|
126 | state=self.state, |
---|
127 | toggle_mode_on=self.toggle_mode_on, |
---|
128 | elapsed=elapsed, |
---|
129 | index=index_model, |
---|
130 | qmin=self.qmin, |
---|
131 | qmax=self.qmax, |
---|
132 | qstep=self.qstep) |
---|
133 | |
---|
134 | |
---|
135 | class Calc1D(CalcThread): |
---|
136 | """ |
---|
137 | Compute 1D data |
---|
138 | """ |
---|
139 | def __init__(self, x, model, |
---|
140 | id, |
---|
141 | data=None, |
---|
142 | qmin=None, |
---|
143 | qmax=None, |
---|
144 | smearer=None, |
---|
145 | toggle_mode_on=False, |
---|
146 | state=None, |
---|
147 | completefn = None, |
---|
148 | updatefn = None, |
---|
149 | yieldtime = 0.01, |
---|
150 | worktime = 0.01 |
---|
151 | ): |
---|
152 | """ |
---|
153 | """ |
---|
154 | CalcThread.__init__(self,completefn, |
---|
155 | updatefn, |
---|
156 | yieldtime, |
---|
157 | worktime) |
---|
158 | self.x = numpy.array(x) |
---|
159 | self.data = data |
---|
160 | self.qmin = qmin |
---|
161 | self.qmax = qmax |
---|
162 | self.model = model |
---|
163 | self.toggle_mode_on = toggle_mode_on |
---|
164 | self.state = state |
---|
165 | self.page_id = id |
---|
166 | self.smearer = smearer |
---|
167 | self.starttime = 0 |
---|
168 | |
---|
169 | def compute(self): |
---|
170 | """ |
---|
171 | Compute model 1d value given qmin , qmax , x value |
---|
172 | """ |
---|
173 | self.starttime = time.time() |
---|
174 | output = numpy.zeros((len(self.x))) |
---|
175 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
176 | |
---|
177 | ##smearer the ouput of the plot |
---|
178 | if self.smearer!=None: |
---|
179 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
180 | output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) |
---|
181 | output = self.smearer(output, first_bin, last_bin) |
---|
182 | else: |
---|
183 | output[index] = self.model.evalDistribution(self.x[index]) |
---|
184 | |
---|
185 | elapsed = time.time() - self.starttime |
---|
186 | |
---|
187 | self.complete(x=self.x[index], y=output[index], |
---|
188 | id=self.page_id, |
---|
189 | state=self.state, |
---|
190 | toggle_mode_on=self.toggle_mode_on, |
---|
191 | elapsed=elapsed,index=index, model=self.model, |
---|
192 | data=self.data) |
---|
193 | |
---|
194 | def results(self): |
---|
195 | """ |
---|
196 | Send resuts of the computation |
---|
197 | """ |
---|
198 | return [self.out, self.index] |
---|
199 | |
---|
200 | """ |
---|
201 | Example: :: |
---|
202 | |
---|
203 | class CalcCommandline: |
---|
204 | def __init__(self, n=20000): |
---|
205 | #print thread.get_ident() |
---|
206 | from sans.models.CylinderModel import CylinderModel |
---|
207 | |
---|
208 | model = CylinderModel() |
---|
209 | |
---|
210 | |
---|
211 | print model.runXY([0.01, 0.02]) |
---|
212 | |
---|
213 | qmax = 0.01 |
---|
214 | qstep = 0.0001 |
---|
215 | self.done = False |
---|
216 | |
---|
217 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
218 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
219 | |
---|
220 | |
---|
221 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
222 | -qmax, qmax,qstep, |
---|
223 | completefn=self.complete, |
---|
224 | updatefn=self.update , |
---|
225 | yieldtime=0.0) |
---|
226 | |
---|
227 | calc_thread_2D.queue() |
---|
228 | calc_thread_2D.ready(2.5) |
---|
229 | |
---|
230 | while not self.done: |
---|
231 | time.sleep(1) |
---|
232 | |
---|
233 | def update(self,output): |
---|
234 | print "update" |
---|
235 | |
---|
236 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
237 | print "complete" |
---|
238 | self.done = True |
---|
239 | |
---|
240 | if __name__ == "__main__": |
---|
241 | CalcCommandline() |
---|
242 | """ |
---|