[9198b83] | 1 | """ |
---|
| 2 | Unit tests for data manipulations |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import unittest |
---|
| 6 | import numpy, math |
---|
| 7 | from DataLoader.loader import Loader |
---|
| 8 | from DataLoader.data_info import Data1D |
---|
| 9 | |
---|
| 10 | import os.path |
---|
| 11 | |
---|
| 12 | class data_info_tests(unittest.TestCase): |
---|
| 13 | |
---|
| 14 | def setUp(self): |
---|
| 15 | self.data = Loader().load("cansas1d.xml") |
---|
| 16 | |
---|
| 17 | def test_clone1D(self): |
---|
| 18 | """ |
---|
| 19 | Test basic cloning |
---|
| 20 | """ |
---|
| 21 | clone = self.data.clone_without_data() |
---|
| 22 | |
---|
| 23 | for i in range(len(self.data.detector)): |
---|
| 24 | self.assertEqual(self.data.detector[i].distance, clone.detector[i].distance) |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | class manip_tests(unittest.TestCase): |
---|
| 28 | |
---|
| 29 | def setUp(self): |
---|
| 30 | # Create two data sets to play with |
---|
| 31 | x_0 = numpy.ones(5) |
---|
| 32 | for i in range(5): |
---|
| 33 | x_0[i] = x_0[i]*(i+1.0) |
---|
| 34 | |
---|
| 35 | y_0 = 2.0*numpy.ones(5) |
---|
| 36 | dy_0 = 0.5*numpy.ones(5) |
---|
| 37 | self.data = Data1D(x_0, y_0, dy=dy_0) |
---|
| 38 | |
---|
| 39 | x = self.data.x |
---|
| 40 | y = numpy.ones(5) |
---|
| 41 | dy = numpy.ones(5) |
---|
| 42 | self.data2 = Data1D(x, y, dy=dy) |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | def test_load(self): |
---|
| 46 | """ |
---|
| 47 | Test whether the test file was loaded properly |
---|
| 48 | """ |
---|
| 49 | # There should be 5 entries in the file |
---|
| 50 | self.assertEqual(len(self.data.x), 5) |
---|
| 51 | |
---|
| 52 | for i in range(5): |
---|
| 53 | # The x values should be from 1 to 5 |
---|
| 54 | self.assertEqual(self.data.x[i], float(i+1)) |
---|
| 55 | |
---|
| 56 | # All y-error values should be 0.5 |
---|
| 57 | self.assertEqual(self.data.dy[i], 0.5) |
---|
| 58 | |
---|
| 59 | # All y values should be 2.0 |
---|
| 60 | self.assertEqual(self.data.y[i], 2.0) |
---|
| 61 | |
---|
| 62 | def test_add(self): |
---|
| 63 | result = self.data2+self.data |
---|
| 64 | for i in range(5): |
---|
| 65 | self.assertEqual(result.y[i], 3.0) |
---|
| 66 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
| 67 | |
---|
| 68 | def test_sub(self): |
---|
| 69 | result = self.data2-self.data |
---|
| 70 | for i in range(5): |
---|
| 71 | self.assertEqual(result.y[i], -1.0) |
---|
| 72 | self.assertEqual(result.dy[i], math.sqrt(0.5**2+1.0)) |
---|
| 73 | |
---|
| 74 | def test_mul(self): |
---|
| 75 | result = self.data2*self.data |
---|
| 76 | for i in range(5): |
---|
| 77 | self.assertEqual(result.y[i], 2.0) |
---|
| 78 | self.assertEqual(result.dy[i], math.sqrt((0.5*1.0)**2+(1.0*2.0)**2)) |
---|
| 79 | |
---|
| 80 | def test_div(self): |
---|
| 81 | result = self.data2/self.data |
---|
| 82 | for i in range(5): |
---|
| 83 | self.assertEqual(result.y[i], 0.5) |
---|
| 84 | self.assertEqual(result.dy[i], math.sqrt((1.0/2.0)**2+(0.5*1.0/4.0)**2)) |
---|
| 85 | |
---|
| 86 | def test_radd(self): |
---|
| 87 | result = self.data+3.0 |
---|
| 88 | for i in range(5): |
---|
| 89 | self.assertEqual(result.y[i], 5.0) |
---|
| 90 | self.assertEqual(result.dy[i], 0.5) |
---|
| 91 | |
---|
| 92 | result = 3.0+self.data |
---|
| 93 | for i in range(5): |
---|
| 94 | self.assertEqual(result.y[i], 5.0) |
---|
| 95 | self.assertEqual(result.dy[i], 0.5) |
---|
| 96 | |
---|
| 97 | def test_rsub(self): |
---|
| 98 | result = self.data-3.0 |
---|
| 99 | for i in range(5): |
---|
| 100 | self.assertEqual(result.y[i], -1.0) |
---|
| 101 | self.assertEqual(result.dy[i], 0.5) |
---|
| 102 | |
---|
| 103 | result = 3.0-self.data |
---|
| 104 | for i in range(5): |
---|
| 105 | self.assertEqual(result.y[i], 1.0) |
---|
| 106 | self.assertEqual(result.dy[i], 0.5) |
---|
| 107 | |
---|
| 108 | def test_rmul(self): |
---|
| 109 | result = self.data*3.0 |
---|
| 110 | for i in range(5): |
---|
| 111 | self.assertEqual(result.y[i], 6.0) |
---|
| 112 | self.assertEqual(result.dy[i], 1.5) |
---|
| 113 | |
---|
| 114 | result = 3.0*self.data |
---|
| 115 | for i in range(5): |
---|
| 116 | self.assertEqual(result.y[i], 6.0) |
---|
| 117 | self.assertEqual(result.dy[i], 1.5) |
---|
| 118 | |
---|
| 119 | def test_rdiv(self): |
---|
| 120 | result = self.data/4.0 |
---|
| 121 | for i in range(5): |
---|
| 122 | self.assertEqual(result.y[i], 0.5) |
---|
| 123 | self.assertEqual(result.dy[i], 0.125) |
---|
| 124 | |
---|
| 125 | result = 6.0/self.data |
---|
| 126 | for i in range(5): |
---|
| 127 | self.assertEqual(result.y[i], 3.0) |
---|
| 128 | self.assertEqual(result.dy[i], 6.0*0.5/4.0) |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | if __name__ == '__main__': |
---|
| 132 | unittest.main() |
---|
| 133 | |
---|