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