Changeset fa4af76 in sasview
- Timestamp:
- Apr 7, 2017 7: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
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/manipulations.py
rccc7192 rfa4af76 1 from __future__ import division 1 2 """ 2 3 Data manipulations for 2D data sets. … … 560 561 561 562 dq_data = None 562 if data2D.dqx_data != None and data2D.dqy_data !=None:563 if data2D.dqx_data is not None and data2D.dqy_data is not None: 563 564 dq_data = get_dq_data(data2D) 564 565 565 566 #q_data_max = np.max(q_data) 566 if len(data2D.q_data) ==None:567 if len(data2D.q_data) is None: 567 568 msg = "Circular averaging: invalid q_data: %g" % data2D.q_data 568 569 raise RuntimeError(msg) … … 610 611 else: 611 612 err_y[i_q] += frac * frac * err_data[npt] * err_data[npt] 612 if dq_data !=None:613 if dq_data is not None: 613 614 # To be consistent with dq calculation in 1d reduction, 614 615 # we need just the averages (not quadratures) because … … 625 626 err_y[n] = -err_y[n] 626 627 err_y[n] = math.sqrt(err_y[n]) 627 # if err_x !=None:628 # if err_x is not None: 628 629 # err_x[n] = math.sqrt(err_x[n]) 629 630 … … 634 635 idx = (np.isfinite(y)) & (np.isfinite(x)) 635 636 636 if err_x !=None:637 if err_x is not None: 637 638 d_x = err_x[idx] / y_counts[idx] 638 639 else: … … 791 792 792 793 dq_data = None 793 if data2D.dqx_data != None and data2D.dqy_data !=None:794 if data2D.dqx_data is not None and data2D.dqy_data is not None: 794 795 dq_data = get_dq_data(data2D) 795 796 … … 799 800 y_err = np.zeros(self.nbins) 800 801 x_err = np.zeros(self.nbins) 801 y_counts = np.zeros(self.nbins) 802 y_counts = np.zeros(self.nbins) # Cycle counts (for the mean) 802 803 803 804 # Get the min and max into the region: 0 <= phi < 2Pi … … 817 818 phi_value = math.atan2(qy_data[n], qx_data[n]) + math.pi 818 819 819 # No need to calculate the frac when all data are within range820 # No need to calculate: data outside of the radius 820 821 if self.r_min > q_value or q_value > self.r_max: 821 822 continue … … 844 845 phi_value < phi_max) 845 846 847 # data oustide of the phi range 846 848 if not is_in: 847 849 continue 850 848 851 # Check which type of averaging we need 849 852 if run.lower() == 'phi': 850 853 temp_x = (self.nbins) * (phi_value - self.phi_min) 851 854 temp_y = (self.phi_max - self.phi_min) 852 i_bin = int(math.floor(temp_x / temp_y))853 855 else: 854 856 temp_x = (self.nbins) * (q_value - self.r_min) 855 857 temp_y = (self.r_max - self.r_min) 856 i_bin = int(math.floor(temp_x / temp_y)) 858 # Bin index calulation 859 i_bin = int(math.floor(temp_x / temp_y)) 857 860 858 861 # Take care of the edge case at phi = 2pi. … … 863 866 y[i_bin] += data_n 864 867 x[i_bin] += q_value 865 if err_data[n] ==None or err_data[n] == 0.0:868 if err_data[n] is None or err_data[n] == 0.0: 866 869 if data_n < 0: 867 870 data_n = -data_n 868 y_err[i_bin] += 871 y_err[i_bin] += data_n 869 872 else: 870 y_err[i_bin] += err_data[n] * err_data[n]871 872 if dq_data !=None:873 y_err[i_bin] += err_data[n]**2 874 875 if dq_data is not None: 873 876 # To be consistent with dq calculation in 1d reduction, 874 877 # we need just the averages (not quadratures) because … … 900 903 y_err[y_err == 0] = np.average(y_err) 901 904 idx = (np.isfinite(y) & np.isfinite(y_err)) 902 if x_err !=None:905 if x_err is not None: 903 906 d_x = x_err[idx] / y_counts[idx] 904 907 else: -
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__': -
test/sasguiframe/test/utest_manipulations.py
rfd5d6eac rfa4af76 11 11 12 12 from sas.sascalc.dataloader.loader import Loader 13 from sas.sasgui.guiframe.dataFitting import Data1D as Theory1D13 from sas.sasgui.guiframe.dataFitting import Data1D 14 14 from sas.sasgui.guiframe.dataFitting import Data2D 15 15 16 16 17 class data_info_tests(unittest.TestCase):17 class DataInfoTests(unittest.TestCase): 18 18 19 19 def setUp(self): … … 32 32 33 33 34 class theory1d_tests(unittest.TestCase):34 class Theory1DTests(unittest.TestCase): 35 35 36 36 def setUp(self): … … 42 42 Test basic cloning 43 43 """ 44 theory = Theory1D(x=[], y=[], dy=None)44 theory = Data1D(x=[], y=[], dy=None) 45 45 theory.clone_without_data(clone=self.data) 46 46 theory.copy_from_datainfo(data1d=self.data) … … 55 55 56 56 57 class manip_tests(unittest.TestCase):57 class ManipTests(unittest.TestCase): 58 58 59 59 def setUp(self): … … 160 160 161 161 162 class manip_2D(unittest.TestCase):162 class Manin2DTests(unittest.TestCase): 163 163 164 164 def setUp(self): … … 270 270 271 271 272 class extra_manip_2D(unittest.TestCase):272 class ExtraManip2DTests(unittest.TestCase): 273 273 274 274 def setUp(self):
Note: See TracChangeset
for help on using the changeset viewer.