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 | # Reshape dimensions of x and y to call evalDistribution |
---|
28 | self.x_array = numpy.reshape(x,[1,len(x)]) |
---|
29 | self.y_array = numpy.reshape(y,[len(y),1]) |
---|
30 | # Numpy array of dimensions 1 used for model.run method |
---|
31 | self.x= numpy.array(x) |
---|
32 | self.y= numpy.array(y) |
---|
33 | self.data= data |
---|
34 | # the model on to calculate |
---|
35 | self.model = model |
---|
36 | self.starttime = 0 |
---|
37 | |
---|
38 | def compute(self): |
---|
39 | """ |
---|
40 | Compute the data given a model function |
---|
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 | # Define matrix where data will be plotted |
---|
52 | radius= numpy.sqrt(self.x_array**2 + self.y_array**2) |
---|
53 | index_data= (self.qmin<= radius) |
---|
54 | index_model = (self.qmin <= radius)&(radius<= self.qmax) |
---|
55 | |
---|
56 | ## receive only list of 2 numpy array |
---|
57 | ## One must reshape to vertical and the other to horizontal |
---|
58 | value = self.model.evalDistribution([self.y_array,self.x_array] ) |
---|
59 | ## for data ignore the qmax |
---|
60 | if self.data == None: |
---|
61 | # Only qmin value will be consider for the detector |
---|
62 | output = value *index_data |
---|
63 | else: |
---|
64 | # The user can define qmin and qmax for the detector |
---|
65 | output = value*index_model |
---|
66 | |
---|
67 | elapsed = time.time()-self.starttime |
---|
68 | self.complete( image = output, |
---|
69 | data = self.data , |
---|
70 | model = self.model, |
---|
71 | elapsed = elapsed, |
---|
72 | qmin = self.qmin, |
---|
73 | qmax =self.qmax, |
---|
74 | qstep = self.qstep ) |
---|
75 | |
---|
76 | def compute_point(self): |
---|
77 | """ |
---|
78 | Compute the data given a model function. Loop through each point |
---|
79 | of x and y to compute the model |
---|
80 | @return output : is a matrix of size x*y |
---|
81 | """ |
---|
82 | output = numpy.zeros((len(self.x),len(self.y))) |
---|
83 | |
---|
84 | for i_x in range(len(self.x)): |
---|
85 | # Check whether we need to bail out |
---|
86 | self.update(output=output ) |
---|
87 | self.isquit() |
---|
88 | |
---|
89 | for i_y in range(int(len(self.y))): |
---|
90 | radius = math.sqrt(self.x[i_x]*self.x[i_x]+self.y[i_y]*self.y[i_y]) |
---|
91 | ## for data ignore the qmax |
---|
92 | if self.data == None: |
---|
93 | if self.qmin <= radius : |
---|
94 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
95 | output[i_y][i_x] =value |
---|
96 | else: |
---|
97 | output[i_y][i_x] =0 |
---|
98 | else: |
---|
99 | if self.qmin <= radius and radius<= self.qmax: |
---|
100 | value = self.model.runXY( [self.x[i_x], self.y[i_y]] ) |
---|
101 | output[i_y][i_x] =value |
---|
102 | else: |
---|
103 | output[i_y][i_x] =0 |
---|
104 | return output |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | class Calc1D(CalcThread): |
---|
109 | """Compute 1D data""" |
---|
110 | |
---|
111 | def __init__(self, x, model, |
---|
112 | data=None, |
---|
113 | qmin=None, |
---|
114 | qmax=None, |
---|
115 | smearer=None, |
---|
116 | completefn = None, |
---|
117 | updatefn = None, |
---|
118 | yieldtime = 0.01, |
---|
119 | worktime = 0.01 |
---|
120 | ): |
---|
121 | CalcThread.__init__(self,completefn, |
---|
122 | updatefn, |
---|
123 | yieldtime, |
---|
124 | worktime) |
---|
125 | self.x = numpy.array(x) |
---|
126 | self.data= data |
---|
127 | self.qmin= qmin |
---|
128 | self.qmax= qmax |
---|
129 | self.model = model |
---|
130 | self.smearer= smearer |
---|
131 | self.starttime = 0 |
---|
132 | |
---|
133 | def compute(self): |
---|
134 | """ |
---|
135 | Compute model 1d value given qmin , qmax , x value |
---|
136 | """ |
---|
137 | |
---|
138 | self.starttime = time.time() |
---|
139 | |
---|
140 | index= (self.qmin <= self.x)& (self.x <= self.qmax) |
---|
141 | output = self.model.evalDistribution(self.x[index]) |
---|
142 | |
---|
143 | ##smearer the ouput of the plot |
---|
144 | if self.smearer!=None: |
---|
145 | output = self.smearer(output) #Todo: Why always output[0]=0??? |
---|
146 | |
---|
147 | ######Temp. FIX for Qrange w/ smear. #ToDo: Should not pass all the data to 'run' or 'smear'... |
---|
148 | new_index = (self.qmin > self.x) |(self.x > self.qmax) |
---|
149 | output[new_index] = None |
---|
150 | |
---|
151 | elapsed = time.time()-self.starttime |
---|
152 | |
---|
153 | self.complete(x= self.x, y= output, |
---|
154 | elapsed=elapsed, model= self.model, data=self.data) |
---|
155 | |
---|
156 | def compute_point(self): |
---|
157 | """ |
---|
158 | Compute the data given a model function. Loop through each point |
---|
159 | of x compute the model |
---|
160 | @return output : is a numpy vector of size x |
---|
161 | """ |
---|
162 | output = numpy.zeros(len(self.x)) |
---|
163 | # Loop through each q of data.x |
---|
164 | for i_x in range(len(self.x)): |
---|
165 | self.update(x= self.x, output=output ) |
---|
166 | # Check whether we need to bail out |
---|
167 | self.isquit() |
---|
168 | if self.qmin <= self.x[i_x] and self.x[i_x] <= self.qmax: |
---|
169 | value = self.model.run(self.x[i_x]) |
---|
170 | output[i_x] = value |
---|
171 | |
---|
172 | return output |
---|
173 | |
---|
174 | |
---|
175 | class CalcCommandline: |
---|
176 | def __init__(self, n=20000): |
---|
177 | #print thread.get_ident() |
---|
178 | from sans.models.CylinderModel import CylinderModel |
---|
179 | |
---|
180 | model = CylinderModel() |
---|
181 | |
---|
182 | |
---|
183 | print model.runXY([0.01, 0.02]) |
---|
184 | |
---|
185 | qmax = 0.01 |
---|
186 | qstep = 0.0001 |
---|
187 | self.done = False |
---|
188 | |
---|
189 | x = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
190 | y = numpy.arange(-qmax, qmax+qstep*0.01, qstep) |
---|
191 | |
---|
192 | |
---|
193 | calc_thread_2D = Calc2D(x, y, None, model.clone(),-qmax, qmax,qstep, |
---|
194 | completefn=self.complete, |
---|
195 | updatefn=self.update , |
---|
196 | yieldtime=0.0) |
---|
197 | |
---|
198 | calc_thread_2D.queue() |
---|
199 | calc_thread_2D.ready(2.5) |
---|
200 | |
---|
201 | while not self.done: |
---|
202 | time.sleep(1) |
---|
203 | |
---|
204 | def update(self,output): |
---|
205 | print "update" |
---|
206 | |
---|
207 | def complete(self, image, data, model, elapsed, qmin, qmax, qstep ): |
---|
208 | print "complete" |
---|
209 | self.done = True |
---|
210 | |
---|
211 | if __name__ == "__main__": |
---|
212 | CalcCommandline() |
---|
213 | |
---|