[a10364b] | 1 | |
---|
[9a5097c] | 2 | import math |
---|
[4fb10e5] | 3 | import os |
---|
[fa4af76] | 4 | import unittest |
---|
[e123eb9] | 5 | from scipy.stats import binned_statistic_2d |
---|
[9a5097c] | 6 | import numpy as np |
---|
[fa4af76] | 7 | |
---|
[b699768] | 8 | import sas.sascalc.dataloader.data_info as data_info |
---|
[fa4af76] | 9 | from sas.sascalc.dataloader.loader import Loader |
---|
| 10 | from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum, |
---|
| 11 | CircularAverage, Ring, |
---|
| 12 | SectorPhi, SectorQ, SlabX, |
---|
| 13 | SlabY, get_q, |
---|
| 14 | reader2D_converter) |
---|
| 15 | |
---|
[a10364b] | 16 | |
---|
| 17 | class Averaging(unittest.TestCase): |
---|
| 18 | """ |
---|
| 19 | Test averaging manipulations on a flat distribution |
---|
| 20 | """ |
---|
[fa4af76] | 21 | |
---|
[a10364b] | 22 | def setUp(self): |
---|
| 23 | """ |
---|
| 24 | Create a flat 2D distribution. All averaging results |
---|
| 25 | should return the predefined height of the distribution (1.0). |
---|
| 26 | """ |
---|
[fa4af76] | 27 | x_0 = np.ones([100, 100]) |
---|
| 28 | dx_0 = np.ones([100, 100]) |
---|
| 29 | |
---|
[a10364b] | 30 | self.data = data_info.Data2D(data=x_0, err_data=dx_0) |
---|
| 31 | detector = data_info.Detector() |
---|
[fa4af76] | 32 | detector.distance = 1000.0 # mm |
---|
| 33 | detector.pixel_size.x = 1.0 # mm |
---|
| 34 | detector.pixel_size.y = 1.0 # mm |
---|
| 35 | |
---|
[a10364b] | 36 | # center in pixel position = (len(x_0)-1)/2 |
---|
[fa4af76] | 37 | detector.beam_center.x = (len(x_0) - 1) / 2 # pixel number |
---|
| 38 | detector.beam_center.y = (len(x_0) - 1) / 2 # pixel number |
---|
[a10364b] | 39 | self.data.detector.append(detector) |
---|
[fa4af76] | 40 | |
---|
[a10364b] | 41 | source = data_info.Source() |
---|
[fa4af76] | 42 | source.wavelength = 10.0 # A |
---|
[a10364b] | 43 | self.data.source = source |
---|
[fa4af76] | 44 | |
---|
| 45 | # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A |
---|
| 46 | # respectively. |
---|
[a10364b] | 47 | self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) |
---|
| 48 | |
---|
| 49 | self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) |
---|
[fa4af76] | 50 | |
---|
[a10364b] | 51 | self.qstep = len(x_0) |
---|
[fa4af76] | 52 | x = np.linspace(start=-1 * self.qmax, |
---|
| 53 | stop=self.qmax, |
---|
| 54 | num=self.qstep, |
---|
| 55 | endpoint=True) |
---|
| 56 | y = np.linspace(start=-1 * self.qmax, |
---|
| 57 | stop=self.qmax, |
---|
| 58 | num=self.qstep, |
---|
| 59 | endpoint=True) |
---|
| 60 | self.data.x_bins = x |
---|
| 61 | self.data.y_bins = y |
---|
[a10364b] | 62 | self.data = reader2D_converter(self.data) |
---|
[fa4af76] | 63 | |
---|
[a10364b] | 64 | def test_ring_flat_distribution(self): |
---|
| 65 | """ |
---|
| 66 | Test ring averaging |
---|
| 67 | """ |
---|
[fa4af76] | 68 | r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin, |
---|
| 69 | center_x=self.data.detector[0].beam_center.x, |
---|
[a10364b] | 70 | center_y=self.data.detector[0].beam_center.y) |
---|
| 71 | r.nbins_phi = 20 |
---|
[fa4af76] | 72 | |
---|
[a10364b] | 73 | o = r(self.data) |
---|
| 74 | for i in range(20): |
---|
| 75 | self.assertEqual(o.y[i], 1.0) |
---|
[fa4af76] | 76 | |
---|
[a10364b] | 77 | def test_sectorphi_full(self): |
---|
| 78 | """ |
---|
| 79 | Test sector averaging |
---|
| 80 | """ |
---|
[fa4af76] | 81 | r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, |
---|
| 82 | phi_min=0, phi_max=math.pi * 2.0) |
---|
[a10364b] | 83 | r.nbins_phi = 20 |
---|
| 84 | o = r(self.data) |
---|
| 85 | for i in range(7): |
---|
| 86 | self.assertEqual(o.y[i], 1.0) |
---|
[fa4af76] | 87 | |
---|
[a10364b] | 88 | def test_sectorphi_partial(self): |
---|
| 89 | """ |
---|
| 90 | """ |
---|
| 91 | phi_max = math.pi * 1.5 |
---|
[fa4af76] | 92 | r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, |
---|
[a10364b] | 93 | phi_min=0, phi_max=phi_max) |
---|
| 94 | self.assertEqual(r.phi_max, phi_max) |
---|
| 95 | r.nbins_phi = 20 |
---|
| 96 | o = r(self.data) |
---|
| 97 | self.assertEqual(r.phi_max, phi_max) |
---|
| 98 | for i in range(17): |
---|
| 99 | self.assertEqual(o.y[i], 1.0) |
---|
| 100 | |
---|
[fa4af76] | 101 | |
---|
| 102 | class DataInfoTests(unittest.TestCase): |
---|
| 103 | |
---|
[a10364b] | 104 | def setUp(self): |
---|
[fa4af76] | 105 | filepath = os.path.join(os.path.dirname( |
---|
| 106 | os.path.realpath(__file__)), 'MAR07232_rest.ASC') |
---|
[4fb10e5] | 107 | self.data = Loader().load(filepath) |
---|
[fa4af76] | 108 | |
---|
[a10364b] | 109 | def test_ring(self): |
---|
| 110 | """ |
---|
| 111 | Test ring averaging |
---|
| 112 | """ |
---|
[fa4af76] | 113 | r = Ring(r_min=.005, r_max=.01, |
---|
| 114 | center_x=self.data.detector[0].beam_center.x, |
---|
[a10364b] | 115 | center_y=self.data.detector[0].beam_center.y, |
---|
[fa4af76] | 116 | nbins=20) |
---|
[a10364b] | 117 | ##r.nbins_phi = 20 |
---|
[fa4af76] | 118 | |
---|
[a10364b] | 119 | o = r(self.data) |
---|
[fa4af76] | 120 | filepath = os.path.join(os.path.dirname( |
---|
| 121 | os.path.realpath(__file__)), 'ring_testdata.txt') |
---|
[4fb10e5] | 122 | answer = Loader().load(filepath) |
---|
[fa4af76] | 123 | |
---|
[a10364b] | 124 | for i in range(r.nbins_phi - 1): |
---|
| 125 | self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4) |
---|
| 126 | self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4) |
---|
| 127 | self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) |
---|
[fa4af76] | 128 | |
---|
[a10364b] | 129 | def test_circularavg(self): |
---|
| 130 | """ |
---|
[fa4af76] | 131 | Test circular averaging |
---|
| 132 | The test data was not generated by IGOR. |
---|
[a10364b] | 133 | """ |
---|
[fa4af76] | 134 | r = CircularAverage(r_min=.00, r_max=.025, |
---|
| 135 | bin_width=0.0003) |
---|
[a10364b] | 136 | r.nbins_phi = 20 |
---|
[fa4af76] | 137 | |
---|
[a10364b] | 138 | o = r(self.data) |
---|
| 139 | |
---|
[fa4af76] | 140 | filepath = os.path.join(os.path.dirname( |
---|
| 141 | os.path.realpath(__file__)), 'avg_testdata.txt') |
---|
[4fb10e5] | 142 | answer = Loader().load(filepath) |
---|
[a10364b] | 143 | for i in range(r.nbins_phi): |
---|
| 144 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 145 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 146 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
[fa4af76] | 147 | |
---|
[a10364b] | 148 | def test_box(self): |
---|
| 149 | """ |
---|
| 150 | Test circular averaging |
---|
| 151 | The test data was not generated by IGOR. |
---|
| 152 | """ |
---|
[fa4af76] | 153 | |
---|
[a10364b] | 154 | r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 155 | s, ds, npoints = r(self.data) |
---|
| 156 | self.assertAlmostEqual(s, 34.278990899999997, 4) |
---|
| 157 | self.assertAlmostEqual(ds, 7.8007981835194293, 4) |
---|
[fa4af76] | 158 | self.assertAlmostEqual(npoints, 324.0000, 4) |
---|
| 159 | |
---|
[a10364b] | 160 | r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) |
---|
| 161 | s, ds = r(self.data) |
---|
| 162 | self.assertAlmostEqual(s, 0.10579935462962962, 4) |
---|
| 163 | self.assertAlmostEqual(ds, 0.024076537603455028, 4) |
---|
[fa4af76] | 164 | |
---|
[a10364b] | 165 | def test_slabX(self): |
---|
| 166 | """ |
---|
| 167 | Test slab in X |
---|
| 168 | The test data was not generated by IGOR. |
---|
| 169 | """ |
---|
[fa4af76] | 170 | |
---|
| 171 | r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, |
---|
| 172 | y_max=0.0002, bin_width=0.0004) |
---|
[a10364b] | 173 | r.fold = False |
---|
| 174 | o = r(self.data) |
---|
| 175 | |
---|
[fa4af76] | 176 | filepath = os.path.join(os.path.dirname( |
---|
| 177 | os.path.realpath(__file__)), 'slabx_testdata.txt') |
---|
[4fb10e5] | 178 | answer = Loader().load(filepath) |
---|
[a10364b] | 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) |
---|
[fa4af76] | 183 | |
---|
[a10364b] | 184 | def test_slabY(self): |
---|
| 185 | """ |
---|
| 186 | Test slab in Y |
---|
| 187 | The test data was not generated by IGOR. |
---|
| 188 | """ |
---|
[fa4af76] | 189 | |
---|
| 190 | r = SlabY(x_min=.005, x_max=.01, y_min=- |
---|
| 191 | 0.01, y_max=0.01, bin_width=0.0004) |
---|
[a10364b] | 192 | r.fold = False |
---|
| 193 | o = r(self.data) |
---|
| 194 | |
---|
[fa4af76] | 195 | filepath = os.path.join(os.path.dirname( |
---|
| 196 | os.path.realpath(__file__)), 'slaby_testdata.txt') |
---|
[4fb10e5] | 197 | answer = Loader().load(filepath) |
---|
[a10364b] | 198 | for i in range(len(o.x)): |
---|
| 199 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 200 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 201 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
[fa4af76] | 202 | |
---|
[a10364b] | 203 | def test_sectorphi_full(self): |
---|
| 204 | """ |
---|
| 205 | Test sector averaging I(phi) |
---|
| 206 | When considering the whole azimuthal range (2pi), |
---|
| 207 | the answer should be the same as ring averaging. |
---|
| 208 | The test data was not generated by IGOR. |
---|
| 209 | """ |
---|
[fa4af76] | 210 | |
---|
[a10364b] | 211 | nbins = 19 |
---|
| 212 | phi_min = math.pi / (nbins + 1) |
---|
| 213 | phi_max = math.pi * 2 - phi_min |
---|
[fa4af76] | 214 | |
---|
[a10364b] | 215 | r = SectorPhi(r_min=.005, |
---|
| 216 | r_max=.01, |
---|
| 217 | phi_min=phi_min, |
---|
| 218 | phi_max=phi_max, |
---|
| 219 | nbins=nbins) |
---|
| 220 | o = r(self.data) |
---|
| 221 | |
---|
[fa4af76] | 222 | filepath = os.path.join(os.path.dirname( |
---|
| 223 | os.path.realpath(__file__)), 'ring_testdata.txt') |
---|
[4fb10e5] | 224 | answer = Loader().load(filepath) |
---|
[a10364b] | 225 | for i in range(len(o.x)): |
---|
| 226 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 227 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 228 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
[fa4af76] | 229 | |
---|
[a10364b] | 230 | def test_sectorphi_quarter(self): |
---|
| 231 | """ |
---|
| 232 | Test sector averaging I(phi) |
---|
| 233 | The test data was not generated by IGOR. |
---|
| 234 | """ |
---|
[fa4af76] | 235 | |
---|
| 236 | r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) |
---|
[a10364b] | 237 | r.nbins_phi = 20 |
---|
| 238 | o = r(self.data) |
---|
| 239 | |
---|
[fa4af76] | 240 | filepath = os.path.join(os.path.dirname( |
---|
| 241 | os.path.realpath(__file__)), 'sectorphi_testdata.txt') |
---|
[4fb10e5] | 242 | answer = Loader().load(filepath) |
---|
[a10364b] | 243 | for i in range(len(o.x)): |
---|
| 244 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 245 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 246 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
[fa4af76] | 247 | |
---|
[a10364b] | 248 | def test_sectorq_full(self): |
---|
| 249 | """ |
---|
| 250 | Test sector averaging I(q) |
---|
| 251 | The test data was not generated by IGOR. |
---|
| 252 | """ |
---|
[fa4af76] | 253 | |
---|
| 254 | r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) |
---|
[a10364b] | 255 | r.nbins_phi = 20 |
---|
| 256 | o = r(self.data) |
---|
| 257 | |
---|
[fa4af76] | 258 | filepath = os.path.join(os.path.dirname( |
---|
| 259 | os.path.realpath(__file__)), 'sectorq_testdata.txt') |
---|
[4fb10e5] | 260 | answer = Loader().load(filepath) |
---|
[a10364b] | 261 | for i in range(len(o.x)): |
---|
| 262 | self.assertAlmostEqual(o.x[i], answer.x[i], 4) |
---|
| 263 | self.assertAlmostEqual(o.y[i], answer.y[i], 4) |
---|
| 264 | self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) |
---|
[fa4af76] | 265 | |
---|
[e123eb9] | 266 | def test_sectorq_log(self): |
---|
| 267 | """ |
---|
| 268 | Test sector averaging I(q) |
---|
| 269 | The test data was not generated by IGOR. |
---|
| 270 | """ |
---|
| 271 | |
---|
| 272 | r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0, base=10) |
---|
| 273 | r.nbins_phi = 20 |
---|
| 274 | o = r(self.data) |
---|
| 275 | |
---|
| 276 | expected_binning = np.logspace(np.log10(0.005), np.log10(0.01), 20, base=10) |
---|
| 277 | for i in range(len(o.x)): |
---|
| 278 | self.assertAlmostEqual(o.x[i], expected_binning[i], 3) |
---|
| 279 | |
---|
| 280 | # TODO: Test for Y values (o.y) |
---|
| 281 | # print len(self.data.x_bins) |
---|
| 282 | # print len(self.data.y_bins) |
---|
| 283 | # print self.data.q_data.shape |
---|
| 284 | # data_to_bin = np.array(self.data.q_data) |
---|
| 285 | # data_to_bin = data_to_bin.reshape(len(self.data.x_bins), len(self.data.y_bins)) |
---|
| 286 | # H, xedges, yedges, binnumber = binned_statistic_2d(self.data.x_bins, self.data.y_bins, data_to_bin, |
---|
| 287 | # bins=[[0, math.pi / 2.0], expected_binning], statistic='mean') |
---|
| 288 | # xedges_width = (xedges[1] - xedges[0]) |
---|
| 289 | # xedges_center = xedges[1:] - xedges_width / 2 |
---|
| 290 | |
---|
| 291 | # yedges_width = (yedges[1] - yedges[0]) |
---|
| 292 | # yedges_center = yedges[1:] - yedges_width / 2 |
---|
| 293 | |
---|
| 294 | # print H.flatten().shape |
---|
| 295 | # print o.y.shape |
---|
| 296 | |
---|
[a10364b] | 297 | |
---|
| 298 | if __name__ == '__main__': |
---|
| 299 | unittest.main() |
---|