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