source: sasview/test/sasdataloader/test/utest_averaging.py @ aa1db44

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since aa1db44 was 2a52b0e, checked in by lewis, 7 years ago

Remove unit test reliance on IgorReader?

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