[76e2369] | 1 | |
---|
| 2 | import unittest |
---|
| 3 | |
---|
| 4 | from DataLoader.loader import Loader |
---|
[e84e1e6] | 5 | from DataLoader.manipulations import Ring, CircularAverage, SectorPhi, get_q |
---|
[76e2369] | 6 | |
---|
| 7 | import os.path |
---|
[e84e1e6] | 8 | import numpy, math |
---|
| 9 | import DataLoader.data_info as data_info |
---|
| 10 | |
---|
| 11 | class Averaging(unittest.TestCase): |
---|
| 12 | """ |
---|
| 13 | Test averaging manipulations on a flat distribution |
---|
| 14 | """ |
---|
| 15 | def setUp(self): |
---|
| 16 | """ |
---|
| 17 | Create a flat 2D distribution. All averaging results |
---|
| 18 | should return the predefined height of the distribution (1.0). |
---|
| 19 | """ |
---|
| 20 | x_0 = numpy.ones([100,100]) |
---|
| 21 | dx_0 = numpy.ones([100,100]) |
---|
| 22 | self.data = data_info.Data2D(x_0, dx_0) |
---|
| 23 | detector = data_info.Detector() |
---|
| 24 | detector.distance = 1000.0 |
---|
| 25 | detector.pixel_size.x = 1.0 |
---|
| 26 | detector.pixel_size.y = 1.0 |
---|
| 27 | detector.beam_center.x = 50 |
---|
| 28 | detector.beam_center.y = 50 |
---|
| 29 | self.data.detector.append(detector) |
---|
| 30 | |
---|
| 31 | source = data_info.Source() |
---|
| 32 | source.wavelength = 10.0 |
---|
| 33 | self.data.source = source |
---|
| 34 | |
---|
| 35 | self.qmin = get_q(1.0, 1.0, 1000.0, 10.0) |
---|
| 36 | |
---|
| 37 | def test_ring_flat_distribution(self): |
---|
| 38 | """ |
---|
| 39 | Test ring averaging |
---|
| 40 | """ |
---|
| 41 | r = Ring(r_min=2*self.qmin, r_max=5*self.qmin, |
---|
| 42 | center_x=self.data.detector[0].beam_center.x, |
---|
| 43 | center_y=self.data.detector[0].beam_center.y) |
---|
| 44 | r.nbins_phi = 20 |
---|
| 45 | |
---|
| 46 | o = r(self.data) |
---|
| 47 | for i in range(20): |
---|
| 48 | self.assertEqual(o.y[i], 1.0) |
---|
| 49 | |
---|
| 50 | def test_sectorphi_full(self): |
---|
| 51 | """ |
---|
| 52 | Test sector averaging |
---|
| 53 | """ |
---|
| 54 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, phi_min=0, phi_max=math.pi*2.0) |
---|
| 55 | r.nbins_phi = 20 |
---|
| 56 | o = r(self.data) |
---|
| 57 | for i in range(20): |
---|
| 58 | self.assertEqual(o.y[i], 1.0) |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | def test_sectorphi_partial(self): |
---|
| 62 | """ |
---|
| 63 | """ |
---|
| 64 | phi_max = 1.5 |
---|
| 65 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, phi_min=0, phi_max=phi_max) |
---|
| 66 | self.assertEqual(r.phi_max, phi_max) |
---|
| 67 | r.nbins_phi = 20 |
---|
| 68 | o = r(self.data) |
---|
| 69 | self.assertEqual(r.phi_max, phi_max) |
---|
| 70 | for i in range(20): |
---|
| 71 | self.assertEqual(o.y[i], 1.0) |
---|
| 72 | |
---|
| 73 | |
---|
[76e2369] | 74 | |
---|
| 75 | class data_info_tests(unittest.TestCase): |
---|
| 76 | |
---|
| 77 | def setUp(self): |
---|
| 78 | self.data = Loader().load('MAR07232_rest.ASC') |
---|
| 79 | |
---|
| 80 | def test_ring(self): |
---|
| 81 | """ |
---|
| 82 | Test ring averaging |
---|
| 83 | """ |
---|
| 84 | r = Ring(r_min=.005, r_max=.01, |
---|
| 85 | center_x=self.data.detector[0].beam_center.x, |
---|
| 86 | center_y=self.data.detector[0].beam_center.y) |
---|
| 87 | r.nbins_phi = 20 |
---|
| 88 | |
---|
| 89 | o = r(self.data) |
---|
| 90 | answer = Loader().load('ring_testdata.txt') |
---|
| 91 | for i in range(r.nbins_phi): |
---|
| 92 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 93 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 94 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 95 | |
---|
| 96 | def test_circularavg(self): |
---|
| 97 | """ |
---|
| 98 | Test circular averaging |
---|
| 99 | The test data was not generated by IGOR. |
---|
| 100 | """ |
---|
| 101 | r = CircularAverage(r_min=.00, r_max=.025, |
---|
| 102 | bin_width=0.0003) |
---|
| 103 | r.nbins_phi = 20 |
---|
| 104 | |
---|
| 105 | o = r(self.data) |
---|
| 106 | answer = Loader().load('avg_testdata.txt') |
---|
| 107 | for i in range(r.nbins_phi): |
---|
| 108 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 109 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 110 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 111 | |
---|
[f8d0ee7] | 112 | def test_box(self): |
---|
| 113 | """ |
---|
| 114 | Test circular averaging |
---|
| 115 | The test data was not generated by IGOR. |
---|
| 116 | """ |
---|
| 117 | from DataLoader.manipulations import Boxsum, Boxavg |
---|
| 118 | |
---|
| 119 | r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 120 | s, ds = r(self.data) |
---|
| 121 | self.assertAlmostEqual(s, 151.81809601016641, 4) |
---|
| 122 | self.assertAlmostEqual(ds, 16.245399156009537, 4) |
---|
| 123 | |
---|
| 124 | r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 125 | s, ds = r(self.data) |
---|
| 126 | self.assertAlmostEqual(s, 0.11195555855955155, 4) |
---|
| 127 | self.assertAlmostEqual(ds, 0.011979881083557541, 4) |
---|
| 128 | |
---|
[2e83ff3] | 129 | def test_slabX(self): |
---|
| 130 | """ |
---|
| 131 | Test slab in X |
---|
| 132 | The test data was not generated by IGOR. |
---|
| 133 | """ |
---|
| 134 | from DataLoader.manipulations import SlabX |
---|
| 135 | |
---|
| 136 | r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) |
---|
| 137 | r.fold = False |
---|
| 138 | o = r(self.data) |
---|
| 139 | |
---|
| 140 | answer = Loader().load('slabx_testdata.txt') |
---|
| 141 | for i in range(len(o.x)): |
---|
| 142 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 143 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 144 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 145 | |
---|
| 146 | def test_slabY(self): |
---|
| 147 | """ |
---|
| 148 | Test slab in Y |
---|
| 149 | The test data was not generated by IGOR. |
---|
| 150 | """ |
---|
| 151 | from DataLoader.manipulations import SlabY |
---|
| 152 | |
---|
| 153 | r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) |
---|
| 154 | r.fold = False |
---|
| 155 | o = r(self.data) |
---|
| 156 | |
---|
| 157 | answer = Loader().load('slaby_testdata.txt') |
---|
| 158 | for i in range(len(o.x)): |
---|
| 159 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 160 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 161 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 162 | |
---|
| 163 | def test_sectorphi_full(self): |
---|
| 164 | """ |
---|
| 165 | Test sector averaging I(phi) |
---|
| 166 | When considering the whole azimuthal range (2pi), |
---|
| 167 | the answer should be the same as ring averaging. |
---|
| 168 | The test data was not generated by IGOR. |
---|
| 169 | """ |
---|
| 170 | from DataLoader.manipulations import SectorPhi |
---|
| 171 | import math |
---|
| 172 | |
---|
| 173 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi*2.0) |
---|
| 174 | r.nbins_phi = 20 |
---|
| 175 | o = r(self.data) |
---|
| 176 | |
---|
| 177 | answer = Loader().load('ring_testdata.txt') |
---|
| 178 | for i in range(len(o.x)): |
---|
| 179 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 180 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 181 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 182 | |
---|
| 183 | def test_sectorphi_quarter(self): |
---|
| 184 | """ |
---|
| 185 | Test sector averaging I(phi) |
---|
| 186 | The test data was not generated by IGOR. |
---|
| 187 | """ |
---|
| 188 | from DataLoader.manipulations import SectorPhi |
---|
| 189 | import math |
---|
| 190 | |
---|
| 191 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
| 192 | r.nbins_phi = 20 |
---|
| 193 | o = r(self.data) |
---|
| 194 | |
---|
| 195 | answer = Loader().load('sectorphi_testdata.txt') |
---|
| 196 | for i in range(len(o.x)): |
---|
| 197 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 198 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 199 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 200 | |
---|
| 201 | def test_sectorq_full(self): |
---|
| 202 | """ |
---|
| 203 | Test sector averaging I(q) |
---|
| 204 | The test data was not generated by IGOR. |
---|
| 205 | """ |
---|
| 206 | from DataLoader.manipulations import SectorQ |
---|
| 207 | import math |
---|
| 208 | |
---|
| 209 | r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
| 210 | r.nbins_phi = 20 |
---|
| 211 | o = r(self.data) |
---|
| 212 | |
---|
| 213 | answer = Loader().load('sectorq_testdata.txt') |
---|
| 214 | for i in range(len(o.x)): |
---|
| 215 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 216 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 217 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 218 | |
---|
[76e2369] | 219 | |
---|
| 220 | if __name__ == '__main__': |
---|
| 221 | unittest.main() |
---|