Changeset 8ffafd1 in sasview


Ignore:
Timestamp:
Apr 17, 2017 1:16:39 PM (7 years ago)
Author:
krzywon
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:
da8bb53
Parents:
b09095a
git-author:
Jeff Krzywon <krzywon@…> (04/17/17 13:16:39)
git-committer:
krzywon <krzywon@…> (04/17/17 13:16:39)
Message:

Fix indexing issue that broke unit tests.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/ascii_reader.py

    rb09095a r8ffafd1  
    8585                    self.reset_data_list(len(lines) - line_no) 
    8686 
    87                 candidate_lines += 1 
    88                 # If 5 or more lines, this is considering the set data 
    89                 if candidate_lines >= self.min_data_pts: 
    90                     is_data = True 
    91  
    92                 self.current_dataset.x[candidate_lines - 1] = float(toks[0]) 
    93                 self.current_dataset.y[candidate_lines - 1] = float(toks[1]) 
     87                self.current_dataset.x[candidate_lines] = float(toks[0]) 
     88                self.current_dataset.y[candidate_lines] = float(toks[1]) 
    9489 
    9590                # If a 3rd row is present, consider it dy 
    9691                if new_lentoks > 2: 
    97                     self.current_dataset.dy[candidate_lines - 1] = \ 
     92                    self.current_dataset.dy[candidate_lines] = \ 
    9893                        float(toks[2]) 
    9994                    has_error_dy = True 
     
    10196                # If a 4th row is present, consider it dx 
    10297                if new_lentoks > 3: 
    103                     self.current_dataset.dx[candidate_lines - 1] = \ 
     98                    self.current_dataset.dx[candidate_lines] = \ 
    10499                        float(toks[3]) 
    105100                    has_error_dx = True 
     101 
     102                candidate_lines += 1 
     103                # If 5 or more lines, this is considering the set data 
     104                if candidate_lines >= self.min_data_pts: 
     105                    is_data = True 
    106106 
    107107                # To remember the # of columns on the current line 
     
    143143        if len(self.current_dataset.x) < 1: 
    144144            raise RuntimeError("ascii_reader: could not load file") 
    145             return None 
    146145 
    147146        # Data 
    148         self.current_dataset.x = \ 
    149             self.current_dataset.x[self.current_dataset.x != 0] 
    150         self.current_dataset.y = \ 
    151             self.current_dataset.y[self.current_dataset.x != 0] 
    152         self.current_dataset.dy = \ 
    153             self.current_dataset.dy[self.current_dataset.x != 0] if \ 
    154                 has_error_dy else np.zeros(len(self.current_dataset.y)) 
    155         self.current_dataset.dx = \ 
    156             self.current_dataset.dx[self.current_dataset.x != 0] if \ 
    157                 has_error_dx else np.zeros(len(self.current_dataset.x)) 
     147        x = self.current_dataset.x 
     148        self.current_dataset.x = self.current_dataset.x[x != 0] 
     149        self.current_dataset.y = self.current_dataset.y[x != 0] 
     150        self.current_dataset.dy = self.current_dataset.dy[x != 0] if \ 
     151            has_error_dy else np.zeros(len(self.current_dataset.y)) 
     152        self.current_dataset.dx = self.current_dataset.dx[x != 0] if \ 
     153            has_error_dx else np.zeros(len(self.current_dataset.x)) 
    158154 
    159155        self.current_dataset.xaxis("\\rm{Q}", 'A^{-1}') 
  • test/sasdataloader/test/utest_averaging.py

    rb09095a r8ffafd1  
    11 
     2import math 
    23import unittest 
    3 import math 
    4  
    5 from sas.sascalc.dataloader.loader import Loader 
    6 from sas.sascalc.dataloader.manipulations import Ring, CircularAverage, SectorPhi, get_q,reader2D_converter 
    7  
    84import numpy as np 
    95import sas.sascalc.dataloader.data_info as data_info 
     6from sas.sascalc.dataloader.loader import Loader 
     7from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg, Ring, get_q,\ 
     8    CircularAverage, SectorPhi, SectorQ, reader2D_converter, SlabX, SlabY 
     9 
    1010 
    1111class Averaging(unittest.TestCase): 
     
    1818            should return the predefined height of the distribution (1.0). 
    1919        """ 
    20         x_0  = np.ones([100,100]) 
     20        x_0 = np.ones([100,100]) 
    2121        dx_0 = np.ones([100,100]) 
    22          
     22 
    2323        self.data = data_info.Data2D(data=x_0, err_data=dx_0) 
    2424        detector = data_info.Detector() 
    25         detector.distance = 1000.0  #mm 
    26         detector.pixel_size.x = 1.0 #mm 
    27         detector.pixel_size.y = 1.0 #mm 
    28          
     25        detector.distance = 1000.0  # mm 
     26        detector.pixel_size.x = 1.0 # mm 
     27        detector.pixel_size.y = 1.0 # mm 
     28 
    2929        # 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 
     30        detector.beam_center.x = (len(x_0)-1)/2 # pixel number 
     31        detector.beam_center.y = (len(x_0)-1)/2 # pixel number 
    3232        self.data.detector.append(detector) 
    3333         
    3434        source = data_info.Source() 
    35         source.wavelength = 10.0 #A 
     35        source.wavelength = 10.0 # A 
    3636        self.data.source = source 
    37          
    38         # get_q(dx, dy, det_dist, wavelength) where units are mm,mm,mm,and A respectively. 
     37 
     38        # get_q(dx, dy, det_dist, wavelength) where units are mm, mm, mm, and A 
    3939        self.qmin = get_q(1.0, 1.0, detector.distance, source.wavelength) 
    40  
    4140        self.qmax = get_q(49.5, 49.5, detector.distance, source.wavelength) 
    42          
     41 
    4342        self.qstep = len(x_0) 
    4443        x=  np.linspace(start= -1*self.qmax, 
     
    5352        self.data.y_bins=y 
    5453        self.data = reader2D_converter(self.data) 
    55              
     54 
    5655    def test_ring_flat_distribution(self): 
    5756        """ 
     
    6261                 center_y=self.data.detector[0].beam_center.y) 
    6362        r.nbins_phi = 20 
    64          
     63 
    6564        o = r(self.data) 
    6665        for i in range(20): 
    6766            self.assertEqual(o.y[i], 1.0) 
    68              
     67 
    6968    def test_sectorphi_full(self): 
    7069        """ 
     
    7776        for i in range(7): 
    7877            self.assertEqual(o.y[i], 1.0) 
    79              
    80              
     78 
    8179    def test_sectorphi_partial(self): 
    8280        """ 
     
    9189        for i in range(17): 
    9290            self.assertEqual(o.y[i], 1.0) 
    93              
    94              
    95  
    96 class data_info_tests(unittest.TestCase): 
    97      
     91 
     92 
     93class DataInfoTests(unittest.TestCase): 
     94 
    9895    def setUp(self): 
    9996        self.data = Loader().load('MAR07232_rest.ASC') 
    100          
     97 
    10198    def test_ring(self): 
    10299        """ 
     
    107104                 center_y=self.data.detector[0].beam_center.y, 
    108105                 nbins = 20) 
    109         ##r.nbins_phi = 20 
    110          
     106        # r.nbins_phi = 20 
     107 
    111108        o = r(self.data) 
    112109        data_list = Loader().load('ring_testdata.txt') 
    113110        answer = data_list[0] 
    114          
     111 
    115112        for i in range(r.nbins_phi - 1): 
    116113            self.assertAlmostEqual(o.x[i + 1], answer.x[i], 4) 
    117114            self.assertAlmostEqual(o.y[i + 1], answer.y[i], 4) 
    118115            self.assertAlmostEqual(o.dy[i + 1], answer.dy[i], 4) 
    119              
    120     def test_circularavg(self): 
     116 
     117    def test_circular_avg(self): 
    121118        """ 
    122119            Test circular averaging 
     
    126123                 bin_width=0.0003) 
    127124        r.nbins_phi = 20 
    128          
    129         o = r(self.data) 
    130  
    131         answer = Loader().load('avg_testdata.txt') 
     125        o = r(self.data) 
     126 
     127        output = Loader().load('avg_testdata.txt') 
     128        answer = output[0] 
    132129        for i in range(r.nbins_phi): 
    133130            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
    134131            self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
    135132            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
    136              
     133 
    137134    def test_box(self): 
    138135        """ 
     
    140137            The test data was not generated by IGOR. 
    141138        """ 
    142         from sas.sascalc.dataloader.manipulations import Boxsum, Boxavg 
    143          
    144139        r = Boxsum(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
    145140        s, ds, npoints = r(self.data) 
     
    147142        self.assertAlmostEqual(ds, 7.8007981835194293, 4) 
    148143        self.assertAlmostEqual(npoints, 324.0000, 4)         
    149      
     144 
    150145        r = Boxavg(x_min=.01, x_max=.015, y_min=0.01, y_max=0.015) 
    151146        s, ds = r(self.data) 
    152147        self.assertAlmostEqual(s, 0.10579935462962962, 4) 
    153148        self.assertAlmostEqual(ds, 0.024076537603455028, 4) 
    154              
     149 
    155150    def test_slabX(self): 
    156151        """ 
     
    158153            The test data was not generated by IGOR. 
    159154        """ 
    160         from sas.sascalc.dataloader.manipulations import SlabX 
    161          
    162155        r = SlabX(x_min=-.01, x_max=.01, y_min=-0.0002, y_max=0.0002, bin_width=0.0004) 
    163156        r.fold = False 
    164157        o = r(self.data) 
    165158 
    166         answer = Loader().load('slabx_testdata.txt') 
    167         for i in range(len(o.x)): 
    168             self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
    169             self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
    170             self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
    171              
     159        output = Loader().load('slabx_testdata.txt') 
     160        answer = output[0] 
     161        for i in range(len(o.x)): 
     162            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     163            self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
     164            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
     165 
    172166    def test_slabY(self): 
    173167        """ 
     
    175169            The test data was not generated by IGOR. 
    176170        """ 
    177         from sas.sascalc.dataloader.manipulations import SlabY 
    178          
    179171        r = SlabY(x_min=.005, x_max=.01, y_min=-0.01, y_max=0.01, bin_width=0.0004) 
    180172        r.fold = False 
    181173        o = r(self.data) 
    182174 
    183         answer = Loader().load('slaby_testdata.txt') 
    184         for i in range(len(o.x)): 
    185             self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
    186             self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
    187             self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
    188              
     175        output = Loader().load('slaby_testdata.txt') 
     176        answer = output[0] 
     177        for i in range(len(o.x)): 
     178            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     179            self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
     180            self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
     181 
    189182    def test_sectorphi_full(self): 
    190183        """ 
     
    194187            The test data was not generated by IGOR. 
    195188        """ 
    196         from sas.sascalc.dataloader.manipulations import SectorPhi 
    197         import math 
    198          
    199189        nbins = 19 
    200190        phi_min = math.pi / (nbins + 1) 
    201191        phi_max = math.pi * 2 - phi_min 
    202          
     192 
    203193        r = SectorPhi(r_min=.005, 
    204194                      r_max=.01, 
     
    208198        o = r(self.data) 
    209199 
    210         answer = Loader().load('ring_testdata.txt') 
     200        output = Loader().load('ring_testdata.txt') 
     201        answer = output[0] 
    211202        for i in range(len(o.x)): 
    212203            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    219210            The test data was not generated by IGOR. 
    220211        """ 
    221         from sas.sascalc.dataloader.manipulations import SectorPhi 
    222         import math 
    223          
    224212        r = SectorPhi(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) 
    225213        r.nbins_phi = 20 
    226214        o = r(self.data) 
    227215 
    228         answer = Loader().load('sectorphi_testdata.txt') 
     216        output = Loader().load('sectorphi_testdata.txt') 
     217        answer = output[0] 
    229218        for i in range(len(o.x)): 
    230219            self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
     
    237226            The test data was not generated by IGOR. 
    238227        """ 
    239         from sas.sascalc.dataloader.manipulations import SectorQ 
    240         import math 
    241          
    242228        r = SectorQ(r_min=.005, r_max=.01, phi_min=0, phi_max=math.pi/2.0) 
    243229        r.nbins_phi = 20 
    244230        o = r(self.data) 
    245231 
    246         answer = Loader().load('sectorq_testdata.txt') 
    247         for i in range(len(o.x)): 
    248             self.assertAlmostEqual(o.x[i], answer.x[i], 4) 
    249             self.assertAlmostEqual(o.y[i], answer.y[i], 4) 
    250             self.assertAlmostEqual(o.dy[i], answer.dy[i], 4) 
    251              
     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) 
     238 
    252239 
    253240if __name__ == '__main__': 
Note: See TracChangeset for help on using the changeset viewer.