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 | from sas.sascalc.fit.MultiplicationModel import MultiplicationModel |
---|
10 | |
---|
11 | class Calc2D(CalcThread): |
---|
12 | """ |
---|
13 | Compute 2D model |
---|
14 | This calculation assumes a 2-fold symmetry of the model |
---|
15 | where points are computed for one half of the detector |
---|
16 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
17 | """ |
---|
18 | def __init__(self, data, model, smearer, qmin, qmax, page_id, |
---|
19 | state=None, |
---|
20 | weight=None, |
---|
21 | fid=None, |
---|
22 | toggle_mode_on=False, |
---|
23 | completefn=None, |
---|
24 | updatefn=None, |
---|
25 | update_chisqr=True, |
---|
26 | source='model', |
---|
27 | yieldtime=0.04, |
---|
28 | worktime=0.04, |
---|
29 | exception_handler=None, |
---|
30 | ): |
---|
31 | CalcThread.__init__(self, completefn, updatefn, yieldtime, worktime, |
---|
32 | exception_handler=exception_handler) |
---|
33 | self.qmin = qmin |
---|
34 | self.qmax = qmax |
---|
35 | self.weight = weight |
---|
36 | self.fid = fid |
---|
37 | #self.qstep = qstep |
---|
38 | self.toggle_mode_on = toggle_mode_on |
---|
39 | self.data = data |
---|
40 | self.page_id = page_id |
---|
41 | self.state = None |
---|
42 | # the model on to calculate |
---|
43 | self.model = model |
---|
44 | self.smearer = smearer |
---|
45 | self.starttime = 0 |
---|
46 | self.update_chisqr = update_chisqr |
---|
47 | self.source = source |
---|
48 | |
---|
49 | def compute(self): |
---|
50 | """ |
---|
51 | Compute the data given a model function |
---|
52 | """ |
---|
53 | self.starttime = time.time() |
---|
54 | # Determine appropriate q range |
---|
55 | if self.qmin == None: |
---|
56 | self.qmin = 0 |
---|
57 | if self.qmax == None: |
---|
58 | if self.data != None: |
---|
59 | newx = math.pow(max(math.fabs(self.data.xmax), |
---|
60 | math.fabs(self.data.xmin)), 2) |
---|
61 | newy = math.pow(max(math.fabs(self.data.ymax), |
---|
62 | math.fabs(self.data.ymin)), 2) |
---|
63 | self.qmax = math.sqrt(newx + newy) |
---|
64 | |
---|
65 | if self.data is None: |
---|
66 | msg = "Compute Calc2D receive data = %s.\n" % str(self.data) |
---|
67 | raise ValueError, msg |
---|
68 | |
---|
69 | # Define matrix where data will be plotted |
---|
70 | radius = numpy.sqrt((self.data.qx_data * self.data.qx_data) + \ |
---|
71 | (self.data.qy_data * self.data.qy_data)) |
---|
72 | |
---|
73 | # For theory, qmax is based on 1d qmax |
---|
74 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
75 | index_model = (self.qmin <= radius) & (radius <= self.qmax) |
---|
76 | index_model = index_model & self.data.mask |
---|
77 | index_model = index_model & numpy.isfinite(self.data.data) |
---|
78 | |
---|
79 | if self.smearer is not None: |
---|
80 | # Set smearer w/ data, model and index. |
---|
81 | fn = self.smearer |
---|
82 | fn.set_model(self.model) |
---|
83 | fn.set_index(index_model) |
---|
84 | # Calculate smeared Intensity |
---|
85 | #(by Gaussian averaging): DataLoader/smearing2d/Smearer2D() |
---|
86 | value = fn.get_value() |
---|
87 | else: |
---|
88 | # calculation w/o smearing |
---|
89 | value = self.model.evalDistribution([ |
---|
90 | self.data.qx_data[index_model], |
---|
91 | self.data.qy_data[index_model] |
---|
92 | ]) |
---|
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 | # If we use a smearer, also return the unsmeared model |
---|
169 | unsmeared_output = None |
---|
170 | unsmeared_data = None |
---|
171 | unsmeared_error = None |
---|
172 | ##smearer the ouput of the plot |
---|
173 | if self.smearer is not None: |
---|
174 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, |
---|
175 | self.qmax) |
---|
176 | mask = self.data.x[first_bin:last_bin+1] |
---|
177 | unsmeared_output = numpy.zeros((len(self.data.x))) |
---|
178 | unsmeared_output[first_bin:last_bin+1] = self.model.evalDistribution(mask) |
---|
179 | output = self.smearer(unsmeared_output, first_bin, last_bin) |
---|
180 | |
---|
181 | # Rescale data to unsmeared model |
---|
182 | # Check that the arrays are compatible. If we only have a model but no data, |
---|
183 | # the length of data.y will be zero. |
---|
184 | if isinstance(self.data.y, numpy.ndarray) and output.shape == self.data.y.shape: |
---|
185 | unsmeared_data = numpy.zeros((len(self.data.x))) |
---|
186 | unsmeared_error = numpy.zeros((len(self.data.x))) |
---|
187 | unsmeared_data[first_bin:last_bin+1] = self.data.y[first_bin:last_bin+1]\ |
---|
188 | * unsmeared_output[first_bin:last_bin+1]\ |
---|
189 | / output[first_bin:last_bin+1] |
---|
190 | unsmeared_error[first_bin:last_bin+1] = self.data.dy[first_bin:last_bin+1]\ |
---|
191 | * unsmeared_output[first_bin:last_bin+1]\ |
---|
192 | / output[first_bin:last_bin+1] |
---|
193 | unsmeared_output=unsmeared_output[index] |
---|
194 | unsmeared_data=unsmeared_data[index] |
---|
195 | unsmeared_error=unsmeared_error |
---|
196 | else: |
---|
197 | output[index] = self.model.evalDistribution(self.data.x[index]) |
---|
198 | |
---|
199 | sq_model = None |
---|
200 | pq_model = None |
---|
201 | if isinstance(self.model, MultiplicationModel): |
---|
202 | sq_model = numpy.zeros((len(self.data.x))) |
---|
203 | pq_model = numpy.zeros((len(self.data.x))) |
---|
204 | sq_model[index] = self.model.s_model.evalDistribution(self.data.x[index]) |
---|
205 | pq_model[index] = self.model.p_model.evalDistribution(self.data.x[index]) |
---|
206 | |
---|
207 | elapsed = time.time() - self.starttime |
---|
208 | |
---|
209 | self.complete(x=self.data.x[index], y=output[index], |
---|
210 | page_id=self.page_id, |
---|
211 | state=self.state, |
---|
212 | weight=self.weight, |
---|
213 | fid=self.fid, |
---|
214 | toggle_mode_on=self.toggle_mode_on, |
---|
215 | elapsed=elapsed, index=index, model=self.model, |
---|
216 | data=self.data, |
---|
217 | update_chisqr=self.update_chisqr, |
---|
218 | source=self.source, |
---|
219 | unsmeared_model=unsmeared_output, |
---|
220 | unsmeared_data=unsmeared_data, |
---|
221 | unsmeared_error=unsmeared_error, |
---|
222 | pq_model=pq_model, |
---|
223 | sq_model=sq_model) |
---|
224 | |
---|
225 | def results(self): |
---|
226 | """ |
---|
227 | Send resuts of the computation |
---|
228 | """ |
---|
229 | return [self.out, self.index] |
---|
230 | |
---|
231 | """ |
---|
232 | Example: :: |
---|
233 | |
---|
234 | class CalcCommandline: |
---|
235 | def __init__(self, n=20000): |
---|
236 | #print thread.get_ident() |
---|
237 | from sas.models.CylinderModel import CylinderModel |
---|
238 | |
---|
239 | model = CylinderModel() |
---|
240 | |
---|
241 | |
---|
242 | print model.runXY([0.01, 0.02]) |
---|
243 | |
---|
244 | qmax = 0.01 |
---|
245 | qstep = 0.0001 |
---|
246 | self.done = False |
---|
247 | |
---|
248 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
249 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
250 | |
---|
251 | |
---|
252 | calc_thread_2D = Calc2D(x, y, None, model.clone(),None, |
---|
253 | -qmax, qmax,qstep, |
---|
254 | completefn=self.complete, |
---|
255 | updatefn=self.update , |
---|
256 | yieldtime=0.0) |
---|
257 | |
---|
258 | calc_thread_2D.queue() |
---|
259 | calc_thread_2D.ready(2.5) |
---|
260 | |
---|
261 | while not self.done: |
---|
262 | time.sleep(1) |
---|
263 | |
---|
264 | def update(self,output): |
---|
265 | print "update" |
---|
266 | |
---|
267 | def complete(self, image, data, model, elapsed, qmin, qmax,index, qstep ): |
---|
268 | print "complete" |
---|
269 | self.done = True |
---|
270 | |
---|
271 | if __name__ == "__main__": |
---|
272 | CalcCommandline() |
---|
273 | """ |
---|