Changeset 1cdbcd8 in sasview
- 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
- Location:
- test/sasrealspace/test
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasrealspace/test/utest_oriented.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 # pylint: disable-msg=W0702 16 16 17 try: 18 import VolumeCanvas 19 print("Testing local version")20 except: 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 17 from sasmodels.sasview_model import _make_standard_model 18 EllipsoidModel = _make_standard_model('ellipsoid') 19 SphereModel = _make_standard_model('sphere') 20 CylinderModel = _make_standard_model('cylinder') 21 CoreShellModel = _make_standard_model('core_shell_sphere') 22 23 import sas.sascalc.realspace.VolumeCanvas as VolumeCanvas 24 25 26 26 27 27 class TestSphere(unittest.TestCase): 28 28 """ Tests for oriented (2D) systems """ 29 29 30 30 def setUp(self): 31 31 """ 32 32 Set up canvas 33 33 """ 34 from sas.models.SphereModel import SphereModel35 34 self.model = VolumeCanvas.VolumeCanvas() 36 35 37 36 handle = self.model.add('sphere') 38 37 39 38 radius = 10 40 39 density = .1 41 40 42 41 ana = SphereModel() 43 42 ana.setParam('scale', 1.0) 44 ana.setParam('contrast', 1.0)45 43 ana.setParam('background', 0.0) 44 ana.setParam('sld', 1.0) 45 ana.setParam('sld_solvent', 0.0) 46 46 ana.setParam('radius', radius) 47 47 self.ana = ana 48 48 49 49 self.model.setParam('lores_density', density) 50 self.model.setParam('scale' , 1.0) 51 self.model.setParam('background' , 0.0) 52 self.model.setParam('%s.contrast' % handle, 1.0) 50 53 self.model.setParam('%s.radius' % handle, radius) 51 self.model.setParam('scale' , 1.0) 52 self.model.setParam('%s.contrast' % handle, 1.0) 53 self.model.setParam('background' , 0.0) 54 55 54 55 56 56 def testdefault(self): 57 57 """ Testing sphere """ … … 60 60 sim_val = self.model.getIq2D(0.1, 0.1) 61 61 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.1 ) 62 62 63 63 class TestCylinderAddObject(unittest.TestCase): 64 64 """ Tests for oriented (2D) systems """ 65 65 66 66 def setUp(self): 67 67 """ Set up cylinder model """ 68 from sas.models.CylinderModel import CylinderModel69 68 radius = 5 70 69 length = 40 71 70 density = 20 72 71 73 72 # Analytical model 74 73 self.ana = CylinderModel() 75 74 self.ana.setParam('scale', 1.0) 76 self.ana.setParam('contrast', 1.0)77 75 self.ana.setParam('background', 0.0) 76 self.ana.setParam('sld', 1.0) 77 self.ana.setParam('sld_solvent', 0.0) 78 78 self.ana.setParam('radius', radius) 79 79 self.ana.setParam('length', length) 80 80 81 81 # Simulation model 82 82 self.model = VolumeCanvas.VolumeCanvas() … … 84 84 self.handle = self.model.addObject(cyl) 85 85 self.model.setParam('lores_density', density) 86 self.model.setParam('scale' , 1.0) 87 self.model.setParam('background' , 0.0) 88 self.model.setParam('%s.contrast' % self.handle, 1.0) 86 89 self.model.setParam('%s.radius' % self.handle, radius) 87 90 self.model.setParam('%s.length' % self.handle, length) 88 self.model.setParam('scale' , 1.0) 89 self.model.setParam('%s.contrast' % self.handle, 1.0) 90 self.model.setParam('background' , 0.0) 91 91 92 92 def testalongY(self): 93 93 """ Testing cylinder along Y axis """ 94 self.ana.setParam(' cyl_theta', math.pi/2.0)95 self.ana.setParam(' cyl_phi', math.pi/2.0)96 94 self.ana.setParam('theta', math.pi/2.0) 95 self.ana.setParam('phi', math.pi/2.0) 96 97 97 self.model.setParam('%s.orientation' % self.handle, [0,0,0]) 98 99 ana_val = self.ana.runXY([0.1, 0.2]) 100 sim_val = self.model.getIq2D(0.1, 0.2) 101 #print ana_val, sim_val, sim_val/ana_val 102 103 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 104 105 98 99 ana_val = self.ana.runXY([0.1, 0.2]) 100 sim_val = self.model.getIq2D(0.1, 0.2) 101 #print ana_val, sim_val, sim_val/ana_val 102 103 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 104 105 106 106 class TestCylinder(unittest.TestCase): 107 107 """ Tests for oriented (2D) systems """ 108 108 109 109 def setUp(self): 110 110 """ Set up cylinder model """ 111 from sas.models.CylinderModel import CylinderModel112 111 radius = 5 113 112 length = 40 114 113 density = 20 115 114 116 115 # Analytical model 117 116 self.ana = CylinderModel() 118 117 self.ana.setParam('scale', 1.0) 119 self.ana.setParam('contrast', 1.0)120 118 self.ana.setParam('background', 0.0) 119 self.ana.setParam('sld', 1.0) 120 self.ana.setParam('sld_solvent', 0.0) 121 121 self.ana.setParam('radius', radius) 122 122 self.ana.setParam('length', length) 123 123 124 124 # Simulation model 125 125 self.model = VolumeCanvas.VolumeCanvas() 126 126 self.handle = self.model.add('cylinder') 127 127 self.model.setParam('lores_density', density) 128 self.model.setParam('scale' , 1.0) 129 self.model.setParam('background' , 0.0) 128 130 self.model.setParam('%s.radius' % self.handle, radius) 129 131 self.model.setParam('%s.length' % self.handle, length) 130 self.model.setParam('scale' , 1.0)131 132 self.model.setParam('%s.contrast' % self.handle, 1.0) 132 self.model.setParam('background' , 0.0) 133 133 134 134 def testalongY(self): 135 135 """ Testing cylinder along Y axis """ 136 self.ana.setParam(' cyl_theta', math.pi/2.0)137 self.ana.setParam(' cyl_phi', math.pi/2.0)138 136 self.ana.setParam('theta', math.pi/2.0) 137 self.ana.setParam('phi', math.pi/2.0) 138 139 139 self.model.setParam('%s.orientation' % self.handle, [0,0,0]) 140 141 ana_val = self.ana.runXY([0.1, 0.2]) 142 sim_val = self.model.getIq2D(0.1, 0.2) 143 #print ana_val, sim_val, sim_val/ana_val 144 145 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 146 140 141 ana_val = self.ana.runXY([0.1, 0.2]) 142 sim_val = self.model.getIq2D(0.1, 0.2) 143 #print ana_val, sim_val, sim_val/ana_val 144 145 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 146 147 147 def testalongZ(self): 148 148 """ Testing cylinder along Z axis """ 149 self.ana.setParam(' cyl_theta', 0)150 self.ana.setParam(' cyl_phi', 0)151 149 self.ana.setParam('theta', 0) 150 self.ana.setParam('phi', 0) 151 152 152 self.model.setParam('%s.orientation' % self.handle, [90,0,0]) 153 154 ana_val = self.ana.runXY([0.1, 0.2]) 155 sim_val = self.model.getIq2D(0.1, 0.2) 156 #print ana_val, sim_val, sim_val/ana_val 157 158 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 159 153 154 ana_val = self.ana.runXY([0.1, 0.2]) 155 sim_val = self.model.getIq2D(0.1, 0.2) 156 #print ana_val, sim_val, sim_val/ana_val 157 158 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 159 160 160 def testalongX(self): 161 161 """ Testing cylinder along X axis """ 162 self.ana.setParam(' cyl_theta', 1.57)163 self.ana.setParam(' cyl_phi', 0)164 162 self.ana.setParam('theta', 1.57) 163 self.ana.setParam('phi', 0) 164 165 165 self.model.setParam('%s.orientation' % self.handle, [0,0,90]) 166 167 ana_val = self.ana.runXY([0.1, 0.2]) 168 sim_val = self.model.getIq2D(0.1, 0.2) 169 #print ana_val, sim_val, sim_val/ana_val 170 171 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 172 166 167 ana_val = self.ana.runXY([0.1, 0.2]) 168 sim_val = self.model.getIq2D(0.1, 0.2) 169 #print ana_val, sim_val, sim_val/ana_val 170 171 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 172 173 173 class TestEllipsoid(unittest.TestCase): 174 174 """ Tests for oriented (2D) systems """ 175 175 176 176 def setUp(self): 177 177 """ Set up ellipsoid """ 178 from sas.models.EllipsoidModel import EllipsoidModel 179 178 180 179 radius_a = 60 181 180 radius_b = 10 182 181 density = 30 183 182 184 183 self.ana = EllipsoidModel() 185 184 self.ana.setParam('scale', 1.0) 186 self.ana.setParam('contrast', 1.0)187 185 self.ana.setParam('background', 0.0) 188 self.ana.setParam('radius_a', radius_a) 189 self.ana.setParam('radius_b', radius_b) 186 self.ana.setParam('sld', 1.0) 187 self.ana.setParam('sld_solvent', 0.0) 188 self.ana.setParam('radius_polar', radius_a) 189 self.ana.setParam('radius_equatorial', radius_b) 190 190 # Default orientation is there=1.57, phi=0 191 191 # Radius_a is along the x direction 192 192 193 193 canvas = VolumeCanvas.VolumeCanvas() 194 self.handle = canvas.add('ellipsoid') 194 195 canvas.setParam('lores_density', density) 195 self.handle = canvas.add('ellipsoid') 196 canvas.setParam('scale' , 1.0) 197 canvas.setParam('background' , 0.0) 196 198 canvas.setParam('%s.radius_x' % self.handle, radius_a) 197 199 canvas.setParam('%s.radius_y' % self.handle, radius_b) 198 200 canvas.setParam('%s.radius_z' % self.handle, radius_b) 199 canvas.setParam('scale' , 1.0)200 201 canvas.setParam('%s.contrast' % self.handle, 1.0) 201 canvas.setParam('background' , 0.0) 202 self.canvas = canvas 202 self.canvas = canvas 203 203 204 204 def testalongX(self): 205 205 """ Testing ellipsoid along X """ 206 self.ana.setParam(' axis_theta', 1.57)207 self.ana.setParam(' axis_phi', 0)208 206 self.ana.setParam('theta', 1.57) 207 self.ana.setParam('phi', 0) 208 209 209 self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) 210 210 211 211 ana_val = self.ana.runXY([0.1, 0.2]) 212 212 sim_val = self.canvas.getIq2D(0.1, 0.2) 213 213 #print ana_val, sim_val, sim_val/ana_val 214 214 215 215 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 216 216 217 217 def testalongZ(self): 218 218 """ Testing ellipsoid along Z """ 219 self.ana.setParam(' axis_theta', 0)220 self.ana.setParam(' axis_phi', 0)221 219 self.ana.setParam('theta', 0) 220 self.ana.setParam('phi', 0) 221 222 222 self.canvas.setParam('%s.orientation' % self.handle, [0,90,0]) 223 223 224 224 ana_val = self.ana.runXY([0.1, 0.2]) 225 225 sim_val = self.canvas.getIq2D(0.1, 0.2) 226 226 #print ana_val, sim_val, sim_val/ana_val 227 227 228 228 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 229 229 230 230 def testalongY(self): 231 231 """ Testing ellipsoid along Y """ 232 self.ana.setParam(' axis_theta', math.pi/2.0)233 self.ana.setParam(' axis_phi', math.pi/2.0)234 232 self.ana.setParam('theta', math.pi/2.0) 233 self.ana.setParam('phi', math.pi/2.0) 234 235 235 self.canvas.setParam('%s.orientation' % self.handle, [0,0,90]) 236 236 237 237 ana_val = self.ana.runXY([0.05, 0.15]) 238 238 sim_val = self.canvas.getIq2D(0.05, 0.15) 239 239 #print ana_val, sim_val, sim_val/ana_val 240 240 241 241 try: 242 242 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 243 except :243 except Exception: 244 244 print("Error", ana_val, sim_val, sim_val/ana_val) 245 raise sys.exc_type, sys.exc_value245 raise 246 246 247 247 class TestCoreShell(unittest.TestCase): 248 248 """ Tests for oriented (2D) systems """ 249 249 250 250 def setUp(self): 251 251 """ Set up zero-SLD-average core-shell model """ 252 from sas.models.CoreShellModel import CoreShellModel 253 252 254 253 radius = 15 255 254 thickness = 5 256 255 density = 20 257 256 258 257 core_vol = 4.0/3.0*math.pi*radius*radius*radius 259 258 self.outer_radius = radius+thickness … … 262 261 263 262 self.density = density 264 263 265 264 # Core-shell 266 265 sphere = CoreShellModel() 266 sphere.setParam('scale', 1.0) 267 sphere.setParam('background', 0.0) 267 268 # Core radius 268 269 sphere.setParam('radius', radius) 269 270 # Shell thickness 270 271 sphere.setParam('thickness', thickness) 271 sphere.setParam('core_sld', 1.0) 272 sphere.setParam('shell_sld', self.shell_sld) 273 sphere.setParam('solvent_sld', 0.0) 274 sphere.setParam('background', 0.0) 275 sphere.setParam('scale', 1.0) 272 sphere.setParam('sld_core', 1.0) 273 sphere.setParam('sld_shell', self.shell_sld) 274 sphere.setParam('sld_solvent', 0.0) 276 275 self.ana = sphere 277 278 canvas = VolumeCanvas.VolumeCanvas() 276 277 canvas = VolumeCanvas.VolumeCanvas() 279 278 canvas.setParam('lores_density', self.density) 280 279 canvas.setParam('scale' , 1.0) 280 canvas.setParam('background' , 0.0) 281 281 282 handle = canvas.add('sphere') 282 283 canvas.setParam('%s.radius' % handle, self.outer_radius) 283 284 canvas.setParam('%s.contrast' % handle, self.shell_sld) 284 285 285 286 handle2 = canvas.add('sphere') 286 287 canvas.setParam('%s.radius' % handle2, radius) 287 288 canvas.setParam('%s.contrast' % handle2, 1.0) 288 289 canvas.setParam('scale' , 1.0) 290 canvas.setParam('background' , 0.0) 291 self.canvas = canvas 292 289 290 self.canvas = canvas 291 293 292 def testdefault(self): 294 293 """ Testing default core-shell orientation """ 295 294 ana_val = self.ana.runXY([0.1, 0.2]) 296 295 sim_val, err = self.canvas.getIq2DError(0.1, 0.2) 297 298 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 299 296 297 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 298 300 299 class TestCoreShellError(unittest.TestCase): 301 300 """ Tests for oriented (2D) systems """ 302 301 303 302 def setUp(self): 304 303 """ Set up zero-SLD-average core-shell model """ 305 from sas.models.CoreShellModel import CoreShellModel 306 304 307 305 radius = 15 308 306 thickness = 5 309 307 density = 5 310 308 311 309 core_vol = 4.0/3.0*math.pi*radius*radius*radius 312 310 self.outer_radius = radius+thickness … … 315 313 316 314 self.density = density 317 315 318 316 # Core-shell 319 317 sphere = CoreShellModel() 318 sphere.setParam('scale', 1.0) 319 sphere.setParam('background', 0.0) 320 320 # Core radius 321 321 sphere.setParam('radius', radius) 322 322 # Shell thickness 323 323 sphere.setParam('thickness', thickness) 324 sphere.setParam('core_sld', 1.0) 325 sphere.setParam('shell_sld', self.shell_sld) 326 sphere.setParam('solvent_sld', 0.0) 327 sphere.setParam('background', 0.0) 328 sphere.setParam('scale', 1.0) 324 sphere.setParam('sld_core', 1.0) 325 sphere.setParam('sld_shell', self.shell_sld) 326 sphere.setParam('sld_solvent', 0.0) 329 327 self.ana = sphere 330 331 canvas = VolumeCanvas.VolumeCanvas() 328 329 canvas = VolumeCanvas.VolumeCanvas() 332 330 canvas.setParam('lores_density', self.density) 333 331 canvas.setParam('scale' , 1.0) 332 canvas.setParam('background' , 0.0) 333 334 334 handle = canvas.add('sphere') 335 335 canvas.setParam('%s.radius' % handle, self.outer_radius) 336 336 canvas.setParam('%s.contrast' % handle, self.shell_sld) 337 337 338 338 handle2 = canvas.add('sphere') 339 339 canvas.setParam('%s.radius' % handle2, radius) 340 340 canvas.setParam('%s.contrast' % handle2, 1.0) 341 342 canvas.setParam('scale' , 1.0) 343 canvas.setParam('background' , 0.0) 344 self.canvas = canvas 345 341 342 self.canvas = canvas 343 346 344 def testdefault(self): 347 345 """ Testing default core-shell orientation """ 348 346 ana_val = self.ana.runXY([0.1, 0.2]) 349 347 sim_val, err = self.canvas.getIq2DError(0.1, 0.2) 350 348 351 349 self.assert_( math.fabs(sim_val-ana_val) < 3.0 * err ) 352 350 … … 356 354 def setUp(self): 357 355 """ Set up ellipsoid """ 358 from sas.models.EllipsoidModel import EllipsoidModel 359 356 360 357 radius_a = 10 361 358 radius_b = 15 362 359 density = 5 363 360 364 361 self.ana = EllipsoidModel() 365 362 self.ana.setParam('scale', 1.0) 366 self.ana.setParam('contrast', 1.0)367 363 self.ana.setParam('background', 0.0) 368 self.ana.setParam('radius_a', radius_a) 369 self.ana.setParam('radius_b', radius_b) 370 371 364 self.ana.setParam('sld', 1.0) 365 self.ana.setParam('sld_solvent', 1.0) 366 self.ana.setParam('radius_polar', radius_a) 367 self.ana.setParam('radius_equatorial', radius_b) 368 369 372 370 canvas = VolumeCanvas.VolumeCanvas() 371 self.handle = canvas.add('ellipsoid') 373 372 canvas.setParam('lores_density', density) 374 self.handle = canvas.add('ellipsoid') 373 canvas.setParam('scale' , 1.0) 374 canvas.setParam('background' , 0.0) 375 375 canvas.setParam('%s.radius_x' % self.handle, radius_a) 376 376 canvas.setParam('%s.radius_y' % self.handle, radius_b) 377 377 canvas.setParam('%s.radius_z' % self.handle, radius_b) 378 canvas.setParam('scale' , 1.0)379 378 canvas.setParam('%s.contrast' % self.handle, 1.0) 380 canvas.setParam('background' , 0.0) 381 self.canvas = canvas 382 383 self.ana.setParam('axis_theta', 1.57) 384 self.ana.setParam('axis_phi', 0) 385 379 self.canvas = canvas 380 381 self.ana.setParam('theta', 1.57) 382 self.ana.setParam('phi', 0) 383 386 384 self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) 387 385 388 386 389 387 def testRunXY_List(self): … … 392 390 sim_val = self.canvas.runXY([0.1, 0.2]) 393 391 #print ana_val, sim_val, sim_val/ana_val 394 392 395 393 try: 396 394 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 397 except :395 except Exception: 398 396 print("Error", ana_val, sim_val, sim_val/ana_val) 399 raise sys.exc_type, sys.exc_value397 raise 400 398 401 399 def testRunXY_float(self): … … 404 402 sim_val = self.canvas.runXY(0.1) 405 403 #print ana_val, sim_val, sim_val/ana_val 406 404 407 405 try: 408 406 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 409 except :407 except Exception: 410 408 print("Error", ana_val, sim_val, sim_val/ana_val) 411 raise sys.exc_type, sys.exc_value409 raise 412 410 413 411 def testRun_float(self): … … 416 414 sim_val = self.canvas.run(0.1) 417 415 #print ana_val, sim_val, sim_val/ana_val 418 416 419 417 try: 420 418 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 421 except :419 except Exception: 422 420 print("Error", ana_val, sim_val, sim_val/ana_val) 423 raise sys.exc_type, sys.exc_value421 raise 424 422 425 423 def testRun_list(self): … … 428 426 sim_val = self.canvas.run([0.1, 33.0]) 429 427 #print ana_val, sim_val, sim_val/ana_val 430 428 431 429 try: 432 430 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 433 except :431 except Exception: 434 432 print("Error", ana_val, sim_val, sim_val/ana_val) 435 raise sys.exc_type, sys.exc_value433 raise 436 434 437 435 class TestParamChange(unittest.TestCase): 438 436 """ Tests for oriented (2D) systems """ 439 437 440 438 def setUp(self): 441 439 """ Set up cylinder model """ 442 from sas.models.CylinderModel import CylinderModel443 440 radius = 5 444 441 length = 40 445 442 density = 20 446 443 447 444 # Analytical model 448 445 self.ana = CylinderModel() 449 446 self.ana.setParam('scale', 1.0) 450 self.ana.setParam('contrast', 1.0)451 447 self.ana.setParam('background', 0.0) 448 self.ana.setParam('sld', 1.0) 449 self.ana.setParam('sld_solvent', 0.0) 452 450 self.ana.setParam('radius', radius) 453 451 self.ana.setParam('length', length) 454 self.ana.setParam(' cyl_theta', math.pi/2.0)455 self.ana.setParam(' cyl_phi', math.pi/2.0)456 452 self.ana.setParam('theta', math.pi/2.0) 453 self.ana.setParam('phi', math.pi/2.0) 454 457 455 # Simulation model 458 456 self.model = VolumeCanvas.VolumeCanvas() 459 457 self.handle = self.model.add('cylinder') 460 458 self.model.setParam('lores_density', density) 459 self.model.setParam('scale' , 1.0) 460 self.model.setParam('background' , 0.0) 461 461 self.model.setParam('%s.radius' % self.handle, radius) 462 462 self.model.setParam('%s.length' % self.handle, length) 463 self.model.setParam('scale' , 1.0)464 463 self.model.setParam('%s.contrast' % self.handle, 1.0) 465 self.model.setParam('background' , 0.0)466 464 self.model.setParam('%s.orientation' % self.handle, [0,0,0]) 467 465 468 466 def testalongY(self): 469 467 """ Test that a parameter change forces the generation … … 472 470 ana_val = self.ana.runXY([0.1, 0.2]) 473 471 sim_val = self.model.getIq2D(0.1, 0.2) 474 475 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 476 472 473 self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) 474 477 475 # Change the radius a re-evaluate 478 476 self.ana.setParam('radius', 10) 479 477 self.model.setParam('%s.radius' % self.handle, 10) 480 478 481 479 ana_val = self.ana.runXY([0.1, 0.2]) 482 480 sim_val = self.model.getIq2D(0.1, 0.2) … … 485 483 486 484 if __name__ == '__main__': 487 unittest.main() 485 unittest.main() -
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.