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