Changeset fa4af76 in sasview for test/sasdataloader
- Timestamp:
- Apr 7, 2017 9:51:47 AM (8 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:
- 168d359
- Parents:
- ccc7192
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasdataloader/test/utest_averaging.py
r4fb10e5 rfa4af76 1 1 2 import unittest3 2 import math 4 3 import os 5 6 from sas.sascalc.dataloader.loader import Loader 7 from sas.sascalc.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter 4 import unittest 8 5 9 6 import numpy as np 7 10 8 import sas.sascalc.dataloader.data_info as data_info 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 11 16 12 17 class Averaging(unittest.TestCase): … … 14 19 Test averaging manipulations on a flat distribution 15 20 """ 21 16 22 def setUp(self): 17 23 """ … … 19 25 should return the predefined height of the distribution (1.0). 20 26 """ 21 x_0 = np.ones([100,100])22 dx_0 = np.ones([100, 100])23 27 x_0 = np.ones([100, 100]) 28 dx_0 = np.ones([100, 100]) 29 24 30 self.data = data_info.Data2D(data=x_0, err_data=dx_0) 25 31 detector = data_info.Detector() 26 detector.distance = 1000.0 # mm27 detector.pixel_size.x = 1.0 #mm28 detector.pixel_size.y = 1.0 #mm29 32 detector.distance = 1000.0 # mm 33 detector.pixel_size.x = 1.0 # mm 34 detector.pixel_size.y = 1.0 # mm 35 30 36 # center in pixel position = (len(x_0)-1)/2 31 detector.beam_center.x = (len(x_0) -1)/2 #pixel number32 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 33 39 self.data.detector.append(detector) 34 40 35 41 source = data_info.Source() 36 source.wavelength = 10.0 #A42 source.wavelength = 10.0 # A 37 43 self.data.source = source 38 39 # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A respectively. 44 45 # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A 46 # respectively. 40 47 self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 41 48 42 49 self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 43 50 44 51 self.qstep = len(x_0) 45 x = np.linspace(start= -1*self.qmax,46 stop=self.qmax,47 num=self.qstep,48 endpoint=True )49 y = np.linspace(start= -1*self.qmax,50 stop=self.qmax,51 num=self.qstep,52 endpoint=True)53 self.data.x_bins =x54 self.data.y_bins =y52 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 55 62 self.data = reader2D_converter(self.data) 56 63 57 64 def test_ring_flat_distribution(self): 58 65 """ 59 66 Test ring averaging 60 67 """ 61 r = Ring(r_min=2 *self.qmin, r_max=5*self.qmin,62 center_x=self.data.detector[0].beam_center.x, 68 r = Ring(r_min=2 * self.qmin, r_max=5 * self.qmin, 69 center_x=self.data.detector[0].beam_center.x, 63 70 center_y=self.data.detector[0].beam_center.y) 64 71 r.nbins_phi = 20 65 72 66 73 o = r(self.data) 67 74 for i in range(20): 68 75 self.assertEqual(o.y[i], 1.0) 69 76 70 77 def test_sectorphi_full(self): 71 78 """ 72 79 Test sector averaging 73 80 """ 74 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,75 phi_min=0, phi_max=math.pi *2.0)81 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 82 phi_min=0, phi_max=math.pi * 2.0) 76 83 r.nbins_phi = 20 77 84 o = r(self.data) 78 85 for i in range(7): 79 86 self.assertEqual(o.y[i], 1.0) 80 81 87 82 88 def test_sectorphi_partial(self): 83 89 """ 84 90 """ 85 91 phi_max = math.pi * 1.5 86 r = SectorPhi(r_min=self.qmin, r_max=3 *self.qmin,92 r = SectorPhi(r_min=self.qmin, r_max=3 * self.qmin, 87 93 phi_min=0, phi_max=phi_max) 88 94 self.assertEqual(r.phi_max, phi_max) … … 92 98 for i in range(17): 93 99 self.assertEqual(o.y[i], 1.0) 94 95 96 97 class data_info_tests(unittest.TestCase): 98 100 101 102 class DataInfoTests(unittest.TestCase): 103 99 104 def setUp(self): 100 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'MAR07232_rest.ASC') 105 filepath = os.path.join(os.path.dirname( 106 os.path.realpath(__file__)), 'MAR07232_rest.ASC') 101 107 self.data = Loader().load(filepath) 102 108 103 109 def test_ring(self): 104 110 """ 105 111 Test ring averaging 106 112 """ 107 r = Ring(r_min=.005, r_max=.01, 108 center_x=self.data.detector[0].beam_center.x, 113 r = Ring(r_min=.005, r_max=.01, 114 center_x=self.data.detector[0].beam_center.x, 109 115 center_y=self.data.detector[0].beam_center.y, 110 nbins =20)116 nbins=20) 111 117 ##r.nbins_phi = 20 112 113 o = r(self.data) 114 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 115 answer = Loader().load(filepath) 116 118 119 o = r(self.data) 120 filepath = os.path.join(os.path.dirname( 121 os.path.realpath(__file__)), 'ring_testdata.txt') 122 answer = Loader().load(filepath) 123 117 124 for i in range(r.nbins_phi - 1): 118 125 self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4) 119 126 self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4) 120 127 self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 121 128 122 129 def test_circularavg(self): 123 130 """ 131 Test circular averaging 132 The test data was not generated by IGOR. 133 """ 134 r = CircularAverage(r_min=.00, r_max=.025, 135 bin_width=0.0003) 136 r.nbins_phi = 20 137 138 o = r(self.data) 139 140 filepath = os.path.join(os.path.dirname( 141 os.path.realpath(__file__)), 'avg_testdata.txt') 142 answer = Loader().load(filepath) 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) 147 148 def test_box(self): 149 """ 124 150 Test circular averaging 125 151 The test data was not generated by IGOR. 126 152 """ 127 r = CircularAverage(r_min=.00, r_max=.025, 128 bin_width=0.0003) 129 r.nbins_phi = 20 130 131 o = r(self.data) 132 133 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'avg_testdata.txt') 134 answer = Loader().load(filepath) 135 for i in range(r.nbins_phi): 136 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 137 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 138 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 139 140 def test_box(self): 141 """ 142 Test circular averaging 143 The test data was not generated by IGOR. 144 """ 145 from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg 146 153 147 154 r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 148 155 s, ds, npoints = r(self.data) 149 156 self.assertAlmostEqual(s, 34.278990899999997, 4) 150 157 self.assertAlmostEqual(ds, 7.8007981835194293, 4) 151 self.assertAlmostEqual(npoints, 324.0000, 4) 152 158 self.assertAlmostEqual(npoints, 324.0000, 4) 159 153 160 r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 154 161 s, ds = r(self.data) 155 162 self.assertAlmostEqual(s, 0.10579935462962962, 4) 156 163 self.assertAlmostEqual(ds, 0.024076537603455028, 4) 157 164 158 165 def test_slabX(self): 159 166 """ … … 161 168 The test data was not generated by IGOR. 162 169 """ 163 from sas.sascalc.dataloader.manipulations import SlabX 164 165 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002,y_max=0.0002, bin_width=0.0004)170 171 r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, 172 y_max=0.0002, bin_width=0.0004) 166 173 r.fold = False 167 174 o = r(self.data) 168 175 169 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slabx_testdata.txt') 170 answer = Loader().load(filepath) 171 for i in range(len(o.x)): 172 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 173 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 174 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 175 176 filepath = os.path.join(os.path.dirname( 177 os.path.realpath(__file__)), 'slabx_testdata.txt') 178 answer = Loader().load(filepath) 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 176 184 def test_slabY(self): 177 185 """ … … 179 187 The test data was not generated by IGOR. 180 188 """ 181 from sas.sascalc.dataloader.manipulations import SlabY 182 183 r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004)189 190 r = SlabY(x_min=.005, x_max=.01, y_min=- 191 0.01, y_max=0.01, bin_width=0.0004) 184 192 r.fold = False 185 193 o = r(self.data) 186 194 187 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'slaby_testdata.txt') 188 answer = Loader().load(filepath) 189 for i in range(len(o.x)): 190 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 191 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 192 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 193 195 filepath = os.path.join(os.path.dirname( 196 os.path.realpath(__file__)), 'slaby_testdata.txt') 197 answer = Loader().load(filepath) 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) 202 194 203 def test_sectorphi_full(self): 195 204 """ … … 199 208 The test data was not generated by IGOR. 200 209 """ 201 from sas.sascalc.dataloader.manipulations import SectorPhi 202 import math 203 210 204 211 nbins = 19 205 212 phi_min = math.pi / (nbins + 1) 206 213 phi_max = math.pi * 2 - phi_min 207 214 208 215 r = SectorPhi(r_min=.005, 209 216 r_max=.01, … … 213 220 o = r(self.data) 214 221 215 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ring_testdata.txt') 216 answer = Loader().load(filepath) 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 filepath = os.path.join(os.path.dirname( 223 os.path.realpath(__file__)), 'ring_testdata.txt') 224 answer = Loader().load(filepath) 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) 229 222 230 def test_sectorphi_quarter(self): 223 231 """ … … 225 233 The test data was not generated by IGOR. 226 234 """ 227 from sas.sascalc.dataloader.manipulations import SectorPhi 228 import math 229 230 r = SectorPhi(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 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorphi_testdata.txt') 235 answer = Loader().load(filepath) 236 for i in range(len(o.x)): 237 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 238 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 239 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 240 235 236 r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 237 r.nbins_phi = 20 238 o = r(self.data) 239 240 filepath = os.path.join(os.path.dirname( 241 os.path.realpath(__file__)), 'sectorphi_testdata.txt') 242 answer = Loader().load(filepath) 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) 247 241 248 def test_sectorq_full(self): 242 249 """ … … 244 251 The test data was not generated by IGOR. 245 252 """ 246 from sas.sascalc.dataloader.manipulations import SectorQ 247 import math 248 249 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) 250 r.nbins_phi = 20 251 o = r(self.data) 252 253 filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'sectorq_testdata.txt') 254 answer = Loader().load(filepath) 255 for i in range(len(o.x)): 256 self.assertAlmostEqual(o.x[i], answer.x[i], 4) 257 self.assertAlmostEqual(o.y[i], answer.y[i], 4) 258 self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 259 253 254 r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi / 2.0) 255 r.nbins_phi = 20 256 o = r(self.data) 257 258 filepath = os.path.join(os.path.dirname( 259 os.path.realpath(__file__)), 'sectorq_testdata.txt') 260 answer = Loader().load(filepath) 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) 265 260 266 261 267 if __name__ == '__main__':
Note: See TracChangeset
for help on using the changeset viewer.