source: sasview/test/corfunc/test/utest_corfunc.py @ 5a525e1

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.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 5a525e1 was acefa2b, checked in by lewis, 8 years ago

Check extrapolation params in unit tests

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"""
2Unit Tests for CorfuncCalculator class
3"""
4
5import unittest
6import time
7import numpy as np
8from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
9from sas.sascalc.dataloader.data_info import Data1D
10import matplotlib.pyplot as plt
11
12class TestCalculator(unittest.TestCase):
13
14    def setUp(self):
15        self.data = load_data()
16        self.calculator = CorfuncCalculator(data=self.data, lowerq=0.013,
17            upperq=(0.15, 0.24))
18        self.extrapolation = None
19
20    def extrapolate(self):
21        params, extrapolation = self.calculator.compute_extrapolation()
22
23        # Check the extrapolation parameters
24        self.assertAlmostEqual(params['A'], 4.19, places=2)
25        self.assertAlmostEqual(params['B'], -25470, places=0)
26        self.assertAlmostEqual(params['K'], 4.5e-5, places=2)
27        self.assertAlmostEqual(params['sigma'], 2.2e-10, places=2)
28
29        # Ensure the extraplation tends to the background value
30        self.assertAlmostEqual(extrapolation.y[-1], self.calculator.background)
31
32        # Test extrapolation for q values between 0.02 and 0.24
33        mask = np.logical_and(self.data.x > 0.02, self.data.x < 0.24)
34        qs = self.data.x[mask]
35        iqs = self.data.y[mask]
36
37        for q, iq in zip(qs, iqs):
38            # Find the q value in the extraplation nearest to the value in
39            # the data
40            q_extrap = min(extrapolation.x, key=lambda x:abs(x-q))
41            # Find the index of this value in the extrapolation
42            index = list(extrapolation.x).index(q_extrap)
43            # Find it's corresponding intensity value
44            iq_extrap = extrapolation.y[index]
45            # Check the extrapolation agrees to the data at this point to 1 d.p
46            self.assertAlmostEqual(iq_extrap, iq, 1)
47
48        self.extrapolation = extrapolation
49
50    def transform(self):
51        self.calculator.compute_transform(self.extrapolation, 'fourier',
52            completefn=self.transform_callback)
53        # Transform is performed asynchronously; give it time to run
54        while True:
55            time.sleep(0.001)
56            if not self.calculator.transform_isrunning():
57                break
58
59    def transform_callback(self, transform):
60        self.assertIsNotNone(transform)
61        self.assertAlmostEqual(transform.y[0], 1)
62        self.assertAlmostEqual(transform.y[-1], 0, 5)
63        self.transformation = transform
64
65    def extract_params(self):
66        params = self.calculator.extract_parameters(self.transformation)
67        self.assertIsNotNone(params)
68        self.assertEqual(len(params), 6)
69        self.assertLess(abs(params['max']-75), 2.5) # L_p ~= 75
70
71
72    # Ensure tests are ran in correct order;
73    # Each test depends on the one before it
74    def test_calculator(self):
75        steps = [self.extrapolate, self.transform, self.extract_params]
76        for test in steps:
77            try:
78                test()
79            except Exception as e:
80                self.fail("{} failed ({}: {})".format(test, type(e), e))
81
82
83def load_data(filename="98929.txt"):
84    data = np.loadtxt(filename, dtype=np.float32)
85    q = data[:,0]
86    iq = data[:,1]
87    return Data1D(x=q, y=iq)
88
89if __name__ == '__main__':
90    unittest.main()
Note: See TracBrowser for help on using the repository browser.