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

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since fe15198 was fe15198, checked in by krzywon, 6 years ago

Add unit tests to be sure the correct number of data sets are returned when loading data.

  • Property mode set to 100644
File size: 10.3 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_list = Loader().load(filepath)
107        self.data = self.data_list[0]
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_list = Loader().load(filepath)
123        answer = answer_list[0]
124
125        self.assertEqual(len(answer_list), 1)
126        for i in range(r.nbins_phi - 1):
127            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4)
128            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4)
129            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4)
130
131    def test_circularavg(self):
132        """
133        Test circular averaging
134        The test data was not generated by IGOR.
135        """
136        r = CircularAverage(r_min=.00, r_max=.025,
137                            bin_width=0.0003)
138        r.nbins_phi = 20
139
140        o = r(self.data)
141
142        filepath = os.path.join(os.path.dirname(
143            os.path.realpath(__file__)), 'avg_testdata.txt')
144        answer = Loader().load(filepath)[0]
145        for i in range(r.nbins_phi):
146            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
147            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
148            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
149
150    def test_box(self):
151        """
152            Test circular averaging
153            The test data was not generated by IGOR.
154        """
155
156        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
157        s, ds, npoints = r(self.data)
158        self.assertAlmostEqual(s, 34.278990899999997, 4)
159        self.assertAlmostEqual(ds, 7.8007981835194293, 4)
160        self.assertAlmostEqual(npoints, 324.0000, 4)
161
162        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
163        s, ds = r(self.data)
164        self.assertAlmostEqual(s, 0.10579935462962962, 4)
165        self.assertAlmostEqual(ds, 0.024076537603455028, 4)
166
167    def test_slabX(self):
168        """
169            Test slab in X
170            The test data was not generated by IGOR.
171        """
172
173        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002,
174                  y_max=0.0002, bin_width=0.0004)
175        r.fold = False
176        o = r(self.data)
177
178        filepath = os.path.join(os.path.dirname(
179            os.path.realpath(__file__)), 'slabx_testdata.txt')
180        answer = Loader().load(filepath)[0]
181        for i in range(len(o.x)):
182            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
183            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
184            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
185
186    def test_slabY(self):
187        """
188            Test slab in Y
189            The test data was not generated by IGOR.
190        """
191
192        r = SlabY(x_min=.005, x_max=.01, y_min=-
193                  0.01, y_max=0.01, bin_width=0.0004)
194        r.fold = False
195        o = r(self.data)
196
197        filepath = os.path.join(os.path.dirname(
198            os.path.realpath(__file__)), 'slaby_testdata.txt')
199        answer = Loader().load(filepath)[0]
200        for i in range(len(o.x)):
201            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
202            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
203            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
204
205    def test_sectorphi_full(self):
206        """
207            Test sector averaging I(phi)
208            When considering the whole azimuthal range (2pi),
209            the answer should be the same as ring averaging.
210            The test data was not generated by IGOR.
211        """
212
213        nbins = 19
214        phi_min = math.pi / (nbins + 1)
215        phi_max = math.pi * 2 - phi_min
216
217        r = SectorPhi(r_min=.005,
218                      r_max=.01,
219                      phi_min=phi_min,
220                      phi_max=phi_max,
221                      nbins=nbins)
222        o = r(self.data)
223
224        filepath = os.path.join(os.path.dirname(
225            os.path.realpath(__file__)), 'ring_testdata.txt')
226        answer = Loader().load(filepath)[0]
227        for i in range(len(o.x)):
228            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
229            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
230            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
231
232    def test_sectorphi_quarter(self):
233        """
234            Test sector averaging I(phi)
235            The test data was not generated by IGOR.
236        """
237
238        r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0)
239        r.nbins_phi = 20
240        o = r(self.data)
241
242        filepath = os.path.join(os.path.dirname(
243            os.path.realpath(__file__)), 'sectorphi_testdata.txt')
244        answer = Loader().load(filepath)[0]
245        for i in range(len(o.x)):
246            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
247            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
248            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
249
250    def test_sectorq_full(self):
251        """
252            Test sector averaging I(q)
253            The test data was not generated by IGOR.
254        """
255
256        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0)
257        r.nbins_phi = 20
258        o = r(self.data)
259
260        filepath = os.path.join(os.path.dirname(
261            os.path.realpath(__file__)), 'sectorq_testdata.txt')
262        answer = Loader().load(filepath)[0]
263        for i in range(len(o.x)):
264            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
265            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
266            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
267
268    def test_sectorq_log(self):
269        """
270            Test sector averaging I(q)
271            The test data was not generated by IGOR.
272        """
273
274        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0, base=10)
275        r.nbins_phi = 20
276        o = r(self.data)
277
278        expected_binning = np.logspace(np.log10(0.005), np.log10(0.01), 20, base=10)
279        for i in range(len(o.x)):
280            self.assertAlmostEqual(o.x[i], expected_binning[i], 3)
281
282        # TODO: Test for Y values (o.y)
283        # print len(self.data.x_bins)
284        # print len(self.data.y_bins)
285        # print self.data.q_data.shape
286        # data_to_bin = np.array(self.data.q_data)
287        # data_to_bin = data_to_bin.reshape(len(self.data.x_bins), len(self.data.y_bins))
288        # H, xedges, yedges, binnumber = binned_statistic_2d(self.data.x_bins, self.data.y_bins, data_to_bin,
289        #     bins=[[0, math.pi / 2.0], expected_binning], statistic='mean')
290        # xedges_width = (xedges[1] - xedges[0])
291        # xedges_center = xedges[1:] - xedges_width / 2
292
293        # yedges_width = (yedges[1] - yedges[0])
294        # yedges_center = yedges[1:] - yedges_width / 2
295
296        # print H.flatten().shape
297        # print o.y.shape
298
299
300if __name__ == '__main__':
301    unittest.main()
Note: See TracBrowser for help on using the repository browser.