Changeset fa4af76 in sasview for test


Ignore:
Timestamp:
Apr 7, 2017 9:51:47 AM (7 years ago)
Author:
Ricardo Ferraz Leal <ricleal@…>
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
Message:

Refactoring

Location:
test
Files:
3 edited

Legend:

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

    r4fb10e5 rfa4af76  
    11 
    2 import unittest 
    32import math 
    43import os 
    5  
    6 from sas.sascalc.dataloader.loader import  Loader 
    7 from sas.sascalc.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter 
     4import unittest 
    85 
    96import numpy as np 
     7 
    108import sas.sascalc.dataloader.data_info as data_info 
     9from sas.sascalc.dataloader.loader import Loader 
     10from sas.sascalc.dataloader.manipulations import (Boxavg, Boxsum, 
     11                                                  CircularAverage, Ring, 
     12                                                  SectorPhi, SectorQ, SlabX, 
     13                                                  SlabY, get_q, 
     14                                                  reader2D_converter) 
     15 
    1116 
    1217class Averaging(unittest.TestCase): 
     
    1419        Test averaging manipulations on a flat distribution 
    1520    """ 
     21 
    1622    def setUp(self): 
    1723        """ 
     
    1925            should return the predefined height of the distribution (1.0). 
    2026        """ 
    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 
    2430        self.data = data_info.Data2D(data=x_0, err_data=dx_0) 
    2531        detector = data_info.Detector() 
    26         detector.distance = 1000.0  #mm 
    27         detector.pixel_size.x = 1.0 #mm 
    28         detector.pixel_size.y = 1.0 #mm 
    29          
     32        detector.distance = 1000.0  # mm 
     33        detector.pixel_size.x = 1.0  # mm 
     34        detector.pixel_size.y = 1.0  # mm 
     35 
    3036        # center in pixel position = (len(x_0)-1)/2 
    31         detector.beam_center.x = (len(x_0)-1)/2 #pixel number 
    32         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 
    3339        self.data.detector.append(detector) 
    34          
     40 
    3541        source = data_info.Source() 
    36         source.wavelength = 10.0 #A 
     42        source.wavelength = 10.0  # A 
    3743        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. 
    4047        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 
    4148 
    4249        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 
    43          
     50 
    4451        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=x 
    54         self.data.y_bins=y 
     52        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 
    5562        self.data = reader2D_converter(self.data) 
    56              
     63 
    5764    def test_ring_flat_distribution(self): 
    5865        """ 
    5966            Test ring averaging 
    6067        """ 
    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, 
    6370                 center_y=self.data.detector[0].beam_center.y) 
    6471        r.nbins_phi = 20 
    65          
     72 
    6673        o = r(self.data) 
    6774        for i in range(20): 
    6875            self.assertEqual(o.y[i], 1.0) 
    69              
     76 
    7077    def test_sectorphi_full(self): 
    7178        """ 
    7279            Test sector averaging 
    7380        """ 
    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) 
    7683        r.nbins_phi = 20 
    7784        o = r(self.data) 
    7885        for i in range(7): 
    7986            self.assertEqual(o.y[i], 1.0) 
    80              
    81              
     87 
    8288    def test_sectorphi_partial(self): 
    8389        """ 
    8490        """ 
    8591        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, 
    8793                      phi_min=0, phi_max=phi_max) 
    8894        self.assertEqual(r.phi_max, phi_max) 
     
    9298        for i in range(17): 
    9399            self.assertEqual(o.y[i], 1.0) 
    94              
    95              
    96  
    97 class data_info_tests(unittest.TestCase): 
    98      
     100 
     101 
     102class DataInfoTests(unittest.TestCase): 
     103 
    99104    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') 
    101107        self.data = Loader().load(filepath) 
    102          
     108 
    103109    def test_ring(self): 
    104110        """ 
    105111            Test ring averaging 
    106112        """ 
    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, 
    109115                 center_y=self.data.detector[0].beam_center.y, 
    110                  nbins = 20) 
     116                 nbins=20) 
    111117        ##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 
    117124        for i in range(r.nbins_phi - 1): 
    118125            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4) 
    119126            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4) 
    120127            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 
    121              
     128 
    122129    def test_circularavg(self): 
    123130        """ 
     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        """ 
    124150            Test circular averaging 
    125151            The test data was not generated by IGOR. 
    126152        """ 
    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 
    147154        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
    148155        s, ds, npoints = r(self.data) 
    149156        self.assertAlmostEqual(s, 34.278990899999997, 4) 
    150157        self.assertAlmostEqual(ds, 7.8007981835194293, 4) 
    151         self.assertAlmostEqual(npoints, 324.0000, 4)         
    152      
     158        self.assertAlmostEqual(npoints, 324.0000, 4) 
     159 
    153160        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
    154161        s, ds = r(self.data) 
    155162        self.assertAlmostEqual(s, 0.10579935462962962, 4) 
    156163        self.assertAlmostEqual(ds, 0.024076537603455028, 4) 
    157              
     164 
    158165    def test_slabX(self): 
    159166        """ 
     
    161168            The test data was not generated by IGOR. 
    162169        """ 
    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) 
    166173        r.fold = False 
    167174        o = r(self.data) 
    168175 
    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 
    176184    def test_slabY(self): 
    177185        """ 
     
    179187            The test data was not generated by IGOR. 
    180188        """ 
    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) 
    184192        r.fold = False 
    185193        o = r(self.data) 
    186194 
    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 
    194203    def test_sectorphi_full(self): 
    195204        """ 
     
    199208            The test data was not generated by IGOR. 
    200209        """ 
    201         from sas.sascalc.dataloader.manipulations import SectorPhi 
    202         import math 
    203          
     210 
    204211        nbins = 19 
    205212        phi_min = math.pi / (nbins + 1) 
    206213        phi_max = math.pi * 2 - phi_min 
    207          
     214 
    208215        r = SectorPhi(r_min=.005, 
    209216                      r_max=.01, 
     
    213220        o = r(self.data) 
    214221 
    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 
    222230    def test_sectorphi_quarter(self): 
    223231        """ 
     
    225233            The test data was not generated by IGOR. 
    226234        """ 
    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 
    241248    def test_sectorq_full(self): 
    242249        """ 
     
    244251            The test data was not generated by IGOR. 
    245252        """ 
    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 
    260266 
    261267if __name__ == '__main__': 
  • test/sasguiframe/test/utest_manipulations.py

    rfd5d6eac rfa4af76  
    1111 
    1212from sas.sascalc.dataloader.loader import Loader 
    13 from sas.sasgui.guiframe.dataFitting import Data1D as Theory1D 
     13from sas.sasgui.guiframe.dataFitting import Data1D 
    1414from sas.sasgui.guiframe.dataFitting import Data2D 
    1515 
    1616 
    17 class data_info_tests(unittest.TestCase): 
     17class DataInfoTests(unittest.TestCase): 
    1818 
    1919    def setUp(self): 
     
    3232 
    3333 
    34 class theory1d_tests(unittest.TestCase): 
     34class Theory1DTests(unittest.TestCase): 
    3535 
    3636    def setUp(self): 
     
    4242            Test basic cloning 
    4343        """ 
    44         theory = Theory1D(x=[], y=[], dy=None) 
     44        theory = Data1D(x=[], y=[], dy=None) 
    4545        theory.clone_without_data(clone=self.data) 
    4646        theory.copy_from_datainfo(data1d=self.data) 
     
    5555 
    5656 
    57 class manip_tests(unittest.TestCase): 
     57class ManipTests(unittest.TestCase): 
    5858 
    5959    def setUp(self): 
     
    160160 
    161161 
    162 class manip_2D(unittest.TestCase): 
     162class Manin2DTests(unittest.TestCase): 
    163163 
    164164    def setUp(self): 
     
    270270 
    271271 
    272 class extra_manip_2D(unittest.TestCase): 
     272class ExtraManip2DTests(unittest.TestCase): 
    273273 
    274274    def setUp(self): 
Note: See TracChangeset for help on using the changeset viewer.