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