source: sasview/DataLoader/test/utest_smearing.py @ febcd79

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since febcd79 was cd2ced80, checked in by Jae Cho <jhjcho@…>, 14 years ago

new algorithm for slit smearing and its test

  • Property mode set to 100644
File size: 6.0 KB
Line 
1"""
2    Unit tests for data manipulations
3"""
4
5
6import unittest
7import numpy, math
8from DataLoader.loader import  Loader
9from DataLoader.data_info import Data1D, Data2D
10from DataLoader.qsmearing import SlitSmearer, QSmearer, smear_selection
11from sans.models.SphereModel import SphereModel
12import os.path
13
14class smear_tests(unittest.TestCase):
15   
16    def setUp(self):
17        self.data = Loader().load("cansas1d_slit.xml")
18       
19        x = 0.001*numpy.arange(1,11)
20        y = 12.0-numpy.arange(1,11)
21        dxl = 0.00*numpy.ones(10)
22        dxw = 0.00*numpy.ones(10)
23        dx = 0.00*numpy.ones(10)
24       
25        self.data.dx = dx
26        self.data.x = x
27        self.data.y = y
28        self.data.dxl = dxl
29        self.data.dxw = dxw
30       
31    def test_slit(self):
32        """
33            Test identity smearing
34        """
35        # Create smearer for our data
36        s = SlitSmearer(self.data)
37       
38        input = 12.0-numpy.arange(1,11)
39        output = s(input)
40        for i in range(len(input)):
41            self.assertEquals(input[i], output[i])
42
43    def test_slit2(self):
44        """
45            Test basic smearing
46        """
47        dxl = 0.005*numpy.ones(10)
48        dxw = 0.0*numpy.ones(10)
49        self.data.dxl = dxl
50        self.data.dxw = dxw
51        # Create smearer for our data
52        s = SlitSmearer(self.data)
53       
54        input = 12.0-numpy.arange(1,11)
55        output = s(input)
56        # The following commented line was the correct output for even bins
57        # [see smearer.cpp for details]
58        #answer = [ 9.666,  9.056,  8.329,  7.494,  6.642,  5.721,  4.774, \
59        #  3.824,  2.871, 2.   ]
60        # The following answer was from numerical weighting algorithm.
61        #answer = [ 9.2302,  8.6806,  7.9533,  7.1673,  6.2889,  5.4,   \
62        #  4.5028,  3.5744,  2.6083, 2.    ]
63        # For the new analytical algorithm, the small difference between
64        #these two could be from the first edge of the q bin size.
65        answer = [ 9.0618,  8.64018,  8.11868,  7.13916,  6.15285,  5.55556,  \
66                     4.55842,  3.56061,  2.56235, 2.    ]
67        for i in range(len(input)):
68            self.assertAlmostEqual(answer[i], output[i], 2)
69       
70    def test_q(self):
71        """
72            Test identity resolution smearing
73        """
74        # Create smearer for our data
75        s = QSmearer(self.data)
76       
77        input = 12.0-numpy.arange(1,11)
78        output = s(input)
79        for i in range(len(input)):
80            self.assertAlmostEquals(input[i], output[i], 5)
81
82    def test_q2(self):
83        """
84            Test basic smearing
85        """
86        dx = 0.001*numpy.ones(10)
87        self.data.dx = dx
88
89        # Create smearer for our data
90        s = QSmearer(self.data)
91       
92        input = 12.0-numpy.arange(1,11)
93        output = s(input)
94       
95        answer = [ 10.44785079,   9.84991299,   8.98101708,   
96                  7.99906585,   6.99998311,   6.00001689,
97                  5.00093415,   4.01898292,   3.15008701,   2.55214921]
98        for i in range(len(input)):
99            self.assertAlmostEqual(answer[i], output[i], 2)
100           
101class smear_slit_h_w_tests(unittest.TestCase):
102   
103    def setUp(self):
104        self.data = Loader().load("1000A_sphere_sm.xml")
105        self.model = SphereModel()
106        # The answer could be improved by developing better algorithm.
107        self.answer1 = Loader().load("slit_1000A_sphere_sm_w_0_0002.txt")
108        self.answer2 = Loader().load("slit_1000A_sphere_sm_h.txt")
109        self.answer3 = Loader().load("slit_1000A_sphere_sm_w_0_0001.txt")
110        # Get inputs
111        self.model.params['scale'] = 0.05
112        self.model.params['background'] = 0.01
113        self.model.params['radius'] = 10000.0
114        self.model.params['sldSolv'] = 6.3e-006
115        self.model.params['sldSph'] = 3.4e-006
116       
117    def test_slit_h_w(self):
118        """
119            Test identity slit smearing w/ h=0.117 w = 0.002
120        """
121        # Set params and dQl
122        data = self.data
123        data.dxw = 0.0002 * numpy.ones(len(self.data.x))
124        data.dxl = 0.117 * numpy.ones(len(self.data.x))
125        # Create smearer for our data
126        s = SlitSmearer(data, self.model)
127        # Get smear
128        input  = self.model.evalDistribution(data.x)
129        output = s(input)
130        # Get pre-calculated values
131        answer = self.answer1.y
132        # Now compare
133        for i in range(len(input)):
134            self.assertAlmostEqual(answer[i], output[i], 0)
135     
136    def test_slit_h(self):
137        """
138            Test identity slit smearing w/ h=0.117 w = 0.0
139        """
140        # Set params and dQl
141        data = self.data
142        data.dxw = 0.0 * numpy.ones(len(self.data.x))
143        data.dxl = 0.117 * numpy.ones(len(self.data.x))
144        # Create smearer for our data
145        s = SlitSmearer(data, self.model)
146        # Get smear
147        input  = self.model.evalDistribution(data.x)
148        output = s(input)
149        # Get pre-calculated values
150        answer = self.answer2.y
151        # Now compare
152        for i in range(len(input)):
153            self.assertAlmostEqual(answer[i], output[i], 0)
154     
155    def test_slit_w(self):
156        """
157            Test identity slit smearing w/ h=0.0 w = 0.001
158        """
159        # Set params and dQl
160        data = self.data
161        data.dxw = 0.0001 * numpy.ones(len(self.data.x))
162        data.dxl = 0.0 * numpy.ones(len(self.data.x))
163        # Create smearer for our data
164        s = SlitSmearer(data, self.model)
165        # Get smear
166        input  = self.model.evalDistribution(data.x)
167        output = s(input)
168        # Get pre-calculated values
169        answer = self.answer3.y
170        # Now compare
171        for i in range(len(input)):
172             if i <= 40:
173                 self.assertAlmostEqual(answer[i], output[i], -3)   
174             else:
175                self.assertAlmostEqual(answer[i], output[i], 0)       
176               
177if __name__ == '__main__':
178    unittest.main()
179   
Note: See TracBrowser for help on using the repository browser.