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