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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e123eb9 was e123eb9, checked in by Ricardo M. Ferraz Leal <ricleal@…>, 7 years ago

Test only for the log binning

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