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 | qx_prime = numpy.reshape(qx, [3,1]) |
---|
60 | qy_prime = numpy.reshape(qy, [1,3]) |
---|
61 | |
---|
62 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
63 | |
---|
64 | for i in range(3): |
---|
65 | for j in range(3): |
---|
66 | self.assertAlmostEquals(iq[i][j], expected[i][j]) |
---|
67 | |
---|
68 | def test_rectangle_methods(self): |
---|
69 | """ |
---|
70 | Compare to the runXY() method for 2D models |
---|
71 | with a non-square matrix. |
---|
72 | TODO: Doesn't work for C models apparently |
---|
73 | |
---|
74 | +--------+--------+--------+ |
---|
75 | qy-0.006 | | | | |
---|
76 | +--------+--------+--------+ |
---|
77 | qy=0.003 | | | | |
---|
78 | +--------+--------+--------+ |
---|
79 | qx=0.001 0.002 0.003 |
---|
80 | |
---|
81 | """ |
---|
82 | # These are the expected values for all bins |
---|
83 | expected = numpy.zeros([3,3]) |
---|
84 | for i in range(3): |
---|
85 | for j in range(2): |
---|
86 | q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) |
---|
87 | expected[i][j] = self.model.run(q_length) |
---|
88 | |
---|
89 | qx_values = [0.001, 0.002, 0.003] |
---|
90 | qy_values = [0.003, 0.006] |
---|
91 | |
---|
92 | qx = numpy.asarray(qx_values) |
---|
93 | qy = numpy.asarray(qy_values) |
---|
94 | |
---|
95 | qx_prime = numpy.reshape(qx, [3,1]) |
---|
96 | qy_prime = numpy.reshape(qy, [1,2]) |
---|
97 | |
---|
98 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
99 | |
---|
100 | for i in range(3): |
---|
101 | for j in range(2): |
---|
102 | self.assertAlmostEquals(iq[i][j], expected[i][j]) |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | class TestEvalMethods(unittest.TestCase): |
---|
107 | """ Testing evalDistribution for C models """ |
---|
108 | |
---|
109 | def setUp(self): |
---|
110 | self.model= SphereModel() |
---|
111 | |
---|
112 | def test_scalar_methods(self): |
---|
113 | """ |
---|
114 | Simple test comparing the run(), runXY() and |
---|
115 | evalDistribution methods |
---|
116 | """ |
---|
117 | q1 = self.model.run(0.001) |
---|
118 | q2 = self.model.runXY(0.001) |
---|
119 | q4 = self.model.run(0.002) |
---|
120 | qlist3 = numpy.asarray([0.001, 0.002]) |
---|
121 | q3 = self.model.evalDistribution(qlist3) |
---|
122 | |
---|
123 | self.assertEqual(q1, q2) |
---|
124 | self.assertEqual(q1, q3[0]) |
---|
125 | self.assertEqual(q4, q3[1]) |
---|
126 | |
---|
127 | def test_XY_methods(self): |
---|
128 | """ |
---|
129 | Compare to the runXY() method for 2D models. |
---|
130 | |
---|
131 | +--------+--------+--------+ |
---|
132 | qy=0.009 | | | | |
---|
133 | +--------+--------+--------+ |
---|
134 | qy-0.006 | | | | |
---|
135 | +--------+--------+--------+ |
---|
136 | qy=0.003 | | | | |
---|
137 | +--------+--------+--------+ |
---|
138 | qx=0.001 0.002 0.003 |
---|
139 | |
---|
140 | """ |
---|
141 | # These are the expected values for all bins |
---|
142 | expected = numpy.zeros([3,3]) |
---|
143 | for i in range(3): |
---|
144 | for j in range(3): |
---|
145 | q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) |
---|
146 | expected[i][j] = self.model.run(q_length) |
---|
147 | |
---|
148 | qx_values = [0.001, 0.002, 0.003] |
---|
149 | qy_values = [0.003, 0.006, 0.009] |
---|
150 | |
---|
151 | qx = numpy.asarray(qx_values) |
---|
152 | qy = numpy.asarray(qy_values) |
---|
153 | |
---|
154 | qx_prime = numpy.reshape(qx, [3,1]) |
---|
155 | qy_prime = numpy.reshape(qy, [1,3]) |
---|
156 | |
---|
157 | iq = self.model.evalDistribution([qx_prime, qy_prime]) |
---|
158 | |
---|
159 | for i in range(3): |
---|
160 | for j in range(3): |
---|
161 | self.assertAlmostEquals(iq[i][j], expected[i][j]) |
---|
162 | |
---|
163 | |
---|
164 | if __name__ == '__main__': |
---|
165 | unittest.main() |
---|