source: sasview/test/corfunc/test/utest_corfunc.py @ 839e5e3

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 839e5e3 was 839e5e3, checked in by lewis, 8 years ago

Fix unit tests

  • Property mode set to 100644
File size: 2.9 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        _, extrapolation = self.calculator.compute_extrapolation()
22
23        # Ensure the extraplation tends to the background value
24        self.assertAlmostEqual(extrapolation.y[-1], self.calculator.background)
25
26        # Test extrapolation for q values between 0.02 and 0.24
27        mask = np.logical_and(self.data.x > 0.02, self.data.x < 0.24)
28        qs = self.data.x[mask]
29        iqs = self.data.y[mask]
30
31        for q, iq in zip(qs, iqs):
32            # Find the q value in the extraplation nearest to the value in
33            # the data
34            q_extrap = min(extrapolation.x, key=lambda x:abs(x-q))
35            # Find the index of this value in the extrapolation
36            index = list(extrapolation.x).index(q_extrap)
37            # Find it's corresponding intensity value
38            iq_extrap = extrapolation.y[index]
39            # Check the extrapolation agrees to the data at this point to 1 d.p
40            self.assertAlmostEqual(iq_extrap, iq, 1)
41
42        self.extrapolation = extrapolation
43
44    def transform(self):
45        self.calculator.compute_transform(self.extrapolation, 'fourier',
46            completefn=self.transform_callback)
47        # Transform is performed asynchronously; give it time to run
48        while True:
49            time.sleep(0.001)
50            if not self.calculator.transform_isrunning():
51                break
52
53    def transform_callback(self, transform):
54        self.assertIsNotNone(transform)
55        self.assertAlmostEqual(transform.y[0], 1)
56        self.assertAlmostEqual(transform.y[-1], 0, 5)
57        self.transformation = transform
58
59    def extract_params(self):
60        params = self.calculator.extract_parameters(self.transformation)
61        self.assertIsNotNone(params)
62        self.assertEqual(len(params), 6)
63        self.assertLess(abs(params['max']-75), 2.5) # L_p ~= 75
64
65
66    # Ensure tests are ran in correct order;
67    # Each test depends on the one before it
68    def test_calculator(self):
69        steps = [self.extrapolate, self.transform, self.extract_params]
70        for test in steps:
71            try:
72                test()
73            except Exception as e:
74                self.fail("{} failed ({}: {})".format(test, type(e), e))
75
76
77def load_data(filename="98929.txt"):
78    data = np.loadtxt(filename, dtype=np.float32)
79    q = data[:,0]
80    iq = data[:,1]
81    return Data1D(x=q, y=iq)
82
83if __name__ == '__main__':
84    unittest.main()
Note: See TracBrowser for help on using the repository browser.