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

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

Add new NXcanSAS compliant test files and update unit tests to match new data sets.

  • Property mode set to 100644
File size: 10.0 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
17def find(filename):
18    return os.path.join(os.path.dirname(__file__), filename)
19
20
21class Averaging(unittest.TestCase):
22    """
23        Test averaging manipulations on a flat distribution
24    """
25
26    def setUp(self):
27        """
28            Create a flat 2D distribution. All averaging results
29            should return the predefined height of the distribution (1.0).
30        """
31        x_0 = np.ones([100, 100])
32        dx_0 = np.ones([100, 100])
33
34        self.data = data_info.Data2D(data=x_0, err_data=dx_0)
35        detector = data_info.Detector()
36        detector.distance = 1000.0  # mm
37        detector.pixel_size.x = 1.0  # mm
38        detector.pixel_size.y = 1.0  # mm
39
40        # center in pixel position = (len(x_0)-1)/2
41        detector.beam_center.x = (len(x_0) - 1) / 2  # pixel number
42        detector.beam_center.y = (len(x_0) - 1) / 2  # pixel number
43        self.data.detector.append(detector)
44
45        source = data_info.Source()
46        source.wavelength = 10.0  # A
47        self.data.source = source
48
49        # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A
50        # respectively.
51        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength)
52        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength)
53
54        self.qstep = len(x_0)
55        x = np.linspace(start=-1 * self.qmax,
56                        stop=self.qmax,
57                        num=self.qstep,
58                        endpoint=True)
59        y = np.linspace(start=-1 * self.qmax,
60                        stop=self.qmax,
61                        num=self.qstep,
62                        endpoint=True)
63        self.data.x_bins = x
64        self.data.y_bins = y
65        self.data = reader2D_converter(self.data)
66
67    def test_ring_flat_distribution(self):
68        """
69            Test ring averaging
70        """
71        r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin,
72                 center_x=self.data.detector[0].beam_center.x,
73                 center_y=self.data.detector[0].beam_center.y)
74        r.nbins_phi = 20
75
76        o = r(self.data)
77        for i in range(20):
78            self.assertEqual(o.y[i], 1.0)
79
80    def test_sectorphi_full(self):
81        """
82            Test sector averaging
83        """
84        r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin,
85                      phi_min=0, phi_max=math.pi * 2.0)
86        r.nbins_phi = 20
87        o = r(self.data)
88        for i in range(7):
89            self.assertEqual(o.y[i], 1.0)
90
91    def test_sectorphi_partial(self):
92        """
93        """
94        phi_max = math.pi * 1.5
95        r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin,
96                      phi_min=0, phi_max=phi_max)
97        self.assertEqual(r.phi_max, phi_max)
98        r.nbins_phi = 20
99        o = r(self.data)
100        self.assertEqual(r.phi_max, phi_max)
101        for i in range(17):
102            self.assertEqual(o.y[i], 1.0)
103
104
105class DataInfoTests(unittest.TestCase):
106
107    def setUp(self):
108        filepath = find('test_data//MAR07232_rest.h5')
109        self.data_list = Loader().load(filepath)
110        self.data = self.data_list[0]
111
112    def test_ring(self):
113        """
114            Test ring averaging
115        """
116        r = Ring(r_min=.005, r_max=.01,
117                 center_x=self.data.detector[0].beam_center.x,
118                 center_y=self.data.detector[0].beam_center.y,
119                 nbins=20)
120        ##r.nbins_phi = 20
121
122        o = r(self.data)
123        filepath = find('test_data//ring_testdata.txt')
124        answer_list = Loader().load(filepath)
125        answer = answer_list[0]
126
127        self.assertEqual(len(answer_list), 1)
128        for i in range(r.nbins_phi - 1):
129            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4)
130            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4)
131            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4)
132
133    def test_circularavg(self):
134        """
135        Test circular averaging
136        The test data was not generated by IGOR.
137        """
138        r = CircularAverage(r_min=.00, r_max=.025,
139                            bin_width=0.0003)
140        r.nbins_phi = 20
141
142        o = r(self.data)
143
144        filepath = find('test_data//avg_testdata.txt')
145        answer = Loader().load(filepath)[0]
146        for i in range(r.nbins_phi):
147            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
148            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
149            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
150
151    def test_box(self):
152        """
153            Test circular averaging
154            The test data was not generated by IGOR.
155        """
156
157        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
158        s, ds, npoints = r(self.data)
159        self.assertAlmostEqual(s, 34.278990899999997, 4)
160        self.assertAlmostEqual(ds, 8.237259999538685, 4)
161        self.assertAlmostEqual(npoints, 324.0000, 4)
162
163        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015)
164        s, ds = r(self.data)
165        self.assertAlmostEqual(s, 0.10579935462962962, 4)
166        self.assertAlmostEqual(ds, 0.02542364197388483, 4)
167
168    def test_slabX(self):
169        """
170            Test slab in X
171            The test data was not generated by IGOR.
172        """
173
174        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002,
175                  y_max=0.0002, bin_width=0.0004)
176        r.fold = False
177        o = r(self.data)
178
179        filepath = find('test_data//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 = find('test_data//slaby_testdata.txt')
198        answer = Loader().load(filepath)[0]
199        for i in range(len(o.x)):
200            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
201            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
202            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
203
204    def test_sectorphi_full(self):
205        """
206            Test sector averaging I(phi)
207            When considering the whole azimuthal range (2pi),
208            the answer should be the same as ring averaging.
209            The test data was not generated by IGOR.
210        """
211
212        nbins = 19
213        phi_min = math.pi / (nbins + 1)
214        phi_max = math.pi * 2 - phi_min
215
216        r = SectorPhi(r_min=.005,
217                      r_max=.01,
218                      phi_min=phi_min,
219                      phi_max=phi_max,
220                      nbins=nbins)
221        o = r(self.data)
222
223        filepath = find('test_data//ring_testdata.txt')
224        answer = Loader().load(filepath)[0]
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 = find('test_data//sectorphi_testdata.txt')
241        answer = Loader().load(filepath)[0]
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)
246
247    def test_sectorq_full(self):
248        """
249            Test sector averaging I(q)
250            The test data was not generated by IGOR.
251        """
252
253        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0)
254        r.nbins_phi = 20
255        o = r(self.data)
256
257        filepath = find('test_data//sectorq_testdata.txt')
258        answer = Loader().load(filepath)[0]
259        for i in range(len(o.x)):
260            self.assertAlmostEqual(o.x[i], answer.x[i], 4)
261            self.assertAlmostEqual(o.y[i], answer.y[i], 4)
262            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4)
263
264    def test_sectorq_log(self):
265        """
266            Test sector averaging I(q)
267            The test data was not generated by IGOR.
268        """
269
270        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0, base=10)
271        r.nbins_phi = 20
272        o = r(self.data)
273
274        expected_binning = np.logspace(np.log10(0.005), np.log10(0.01), 20, base=10)
275        for i in range(len(o.x)):
276            self.assertAlmostEqual(o.x[i], expected_binning[i], 3)
277
278        # TODO: Test for Y values (o.y)
279        # print len(self.data.x_bins)
280        # print len(self.data.y_bins)
281        # print self.data.q_data.shape
282        # data_to_bin = np.array(self.data.q_data)
283        # data_to_bin = data_to_bin.reshape(len(self.data.x_bins), len(self.data.y_bins))
284        # H, xedges, yedges, binnumber = binned_statistic_2d(self.data.x_bins, self.data.y_bins, data_to_bin,
285        #     bins=[[0, math.pi / 2.0], expected_binning], statistic='mean')
286        # xedges_width = (xedges[1] - xedges[0])
287        # xedges_center = xedges[1:] - xedges_width / 2
288
289        # yedges_width = (yedges[1] - yedges[0])
290        # yedges_center = yedges[1:] - yedges_width / 2
291
292        # print H.flatten().shape
293        # print o.y.shape
294
295
296if __name__ == '__main__':
297    unittest.main()
Note: See TracBrowser for help on using the repository browser.