Ignore:
Timestamp:
Aug 1, 2017 12:02:35 PM (7 years ago)
Author:
lewis
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.
Message:

Merge branch 'master' into ticket-876

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/sasdataloader/test/utest_averaging.py

    r8ffafd1 r5a8cdbb  
    11 
    22import math 
     3import os 
    34import unittest 
     5from scipy.stats import binned_statistic_2d 
    46import numpy as np 
     7 
    58import sas.sascalc.dataloader.data_info as data_info 
    69from 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 
     10from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum, 
     11                                                  CircularAverage, Ring, 
     12                                                  SectorPhi, SectorQ, SlabX, 
     13                                                  SlabY, get_q, 
     14                                                  reader2D_converter) 
    915 
    1016 
     
    1319        Test averaging manipulations on a flat distribution 
    1420    """ 
     21 
    1522    def setUp(self): 
    1623        """ 
     
    1825            should return the predefined height of the distribution (1.0). 
    1926        """ 
    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]) 
    2229 
    2330        self.data = data_info.Data2D(data=x_0, err_data=dx_0) 
    2431        detector = data_info.Detector() 
    2532        detector.distance = 1000.0  # mm 
    26         detector.pixel_size.x = 1.0 # mm 
    27         detector.pixel_size.y = 1.0 # mm 
     33        detector.pixel_size.x = 1.0  # mm 
     34        detector.pixel_size.y = 1.0  # mm 
    2835 
    2936        # center in pixel position = (len(x_0)-1)/2 
    30         detector.beam_center.x = (len(x_0)-1)/2 # pixel number 
    31         detector.beam_center.y = (len(x_0)-1)/2 # pixel number 
     37        detector.beam_center.x = (len(x_0) - 1) / 2 # pixel number 
     38        detector.beam_center.y = (len(x_0) - 1) / 2 # pixel number 
    3239        self.data.detector.append(detector) 
    33          
     40 
    3441        source = data_info.Source() 
    35         source.wavelength = 10.0 # A 
     42        source.wavelength = 10.0  # A 
    3643        self.data.source = source 
    3744 
    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. 
    3947        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 
    4048        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 
    4149 
    4250        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=x 
    52         self.data.y_bins=y 
     51        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 
    5361        self.data = reader2D_converter(self.data) 
    5462 
     
    5765            Test ring averaging 
    5866        """ 
    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, 
    6169                 center_y=self.data.detector[0].beam_center.y) 
    6270        r.nbins_phi = 20 
     
    7078            Test sector averaging 
    7179        """ 
    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) 
    7482        r.nbins_phi = 20 
    7583        o = r(self.data) 
     
    8189        """ 
    8290        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, 
    8492                      phi_min=0, phi_max=phi_max) 
    8593        self.assertEqual(r.phi_max, phi_max) 
     
    94102 
    95103    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) 
    97107 
    98108    def test_ring(self): 
     
    100110            Test ring averaging 
    101111        """ 
    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, 
    104114                 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) 
    111122 
    112123        for i in range(r.nbins_phi - 1): 
     
    115126            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 
    116127 
    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): 
    118148        """ 
    119149            Test circular averaging 
    120150            The test data was not generated by IGOR. 
    121151        """ 
    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 
    139153        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
    140154        s, ds, npoints = r(self.data) 
    141155        self.assertAlmostEqual(s, 34.278990899999997, 4) 
    142156        self.assertAlmostEqual(ds, 7.8007981835194293, 4) 
    143         self.assertAlmostEqual(npoints, 324.0000, 4)         
     157        self.assertAlmostEqual(npoints, 324.0000, 4) 
    144158 
    145159        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
     
    153167            The test data was not generated by IGOR. 
    154168        """ 
    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) 
    156172        r.fold = False 
    157173        o = r(self.data) 
    158174 
    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) 
    161178        for i in range(len(o.x)): 
    162179            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    169186            The test data was not generated by IGOR. 
    170187        """ 
    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) 
    172191        r.fold = False 
    173192        o = r(self.data) 
    174193 
    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) 
    177197        for i in range(len(o.x)): 
    178198            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    183203        """ 
    184204            Test sector averaging I(phi) 
    185             When considering the whole azimuthal range (2pi),  
     205            When considering the whole azimuthal range (2pi), 
    186206            the answer should be the same as ring averaging. 
    187207            The test data was not generated by IGOR. 
    188208        """ 
     209 
    189210        nbins = 19 
    190211        phi_min = math.pi / (nbins + 1) 
     
    198219        o = r(self.data) 
    199220 
    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 
    207229    def test_sectorphi_quarter(self): 
    208230        """ 
     
    210232            The test data was not generated by IGOR. 
    211233        """ 
    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 
    223247    def test_sectorq_full(self): 
    224248        """ 
     
    226250            The test data was not generated by IGOR. 
    227251        """ 
    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 
    238295 
    239296 
Note: See TracChangeset for help on using the changeset viewer.