1 | |
---|
2 | import time |
---|
3 | import sys |
---|
4 | import numpy |
---|
5 | import math |
---|
6 | from data_util.calcthread import CalcThread |
---|
7 | |
---|
8 | class Calc2D(CalcThread): |
---|
9 | """ |
---|
10 | Compute 2D model |
---|
11 | This calculation assumes a 2-fold symmetry of the model |
---|
12 | where points are computed for one half of the detector |
---|
13 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
14 | |
---|
15 | """ |
---|
16 | |
---|
17 | def __init__(self, x, y, data,model,qmin, qmax,qstep, |
---|
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.model = model |
---|
35 | self.starttime = 0 |
---|
36 | |
---|
37 | def compute(self): |
---|
38 | """ |
---|
39 | Compute the data given a model function |
---|
40 | |
---|
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.mask = self.data.mask |
---|
57 | else: |
---|
58 | xbin = numpy.linspace(start= -1*self.qmax, |
---|
59 | stop= self.qmax, |
---|
60 | num= self.qstep, |
---|
61 | endpoint=True ) |
---|
62 | ybin = numpy.linspace(start= -1*self.qmax, |
---|
63 | stop= self.qmax, |
---|
64 | num= self.qstep, |
---|
65 | endpoint=True ) |
---|
66 | |
---|
67 | new_xbin = numpy.tile(xbin, (len(ybin),1)) |
---|
68 | new_ybin = numpy.tile(ybin, (len(xbin),1)) |
---|
69 | new_ybin = new_ybin.swapaxes(0,1) |
---|
70 | new_xbin = new_xbin.flatten() |
---|
71 | new_ybin = new_ybin.flatten() |
---|
72 | self.qy_data = new_ybin |
---|
73 | self.qx_data = new_xbin |
---|
74 | # fake data |
---|
75 | self.I_data = numpy.ones(len(self.qx_data)) |
---|
76 | |
---|
77 | self.mask = numpy.ones(len(self.qx_data),dtype=bool) |
---|
78 | |
---|
79 | # Define matrix where data will be plotted |
---|
80 | radius= numpy.sqrt( self.qx_data*self.qx_data + self.qy_data*self.qy_data ) |
---|
81 | index_data= (self.qmin<= radius)&(self.mask) |
---|
82 | |
---|
83 | # For theory, qmax is based on 1d qmax |
---|
84 | # so that must be mulitified by sqrt(2) to get actual max for 2d |
---|
85 | index_model = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
86 | self.mask = (index_model)&(self.mask) |
---|
87 | self.mask = (self.mask)&(numpy.isfinite(self.I_data)) |
---|
88 | if self.data ==None: |
---|
89 | # Only qmin value will be consider for the detector |
---|
90 | self.mask = index_data |
---|
91 | |
---|
92 | value = self.model.evalDistribution([self.qx_data[self.mask],self.qy_data[self.mask]] ) |
---|
93 | |
---|
94 | output = numpy.zeros(len(self.mask)) |
---|
95 | |
---|
96 | # output default is None |
---|
97 | # This method is to distinguish between masked point and data point = 0. |
---|
98 | output = output/output |
---|
99 | # set value for self.mask==True, else still None to Plottools |
---|
100 | output[self.mask] = value |
---|
101 | |
---|
102 | elapsed = time.time()-self.starttime |
---|
103 | self.complete( image = output, |
---|
104 | data = self.data , |
---|
105 | model = self.model, |
---|
106 | elapsed = elapsed, |
---|
107 | qmin = self.qmin, |
---|
108 | qmax = self.qmax, |
---|
109 | qstep = self.qstep ) |
---|
110 | |
---|
111 | class Calc1D(CalcThread): |
---|
112 | """ |
---|
113 | Compute 1D data |
---|
114 | |
---|
115 | """ |
---|
116 | |
---|
117 | def __init__(self, x, model, |
---|
118 | data=None, |
---|
119 | qmin=None, |
---|
120 | qmax=None, |
---|
121 | smearer=None, |
---|
122 | completefn = None, |
---|
123 | updatefn = None, |
---|
124 | yieldtime = 0.01, |
---|
125 | worktime = 0.01 |
---|
126 | ): |
---|
127 | CalcThread.__init__(self,completefn, |
---|
128 | updatefn, |
---|
129 | yieldtime, |
---|
130 | worktime) |
---|
131 | self.x = numpy.array(x) |
---|
132 | self.data= data |
---|
133 | self.qmin= qmin |
---|
134 | self.qmax= qmax |
---|
135 | self.model = model |
---|
136 | self.smearer= smearer |
---|
137 | self.starttime = 0 |
---|
138 | |
---|
139 | def compute(self): |
---|
140 | """ |
---|
141 | Compute model 1d value given qmin , qmax , x value |
---|
142 | |
---|
143 | """ |
---|
144 | |
---|
145 | self.starttime = time.time() |
---|
146 | output = numpy.zeros((len(self.x))) |
---|
147 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
148 | |
---|
149 | ##smearer the ouput of the plot |
---|
150 | if self.smearer!=None: |
---|
151 | first_bin, last_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
152 | output[first_bin:last_bin] = self.model.evalDistribution(self.x[first_bin:last_bin]) |
---|
153 | output = self.smearer(output, first_bin, last_bin) |
---|
154 | else: |
---|
155 | output[index] = self.model.evalDistribution(self.x[index]) |
---|
156 | |
---|
157 | elapsed = time.time()-self.starttime |
---|
158 | |
---|
159 | self.complete(x= self.x[index], y= output[index], |
---|
160 | elapsed=elapsed, model= self.model, data=self.data) |
---|
161 | """ |
---|
162 | Example of use of Calc2D :: |
---|
163 | |
---|
164 | class CalcCommandline: |
---|
165 | |
---|
166 | def __init__(self, n=20000): |
---|
167 | #print thread.get_ident() |
---|
168 | from sans.models.CylinderModel import CylinderModel |
---|
169 | |
---|
170 | model = CylinderModel() |
---|
171 | print model.runXY([0.01, 0.02]) |
---|
172 | |
---|
173 | qmax = 0.01 |
---|
174 | qstep = 0.0001 |
---|
175 | self.done = False |
---|
176 | |
---|
177 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
178 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
179 | |
---|
180 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
181 | completefn=self.complete, |
---|
182 | updatefn=self.update , |
---|
183 | yieldtime=0.0) |
---|
184 | |
---|
185 | calc_thread_2D.queue() |
---|
186 | calc_thread_2D.ready(2.5) |
---|
187 | |
---|
188 | while not self.done: |
---|
189 | time.sleep(1) |
---|
190 | |
---|
191 | def update(self,output): |
---|
192 | print "update" |
---|
193 | |
---|
194 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
195 | print "complete" |
---|
196 | self.done = True |
---|
197 | |
---|
198 | if __name__ == "__main__": |
---|
199 | CalcCommandline() |
---|
200 | |
---|
201 | """ |
---|
202 | |
---|