Changeset 5a8cdbb in sasview for test/sasdataloader/test/utest_averaging.py
- Timestamp:
- Aug 1, 2017 12:02:35 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 511ccb2d
- Parents:
- 248ff73 (diff), bc04647 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasdataloader/test/utest_averaging.py
r8ffafd1 r5a8cdbb 1 1 2 2 import math 3 import os 3 4 import unittest 5 from scipy.stats import binned_statistic_2d 4 6 import numpy as np 7 5 8 import sas.sascalc.dataloader.data_info as data_info 6 9 from sas.sascalc.dataloader.loader import Loader 7 from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg, Ring, get_q,\ 8 CircularAverage, SectorPhi, SectorQ, reader2D_converter, SlabX, SlabY 10 from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum, 11 CircularAverage, Ring, 12 SectorPhi, SectorQ, SlabX, 13 SlabY, get_q, 14 reader2D_converter) 9 15 10 16 … … 13 19 Test averaging manipulations on a flat distribution 14 20 """ 21 15 22 def setUp(self): 16 23 """ … … 18 25 should return the predefined height of the distribution (1.0). 19 26 """ 20 x_0 = np.ones([100, 100])21 dx_0 = np.ones([100, 100])27 x_0 = np.ones([100, 100]) 28 dx_0 = np.ones([100, 100]) 22 29 23 30 self.data = data_info.Data2D(data=x_0, err_data=dx_0) 24 31 detector = data_info.Detector() 25 32 detector.distance = 1000.0 # mm 26 detector.pixel_size.x = 1.0 # mm27 detector.pixel_size.y = 1.0 # mm33 detector.pixel_size.x = 1.0 # mm 34 detector.pixel_size.y = 1.0 # mm 28 35 29 36 # center in pixel position = (len(x_0)-1)/2 30 detector.beam_center.x = (len(x_0) -1)/2# pixel number31 detector.beam_center.y = (len(x_0) -1)/2# pixel number37 detector.beam_center.x = (len(x_0) - 1) / 2 # pixel number 38 detector.beam_center.y = (len(x_0) - 1) / 2 # pixel number 32 39 self.data.detector.append(detector) 33 40 34 41 source = data_info.Source() 35 source.wavelength = 10.0 # A42 source.wavelength = 10.0 # A 36 43 self.data.source = source 37 44 38 # get_q(dx, dy, det_dist, wavelength) where units are mm, mm, mm, and A 45 # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A 46 # respectively. 39 47 self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 40 48 self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 41 49 42 50 self.qstep = len(x_0) 43 x = np.linspace(start= -1*self.qmax,44 stop=self.qmax,45 num=self.qstep,46 endpoint=True )47 y = np.linspace(start= -1*self.qmax,48 stop=self.qmax,49 num=self.qstep,50 endpoint=True)51 self.data.x_bins =x52 self.data.y_bins =y51 x = np.linspace(start=-1 * self.qmax, 52 stop=self.qmax, 53 num=self.qstep, 54 endpoint=True) 55 y = np.linspace(start=-1 * self.qmax, 56 stop=self.qmax, 57 num=self.qstep, 58 endpoint=True) 59 self.data.x_bins = x 60 self.data.y_bins = y 53 61 self.data = reader2D_converter(self.data) 54 62 … … 57 65 Test ring averaging 58 66 """ 59 r = Ring(r_min=2 *self.qmin, r_max=5*self.qmin,60 center_x=self.data.detector[0].beam_center.x, 67 r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin, 68 center_x=self.data.detector[0].beam_center.x, 61 69 center_y=self.data.detector[0].beam_center.y) 62 70 r.nbins_phi = 20 … … 70 78 Test sector averaging 71 79 """ 72 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,73 phi_min=0, phi_max=math.pi *2.0)80 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 81 phi_min=0, phi_max=math.pi * 2.0) 74 82 r.nbins_phi = 20 75 83 o = r(self.data) … … 81 89 """ 82 90 phi_max = math.pi * 1.5 83 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,91 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 84 92 phi_min=0, phi_max=phi_max) 85 93 self.assertEqual(r.phi_max, phi_max) … … 94 102 95 103 def setUp(self): 96 self.data = Loader().load('MAR07232_rest.ASC') 104 filepath = os.path.join(os.path.dirname( 105 os.path.realpath(__file__)), 'MAR07232_rest.ASC') 106 self.data = Loader().load(filepath) 97 107 98 108 def test_ring(self): … … 100 110 Test ring averaging 101 111 """ 102 r = Ring(r_min=.005, r_max=.01, 103 center_x=self.data.detector[0].beam_center.x, 112 r = Ring(r_min=.005, r_max=.01, 113 center_x=self.data.detector[0].beam_center.x, 104 114 center_y=self.data.detector[0].beam_center.y, 105 nbins = 20) 106 # r.nbins_phi = 20 107 108 o = r(self.data) 109 data_list = Loader().load('ring_testdata.txt') 110 answer = data_list[0] 115 nbins=20) 116 ##r.nbins_phi = 20 117 118 o = r(self.data) 119 filepath = os.path.join(os.path.dirname( 120 os.path.realpath(__file__)), 'ring_testdata.txt') 121 answer = Loader().load(filepath) 111 122 112 123 for i in range(r.nbins_phi - 1): … … 115 126 self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 116 127 117 def test_circular_avg(self): 128 def test_circularavg(self): 129 """ 130 Test circular averaging 131 The test data was not generated by IGOR. 132 """ 133 r = CircularAverage(r_min=.00, r_max=.025, 134 bin_width=0.0003) 135 r.nbins_phi = 20 136 137 o = r(self.data) 138 139 filepath = os.path.join(os.path.dirname( 140 os.path.realpath(__file__)), 'avg_testdata.txt') 141 answer = Loader().load(filepath) 142 for i in range(r.nbins_phi): 143 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 144 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 145 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 146 147 def test_box(self): 118 148 """ 119 149 Test circular averaging 120 150 The test data was not generated by IGOR. 121 151 """ 122 r = CircularAverage(r_min=.00, r_max=.025, 123 bin_width=0.0003) 124 r.nbins_phi = 20 125 o = r(self.data) 126 127 output = Loader().load('avg_testdata.txt') 128 answer = output[0] 129 for i in range(r.nbins_phi): 130 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 131 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 132 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 133 134 def test_box(self): 135 """ 136 Test circular averaging 137 The test data was not generated by IGOR. 138 """ 152 139 153 r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 140 154 s, ds, npoints = r(self.data) 141 155 self.assertAlmostEqual(s, 34.278990899999997, 4) 142 156 self.assertAlmostEqual(ds, 7.8007981835194293, 4) 143 self.assertAlmostEqual(npoints, 324.0000, 4) 157 self.assertAlmostEqual(npoints, 324.0000, 4) 144 158 145 159 r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) … … 153 167 The test data was not generated by IGOR. 154 168 """ 155 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) 169 170 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, 171 y_max=0.0002, bin_width=0.0004) 156 172 r.fold = False 157 173 o = r(self.data) 158 174 159 output = Loader().load('slabx_testdata.txt') 160 answer = output[0] 175 filepath = os.path.join(os.path.dirname( 176 os.path.realpath(__file__)), 'slabx_testdata.txt') 177 answer = Loader().load(filepath) 161 178 for i in range(len(o.x)): 162 179 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 169 186 The test data was not generated by IGOR. 170 187 """ 171 r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) 188 189 r = SlabY(x_min=.005, x_max=.01, y_min=- 190 0.01, y_max=0.01, bin_width=0.0004) 172 191 r.fold = False 173 192 o = r(self.data) 174 193 175 output = Loader().load('slaby_testdata.txt') 176 answer = output[0] 194 filepath = os.path.join(os.path.dirname( 195 os.path.realpath(__file__)), 'slaby_testdata.txt') 196 answer = Loader().load(filepath) 177 197 for i in range(len(o.x)): 178 198 self.assertAlmostEqual(o.x[i], answer.x[i], 4) … … 183 203 """ 184 204 Test sector averaging I(phi) 185 When considering the whole azimuthal range (2pi), 205 When considering the whole azimuthal range (2pi), 186 206 the answer should be the same as ring averaging. 187 207 The test data was not generated by IGOR. 188 208 """ 209 189 210 nbins = 19 190 211 phi_min = math.pi / (nbins + 1) … … 198 219 o = r(self.data) 199 220 200 output = Loader().load('ring_testdata.txt') 201 answer = output[0] 202 for i in range(len(o.x)): 203 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 204 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 205 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 206 221 filepath = os.path.join(os.path.dirname( 222 os.path.realpath(__file__)), 'ring_testdata.txt') 223 answer = Loader().load(filepath) 224 for i in range(len(o.x)): 225 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 226 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 227 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 228 207 229 def test_sectorphi_quarter(self): 208 230 """ … … 210 232 The test data was not generated by IGOR. 211 233 """ 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 output = Loader().load('sectorphi_testdata.txt') 217 answer = output[0] 218 for i in range(len(o.x)): 219 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 220 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 221 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 222 234 235 r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 236 r.nbins_phi = 20 237 o = r(self.data) 238 239 filepath = os.path.join(os.path.dirname( 240 os.path.realpath(__file__)), 'sectorphi_testdata.txt') 241 answer = Loader().load(filepath) 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) 246 223 247 def test_sectorq_full(self): 224 248 """ … … 226 250 The test data was not generated by IGOR. 227 251 """ 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 output = Loader().load('sectorq_testdata.txt') 233 answer = output[0] 234 for i in range(len(o.x)): 235 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 236 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 237 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 252 253 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 254 r.nbins_phi = 20 255 o = r(self.data) 256 257 filepath = os.path.join(os.path.dirname( 258 os.path.realpath(__file__)), 'sectorq_testdata.txt') 259 answer = Loader().load(filepath) 260 for i in range(len(o.x)): 261 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 262 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 263 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 264 265 def test_sectorq_log(self): 266 """ 267 Test sector averaging I(q) 268 The test data was not generated by IGOR. 269 """ 270 271 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0, base=10) 272 r.nbins_phi = 20 273 o = r(self.data) 274 275 expected_binning = np.logspace(np.log10(0.005), np.log10(0.01), 20, base=10) 276 for i in range(len(o.x)): 277 self.assertAlmostEqual(o.x[i], expected_binning[i], 3) 278 279 # TODO: Test for Y values (o.y) 280 # print len(self.data.x_bins) 281 # print len(self.data.y_bins) 282 # print self.data.q_data.shape 283 # data_to_bin = np.array(self.data.q_data) 284 # data_to_bin = data_to_bin.reshape(len(self.data.x_bins), len(self.data.y_bins)) 285 # H, xedges, yedges, binnumber = binned_statistic_2d(self.data.x_bins, self.data.y_bins, data_to_bin, 286 # bins=[[0, math.pi / 2.0], expected_binning], statistic='mean') 287 # xedges_width = (xedges[1] - xedges[0]) 288 # xedges_center = xedges[1:] - xedges_width / 2 289 290 # yedges_width = (yedges[1] - yedges[0]) 291 # yedges_center = yedges[1:] - yedges_width / 2 292 293 # print H.flatten().shape 294 # print o.y.shape 238 295 239 296
Note: See TracChangeset
for help on using the changeset viewer.