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