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