source: sasview/test/sasdataloader/test/utest_averaging.py @ 2a52b0e

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 2a52b0e 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
RevLine 
[a10364b]1
[9a5097c]2import math
[4fb10e5]3import os
[8ffafd1]4import unittest
[e123eb9]5from scipy.stats import binned_statistic_2d
[9a5097c]6import numpy as np
[fa4af76]7
[b699768]8import sas.sascalc.dataloader.data_info as data_info
[8ffafd1]9from sas.sascalc.dataloader.loader import Loader
[fa4af76]10from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum,
11                                                  CircularAverage, Ring,
12                                                  SectorPhi, SectorQ, SlabX,
13                                                  SlabY, get_q,
14                                                  reader2D_converter)
[8ffafd1]15
[a10364b]16
17class Averaging(unittest.TestCase):
18    """
19        Test averaging manipulations on a flat distribution
20    """
[fa4af76]21
[a10364b]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        """
[fa4af76]27        x_0 = np.ones([100, 100])
28        dx_0 = np.ones([100, 100])
[8ffafd1]29
[a10364b]30        self.data = data_info.Data2D(data=x_0, err_data=dx_0)
31        detector = data_info.Detector()
[8ffafd1]32        detector.distance = 1000.0  # mm
[fa4af76]33        detector.pixel_size.x = 1.0  # mm
34        detector.pixel_size.y = 1.0  # mm
[8ffafd1]35
[a10364b]36        # center in pixel position = (len(x_0)-1)/2
[fa4af76]37        detector.beam_center.x = (len(x_0) - 1) / 2  # pixel number
38        detector.beam_center.y = (len(x_0) - 1) / 2  # pixel number
[a10364b]39        self.data.detector.append(detector)
[fa4af76]40
[a10364b]41        source = data_info.Source()
[fa4af76]42        source.wavelength = 10.0  # A
[a10364b]43        self.data.source = source
44
[fa4af76]45        # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A
46        # respectively.
[8ffafd1]47        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength)
[a10364b]48        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength)
[8ffafd1]49
[a10364b]50        self.qstep = len(x_0)
[fa4af76]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
[a10364b]61        self.data = reader2D_converter(self.data)
[8ffafd1]62
[a10364b]63    def test_ring_flat_distribution(self):
64        """
65            Test ring averaging
66        """
[fa4af76]67        r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin,
68                 center_x=self.data.detector[0].beam_center.x,
[a10364b]69                 center_y=self.data.detector[0].beam_center.y)
70        r.nbins_phi = 20
[8ffafd1]71
[a10364b]72        o = r(self.data)
73        for i in range(20):
74            self.assertEqual(o.y[i], 1.0)
[8ffafd1]75
[a10364b]76    def test_sectorphi_full(self):
77        """
78            Test sector averaging
79        """
[fa4af76]80        r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin,
81                      phi_min=0, phi_max=math.pi * 2.0)
[a10364b]82        r.nbins_phi = 20
83        o = r(self.data)
84        for i in range(7):
85            self.assertEqual(o.y[i], 1.0)
[8ffafd1]86
[a10364b]87    def test_sectorphi_partial(self):
88        """
89        """
90        phi_max = math.pi * 1.5
[fa4af76]91        r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin,
[a10364b]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
[8ffafd1]100
101class DataInfoTests(unittest.TestCase):
102
[a10364b]103    def setUp(self):
[fa4af76]104        filepath = os.path.join(os.path.dirname(
[2a52b0e]105            os.path.realpath(__file__)), 'MAR07232_rest.h5')
106        self.data = Loader().load(filepath)[0]
[8ffafd1]107
[a10364b]108    def test_ring(self):
109        """
110            Test ring averaging
111        """
[fa4af76]112        r = Ring(r_min=.005, r_max=.01,
113                 center_x=self.data.detector[0].beam_center.x,
[a10364b]114                 center_y=self.data.detector[0].beam_center.y,
[fa4af76]115                 nbins=20)
[a10364b]116        ##r.nbins_phi = 20
[8ffafd1]117
[a10364b]118        o = r(self.data)
[fa4af76]119        filepath = os.path.join(os.path.dirname(
120            os.path.realpath(__file__)), 'ring_testdata.txt')
[2a52b0e]121        answer = Loader().load(filepath)[0]
[8ffafd1]122
[a10364b]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)
[8ffafd1]127
[a10364b]128    def test_circularavg(self):
129        """
[fa4af76]130        Test circular averaging
131        The test data was not generated by IGOR.
[a10364b]132        """
[fa4af76]133        r = CircularAverage(r_min=.00, r_max=.025,
134                            bin_width=0.0003)
[a10364b]135        r.nbins_phi = 20
[fa4af76]136
[a10364b]137        o = r(self.data)
138
[fa4af76]139        filepath = os.path.join(os.path.dirname(
140            os.path.realpath(__file__)), 'avg_testdata.txt')
[2a52b0e]141        answer = Loader().load(filepath)[0]
[a10364b]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)
[8ffafd1]146
[a10364b]147    def test_box(self):
148        """
149            Test circular averaging
150            The test data was not generated by IGOR.
151        """
[fa4af76]152
[a10364b]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)
[fa4af76]157        self.assertAlmostEqual(npoints, 324.0000, 4)
[8ffafd1]158
[a10364b]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)
[8ffafd1]163
[a10364b]164    def test_slabX(self):
165        """
166            Test slab in X
167            The test data was not generated by IGOR.
168        """
[fa4af76]169
170        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002,
171                  y_max=0.0002, bin_width=0.0004)
[a10364b]172        r.fold = False
173        o = r(self.data)
174
[fa4af76]175        filepath = os.path.join(os.path.dirname(
176            os.path.realpath(__file__)), 'slabx_testdata.txt')
[2a52b0e]177        answer = Loader().load(filepath)[0]
[a10364b]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)
[8ffafd1]182
[a10364b]183    def test_slabY(self):
184        """
185            Test slab in Y
186            The test data was not generated by IGOR.
187        """
[fa4af76]188
189        r = SlabY(x_min=.005, x_max=.01, y_min=-
190                  0.01, y_max=0.01, bin_width=0.0004)
[a10364b]191        r.fold = False
192        o = r(self.data)
193
[fa4af76]194        filepath = os.path.join(os.path.dirname(
195            os.path.realpath(__file__)), 'slaby_testdata.txt')
[2a52b0e]196        answer = Loader().load(filepath)[0]
[a10364b]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)
[8ffafd1]201
[a10364b]202    def test_sectorphi_full(self):
203        """
204            Test sector averaging I(phi)
[5a8cdbb]205            When considering the whole azimuthal range (2pi),
[a10364b]206            the answer should be the same as ring averaging.
207            The test data was not generated by IGOR.
208        """
[fa4af76]209
[a10364b]210        nbins = 19
211        phi_min = math.pi / (nbins + 1)
212        phi_max = math.pi * 2 - phi_min
[8ffafd1]213
[a10364b]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
[fa4af76]221        filepath = os.path.join(os.path.dirname(
222            os.path.realpath(__file__)), 'ring_testdata.txt')
[2a52b0e]223        answer = Loader().load(filepath)[0]
[a10364b]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)
[fa4af76]228
[a10364b]229    def test_sectorphi_quarter(self):
230        """
231            Test sector averaging I(phi)
232            The test data was not generated by IGOR.
233        """
[fa4af76]234
235        r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0)
[a10364b]236        r.nbins_phi = 20
237        o = r(self.data)
238
[fa4af76]239        filepath = os.path.join(os.path.dirname(
240            os.path.realpath(__file__)), 'sectorphi_testdata.txt')
[2a52b0e]241        answer = Loader().load(filepath)[0]
[a10364b]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)
[fa4af76]246
[a10364b]247    def test_sectorq_full(self):
248        """
249            Test sector averaging I(q)
250            The test data was not generated by IGOR.
251        """
[fa4af76]252
253        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0)
[a10364b]254        r.nbins_phi = 20
255        o = r(self.data)
256
[fa4af76]257        filepath = os.path.join(os.path.dirname(
258            os.path.realpath(__file__)), 'sectorq_testdata.txt')
[2a52b0e]259        answer = Loader().load(filepath)[0]
[a10364b]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)
[8ffafd1]264
[e123eb9]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)
[5a8cdbb]278
[e123eb9]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
[5a8cdbb]289
[e123eb9]290        # yedges_width = (yedges[1] - yedges[0])
291        # yedges_center = yedges[1:] - yedges_width / 2
[5a8cdbb]292
[e123eb9]293        # print H.flatten().shape
294        # print o.y.shape
[5a8cdbb]295
[a10364b]296
297if __name__ == '__main__':
298    unittest.main()
Note: See TracBrowser for help on using the repository browser.