[76e2369] | 1 | |
---|
| 2 | import unittest |
---|
| 3 | |
---|
[ca3b9c5d] | 4 | from sans.dataloader.loader import Loader |
---|
| 5 | from sans.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter |
---|
[76e2369] | 6 | |
---|
| 7 | import os.path |
---|
[e84e1e6] | 8 | import numpy, math |
---|
[ca3b9c5d] | 9 | import sans.dataloader.data_info as data_info |
---|
[e84e1e6] | 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 | """ |
---|
[342a506] | 73 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, |
---|
| 74 | phi_min=0, phi_max=math.pi*2.0) |
---|
[e84e1e6] | 75 | r.nbins_phi = 20 |
---|
| 76 | o = r(self.data) |
---|
[342a506] | 77 | for i in range(7): |
---|
[e84e1e6] | 78 | self.assertEqual(o.y[i], 1.0) |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | def test_sectorphi_partial(self): |
---|
| 82 | """ |
---|
| 83 | """ |
---|
[342a506] | 84 | phi_max = math.pi * 1.5 |
---|
| 85 | r = SectorPhi(r_min=self.qmin, r_max=3*self.qmin, |
---|
| 86 | phi_min=0, phi_max=phi_max) |
---|
[e84e1e6] | 87 | self.assertEqual(r.phi_max, phi_max) |
---|
| 88 | r.nbins_phi = 20 |
---|
| 89 | o = r(self.data) |
---|
| 90 | self.assertEqual(r.phi_max, phi_max) |
---|
[342a506] | 91 | for i in range(17): |
---|
[e84e1e6] | 92 | self.assertEqual(o.y[i], 1.0) |
---|
| 93 | |
---|
| 94 | |
---|
[76e2369] | 95 | |
---|
| 96 | class data_info_tests(unittest.TestCase): |
---|
| 97 | |
---|
| 98 | def setUp(self): |
---|
| 99 | self.data = Loader().load('MAR07232_rest.ASC') |
---|
| 100 | |
---|
| 101 | def test_ring(self): |
---|
| 102 | """ |
---|
| 103 | Test ring averaging |
---|
| 104 | """ |
---|
| 105 | r = Ring(r_min=.005, r_max=.01, |
---|
| 106 | center_x=self.data.detector[0].beam_center.x, |
---|
| 107 | center_y=self.data.detector[0].beam_center.y) |
---|
| 108 | r.nbins_phi = 20 |
---|
| 109 | |
---|
| 110 | o = r(self.data) |
---|
| 111 | answer = Loader().load('ring_testdata.txt') |
---|
| 112 | for i in range(r.nbins_phi): |
---|
| 113 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 114 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 115 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 116 | |
---|
| 117 | def test_circularavg(self): |
---|
| 118 | """ |
---|
| 119 | Test circular averaging |
---|
| 120 | The test data was not generated by IGOR. |
---|
| 121 | """ |
---|
| 122 | r = CircularAverage(r_min=.00, r_max=.025, |
---|
| 123 | bin_width=0.0003) |
---|
| 124 | r.nbins_phi = 20 |
---|
| 125 | |
---|
| 126 | o = r(self.data) |
---|
| 127 | answer = Loader().load('avg_testdata.txt') |
---|
| 128 | for i in range(r.nbins_phi): |
---|
| 129 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 130 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 131 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 132 | |
---|
[f8d0ee7] | 133 | def test_box(self): |
---|
| 134 | """ |
---|
| 135 | Test circular averaging |
---|
| 136 | The test data was not generated by IGOR. |
---|
| 137 | """ |
---|
[ca3b9c5d] | 138 | from sans.dataloader.manipulations import Boxsum, Boxavg |
---|
[f8d0ee7] | 139 | |
---|
| 140 | r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 141 | s, ds = r(self.data) |
---|
| 142 | self.assertAlmostEqual(s, 151.81809601016641, 4) |
---|
| 143 | self.assertAlmostEqual(ds, 16.245399156009537, 4) |
---|
| 144 | |
---|
| 145 | r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 146 | s, ds = r(self.data) |
---|
| 147 | self.assertAlmostEqual(s, 0.11195555855955155, 4) |
---|
| 148 | self.assertAlmostEqual(ds, 0.011979881083557541, 4) |
---|
| 149 | |
---|
[2e83ff3] | 150 | def test_slabX(self): |
---|
| 151 | """ |
---|
| 152 | Test slab in X |
---|
| 153 | The test data was not generated by IGOR. |
---|
| 154 | """ |
---|
[ca3b9c5d] | 155 | from sans.dataloader.manipulations import SlabX |
---|
[2e83ff3] | 156 | |
---|
| 157 | r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) |
---|
| 158 | r.fold = False |
---|
| 159 | o = r(self.data) |
---|
| 160 | |
---|
| 161 | answer = Loader().load('slabx_testdata.txt') |
---|
| 162 | for i in range(len(o.x)): |
---|
| 163 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 164 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 165 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 166 | |
---|
| 167 | def test_slabY(self): |
---|
| 168 | """ |
---|
| 169 | Test slab in Y |
---|
| 170 | The test data was not generated by IGOR. |
---|
| 171 | """ |
---|
[ca3b9c5d] | 172 | from sans.dataloader.manipulations import SlabY |
---|
[2e83ff3] | 173 | |
---|
| 174 | r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) |
---|
| 175 | r.fold = False |
---|
| 176 | o = r(self.data) |
---|
| 177 | |
---|
| 178 | answer = Loader().load('slaby_testdata.txt') |
---|
| 179 | for i in range(len(o.x)): |
---|
| 180 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 181 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 182 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 183 | |
---|
| 184 | def test_sectorphi_full(self): |
---|
| 185 | """ |
---|
| 186 | Test sector averaging I(phi) |
---|
| 187 | When considering the whole azimuthal range (2pi), |
---|
| 188 | the answer should be the same as ring averaging. |
---|
| 189 | The test data was not generated by IGOR. |
---|
| 190 | """ |
---|
[ca3b9c5d] | 191 | from sans.dataloader.manipulations import SectorPhi |
---|
[2e83ff3] | 192 | import math |
---|
| 193 | |
---|
| 194 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi*2.0) |
---|
| 195 | r.nbins_phi = 20 |
---|
| 196 | o = r(self.data) |
---|
| 197 | |
---|
| 198 | answer = Loader().load('ring_testdata.txt') |
---|
| 199 | for i in range(len(o.x)): |
---|
| 200 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 201 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 202 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 203 | |
---|
| 204 | def test_sectorphi_quarter(self): |
---|
| 205 | """ |
---|
| 206 | Test sector averaging I(phi) |
---|
| 207 | The test data was not generated by IGOR. |
---|
| 208 | """ |
---|
[ca3b9c5d] | 209 | from sans.dataloader.manipulations import SectorPhi |
---|
[2e83ff3] | 210 | import math |
---|
| 211 | |
---|
| 212 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
| 213 | r.nbins_phi = 20 |
---|
| 214 | o = r(self.data) |
---|
| 215 | |
---|
| 216 | answer = Loader().load('sectorphi_testdata.txt') |
---|
| 217 | for i in range(len(o.x)): |
---|
| 218 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 219 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 220 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 221 | |
---|
| 222 | def test_sectorq_full(self): |
---|
| 223 | """ |
---|
| 224 | Test sector averaging I(q) |
---|
| 225 | The test data was not generated by IGOR. |
---|
| 226 | """ |
---|
[ca3b9c5d] | 227 | from sans.dataloader.manipulations import SectorQ |
---|
[2e83ff3] | 228 | import math |
---|
| 229 | |
---|
| 230 | r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) |
---|
| 231 | r.nbins_phi = 20 |
---|
| 232 | o = r(self.data) |
---|
| 233 | |
---|
| 234 | answer = Loader().load('sectorq_testdata.txt') |
---|
| 235 | for i in range(len(o.x)): |
---|
| 236 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 237 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 238 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
| 239 | |
---|
[76e2369] | 240 | |
---|
| 241 | if __name__ == '__main__': |
---|
| 242 | unittest.main() |
---|