Changeset 1cdbcd8 in sasview for test/sasrealspace/test/utest_realspace.py
- Timestamp:
- Sep 23, 2017 1:06:57 PM (7 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, 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasrealspace/test/utest_realspace.py
raaf5e49 r1cdbcd8 9 9 # Disable "missing docstring" complaint 10 10 # 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 14 14 # pylint: disable-msg=R0201 15 15 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 16 from sasmodels.sasview_model import _make_standard_model 17 EllipsoidModel = _make_standard_model('ellipsoid') 18 SphereModel = _make_standard_model('sphere') 19 CylinderModel = _make_standard_model('cylinder') 20 CoreShellModel = _make_standard_model('core_shell_sphere') 21 22 import sas.sascalc.realspace.VolumeCanvas as VolumeCanvas 23 26 24 class TestRealSpaceModel(unittest.TestCase): 27 25 """ Unit tests for sphere model """ 28 26 29 27 def setUp(self): 30 28 self.model = VolumeCanvas.VolumeCanvas() … … 33 31 self.model.add('ellipsoid', 'elli') 34 32 self.model.add('singlehelix', 'shelix') 35 33 36 34 def testAdding(self): 37 self.assertEqual('cyl', self.model.add('cylinder', 'cyl')) 35 self.assertEqual('cyl', self.model.add('cylinder', 'cyl')) 38 36 39 37 def testDeleting(self): … … 55 53 #print "pr is calculated", self.model.hasPr 56 54 result = self.model.getIq(0.1) 57 #print "I(0.1) is calculated: ", result 55 #print "I(0.1) is calculated: ", result 58 56 59 57 class TestSphere(unittest.TestCase): … … 62 60 def setUp(self): 63 61 self.canvas = VolumeCanvas.VolumeCanvas() 64 65 62 63 66 64 def testSetQmax(self): 67 65 old_value = self.canvas.getParam('q_max') 68 66 new_value = old_value + 0.1 69 67 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): 73 71 self.canvas.setParam('lores_density', 0.1) 74 72 handle = self.canvas.add('sphere') … … 77 75 npts_1 = vol/0.1 78 76 value_1 = self.canvas.getIq(0.001) 79 77 80 78 # Change density, the answer should be the same 81 79 self.canvas.setParam('lores_density', 0.2) 82 80 npts_2 = vol/0.2 83 81 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): 88 86 """Testing change in computation time with density""" 89 87 handle = self.canvas.add('sphere') 90 88 self.canvas.setParam("%s.radius" % handle, 15.0) 91 89 92 90 self.canvas.setParam('lores_density', 0.6) 93 91 t_0 = time.time() 94 92 self.canvas.getIq(0.001) 95 93 t_1 = time.time()-t_0 96 94 97 95 # Change density, the answer should be the same 98 96 self.canvas.setParam('lores_density', 0.1) … … 100 98 self.canvas.getIq(0.001) 101 99 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 105 103 def testGetParamList(self): 106 104 """ Test GetParamList on empty canvas""" 107 105 self.assert_('lores_density' in self.canvas.getParamList()) 108 106 handle = self.canvas.add('sphere') 109 107 110 108 def testGetParamListWithShape(self): 111 109 """ Test GetParamList on filled canvas""" 112 110 self.canvas.add('sphere') 113 111 self.assert_('lores_density' in self.canvas.getParamList()) 114 112 115 113 def testAdd(self): 116 114 handle = "s1" 117 115 self.assertEqual(handle, self.canvas.add('sphere', handle)) 118 116 119 117 #TODO: test for current list of shape 120 118 self.assertEqual( [handle] , self.canvas.getShapeList()) 121 119 122 120 def testSetRadius(self): 123 121 handle = self.canvas.add('sphere') 124 self.canvas.setParam("%s.r Adius" % handle, 24.0)125 self.assertEqual(self.canvas.getParam("%s.r Adius" % handle), 24.0)122 self.canvas.setParam("%s.radius" % handle, 24.0) 123 self.assertEqual(self.canvas.getParam("%s.radius" % handle), 24.0) 126 124 127 125 def testGetIq(self): 128 126 """ Test the output of I(q) to the analytical solution 129 127 If the normalization is wrong, we will have to fix it. 130 128 131 129 getIq() should call getPr() behind the scenes so that 132 130 the user doesnt have to do it if he doesn't need to. 133 131 """ 134 from sas.models.SphereModel import SphereModel135 132 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) 136 137 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 141 139 handle = self.canvas.add('sphere') 142 140 self.canvas.setParam('%s.radius' % handle, 10.0) 143 141 self.canvas.setParam('%s.contrast' % handle, 1.0) 144 145 142 143 146 144 sim_1 = self.canvas.getIq(0.001) 147 145 ana_1 = sphere.run(0.001) 148 146 sim_2 = self.canvas.getIq(0.01) 149 147 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 152 150 # on the output and it should be compatible with zero 153 151 # THIS WILL DEPEND ON THE NUMBER OF SPACE POINTS: 154 152 # that why we need some error analysis. 155 153 self.assert_( (sim_2*ana_1/sim_1 - ana_2)/ana_2 < 0.1) 156 154 157 155 # test the absolute amplitude 158 156 self.assert_( math.fabs(sim_2-ana_2)/ana_2 < 0.1) 159 157 160 158 def testGetIq2(self): 161 159 """ Test two different q values … … 163 161 handle = self.canvas.add('sphere') 164 162 self.canvas.setParam('%s.radius' % handle, 10.0) 165 163 166 164 sim_1 = self.canvas.getIq(0.001) 167 165 sim_2 = self.canvas.getIq(0.01) 168 166 169 167 self.assertNotAlmostEqual(sim_2, sim_1, 3) 170 168 171 169 def testGetIq_Identical(self): 172 170 """ Test for identical model / no param change … … 174 172 handle = self.canvas.add('sphere') 175 173 self.canvas.setParam('%s.radius' % handle, 10.0) 176 174 177 175 sim_1 = self.canvas.getIq(0.01) 178 176 sim_2 = self.canvas.getIq(0.01) 179 177 180 178 self.assertEqual(sim_2, sim_1) 181 179 182 180 def testGetIq_Identical2(self): 183 181 """ Test for identical model after a parameter change … … 187 185 handle = self.canvas.add('sphere') 188 186 self.canvas.setParam('%s.radius' % handle, 10.0) 189 187 190 188 self.canvas.setParam('lores_density', 0.1) 191 189 sim_1 = self.canvas.getIq(0.01) 192 190 193 191 # Try to fool the code by changing to a different value 194 192 self.canvas.setParam('lores_density', 0.2) 195 193 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 200 198 self.assert_((sim_2-sim_1)/sim_1<0.05) 201 199 202 200 def testGetIq_time(self): 203 201 """ Time profile … … 205 203 handle = self.canvas.add('sphere') 206 204 self.canvas.setParam('%s.radius' % handle, 15.0) 207 208 205 206 209 207 self.canvas.setParam('lores_density', 0.1) 210 208 t_0 = time.time() 211 209 sim_1 = self.canvas.getIq(0.01) 212 210 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 216 214 t_0 = time.time() 217 215 sim_2 = self.canvas.getIq(0.01) 218 216 delta_2 = time.time()-t_0 219 217 220 218 self.assert_((delta_2-delta_1)/delta_1<0.05) 221 222 219 220 223 221 def testGetPr(self): 224 222 """Compare the output of P(r) to the theoretical value""" … … 231 229 get the right output after changing a parameter 232 230 """ 233 231 234 232 handle = self.canvas.add('sphere') 235 233 self.canvas.setParam('%s.radius' % handle, 10.0) … … 291 289 result_3 = self.canvas.getIq(0.1) 292 290 self.assertNotEqual(result_1, result_3) 293 291 294 292 class TestOrdering(unittest.TestCase): 295 293 """ Unit tests for all shapes in canvas model """ 296 294 297 295 def setUp(self): 298 from sas.models.CoreShellModel import CoreShellModel299 296 radius = 15 300 297 thickness = 5 … … 303 300 shell_vol = 4.0/3.0*math.pi*outer_radius*outer_radius*outer_radius - core_vol 304 301 self.shell_sld = -1.0*core_vol/shell_vol 305 302 306 303 self.canvas = VolumeCanvas.VolumeCanvas() 307 304 self.canvas.params['lores_density'] = 0.1 308 305 309 306 # Core shell model 310 307 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) 311 313 # Core radius 312 314 sphere.setParam('radius', radius) 313 315 # Shell thickness 314 316 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)320 317 self.sphere = sphere 321 318 self.radius = radius 322 319 self.outer_radius = outer_radius 323 320 324 321 def set_coreshell_on_canvas(self, order1=None, order2=None): 325 322 326 323 handle = self.canvas.add('sphere') 324 self.canvas.setParam('scale' , 1.0) 325 self.canvas.setParam('background' , 0.0) 326 327 327 self.canvas.setParam('%s.radius' % handle, self.outer_radius) 328 328 self.canvas.setParam('%s.contrast' % handle, self.shell_sld) 329 329 if order1 is not None: 330 330 self.canvas.setParam('%s.order' % handle, order1) 331 331 332 332 handle2 = self.canvas.add('sphere') 333 333 self.canvas.setParam('%s.radius' % handle2, self.radius) … … 335 335 if order2 is not None: 336 336 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 342 339 def testDefaultOrder(self): 343 340 self.set_coreshell_on_canvas() 344 341 345 342 ana = self.sphere.run(0.05) 346 343 val, err = self.canvas.getIqError(0.05) 347 344 self.assert_(math.fabs(ana-val)<2.0*err) 348 345 349 346 def testRightOrder(self): 350 347 self.set_coreshell_on_canvas(3.0, 6.0) 351 348 352 349 ana = self.sphere.run(0.05) 353 350 val, err = self.canvas.getIqError(0.05) 354 351 #print 'right', ana, val, err 355 352 self.assert_(math.fabs(ana-val)/ana < 1.1) 356 353 357 354 def testWrongOrder(self): 358 from sas.models.SphereModel import SphereModel359 355 self.set_coreshell_on_canvas(1, 0) 360 361 # Core shell model 356 362 357 sphere = SphereModel() 363 # Core radius 358 sphere.setParam('scale', 1.0) 359 sphere.setParam('background', 0.0) 364 360 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 370 364 ana = sphere.run(0.05) 371 365 val, err = self.canvas.getIqError(0.05) 372 366 #print 'wrong', ana, val, err 373 367 self.assert_(math.fabs(ana-val)/ana < 1.1) 374 375 368 369 376 370 if __name__ == '__main__': 377 371 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.