[0bc0683] | 1 | """ |
---|
| 2 | Unit Tests for CorfuncCalculator class |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import unittest |
---|
| 6 | import time |
---|
| 7 | import numpy as np |
---|
| 8 | from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator |
---|
| 9 | from sas.sascalc.dataloader.data_info import Data1D |
---|
| 10 | import matplotlib.pyplot as plt |
---|
| 11 | |
---|
| 12 | class 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): |
---|
[acefa2b] | 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) |
---|
[0bc0683] | 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 | |
---|
| 83 | def 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 | |
---|
| 89 | if __name__ == '__main__': |
---|
| 90 | unittest.main() |
---|