1 | """ |
---|
2 | Unit tests for data manipulations |
---|
3 | """ |
---|
4 | #TODO: what happens if you add a Data1D to a Data2D? |
---|
5 | |
---|
6 | import unittest |
---|
7 | import numpy, math |
---|
8 | from DataLoader.loader import Loader |
---|
9 | from DataLoader.data_info import Data1D, Data2D |
---|
10 | |
---|
11 | import os.path |
---|
12 | |
---|
13 | class data_info_tests(unittest.TestCase): |
---|
14 | |
---|
15 | def setUp(self): |
---|
16 | self.data = Loader().load("cansas1d.xml") |
---|
17 | |
---|
18 | def test_clone1D(self): |
---|
19 | """ |
---|
20 | Test basic cloning |
---|
21 | """ |
---|
22 | clone = self.data.clone_without_data() |
---|
23 | |
---|
24 | for i in range(len(self.data.detector)): |
---|
25 | self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance) |
---|
26 | |
---|
27 | |
---|
28 | class manip_tests(unittest.TestCase): |
---|
29 | |
---|
30 | def setUp(self): |
---|
31 | # Create two data sets to play with |
---|
32 | x_0 = numpy.ones(5) |
---|
33 | for i in range(5): |
---|
34 | x_0[i] = x_0[i]*(i+1.0) |
---|
35 | |
---|
36 | y_0 = 2.0*numpy.ones(5) |
---|
37 | dy_0 = 0.5*numpy.ones(5) |
---|
38 | self.data = Data1D(x_0, y_0, dy=dy_0) |
---|
39 | |
---|
40 | x = self.data.x |
---|
41 | y = numpy.ones(5) |
---|
42 | dy = numpy.ones(5) |
---|
43 | self.data2 = Data1D(x, y, dy=dy) |
---|
44 | |
---|
45 | |
---|
46 | def test_load(self): |
---|
47 | """ |
---|
48 | Test whether the test file was loaded properly |
---|
49 | """ |
---|
50 | # There should be 5 entries in the file |
---|
51 | self.assertEqual(len(self.data.x), 5) |
---|
52 | |
---|
53 | for i in range(5): |
---|
54 | # The x values should be from 1 to 5 |
---|
55 | self.assertEqual(self.data.x[i], float(i+1)) |
---|
56 | |
---|
57 | # All y-error values should be 0.5 |
---|
58 | self.assertEqual(self.data.dy[i], 0.5) |
---|
59 | |
---|
60 | # All y values should be 2.0 |
---|
61 | self.assertEqual(self.data.y[i], 2.0) |
---|
62 | |
---|
63 | def test_add(self): |
---|
64 | result = self.data2+self.data |
---|
65 | for i in range(5): |
---|
66 | self.assertEqual(result.y[i], 3.0) |
---|
67 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
68 | |
---|
69 | def test_sub(self): |
---|
70 | result = self.data2-self.data |
---|
71 | for i in range(5): |
---|
72 | self.assertEqual(result.y[i], -1.0) |
---|
73 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
74 | |
---|
75 | def test_mul(self): |
---|
76 | result = self.data2*self.data |
---|
77 | for i in range(5): |
---|
78 | self.assertEqual(result.y[i], 2.0) |
---|
79 | self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
80 | |
---|
81 | def test_div(self): |
---|
82 | result = self.data2/self.data |
---|
83 | for i in range(5): |
---|
84 | self.assertEqual(result.y[i], 0.5) |
---|
85 | self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
86 | |
---|
87 | def test_radd(self): |
---|
88 | result = self.data+3.0 |
---|
89 | for i in range(5): |
---|
90 | self.assertEqual(result.y[i], 5.0) |
---|
91 | self.assertEqual(result.dy[i], 0.5) |
---|
92 | |
---|
93 | result = 3.0+self.data |
---|
94 | for i in range(5): |
---|
95 | self.assertEqual(result.y[i], 5.0) |
---|
96 | self.assertEqual(result.dy[i], 0.5) |
---|
97 | |
---|
98 | def test_rsub(self): |
---|
99 | result = self.data-3.0 |
---|
100 | for i in range(5): |
---|
101 | self.assertEqual(result.y[i], -1.0) |
---|
102 | self.assertEqual(result.dy[i], 0.5) |
---|
103 | |
---|
104 | result = 3.0-self.data |
---|
105 | for i in range(5): |
---|
106 | self.assertEqual(result.y[i], 1.0) |
---|
107 | self.assertEqual(result.dy[i], 0.5) |
---|
108 | |
---|
109 | def test_rmul(self): |
---|
110 | result = self.data*3.0 |
---|
111 | for i in range(5): |
---|
112 | self.assertEqual(result.y[i], 6.0) |
---|
113 | self.assertEqual(result.dy[i], 1.5) |
---|
114 | |
---|
115 | result = 3.0*self.data |
---|
116 | for i in range(5): |
---|
117 | self.assertEqual(result.y[i], 6.0) |
---|
118 | self.assertEqual(result.dy[i], 1.5) |
---|
119 | |
---|
120 | def test_rdiv(self): |
---|
121 | result = self.data/4.0 |
---|
122 | for i in range(5): |
---|
123 | self.assertEqual(result.y[i], 0.5) |
---|
124 | self.assertEqual(result.dy[i], 0.125) |
---|
125 | |
---|
126 | result = 6.0/self.data |
---|
127 | for i in range(5): |
---|
128 | self.assertEqual(result.y[i], 3.0) |
---|
129 | self.assertEqual(result.dy[i], 6.0*0.5/4.0) |
---|
130 | |
---|
131 | class manip_2D(unittest.TestCase): |
---|
132 | |
---|
133 | def setUp(self): |
---|
134 | # Create two data sets to play with |
---|
135 | x_0 = 2.0*numpy.ones([5,4]) |
---|
136 | dx_0 = 0.5*numpy.ones([5,4]) |
---|
137 | self.data = Data2D(x_0, dx_0) |
---|
138 | |
---|
139 | y = numpy.ones([5,4]) |
---|
140 | dy = numpy.ones([5,4]) |
---|
141 | self.data2 = Data2D(y, dy) |
---|
142 | |
---|
143 | |
---|
144 | def test_load(self): |
---|
145 | """ |
---|
146 | Test whether the test file was loaded properly |
---|
147 | """ |
---|
148 | # There should be 5 entries in the file |
---|
149 | self.assertEqual(numpy.size(self.data.data, 0), 5) |
---|
150 | self.assertEqual(numpy.size(self.data.data, 1), 4) |
---|
151 | |
---|
152 | for i in range(5): |
---|
153 | for j in range(4): |
---|
154 | # All y-error values should be 0.5 |
---|
155 | self.assertEqual(self.data.err_data[i][j], 0.5) |
---|
156 | |
---|
157 | # All y values should be 2.0 |
---|
158 | self.assertEqual(self.data.data[i][j], 2.0) |
---|
159 | |
---|
160 | def test_add(self): |
---|
161 | result = self.data2+self.data |
---|
162 | for i in range(5): |
---|
163 | for j in range(4): |
---|
164 | self.assertEqual(result.data[i][j], 3.0) |
---|
165 | self.assertEqual(result.err_data[i][j], math.sqrt(0.5**2+1.0)) |
---|
166 | |
---|
167 | def test_sub(self): |
---|
168 | result = self.data2-self.data |
---|
169 | for i in range(5): |
---|
170 | for j in range(4): |
---|
171 | self.assertEqual(result.data[i][j], -1.0) |
---|
172 | self.assertEqual(result.err_data[i][j], math.sqrt(0.5**2+1.0)) |
---|
173 | |
---|
174 | def test_mul(self): |
---|
175 | result = self.data2*self.data |
---|
176 | for i in range(5): |
---|
177 | for j in range(4): |
---|
178 | self.assertEqual(result.data[i][j], 2.0) |
---|
179 | self.assertEqual(result.err_data[i][j], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
180 | |
---|
181 | def test_div(self): |
---|
182 | result = self.data2/self.data |
---|
183 | for i in range(5): |
---|
184 | for j in range(4): |
---|
185 | self.assertEqual(result.data[i][j], 0.5) |
---|
186 | self.assertEqual(result.err_data[i][j], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
187 | |
---|
188 | def test_radd(self): |
---|
189 | result = self.data+3.0 |
---|
190 | for i in range(5): |
---|
191 | for j in range(4): |
---|
192 | self.assertEqual(result.data[i][j], 5.0) |
---|
193 | self.assertEqual(result.err_data[i][j], 0.5) |
---|
194 | |
---|
195 | result = 3.0+self.data |
---|
196 | for i in range(5): |
---|
197 | for j in range(4): |
---|
198 | self.assertEqual(result.data[i][j], 5.0) |
---|
199 | self.assertEqual(result.err_data[i][j], 0.5) |
---|
200 | |
---|
201 | def test_rsub(self): |
---|
202 | result = self.data-3.0 |
---|
203 | for i in range(5): |
---|
204 | for j in range(4): |
---|
205 | self.assertEqual(result.data[i][j], -1.0) |
---|
206 | self.assertEqual(result.err_data[i][j], 0.5) |
---|
207 | |
---|
208 | result = 3.0-self.data |
---|
209 | for i in range(5): |
---|
210 | for j in range(4): |
---|
211 | self.assertEqual(result.data[i][j], 1.0) |
---|
212 | self.assertEqual(result.err_data[i][j], 0.5) |
---|
213 | |
---|
214 | def test_rmul(self): |
---|
215 | result = self.data*3.0 |
---|
216 | for i in range(5): |
---|
217 | for j in range(4): |
---|
218 | self.assertEqual(result.data[i][j], 6.0) |
---|
219 | self.assertEqual(result.err_data[i][j], 1.5) |
---|
220 | |
---|
221 | result = 3.0*self.data |
---|
222 | for i in range(5): |
---|
223 | for j in range(4): |
---|
224 | self.assertEqual(result.data[i][j], 6.0) |
---|
225 | self.assertEqual(result.err_data[i][j], 1.5) |
---|
226 | |
---|
227 | def test_rdiv(self): |
---|
228 | result = self.data/4.0 |
---|
229 | for i in range(5): |
---|
230 | for j in range(4): |
---|
231 | self.assertEqual(result.data[i][j], 0.5) |
---|
232 | self.assertEqual(result.err_data[i][j], 0.125) |
---|
233 | |
---|
234 | result = 6.0/self.data |
---|
235 | for i in range(5): |
---|
236 | for j in range(4): |
---|
237 | self.assertEqual(result.data[i][j], 3.0) |
---|
238 | self.assertEqual(result.err_data[i][j], 6.0*0.5/4.0) |
---|
239 | |
---|
240 | |
---|
241 | if __name__ == '__main__': |
---|
242 | unittest.main() |
---|
243 | |
---|