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