source: sasview/test/sasdataloader/test/utest_averaging.py @ 8ffafd1

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 8ffafd1 was 8ffafd1, checked in by krzywon, 7 years ago

Fix indexing issue that broke unit tests.

  • Property mode set to 100644
File size: 8.1 KB
Line 
1
2import math
3import unittest
4import numpy as np
5import sas.sascalc.dataloader.data_info as data_info
6from sas.sascalc.dataloader.loader import Loader
7from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg, Ring, get_q,\
8    CircularAverage, SectorPhi, SectorQ, reader2D_converter, SlabX, SlabY
9
10
11class 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 = np.ones([100,100])
21        dx_0 = np.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
39        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength)
40        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength)
41
42        self.qstep = len(x_0)
43        x=  np.linspace(start= -1*self.qmax,
44                               stop= self.qmax,
45                               num= self.qstep,
46                               endpoint=True ) 
47        y = np.linspace(start= -1*self.qmax,
48                               stop= self.qmax,
49                               num= self.qstep,
50                               endpoint=True )
51        self.data.x_bins=x
52        self.data.y_bins=y
53        self.data = reader2D_converter(self.data)
54
55    def test_ring_flat_distribution(self):
56        """
57            Test ring averaging
58        """
59        r = Ring(r_min=2*self.qmin, r_max=5*self.qmin, 
60                 center_x=self.data.detector[0].beam_center.x, 
61                 center_y=self.data.detector[0].beam_center.y)
62        r.nbins_phi = 20
63
64        o = r(self.data)
65        for i in range(20):
66            self.assertEqual(o.y[i], 1.0)
67
68    def test_sectorphi_full(self):
69        """
70            Test sector averaging
71        """
72        r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, 
73                      phi_min=0, phi_max=math.pi*2.0)
74        r.nbins_phi = 20
75        o = r(self.data)
76        for i in range(7):
77            self.assertEqual(o.y[i], 1.0)
78
79    def test_sectorphi_partial(self):
80        """
81        """
82        phi_max = math.pi * 1.5
83        r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, 
84                      phi_min=0, phi_max=phi_max)
85        self.assertEqual(r.phi_max, phi_max)
86        r.nbins_phi = 20
87        o = r(self.data)
88        self.assertEqual(r.phi_max, phi_max)
89        for i in range(17):
90            self.assertEqual(o.y[i], 1.0)
91
92
93class DataInfoTests(unittest.TestCase):
94
95    def setUp(self):
96        self.data = Loader().load('MAR07232_rest.ASC')
97
98    def test_ring(self):
99        """
100            Test ring averaging
101        """
102        r = Ring(r_min=.005, r_max=.01, 
103                 center_x=self.data.detector[0].beam_center.x, 
104                 center_y=self.data.detector[0].beam_center.y,
105                 nbins = 20)
106        # r.nbins_phi = 20
107
108        o = r(self.data)
109        data_list = Loader().load('ring_testdata.txt')
110        answer = data_list[0]
111
112        for i in range(r.nbins_phi - 1):
113            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4)
114            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4)
115            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4)
116
117    def test_circular_avg(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        o = r(self.data)
126
127        output = Loader().load('avg_testdata.txt')
128        answer = output[0]
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        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
140        s, ds, npoints = r(self.data)
141        self.assertAlmostEqual(s, 34.278990899999997, 4)
142        self.assertAlmostEqual(ds, 7.8007981835194293, 4)
143        self.assertAlmostEqual(npoints, 324.0000, 4)       
144
145        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
146        s, ds = r(self.data)
147        self.assertAlmostEqual(s, 0.10579935462962962, 4)
148        self.assertAlmostEqual(ds, 0.024076537603455028, 4)
149
150    def test_slabX(self):
151        """
152            Test slab in X
153            The test data was not generated by IGOR.
154        """
155        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004)
156        r.fold = False
157        o = r(self.data)
158
159        output = Loader().load('slabx_testdata.txt')
160        answer = output[0]
161        for i in range(len(o.x)):
162            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
163            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
164            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
165
166    def test_slabY(self):
167        """
168            Test slab in Y
169            The test data was not generated by IGOR.
170        """
171        r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004)
172        r.fold = False
173        o = r(self.data)
174
175        output = Loader().load('slaby_testdata.txt')
176        answer = output[0]
177        for i in range(len(o.x)):
178            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
179            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
180            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
181
182    def test_sectorphi_full(self):
183        """
184            Test sector averaging I(phi)
185            When considering the whole azimuthal range (2pi),
186            the answer should be the same as ring averaging.
187            The test data was not generated by IGOR.
188        """
189        nbins = 19
190        phi_min = math.pi / (nbins + 1)
191        phi_max = math.pi * 2 - phi_min
192
193        r = SectorPhi(r_min=.005,
194                      r_max=.01,
195                      phi_min=phi_min,
196                      phi_max=phi_max,
197                      nbins=nbins)
198        o = r(self.data)
199
200        output = Loader().load('ring_testdata.txt')
201        answer = output[0]
202        for i in range(len(o.x)):
203            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
204            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
205            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
206           
207    def test_sectorphi_quarter(self):
208        """
209            Test sector averaging I(phi)
210            The test data was not generated by IGOR.
211        """
212        r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0)
213        r.nbins_phi = 20
214        o = r(self.data)
215
216        output = Loader().load('sectorphi_testdata.txt')
217        answer = output[0]
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        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0)
229        r.nbins_phi = 20
230        o = r(self.data)
231
232        output = Loader().load('sectorq_testdata.txt')
233        answer = output[0]
234        for i in range(len(o.x)):
235            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
236            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
237            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
238
239
240if __name__ == '__main__':
241    unittest.main()
Note: See TracBrowser for help on using the repository browser.