[9198b83] | 1 | """ |
---|
| 2 | Unit tests for data manipulations |
---|
| 3 | """ |
---|
[442f42f] | 4 | #TODO: what happens if you add a Data1D to a Data2D? |
---|
[9198b83] | 5 | |
---|
| 6 | import unittest |
---|
| 7 | import numpy, math |
---|
[ca3b9c5d] | 8 | from sans.dataloader.loader import Loader |
---|
| 9 | from sans.dataloader.data_info import Data1D, Data2D |
---|
[9198b83] | 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 | |
---|
[442f42f] | 131 | class manip_2D(unittest.TestCase): |
---|
| 132 | |
---|
| 133 | def setUp(self): |
---|
| 134 | # Create two data sets to play with |
---|
[37dea17] | 135 | x_0 = 2.0*numpy.ones(25) |
---|
| 136 | dx_0 = 0.5*numpy.ones(25) |
---|
| 137 | qx_0 = numpy.arange(25) |
---|
| 138 | qy_0 = numpy.arange(25) |
---|
| 139 | mask_0 = numpy.zeros(25) |
---|
| 140 | dqx_0 = numpy.arange(25)/100 |
---|
| 141 | dqy_0 = numpy.arange(25)/100 |
---|
[e2605a5] | 142 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
| 143 | self.data = Data2D(x_0, dx_0, qx_0, qy_0, q_0, mask_0, dqx_0, dqy_0) |
---|
| 144 | |
---|
[37dea17] | 145 | y = numpy.ones(25) |
---|
| 146 | dy = numpy.ones(25) |
---|
| 147 | qx = numpy.arange(25) |
---|
| 148 | qy = numpy.arange(25) |
---|
| 149 | mask = numpy.zeros(25) |
---|
[e2605a5] | 150 | q = numpy.sqrt(qx * qx + qy * qy) |
---|
| 151 | self.data2 = Data2D(y, dy, qx, qy, q, mask) |
---|
[442f42f] | 152 | |
---|
| 153 | |
---|
| 154 | def test_load(self): |
---|
| 155 | """ |
---|
| 156 | Test whether the test file was loaded properly |
---|
| 157 | """ |
---|
| 158 | # There should be 5 entries in the file |
---|
[37dea17] | 159 | self.assertEqual(numpy.size(self.data.data), 25) |
---|
[442f42f] | 160 | |
---|
[37dea17] | 161 | for i in range(25): |
---|
[e2605a5] | 162 | # All y-error values should be 0.5 |
---|
| 163 | self.assertEqual(self.data.err_data[i], 0.5) |
---|
| 164 | |
---|
| 165 | # All y values should be 2.0 |
---|
| 166 | self.assertEqual(self.data.data[i], 2.0) |
---|
[442f42f] | 167 | |
---|
| 168 | def test_add(self): |
---|
| 169 | result = self.data2+self.data |
---|
[37dea17] | 170 | for i in range(25): |
---|
[e2605a5] | 171 | self.assertEqual(result.data[i], 3.0) |
---|
| 172 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
[442f42f] | 173 | |
---|
| 174 | def test_sub(self): |
---|
| 175 | result = self.data2-self.data |
---|
[37dea17] | 176 | for i in range(25): |
---|
[e2605a5] | 177 | self.assertEqual(result.data[i], -1.0) |
---|
| 178 | self.assertEqual(result.err_data[i], math.sqrt(0.5**2+1.0)) |
---|
[442f42f] | 179 | |
---|
| 180 | def test_mul(self): |
---|
| 181 | result = self.data2*self.data |
---|
[37dea17] | 182 | for i in range(25): |
---|
[e2605a5] | 183 | self.assertEqual(result.data[i], 2.0) |
---|
| 184 | self.assertEqual(result.err_data[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
[442f42f] | 185 | |
---|
| 186 | def test_div(self): |
---|
| 187 | result = self.data2/self.data |
---|
[37dea17] | 188 | for i in range(25): |
---|
[e2605a5] | 189 | self.assertEqual(result.data[i], 0.5) |
---|
| 190 | self.assertEqual(result.err_data[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
[442f42f] | 191 | |
---|
| 192 | def test_radd(self): |
---|
| 193 | result = self.data+3.0 |
---|
[37dea17] | 194 | for i in range(25): |
---|
[e2605a5] | 195 | self.assertEqual(result.data[i], 5.0) |
---|
| 196 | self.assertEqual(result.err_data[i], 0.5) |
---|
[442f42f] | 197 | |
---|
| 198 | result = 3.0+self.data |
---|
[37dea17] | 199 | for i in range(25): |
---|
[e2605a5] | 200 | self.assertEqual(result.data[i], 5.0) |
---|
| 201 | self.assertEqual(result.err_data[i], 0.5) |
---|
[442f42f] | 202 | |
---|
| 203 | def test_rsub(self): |
---|
| 204 | result = self.data-3.0 |
---|
[37dea17] | 205 | for i in range(25): |
---|
[e2605a5] | 206 | self.assertEqual(result.data[i], -1.0) |
---|
| 207 | self.assertEqual(result.err_data[i], 0.5) |
---|
[442f42f] | 208 | |
---|
| 209 | result = 3.0-self.data |
---|
[37dea17] | 210 | for i in range(25): |
---|
[e2605a5] | 211 | self.assertEqual(result.data[i], 1.0) |
---|
| 212 | self.assertEqual(result.err_data[i], 0.5) |
---|
[442f42f] | 213 | |
---|
| 214 | def test_rmul(self): |
---|
| 215 | result = self.data*3.0 |
---|
[37dea17] | 216 | for i in range(25): |
---|
[e2605a5] | 217 | self.assertEqual(result.data[i], 6.0) |
---|
| 218 | self.assertEqual(result.err_data[i], 1.5) |
---|
[442f42f] | 219 | |
---|
| 220 | result = 3.0*self.data |
---|
[37dea17] | 221 | for i in range(25): |
---|
[e2605a5] | 222 | self.assertEqual(result.data[i], 6.0) |
---|
| 223 | self.assertEqual(result.err_data[i], 1.5) |
---|
[442f42f] | 224 | |
---|
| 225 | def test_rdiv(self): |
---|
| 226 | result = self.data/4.0 |
---|
[37dea17] | 227 | for i in range(25): |
---|
[e2605a5] | 228 | self.assertEqual(result.data[i], 0.5) |
---|
| 229 | self.assertEqual(result.err_data[i], 0.125) |
---|
[442f42f] | 230 | |
---|
| 231 | result = 6.0/self.data |
---|
[37dea17] | 232 | for i in range(25): |
---|
[e2605a5] | 233 | self.assertEqual(result.data[i], 3.0) |
---|
| 234 | self.assertEqual(result.err_data[i], 6.0*0.5/4.0) |
---|
[442f42f] | 235 | |
---|
[9198b83] | 236 | |
---|
| 237 | if __name__ == '__main__': |
---|
| 238 | unittest.main() |
---|
| 239 | |
---|