1 | """ |
---|
2 | Test for the BaseComponent.evalDistribution |
---|
3 | See method documentation for more details |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | import numpy, math |
---|
7 | from sans.models.SphereModel import SphereModel |
---|
8 | from sans.models.Cos import Cos |
---|
9 | |
---|
10 | |
---|
11 | class TestEvalPythonMethods(unittest.TestCase): |
---|
12 | """ Testing evalDistribution for pure python models """ |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | self.model= Cos() |
---|
16 | |
---|
17 | def test_scalar_methods(self): |
---|
18 | """ |
---|
19 | Simple test comparing the run(), runXY() and |
---|
20 | evalDistribution methods |
---|
21 | """ |
---|
22 | q1 = self.model.run(0.001) |
---|
23 | q2 = self.model.runXY(0.001) |
---|
24 | qlist3 = numpy.asarray([0.001, 0.002]) |
---|
25 | q3 = self.model.evalDistribution(qlist3) |
---|
26 | q4 = self.model.run(0.002) |
---|
27 | |
---|
28 | self.assertEqual(q1, q2) |
---|
29 | self.assertEqual(q1, q3[0]) |
---|
30 | self.assertEqual(q4, q3[1]) |
---|
31 | |
---|
32 | def test_XY_methods(self): |
---|
33 | """ |
---|
34 | Compare to the runXY() method for 2D models. |
---|
35 | |
---|
36 | +--------+--------+--------+ |
---|
37 | qy=0.009 | | | | |
---|
38 | +--------+--------+--------+ |
---|
39 | qy-0.006 | | | | |
---|
40 | +--------+--------+--------+ |
---|
41 | qy=0.003 | | | | |
---|
42 | +--------+--------+--------+ |
---|
43 | qx=0.001 0.002 0.003 |
---|
44 | |
---|
45 | """ |
---|
46 | # These are the expected values for all bins |
---|
47 | expected = numpy.zeros([3,3]) |
---|
48 | for i in range(3): |
---|
49 | for j in range(3): |
---|
50 | q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) |
---|
51 | expected[i][j] = self.model.run(q_length) |
---|
52 | |
---|
53 | qx_values = [0.001, 0.002, 0.003] |
---|
54 | qy_values = [0.003, 0.006, 0.009] |
---|
55 | |
---|
56 | qx = numpy.asarray(qx_values) |
---|
57 | qy = numpy.asarray(qy_values) |
---|
58 | |
---|
59 | new_x = numpy.tile(qx, (len(qy),1)) |
---|
60 | new_y = numpy.tile(qy, (len(qx),1)) |
---|
61 | new_y = new_y.swapaxes(0,1) |
---|
62 | |
---|
63 | #iq is 1d array now (since 03-12-2010) |
---|
64 | qx_prime = new_x.flatten() |
---|
65 | qy_prime = new_y.flatten() |
---|
66 | |
---|
67 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
68 | |
---|
69 | for i in range(3): |
---|
70 | for j in range(3): |
---|
71 | k = i+len(qx)*j |
---|
72 | self.assertAlmostEquals(iq[k], expected[i][j]) |
---|
73 | |
---|
74 | def test_rectangle_methods(self): |
---|
75 | """ |
---|
76 | Compare to the runXY() method for 2D models |
---|
77 | with a non-square matrix. |
---|
78 | TODO: Doesn't work for C models apparently |
---|
79 | |
---|
80 | +--------+--------+--------+ |
---|
81 | qy-0.006 | | | | |
---|
82 | +--------+--------+--------+ |
---|
83 | qy=0.003 | | | | |
---|
84 | +--------+--------+--------+ |
---|
85 | qx=0.001 0.002 0.003 |
---|
86 | |
---|
87 | """ |
---|
88 | # These are the expected values for all bins |
---|
89 | expected = numpy.zeros([3,3]) |
---|
90 | for i in range(3): |
---|
91 | for j in range(2): |
---|
92 | q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) |
---|
93 | expected[i][j] = self.model.run(q_length) |
---|
94 | |
---|
95 | qx_values = [0.001, 0.002, 0.003] |
---|
96 | qy_values = [0.003, 0.006] |
---|
97 | |
---|
98 | qx = numpy.asarray(qx_values) |
---|
99 | qy = numpy.asarray(qy_values) |
---|
100 | |
---|
101 | new_x = numpy.tile(qx, (len(qy),1)) |
---|
102 | new_y = numpy.tile(qy, (len(qx),1)) |
---|
103 | new_y = new_y.swapaxes(0,1) |
---|
104 | |
---|
105 | #iq is 1d array now (since 03-12-2010) |
---|
106 | qx_prime = new_x.flatten() |
---|
107 | qy_prime = new_y.flatten() |
---|
108 | |
---|
109 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
110 | |
---|
111 | for i in range(3): |
---|
112 | for j in range(2): |
---|
113 | k = i+len(qx)*j |
---|
114 | self.assertAlmostEquals(iq[k], expected[i][j]) |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | class TestEvalMethods(unittest.TestCase): |
---|
119 | """ Testing evalDistribution for C models """ |
---|
120 | |
---|
121 | def setUp(self): |
---|
122 | self.model= SphereModel() |
---|
123 | |
---|
124 | def test_scalar_methods(self): |
---|
125 | """ |
---|
126 | Simple test comparing the run(), runXY() and |
---|
127 | evalDistribution methods |
---|
128 | """ |
---|
129 | q1 = self.model.run(0.001) |
---|
130 | q2 = self.model.runXY(0.001) |
---|
131 | q4 = self.model.run(0.002) |
---|
132 | qlist3 = numpy.asarray([0.001, 0.002]) |
---|
133 | q3 = self.model.evalDistribution(qlist3) |
---|
134 | |
---|
135 | self.assertEqual(q1, q2) |
---|
136 | self.assertEqual(q1, q3[0]) |
---|
137 | self.assertEqual(q4, q3[1]) |
---|
138 | |
---|
139 | def test_XY_methods(self): |
---|
140 | """ |
---|
141 | Compare to the runXY() method for 2D models. |
---|
142 | |
---|
143 | +--------+--------+--------+ |
---|
144 | qy=0.009 | | | | |
---|
145 | +--------+--------+--------+ |
---|
146 | qy-0.006 | | | | |
---|
147 | +--------+--------+--------+ |
---|
148 | qy=0.003 | | | | |
---|
149 | +--------+--------+--------+ |
---|
150 | qx=0.001 0.002 0.003 |
---|
151 | |
---|
152 | """ |
---|
153 | # These are the expected values for all bins |
---|
154 | expected = numpy.zeros([3,3]) |
---|
155 | for i in range(3): |
---|
156 | for j in range(3): |
---|
157 | q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) |
---|
158 | expected[i][j] = self.model.run(q_length) |
---|
159 | |
---|
160 | qx_values = [0.001, 0.002, 0.003] |
---|
161 | qy_values = [0.003, 0.006, 0.009] |
---|
162 | |
---|
163 | qx = numpy.asarray(qx_values) |
---|
164 | qy = numpy.asarray(qy_values) |
---|
165 | |
---|
166 | new_x = numpy.tile(qx, (len(qy),1)) |
---|
167 | new_y = numpy.tile(qy, (len(qx),1)) |
---|
168 | new_y = new_y.swapaxes(0,1) |
---|
169 | |
---|
170 | #iq is 1d array now (since 03-12-2010) |
---|
171 | qx_prime = new_x.flatten() |
---|
172 | qy_prime = new_y.flatten() |
---|
173 | |
---|
174 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
175 | |
---|
176 | for i in range(3): |
---|
177 | for j in range(3): |
---|
178 | # convert index into 1d array |
---|
179 | k = i+len(qx)*j |
---|
180 | self.assertAlmostEquals(iq[k], expected[i][j]) |
---|
181 | |
---|
182 | |
---|
183 | if __name__ == '__main__': |
---|
184 | unittest.main() |
---|