1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | """ |
---|
4 | |
---|
5 | |
---|
6 | import unittest |
---|
7 | import numpy, math |
---|
8 | from DataLoader.loader import Loader |
---|
9 | from DataLoader.data_info import Data1D, Data2D |
---|
10 | from DataLoader.qsmearing import SlitSmearer, QSmearer, smear_selection |
---|
11 | from sans.models.SphereModel import SphereModel |
---|
12 | import os.path |
---|
13 | |
---|
14 | class 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 | |
---|
101 | class 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 | |
---|
177 | if __name__ == '__main__': |
---|
178 | unittest.main() |
---|
179 | |
---|