source: sasview/test/corfunc/test/utest_corfunc.py @ 253eb6c6

ESS_GUIESS_GUI_DocsESS_GUI_PrESS_GUI_Pr_fixesESS_GUI_corfESS_GUI_iss879ESS_GUI_iss959ESS_GUI_project_saveSVCC-1SasView-664costrafo411gpu-options-handlingpy37-allpy37-sascalcpy37-sasguipytestsimplify-c-buildticket-1094-headlessticket-1111ticket-818ticket-933ticket885unittest-saveloadwin64bit_conda_vm
Last change on this file since 253eb6c6 was 253eb6c6, checked in by andyfaff, 19 months ago

MAINT: remove unused matplotlib import

  • 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
10
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    # Ensure tests are ran in correct order;
72    # Each test depends on the one before it
73    def test_calculator(self):
74        steps = [self.extrapolate, self.transform, self.extract_params]
75        for test in steps:
76            try:
77                test()
78            except Exception as e:
79                self.fail("{} failed ({}: {})".format(test, type(e), e))
80
81
82def load_data(filename="98929.txt"):
83    data = np.loadtxt(filename, dtype=np.float32)
84    q = data[:,0]
85    iq = data[:,1]
86    return Data1D(x=q, y=iq)
87
88if __name__ == '__main__':
89    unittest.main()
Note: See TracBrowser for help on using the repository browser.