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