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