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

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

Refactor ASCII reader to use FileReader? class.

  • Property mode set to 100644
File size: 8.7 KB
Line 
1
2import unittest
3import math
4
5from sas.sascalc.dataloader.loader import Loader
6from sas.sascalc.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter
7
8import numpy as np
9import sas.sascalc.dataloader.data_info as data_info
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 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=  np.linspace(start= -1*self.qmax,
45                               stop= self.qmax,
46                               num= self.qstep,
47                               endpoint=True ) 
48        y = np.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
96class 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                 nbins = 20)
109        ##r.nbins_phi = 20
110       
111        o = r(self.data)
112        data_list = Loader().load('ring_testdata.txt')
113        answer = data_list[0]
114       
115        for i in range(r.nbins_phi - 1):
116            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4)
117            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4)
118            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4)
119           
120    def test_circularavg(self):
121        """
122            Test circular averaging
123            The test data was not generated by IGOR.
124        """
125        r = CircularAverage(r_min=.00, r_max=.025, 
126                 bin_width=0.0003)
127        r.nbins_phi = 20
128       
129        o = r(self.data)
130
131        answer = Loader().load('avg_testdata.txt')
132        for i in range(r.nbins_phi):
133            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
134            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
135            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
136           
137    def test_box(self):
138        """
139            Test circular averaging
140            The test data was not generated by IGOR.
141        """
142        from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg
143       
144        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
145        s, ds, npoints = r(self.data)
146        self.assertAlmostEqual(s, 34.278990899999997, 4)
147        self.assertAlmostEqual(ds, 7.8007981835194293, 4)
148        self.assertAlmostEqual(npoints, 324.0000, 4)       
149   
150        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
151        s, ds = r(self.data)
152        self.assertAlmostEqual(s, 0.10579935462962962, 4)
153        self.assertAlmostEqual(ds, 0.024076537603455028, 4)
154           
155    def test_slabX(self):
156        """
157            Test slab in X
158            The test data was not generated by IGOR.
159        """
160        from sas.sascalc.dataloader.manipulations import SlabX
161       
162        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004)
163        r.fold = False
164        o = r(self.data)
165
166        answer = Loader().load('slabx_testdata.txt')
167        for i in range(len(o.x)):
168            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
169            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
170            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
171           
172    def test_slabY(self):
173        """
174            Test slab in Y
175            The test data was not generated by IGOR.
176        """
177        from sas.sascalc.dataloader.manipulations import SlabY
178       
179        r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004)
180        r.fold = False
181        o = r(self.data)
182
183        answer = Loader().load('slaby_testdata.txt')
184        for i in range(len(o.x)):
185            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
186            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
187            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
188           
189    def test_sectorphi_full(self):
190        """
191            Test sector averaging I(phi)
192            When considering the whole azimuthal range (2pi),
193            the answer should be the same as ring averaging.
194            The test data was not generated by IGOR.
195        """
196        from sas.sascalc.dataloader.manipulations import SectorPhi
197        import math
198       
199        nbins = 19
200        phi_min = math.pi / (nbins + 1)
201        phi_max = math.pi * 2 - phi_min
202       
203        r = SectorPhi(r_min=.005,
204                      r_max=.01,
205                      phi_min=phi_min,
206                      phi_max=phi_max,
207                      nbins=nbins)
208        o = r(self.data)
209
210        answer = Loader().load('ring_testdata.txt')
211        for i in range(len(o.x)):
212            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
213            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
214            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
215           
216    def test_sectorphi_quarter(self):
217        """
218            Test sector averaging I(phi)
219            The test data was not generated by IGOR.
220        """
221        from sas.sascalc.dataloader.manipulations import SectorPhi
222        import math
223       
224        r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0)
225        r.nbins_phi = 20
226        o = r(self.data)
227
228        answer = Loader().load('sectorphi_testdata.txt')
229        for i in range(len(o.x)):
230            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
231            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
232            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
233           
234    def test_sectorq_full(self):
235        """
236            Test sector averaging I(q)
237            The test data was not generated by IGOR.
238        """
239        from sas.sascalc.dataloader.manipulations import SectorQ
240        import math
241       
242        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0)
243        r.nbins_phi = 20
244        o = r(self.data)
245
246        answer = Loader().load('sectorq_testdata.txt')
247        for i in range(len(o.x)):
248            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
249            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
250            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
251           
252
253if __name__ == '__main__':
254    unittest.main()
Note: See TracBrowser for help on using the repository browser.