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