1 | |
---|
2 | import unittest |
---|
3 | import math |
---|
4 | import os |
---|
5 | |
---|
6 | from sas.sascalc.dataloader.loader import Loader |
---|
7 | from sas.sascalc.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter |
---|
8 | |
---|
9 | import numpy as np |
---|
10 | import sas.sascalc.dataloader.data_info as data_info |
---|
11 | |
---|
12 | class Averaging(unittest.TestCase): |
---|
13 | """ |
---|
14 | Test averaging manipulations on a flat distribution |
---|
15 | """ |
---|
16 | def setUp(self): |
---|
17 | """ |
---|
18 | Create a flat 2D distribution. All averaging results |
---|
19 | should return the predefined height of the distribution (1.0). |
---|
20 | """ |
---|
21 | x_0 = np.ones([100,100]) |
---|
22 | dx_0 = np.ones([100,100]) |
---|
23 | |
---|
24 | self.data = data_info.Data2D(data=x_0, err_data=dx_0) |
---|
25 | detector = data_info.Detector() |
---|
26 | detector.distance = 1000.0 #mm |
---|
27 | detector.pixel_size.x = 1.0 #mm |
---|
28 | detector.pixel_size.y = 1.0 #mm |
---|
29 | |
---|
30 | # center in pixel position = (len(x_0)-1)/2 |
---|
31 | detector.beam_center.x = (len(x_0)-1)/2 #pixel number |
---|
32 | detector.beam_center.y = (len(x_0)-1)/2 #pixel number |
---|
33 | self.data.detector.append(detector) |
---|
34 | |
---|
35 | source = data_info.Source() |
---|
36 | source.wavelength = 10.0 #A |
---|
37 | self.data.source = source |
---|
38 | |
---|
39 | # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A respectively. |
---|
40 | self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) |
---|
41 | |
---|
42 | self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) |
---|
43 | |
---|
44 | self.qstep = len(x_0) |
---|
45 | x= np.linspace(start= -1*self.qmax, |
---|
46 | stop= self.qmax, |
---|
47 | num= self.qstep, |
---|
48 | endpoint=True ) |
---|
49 | y = np.linspace(start= -1*self.qmax, |
---|
50 | stop= self.qmax, |
---|
51 | num= self.qstep, |
---|
52 | endpoint=True ) |
---|
53 | self.data.x_bins=x |
---|
54 | self.data.y_bins=y |
---|
55 | self.data = reader2D_converter(self.data) |
---|
56 | |
---|
57 | def test_ring_flat_distribution(self): |
---|
58 | """ |
---|
59 | Test ring averaging |
---|
60 | """ |
---|
61 | r = Ring(r_min=2*self.qmin, r_max=5*self.qmin, |
---|
62 | center_x=self.data.detector[0].beam_center.x, |
---|
63 | center_y=self.data.detector[0].beam_center.y) |
---|
64 | r.nbins_phi = 20 |
---|
65 | |
---|
66 | o = r(self.data) |
---|
67 | for i in range(20): |
---|
68 | self.assertEqual(o.y[i], 1.0) |
---|
69 | |
---|
70 | def test_sectorphi_full(self): |
---|
71 | """ |
---|
72 | Test sector averaging |
---|
73 | """ |
---|
74 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, |
---|
75 | phi_min=0, phi_max=math.pi*2.0) |
---|
76 | r.nbins_phi = 20 |
---|
77 | o = r(self.data) |
---|
78 | for i in range(7): |
---|
79 | self.assertEqual(o.y[i], 1.0) |
---|
80 | |
---|
81 | |
---|
82 | def test_sectorphi_partial(self): |
---|
83 | """ |
---|
84 | """ |
---|
85 | phi_max = math.pi * 1.5 |
---|
86 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, |
---|
87 | phi_min=0, phi_max=phi_max) |
---|
88 | self.assertEqual(r.phi_max, phi_max) |
---|
89 | r.nbins_phi = 20 |
---|
90 | o = r(self.data) |
---|
91 | self.assertEqual(r.phi_max, phi_max) |
---|
92 | for i in range(17): |
---|
93 | self.assertEqual(o.y[i], 1.0) |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | class data_info_tests(unittest.TestCase): |
---|
98 | |
---|
99 | def setUp(self): |
---|
100 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'MAR07232_rest.ASC') |
---|
101 | self.data = Loader().load(filepath) |
---|
102 | |
---|
103 | def test_ring(self): |
---|
104 | """ |
---|
105 | Test ring averaging |
---|
106 | """ |
---|
107 | r = Ring(r_min=.005, r_max=.01, |
---|
108 | center_x=self.data.detector[0].beam_center.x, |
---|
109 | center_y=self.data.detector[0].beam_center.y, |
---|
110 | nbins = 20) |
---|
111 | ##r.nbins_phi = 20 |
---|
112 | |
---|
113 | o = r(self.data) |
---|
114 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') |
---|
115 | answer = Loader().load(filepath) |
---|
116 | |
---|
117 | for i in range(r.nbins_phi - 1): |
---|
118 | self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4) |
---|
119 | self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4) |
---|
120 | self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) |
---|
121 | |
---|
122 | def test_circularavg(self): |
---|
123 | """ |
---|
124 | Test circular averaging |
---|
125 | The test data was not generated by IGOR. |
---|
126 | """ |
---|
127 | r = CircularAverage(r_min=.00, r_max=.025, |
---|
128 | bin_width=0.0003) |
---|
129 | r.nbins_phi = 20 |
---|
130 | |
---|
131 | o = r(self.data) |
---|
132 | |
---|
133 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'avg_testdata.txt') |
---|
134 | answer = Loader().load(filepath) |
---|
135 | for i in range(r.nbins_phi): |
---|
136 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
137 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
138 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
139 | |
---|
140 | def test_box(self): |
---|
141 | """ |
---|
142 | Test circular averaging |
---|
143 | The test data was not generated by IGOR. |
---|
144 | """ |
---|
145 | from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg |
---|
146 | |
---|
147 | r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
148 | s, ds, npoints = r(self.data) |
---|
149 | self.assertAlmostEqual(s, 34.278990899999997, 4) |
---|
150 | self.assertAlmostEqual(ds, 7.8007981835194293, 4) |
---|
151 | self.assertAlmostEqual(npoints, 324.0000, 4) |
---|
152 | |
---|
153 | r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
154 | s, ds = r(self.data) |
---|
155 | self.assertAlmostEqual(s, 0.10579935462962962, 4) |
---|
156 | self.assertAlmostEqual(ds, 0.024076537603455028, 4) |
---|
157 | |
---|
158 | def test_slabX(self): |
---|
159 | """ |
---|
160 | Test slab in X |
---|
161 | The test data was not generated by IGOR. |
---|
162 | """ |
---|
163 | from sas.sascalc.dataloader.manipulations import SlabX |
---|
164 | |
---|
165 | r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) |
---|
166 | r.fold = False |
---|
167 | o = r(self.data) |
---|
168 | |
---|
169 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slabx_testdata.txt') |
---|
170 | answer = Loader().load(filepath) |
---|
171 | for i in range(len(o.x)): |
---|
172 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
173 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
174 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
175 | |
---|
176 | def test_slabY(self): |
---|
177 | """ |
---|
178 | Test slab in Y |
---|
179 | The test data was not generated by IGOR. |
---|
180 | """ |
---|
181 | from sas.sascalc.dataloader.manipulations import SlabY |
---|
182 | |
---|
183 | r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) |
---|
184 | r.fold = False |
---|
185 | o = r(self.data) |
---|
186 | |
---|
187 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slaby_testdata.txt') |
---|
188 | answer = Loader().load(filepath) |
---|
189 | for i in range(len(o.x)): |
---|
190 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
191 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
192 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
193 | |
---|
194 | def test_sectorphi_full(self): |
---|
195 | """ |
---|
196 | Test sector averaging I(phi) |
---|
197 | When considering the whole azimuthal range (2pi), |
---|
198 | the answer should be the same as ring averaging. |
---|
199 | The test data was not generated by IGOR. |
---|
200 | """ |
---|
201 | from sas.sascalc.dataloader.manipulations import SectorPhi |
---|
202 | import math |
---|
203 | |
---|
204 | nbins = 19 |
---|
205 | phi_min = math.pi / (nbins + 1) |
---|
206 | phi_max = math.pi * 2 - phi_min |
---|
207 | |
---|
208 | r = SectorPhi(r_min=.005, |
---|
209 | r_max=.01, |
---|
210 | phi_min=phi_min, |
---|
211 | phi_max=phi_max, |
---|
212 | nbins=nbins) |
---|
213 | o = r(self.data) |
---|
214 | |
---|
215 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') |
---|
216 | answer = Loader().load(filepath) |
---|
217 | for i in range(len(o.x)): |
---|
218 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
219 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
220 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
221 | |
---|
222 | def test_sectorphi_quarter(self): |
---|
223 | """ |
---|
224 | Test sector averaging I(phi) |
---|
225 | The test data was not generated by IGOR. |
---|
226 | """ |
---|
227 | from sas.sascalc.dataloader.manipulations import SectorPhi |
---|
228 | import math |
---|
229 | |
---|
230 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
231 | r.nbins_phi = 20 |
---|
232 | o = r(self.data) |
---|
233 | |
---|
234 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorphi_testdata.txt') |
---|
235 | answer = Loader().load(filepath) |
---|
236 | for i in range(len(o.x)): |
---|
237 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
238 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
239 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
240 | |
---|
241 | def test_sectorq_full(self): |
---|
242 | """ |
---|
243 | Test sector averaging I(q) |
---|
244 | The test data was not generated by IGOR. |
---|
245 | """ |
---|
246 | from sas.sascalc.dataloader.manipulations import SectorQ |
---|
247 | import math |
---|
248 | |
---|
249 | r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
250 | r.nbins_phi = 20 |
---|
251 | o = r(self.data) |
---|
252 | |
---|
253 | filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorq_testdata.txt') |
---|
254 | answer = Loader().load(filepath) |
---|
255 | for i in range(len(o.x)): |
---|
256 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
257 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
258 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
259 | |
---|
260 | |
---|
261 | if __name__ == '__main__': |
---|
262 | unittest.main() |
---|