1 | ##################################################################### |
---|
2 | #This software was developed by the University of Tennessee as part of the |
---|
3 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | #project funded by the US National Science Foundation. |
---|
5 | #See the license text in license.txt |
---|
6 | #copyright 2008, University of Tennessee |
---|
7 | ###################################################################### |
---|
8 | |
---|
9 | ## TODO: Need test,and check Gaussian averaging |
---|
10 | import numpy, math,time |
---|
11 | ## Singular point |
---|
12 | SIGMA_ZERO = 1.0e-010 |
---|
13 | ## Limit of how many sigmas to be covered for the Gaussian smearing |
---|
14 | # default: 2.5 to cover 98.7% of Gaussian |
---|
15 | LIMIT = 2.5 |
---|
16 | ## Defaults |
---|
17 | R_BIN = {'Xhigh':10.0, 'High':5.0,'Med':5.0,'Low':3.0} |
---|
18 | PHI_BIN ={'Xhigh':20.0,'High':12.0,'Med':6.0,'Low':4.0} |
---|
19 | |
---|
20 | class Smearer2D: |
---|
21 | """ |
---|
22 | Gaussian Q smearing class for SANS 2d data |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, data=None, model=None, index=None, |
---|
26 | limit=LIMIT, accuracy='Low'): |
---|
27 | """ |
---|
28 | Assumption: equally spaced bins in dq_r, dq_phi space. |
---|
29 | |
---|
30 | :param data: 2d data used to set the smearing parameters |
---|
31 | :param model: model function |
---|
32 | :param index: 1d array with len(data) to define the range of the calculation: elements are given as True or False |
---|
33 | :param nr: number of bins in dq_r-axis |
---|
34 | :param nphi: number of bins in dq_phi-axis |
---|
35 | """ |
---|
36 | ## data |
---|
37 | self.data = data |
---|
38 | ## model |
---|
39 | self.model = model |
---|
40 | ## Accuracy: Higher stands for more sampling points in both directions of r and phi. |
---|
41 | self.accuracy = accuracy |
---|
42 | ## number of bins in r axis for over-sampling |
---|
43 | self.nr = R_BIN |
---|
44 | ## number of bins in phi axis for over-sampling |
---|
45 | self.nphi = PHI_BIN |
---|
46 | ## maximum nsigmas |
---|
47 | self.limit = limit |
---|
48 | self.index = index |
---|
49 | self.smearer = True |
---|
50 | |
---|
51 | |
---|
52 | def get_data(self): |
---|
53 | """ |
---|
54 | get qx_data, qy_data, dqx_data,dqy_data,and calculate phi_data=arctan(qx_data/qy_data) |
---|
55 | """ |
---|
56 | if self.data == None or self.data.__class__.__name__ == 'Data1D': |
---|
57 | return None |
---|
58 | if self.data.dqx_data == None or self.data.dqy_data == None: |
---|
59 | return None |
---|
60 | self.qx_data = self.data.qx_data[self.index] |
---|
61 | self.qy_data = self.data.qy_data[self.index] |
---|
62 | self.dqx_data = self.data.dqx_data[self.index] |
---|
63 | self.dqy_data = self.data.dqy_data[self.index] |
---|
64 | self.phi_data = numpy.arctan(self.qx_data/self.qy_data) |
---|
65 | ## Remove singular points if exists |
---|
66 | self.dqx_data[self.dqx_data<SIGMA_ZERO]=SIGMA_ZERO |
---|
67 | self.dqy_data[self.dqy_data<SIGMA_ZERO]=SIGMA_ZERO |
---|
68 | return True |
---|
69 | |
---|
70 | def set_accuracy(self, accuracy='Low'): |
---|
71 | """ |
---|
72 | Set accuracy. |
---|
73 | |
---|
74 | :param accuracy: string |
---|
75 | """ |
---|
76 | self.accuracy = accuracy |
---|
77 | |
---|
78 | def set_smearer(self, smearer=True): |
---|
79 | """ |
---|
80 | Set whether or not smearer will be used |
---|
81 | |
---|
82 | :param smearer: smear object |
---|
83 | |
---|
84 | """ |
---|
85 | self.smearer = smearer |
---|
86 | |
---|
87 | def set_data(self, data=None): |
---|
88 | """ |
---|
89 | Set data. |
---|
90 | |
---|
91 | :param data: DataLoader.Data_info type |
---|
92 | """ |
---|
93 | self.data = data |
---|
94 | |
---|
95 | |
---|
96 | def set_model(self, model=None): |
---|
97 | """ |
---|
98 | Set model. |
---|
99 | |
---|
100 | :param model: sans.models instance |
---|
101 | """ |
---|
102 | self.model = model |
---|
103 | |
---|
104 | def set_index(self, index=None): |
---|
105 | """ |
---|
106 | Set index. |
---|
107 | |
---|
108 | :param index: 1d arrays |
---|
109 | """ |
---|
110 | self.index = index |
---|
111 | |
---|
112 | def get_value(self): |
---|
113 | """ |
---|
114 | Over sampling of r_nbins times phi_nbins, calculate Gaussian weights, then find smeared intensity |
---|
115 | For the default values, this is equivalent (but by using numpy array |
---|
116 | the speed optimized by a factor of ten)to the following: :: |
---|
117 | |
---|
118 | |
---|
119 | Remove the singular points if exists |
---|
120 | self.dqx_data[self.dqx_data==0]=SIGMA_ZERO |
---|
121 | self.dqy_data[self.dqy_data==0]=SIGMA_ZERO |
---|
122 | |
---|
123 | for phi in range(0,4): |
---|
124 | for r in range(0,5): |
---|
125 | n = (phi)*5+(r) |
---|
126 | r = r+0.25 |
---|
127 | dphi = phi*2.0*math.pi/4.0 + numpy.arctan(self.qy_data[index_model]/self.dqy_data[index_model]/self.qx_data[index_model]*/self.dqx_data[index_model]) |
---|
128 | dq = r*sqrt( self.dqx_data[index_model]*self.dqx_data[index_model] \ |
---|
129 | + self.dqy_data[index_model]*self.dqy_data[index_model] ) |
---|
130 | #integrant of exp(-0.5*r*r) r dr at each bins : The integration may not need. |
---|
131 | weight_res[n] = e^{(-0.5*((r-0.25)*(r-0.25)))}- e^{(-0.5*((r-0.25)*(r-0.25)))} |
---|
132 | #if phi != 0 and r != 0: |
---|
133 | qx_res = numpy.append(qx_res,self.qx_data[index_model]+ dq * cos(dphi)) |
---|
134 | qy_res = numpy.append(qy_res,self.qy_data[index_model]+ dq * sin(dphi)) |
---|
135 | |
---|
136 | Then compute I(qx_res,qy_res) and do weighted averaging. |
---|
137 | |
---|
138 | """ |
---|
139 | valid = self.get_data() |
---|
140 | if valid == None: |
---|
141 | return valid |
---|
142 | # all zero values of dq |
---|
143 | if numpy.all(self.dqx_data <= 1.1e-10) and \ |
---|
144 | numpy.all(self.dqy_data <= 1.1e-10): |
---|
145 | self.smearer = False |
---|
146 | if self.smearer == False: |
---|
147 | return self.model.evalDistribution([self.qx_data,self.qy_data]) |
---|
148 | st = time.time() |
---|
149 | nr = self.nr[self.accuracy] |
---|
150 | nphi = self.nphi[self.accuracy] |
---|
151 | |
---|
152 | # data length in the range of self.index |
---|
153 | len_data = len(self.qx_data) |
---|
154 | len_datay = len(self.qy_data) |
---|
155 | |
---|
156 | # Number of bins in the dqr direction (polar coordinate of dqx and dqy) |
---|
157 | bin_size = self.limit/nr |
---|
158 | # Total number of bins = # of bins in dq_r-direction times # of bins in dq_phi-direction |
---|
159 | n_bins = nr * nphi |
---|
160 | # Mean values of dqr at each bins ,starting from the half of bin size |
---|
161 | r = bin_size/2.0+numpy.arange(nr)*bin_size |
---|
162 | # mean values of qphi at each bines |
---|
163 | phi = numpy.arange(nphi) |
---|
164 | dphi = phi*2.0*math.pi/nphi |
---|
165 | dphi = dphi.repeat(nr) |
---|
166 | ## Transform to polar coordinate, and set dphi at each data points ; 1d array |
---|
167 | dphi = dphi.repeat(len_data)+numpy.arctan(self.qy_data*self.dqx_data/self.qx_data/self.dqy_data).repeat(n_bins).reshape(len_data,n_bins).transpose().flatten() |
---|
168 | ## Find Gaussian weight for each dq bins: The weight depends only on r-direction (The integration may not need) |
---|
169 | weight_res = numpy.exp(-0.5*((r-bin_size/2.0)*(r-bin_size/2.0)))-numpy.exp(-0.5*((r+bin_size/2.0)*(r+bin_size/2.0))) |
---|
170 | weight_res = weight_res.repeat(nphi).reshape(nr,nphi).transpose().flatten() |
---|
171 | |
---|
172 | ## Set dr for all dq bins for averaging |
---|
173 | dr = r.repeat(nphi).reshape(nr,nphi).transpose().flatten() |
---|
174 | ## Set dqr for all data points |
---|
175 | dqx = numpy.outer(dr,self.dqx_data).flatten() |
---|
176 | dqy = numpy.outer(dr,self.dqy_data).flatten() |
---|
177 | qx = self.qx_data.repeat(n_bins).reshape(len_data,n_bins).transpose().flatten() |
---|
178 | qy = self.qy_data.repeat(n_bins).reshape(len_data,n_bins).transpose().flatten() |
---|
179 | |
---|
180 | |
---|
181 | ## Over-sampled qx_data and qy_data. |
---|
182 | qx_res = qx+ dqx*numpy.cos(dphi) |
---|
183 | qy_res = qy+ dqy*numpy.sin(dphi) |
---|
184 | |
---|
185 | ## Evaluate all points |
---|
186 | val = self.model.evalDistribution([qx_res,qy_res]) |
---|
187 | |
---|
188 | ## Reshape into 2d array to use numpy weighted averaging |
---|
189 | value_res= val.reshape(n_bins,len(self.qx_data)) |
---|
190 | |
---|
191 | ## Averaging with Gaussian weighting: normalization included. |
---|
192 | value =numpy.average(value_res,axis=0,weights=weight_res) |
---|
193 | |
---|
194 | ## Return the smeared values in the range of self.index |
---|
195 | return value |
---|
196 | |
---|
197 | if __name__ == '__main__': |
---|
198 | ## Test w/ 2D linear function |
---|
199 | x = 0.001*numpy.arange(1,11) |
---|
200 | dx = numpy.ones(len(x))*0.001 |
---|
201 | y = 0.001*numpy.arange(1,11) |
---|
202 | dy = numpy.ones(len(x))*0.001 |
---|
203 | z = numpy.ones(10) |
---|
204 | dz = numpy.sqrt(z) |
---|
205 | |
---|
206 | from DataLoader import Data2D |
---|
207 | #for i in range(10): print i, 0.001 + i*0.008/9.0 |
---|
208 | #for i in range(100): print i, int(math.floor( (i/ (100/9.0)) )) |
---|
209 | out = Data2D() |
---|
210 | out.data = z |
---|
211 | out.qx_data = x |
---|
212 | out.qy_data = y |
---|
213 | out.dqx_data = dx |
---|
214 | out.dqy_data = dy |
---|
215 | index = numpy.ones(len(x), dtype = bool) |
---|
216 | out.mask = index |
---|
217 | from sans.models.LineModel import LineModel |
---|
218 | model = LineModel() |
---|
219 | model.setParam("A", 0) |
---|
220 | |
---|
221 | smear = Smearer2D(out,model,index) |
---|
222 | #smear.set_accuracy('Xhigh') |
---|
223 | value = smear.get_value() |
---|
224 | ## All data are ones, so the smeared should also be ones. |
---|
225 | print "Data length =",len(value) |
---|
226 | print " 2D linear function, I = 0 + 1*qx*qy" |
---|
227 | print " Gaussian weighted averaging on a 2D linear function will provides the results same as without the averaging." |
---|
228 | print "qx_data", "qy_data", "I_nonsmear", "I_smeared" |
---|
229 | for ind in range(len(value)): |
---|
230 | print x[ind],y[ind],model.evalDistribution([x,y])[ind], value[ind] |
---|
231 | |
---|
232 | """ |
---|
233 | if __name__ == '__main__': |
---|
234 | ## Another Test w/ constant function |
---|
235 | x = 0.001*numpy.arange(1,11) |
---|
236 | dx = numpy.ones(len(x))*0.001 |
---|
237 | y = 0.001*numpy.arange(1,11) |
---|
238 | dy = numpy.ones(len(x))*0.001 |
---|
239 | z = numpy.ones(10) |
---|
240 | dz = numpy.sqrt(z) |
---|
241 | |
---|
242 | from DataLoader import Data2D |
---|
243 | #for i in range(10): print i, 0.001 + i*0.008/9.0 |
---|
244 | #for i in range(100): print i, int(math.floor( (i/ (100/9.0)) )) |
---|
245 | out = Data2D() |
---|
246 | out.data = z |
---|
247 | out.qx_data = x |
---|
248 | out.qy_data = y |
---|
249 | out.dqx_data = dx |
---|
250 | out.dqy_data = dy |
---|
251 | index = numpy.ones(len(x), dtype = bool) |
---|
252 | out.mask = index |
---|
253 | from sans.models.Constant import Constant |
---|
254 | model = Constant() |
---|
255 | |
---|
256 | value = Smearer2D(out,model,index).get_value() |
---|
257 | ## All data are ones, so the smeared values should also be ones. |
---|
258 | print "Data length =",len(value), ", Data=",value |
---|
259 | """ |
---|