Ignore:
Timestamp:
Sep 23, 2017 1:06:57 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
aaa801e
Parents:
f00072f
Message:

move tests to use sasmodels rather than sas.models (tests will still fail)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/sasrealspace/test/utest_realspace.py

    raaf5e49 r1cdbcd8  
    99# Disable "missing docstring" complaint 
    1010# pylint: disable-msg=C0111 
    11 # Disable "too many methods" complaint  
    12 # pylint: disable-msg=R0904  
    13 # Disable "could be a function" complaint  
     11# Disable "too many methods" complaint 
     12# pylint: disable-msg=R0904 
     13# Disable "could be a function" complaint 
    1414# pylint: disable-msg=R0201 
    1515 
    16 try: 
    17     import VolumeCanvas 
    18     print("Testing local version") 
    19 except: 
    20     import sys 
    21     print(sys.exc_value) 
    22     #testing the version that is working on 
    23     print("Testing installed version") 
    24     import sas.sascalc.realspace.VolumeCanvas as VolumeCanvas 
    25       
     16from sasmodels.sasview_model import _make_standard_model 
     17EllipsoidModel = _make_standard_model('ellipsoid') 
     18SphereModel = _make_standard_model('sphere') 
     19CylinderModel = _make_standard_model('cylinder') 
     20CoreShellModel = _make_standard_model('core_shell_sphere') 
     21 
     22import sas.sascalc.realspace.VolumeCanvas as VolumeCanvas 
     23 
    2624class TestRealSpaceModel(unittest.TestCase): 
    2725    """ Unit tests for sphere model """ 
    28      
     26 
    2927    def setUp(self): 
    3028        self.model = VolumeCanvas.VolumeCanvas() 
     
    3331        self.model.add('ellipsoid', 'elli') 
    3432        self.model.add('singlehelix', 'shelix') 
    35          
     33 
    3634    def testAdding(self): 
    37         self.assertEqual('cyl', self.model.add('cylinder', 'cyl'))    
     35        self.assertEqual('cyl', self.model.add('cylinder', 'cyl')) 
    3836 
    3937    def testDeleting(self): 
     
    5553        #print "pr is calculated", self.model.hasPr 
    5654        result = self.model.getIq(0.1) 
    57         #print "I(0.1) is calculated: ", result        
     55        #print "I(0.1) is calculated: ", result 
    5856 
    5957class TestSphere(unittest.TestCase): 
     
    6260    def setUp(self): 
    6361        self.canvas = VolumeCanvas.VolumeCanvas() 
    64          
    65          
     62 
     63 
    6664    def testSetQmax(self): 
    6765        old_value = self.canvas.getParam('q_max') 
    6866        new_value = old_value + 0.1 
    6967        self.canvas.setParam('q_max', new_value) 
    70         self.assertEqual(self.canvas.getParam("Q_MAx"), new_value)          
    71          
    72     def testSetDensity(self):         
     68        self.assertEqual(self.canvas.getParam("Q_MAx"), new_value) 
     69 
     70    def testSetDensity(self): 
    7371        self.canvas.setParam('lores_density', 0.1) 
    7472        handle = self.canvas.add('sphere') 
     
    7775        npts_1 = vol/0.1 
    7876        value_1 = self.canvas.getIq(0.001) 
    79          
     77 
    8078        # Change density, the answer should be the same 
    8179        self.canvas.setParam('lores_density', 0.2) 
    8280        npts_2 = vol/0.2 
    8381        value_2 = self.canvas.getIq(0.001) 
    84          
    85         self.assert_( (value_1-value_2)/value_1 < 0.1)          
    86          
    87     def testSetDensityTiming(self):       
     82 
     83        self.assert_( (value_1-value_2)/value_1 < 0.1) 
     84 
     85    def testSetDensityTiming(self): 
    8886        """Testing change in computation time with density""" 
    8987        handle = self.canvas.add('sphere') 
    9088        self.canvas.setParam("%s.radius" % handle, 15.0) 
    91          
     89 
    9290        self.canvas.setParam('lores_density', 0.6) 
    9391        t_0 = time.time() 
    9492        self.canvas.getIq(0.001) 
    9593        t_1 = time.time()-t_0 
    96          
     94 
    9795        # Change density, the answer should be the same 
    9896        self.canvas.setParam('lores_density', 0.1) 
     
    10098        self.canvas.getIq(0.001) 
    10199        t_2 = time.time()-t_0 
    102          
    103         self.assert_( t_2 < t_1 and (t_1-t_2)/t_2 > 2)          
    104          
     100 
     101        self.assert_( t_2 < t_1 and (t_1-t_2)/t_2 > 2) 
     102 
    105103    def testGetParamList(self): 
    106104        """ Test GetParamList on empty canvas""" 
    107105        self.assert_('lores_density' in self.canvas.getParamList()) 
    108106        handle = self.canvas.add('sphere') 
    109          
     107 
    110108    def testGetParamListWithShape(self): 
    111109        """ Test GetParamList on filled canvas""" 
    112110        self.canvas.add('sphere') 
    113111        self.assert_('lores_density' in self.canvas.getParamList()) 
    114          
     112 
    115113    def testAdd(self): 
    116114        handle = "s1" 
    117115        self.assertEqual(handle, self.canvas.add('sphere', handle)) 
    118   
     116 
    119117        #TODO: test for current list of shape 
    120118        self.assertEqual( [handle] , self.canvas.getShapeList()) 
    121          
     119 
    122120    def testSetRadius(self): 
    123121        handle = self.canvas.add('sphere') 
    124         self.canvas.setParam("%s.rAdius" % handle, 24.0) 
    125         self.assertEqual(self.canvas.getParam("%s.rAdius" % handle), 24.0) 
     122        self.canvas.setParam("%s.radius" % handle, 24.0) 
     123        self.assertEqual(self.canvas.getParam("%s.radius" % handle), 24.0) 
    126124 
    127125    def testGetIq(self): 
    128126        """ Test the output of I(q) to the analytical solution 
    129127            If the normalization is wrong, we will have to fix it. 
    130              
     128 
    131129            getIq() should call getPr() behind the scenes so that 
    132130            the user doesnt have to do it if he doesn't need to. 
    133131        """ 
    134         from sas.models.SphereModel import SphereModel 
    135132        sphere = SphereModel() 
     133        sphere.setParam('scale', 1.0) 
     134        sphere.setParam('background', 0.0) 
     135        sphere.setParam('sld', 1.0) 
     136        sphere.setParam('sld_solvent', 0.0) 
    136137        sphere.setParam('radius', 10.0) 
    137         sphere.setParam('contrast', 1.0) 
    138         sphere.setParam('background', 0.0) 
    139         sphere.setParam('scale', 1.0) 
    140                  
     138 
    141139        handle = self.canvas.add('sphere') 
    142140        self.canvas.setParam('%s.radius' % handle, 10.0) 
    143141        self.canvas.setParam('%s.contrast' % handle, 1.0) 
    144          
    145          
     142 
     143 
    146144        sim_1 = self.canvas.getIq(0.001) 
    147145        ana_1 = sphere.run(0.001) 
    148146        sim_2 = self.canvas.getIq(0.01) 
    149147        ana_2 = sphere.run(0.01) 
    150          
    151         # test the shape of the curve (calculate relative error  
     148 
     149        # test the shape of the curve (calculate relative error 
    152150        # on the output and it should be compatible with zero 
    153151        # THIS WILL DEPEND ON THE NUMBER OF SPACE POINTS: 
    154152        # that why we need some error analysis. 
    155153        self.assert_( (sim_2*ana_1/sim_1 - ana_2)/ana_2 < 0.1) 
    156          
     154 
    157155        # test the absolute amplitude 
    158156        self.assert_( math.fabs(sim_2-ana_2)/ana_2 < 0.1) 
    159          
     157 
    160158    def testGetIq2(self): 
    161159        """ Test two different q values 
     
    163161        handle = self.canvas.add('sphere') 
    164162        self.canvas.setParam('%s.radius' % handle, 10.0) 
    165          
     163 
    166164        sim_1 = self.canvas.getIq(0.001) 
    167165        sim_2 = self.canvas.getIq(0.01) 
    168          
     166 
    169167        self.assertNotAlmostEqual(sim_2, sim_1, 3) 
    170          
     168 
    171169    def testGetIq_Identical(self): 
    172170        """ Test for identical model / no param change 
     
    174172        handle = self.canvas.add('sphere') 
    175173        self.canvas.setParam('%s.radius' % handle, 10.0) 
    176          
     174 
    177175        sim_1 = self.canvas.getIq(0.01) 
    178176        sim_2 = self.canvas.getIq(0.01) 
    179          
     177 
    180178        self.assertEqual(sim_2, sim_1) 
    181          
     179 
    182180    def testGetIq_Identical2(self): 
    183181        """ Test for identical model after a parameter change 
     
    187185        handle = self.canvas.add('sphere') 
    188186        self.canvas.setParam('%s.radius' % handle, 10.0) 
    189          
     187 
    190188        self.canvas.setParam('lores_density', 0.1) 
    191189        sim_1 = self.canvas.getIq(0.01) 
    192          
     190 
    193191        # Try to fool the code by changing to a different value 
    194192        self.canvas.setParam('lores_density', 0.2) 
    195193        self.canvas.getIq(0.01) 
    196          
    197         self.canvas.setParam('lores_density', 0.1) 
    198         sim_2 = self.canvas.getIq(0.01) 
    199          
     194 
     195        self.canvas.setParam('lores_density', 0.1) 
     196        sim_2 = self.canvas.getIq(0.01) 
     197 
    200198        self.assert_((sim_2-sim_1)/sim_1<0.05) 
    201          
     199 
    202200    def testGetIq_time(self): 
    203201        """ Time profile 
     
    205203        handle = self.canvas.add('sphere') 
    206204        self.canvas.setParam('%s.radius' % handle, 15.0) 
    207          
    208          
     205 
     206 
    209207        self.canvas.setParam('lores_density', 0.1) 
    210208        t_0 = time.time() 
    211209        sim_1 = self.canvas.getIq(0.01) 
    212210        delta_1 = time.time()-t_0 
    213          
    214         self.canvas.setParam('lores_density', 0.1) 
    215          
     211 
     212        self.canvas.setParam('lores_density', 0.1) 
     213 
    216214        t_0 = time.time() 
    217215        sim_2 = self.canvas.getIq(0.01) 
    218216        delta_2 = time.time()-t_0 
    219          
     217 
    220218        self.assert_((delta_2-delta_1)/delta_1<0.05) 
    221          
    222          
     219 
     220 
    223221    def testGetPr(self): 
    224222        """Compare the output of P(r) to the theoretical value""" 
     
    231229            get the right output after changing a parameter 
    232230        """ 
    233          
     231 
    234232        handle = self.canvas.add('sphere') 
    235233        self.canvas.setParam('%s.radius' % handle, 10.0) 
     
    291289        result_3 = self.canvas.getIq(0.1) 
    292290        self.assertNotEqual(result_1, result_3) 
    293          
     291 
    294292class TestOrdering(unittest.TestCase): 
    295293    """ Unit tests for all shapes in canvas model """ 
    296294 
    297295    def setUp(self): 
    298         from sas.models.CoreShellModel import CoreShellModel 
    299296        radius = 15 
    300297        thickness = 5 
     
    303300        shell_vol = 4.0/3.0*math.pi*outer_radius*outer_radius*outer_radius - core_vol 
    304301        self.shell_sld = -1.0*core_vol/shell_vol 
    305          
     302 
    306303        self.canvas = VolumeCanvas.VolumeCanvas() 
    307304        self.canvas.params['lores_density'] = 0.1 
    308          
     305 
    309306        # Core shell model 
    310307        sphere = CoreShellModel() 
     308        sphere.setParam('scale', 1.0) 
     309        sphere.setParam('background', 0.0) 
     310        sphere.setParam('sld_core', 1.0) 
     311        sphere.setParam('sld_shell', self.shell_sld) 
     312        sphere.setParam('sld_solvent', 0.0) 
    311313        # Core radius 
    312314        sphere.setParam('radius', radius) 
    313315        # Shell thickness 
    314316        sphere.setParam('thickness', thickness) 
    315         sphere.setParam('core_sld', 1.0) 
    316         sphere.setParam('shell_sld', self.shell_sld) 
    317         sphere.setParam('solvent_sld', 0.0) 
    318         sphere.setParam('background', 0.0) 
    319         sphere.setParam('scale', 1.0) 
    320317        self.sphere = sphere 
    321318        self.radius = radius 
    322319        self.outer_radius = outer_radius 
    323          
     320 
    324321    def set_coreshell_on_canvas(self, order1=None, order2=None): 
    325322 
    326323        handle = self.canvas.add('sphere') 
     324        self.canvas.setParam('scale' , 1.0) 
     325        self.canvas.setParam('background' , 0.0) 
     326 
    327327        self.canvas.setParam('%s.radius' % handle, self.outer_radius) 
    328328        self.canvas.setParam('%s.contrast' % handle, self.shell_sld) 
    329329        if order1 is not None: 
    330330            self.canvas.setParam('%s.order' % handle, order1) 
    331         
     331 
    332332        handle2 = self.canvas.add('sphere') 
    333333        self.canvas.setParam('%s.radius' % handle2, self.radius) 
     
    335335        if order2 is not None: 
    336336            self.canvas.setParam('%s.order' % handle2, order2) 
    337                 
    338         self.canvas.setParam('scale' , 1.0) 
    339         self.canvas.setParam('background' , 0.0) 
    340          
    341          
     337 
     338 
    342339    def testDefaultOrder(self): 
    343340        self.set_coreshell_on_canvas() 
    344          
     341 
    345342        ana = self.sphere.run(0.05) 
    346343        val, err = self.canvas.getIqError(0.05) 
    347344        self.assert_(math.fabs(ana-val)<2.0*err) 
    348          
     345 
    349346    def testRightOrder(self): 
    350347        self.set_coreshell_on_canvas(3.0, 6.0) 
    351          
     348 
    352349        ana = self.sphere.run(0.05) 
    353350        val, err = self.canvas.getIqError(0.05) 
    354351        #print 'right', ana, val, err 
    355352        self.assert_(math.fabs(ana-val)/ana < 1.1) 
    356          
     353 
    357354    def testWrongOrder(self): 
    358         from sas.models.SphereModel import SphereModel 
    359355        self.set_coreshell_on_canvas(1, 0) 
    360          
    361         # Core shell model 
     356 
    362357        sphere = SphereModel() 
    363         # Core radius 
     358        sphere.setParam('scale', 1.0) 
     359        sphere.setParam('background', 0.0) 
    364360        sphere.setParam('radius', self.outer_radius) 
    365         # Shell thickness 
    366         sphere.setParam('contrast', self.shell_sld) 
    367         sphere.setParam('background', 0.0) 
    368         sphere.setParam('scale', 1.0) 
    369          
     361        sphere.setParam('sld', self.shell_sld) 
     362        sphere.setParam('sld_solvent', 0.0) 
     363 
    370364        ana = sphere.run(0.05) 
    371365        val, err = self.canvas.getIqError(0.05) 
    372366        #print 'wrong', ana, val, err 
    373367        self.assert_(math.fabs(ana-val)/ana < 1.1) 
    374              
    375          
     368 
     369 
    376370if __name__ == '__main__': 
    377371    unittest.main() 
Note: See TracChangeset for help on using the changeset viewer.