[02cb31a] | 1 | """ |
---|
| 2 | Unit tests for specific oriented models |
---|
| 3 | @copyright: University of Tennessee, for the DANSE project |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | import unittest, math, sys |
---|
| 7 | |
---|
| 8 | # Disable "missing docstring" complaint |
---|
| 9 | # pylint: disable-msg=C0111 |
---|
| 10 | # Disable "too many methods" complaint |
---|
| 11 | # pylint: disable-msg=R0904 |
---|
| 12 | # Disable "could be a function" complaint |
---|
| 13 | # pylint: disable-msg=R0201 |
---|
| 14 | # pylint: disable-msg=W0702 |
---|
| 15 | |
---|
| 16 | try: |
---|
| 17 | import VolumeCanvas |
---|
| 18 | print "Testing local version" |
---|
| 19 | except: |
---|
| 20 | print sys.exc_value |
---|
| 21 | #testing the version that is working on |
---|
| 22 | print "Testing installed version" |
---|
| 23 | import sans.realspace.VolumeCanvas as VolumeCanvas |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | class TestSphere(unittest.TestCase): |
---|
| 27 | """ Tests for oriented (2D) systems """ |
---|
| 28 | |
---|
| 29 | def setUp(self): |
---|
| 30 | """ |
---|
| 31 | Set up canvas |
---|
| 32 | """ |
---|
| 33 | from sans.models.SphereModel import SphereModel |
---|
| 34 | self.model = VolumeCanvas.VolumeCanvas() |
---|
| 35 | |
---|
| 36 | handle = self.model.add('sphere') |
---|
| 37 | |
---|
| 38 | radius = 10 |
---|
| 39 | density = .1 |
---|
| 40 | |
---|
| 41 | ana = SphereModel() |
---|
| 42 | ana.setParam('scale', 1.0) |
---|
| 43 | ana.setParam('contrast', 1.0) |
---|
| 44 | ana.setParam('background', 0.0) |
---|
| 45 | ana.setParam('radius', radius) |
---|
| 46 | self.ana = ana |
---|
| 47 | |
---|
| 48 | self.model.setParam('lores_density', density) |
---|
| 49 | self.model.setParam('%s.radius' % handle, radius) |
---|
| 50 | self.model.setParam('scale' , 1.0) |
---|
| 51 | self.model.setParam('%s.contrast' % handle, 1.0) |
---|
| 52 | self.model.setParam('background' , 0.0) |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | def testdefault(self): |
---|
| 56 | """ Testing sphere """ |
---|
| 57 | # Default orientation |
---|
| 58 | ana_val = self.ana.runXY([0.1, 0.1]) |
---|
| 59 | sim_val = self.model.getIq2D(0.1, 0.1) |
---|
| 60 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.1 ) |
---|
| 61 | |
---|
[3c75696] | 62 | class TestCylinderAddObject(unittest.TestCase): |
---|
| 63 | """ Tests for oriented (2D) systems """ |
---|
| 64 | |
---|
| 65 | def setUp(self): |
---|
| 66 | """ Set up cylinder model """ |
---|
| 67 | from sans.models.CylinderModel import CylinderModel |
---|
| 68 | radius = 5 |
---|
| 69 | length = 40 |
---|
| 70 | density = 20 |
---|
| 71 | |
---|
| 72 | # Analytical model |
---|
| 73 | self.ana = CylinderModel() |
---|
| 74 | self.ana.setParam('scale', 1.0) |
---|
| 75 | self.ana.setParam('contrast', 1.0) |
---|
| 76 | self.ana.setParam('background', 0.0) |
---|
| 77 | self.ana.setParam('radius', radius) |
---|
| 78 | self.ana.setParam('length', length) |
---|
| 79 | |
---|
| 80 | # Simulation model |
---|
| 81 | self.model = VolumeCanvas.VolumeCanvas() |
---|
| 82 | cyl = VolumeCanvas.CylinderDescriptor() |
---|
| 83 | self.handle = self.model.addObject(cyl) |
---|
| 84 | self.model.setParam('lores_density', density) |
---|
| 85 | self.model.setParam('%s.radius' % self.handle, radius) |
---|
| 86 | self.model.setParam('%s.length' % self.handle, length) |
---|
| 87 | self.model.setParam('scale' , 1.0) |
---|
| 88 | self.model.setParam('%s.contrast' % self.handle, 1.0) |
---|
| 89 | self.model.setParam('background' , 0.0) |
---|
| 90 | |
---|
| 91 | def testalongY(self): |
---|
| 92 | """ Testing cylinder along Y axis """ |
---|
| 93 | self.ana.setParam('cyl_theta', math.pi/2.0) |
---|
| 94 | self.ana.setParam('cyl_phi', math.pi/2.0) |
---|
| 95 | |
---|
| 96 | self.model.setParam('%s.orientation' % self.handle, [0,0,0]) |
---|
| 97 | |
---|
| 98 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
| 99 | sim_val = self.model.getIq2D(0.1, 0.2) |
---|
| 100 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 101 | |
---|
| 102 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 103 | |
---|
| 104 | |
---|
[02cb31a] | 105 | class TestCylinder(unittest.TestCase): |
---|
| 106 | """ Tests for oriented (2D) systems """ |
---|
| 107 | |
---|
| 108 | def setUp(self): |
---|
| 109 | """ Set up cylinder model """ |
---|
| 110 | from sans.models.CylinderModel import CylinderModel |
---|
| 111 | radius = 5 |
---|
| 112 | length = 40 |
---|
| 113 | density = 20 |
---|
| 114 | |
---|
| 115 | # Analytical model |
---|
| 116 | self.ana = CylinderModel() |
---|
| 117 | self.ana.setParam('scale', 1.0) |
---|
| 118 | self.ana.setParam('contrast', 1.0) |
---|
| 119 | self.ana.setParam('background', 0.0) |
---|
| 120 | self.ana.setParam('radius', radius) |
---|
| 121 | self.ana.setParam('length', length) |
---|
| 122 | |
---|
| 123 | # Simulation model |
---|
| 124 | self.model = VolumeCanvas.VolumeCanvas() |
---|
| 125 | self.handle = self.model.add('cylinder') |
---|
| 126 | self.model.setParam('lores_density', density) |
---|
| 127 | self.model.setParam('%s.radius' % self.handle, radius) |
---|
| 128 | self.model.setParam('%s.length' % self.handle, length) |
---|
| 129 | self.model.setParam('scale' , 1.0) |
---|
| 130 | self.model.setParam('%s.contrast' % self.handle, 1.0) |
---|
| 131 | self.model.setParam('background' , 0.0) |
---|
| 132 | |
---|
| 133 | def testalongY(self): |
---|
| 134 | """ Testing cylinder along Y axis """ |
---|
| 135 | self.ana.setParam('cyl_theta', math.pi/2.0) |
---|
| 136 | self.ana.setParam('cyl_phi', math.pi/2.0) |
---|
| 137 | |
---|
| 138 | self.model.setParam('%s.orientation' % self.handle, [0,0,0]) |
---|
| 139 | |
---|
| 140 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
[1b0707e1] | 141 | sim_val = self.model.getIq2D(0.1, 0.2) |
---|
[02cb31a] | 142 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 143 | |
---|
| 144 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 145 | |
---|
| 146 | def testalongZ(self): |
---|
| 147 | """ Testing cylinder along Z axis """ |
---|
| 148 | self.ana.setParam('cyl_theta', 0) |
---|
| 149 | self.ana.setParam('cyl_phi', 0) |
---|
| 150 | |
---|
| 151 | self.model.setParam('%s.orientation' % self.handle, [90,0,0]) |
---|
| 152 | |
---|
| 153 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
[1b0707e1] | 154 | sim_val = self.model.getIq2D(0.1, 0.2) |
---|
[02cb31a] | 155 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 156 | |
---|
| 157 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 158 | |
---|
| 159 | def testalongX(self): |
---|
| 160 | """ Testing cylinder along X axis """ |
---|
| 161 | self.ana.setParam('cyl_theta', 1.57) |
---|
| 162 | self.ana.setParam('cyl_phi', 0) |
---|
| 163 | |
---|
| 164 | self.model.setParam('%s.orientation' % self.handle, [0,0,90]) |
---|
| 165 | |
---|
| 166 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
[1b0707e1] | 167 | sim_val = self.model.getIq2D(0.1, 0.2) |
---|
[02cb31a] | 168 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 169 | |
---|
| 170 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 171 | |
---|
| 172 | class TestEllipsoid(unittest.TestCase): |
---|
| 173 | """ Tests for oriented (2D) systems """ |
---|
| 174 | |
---|
| 175 | def setUp(self): |
---|
| 176 | """ Set up ellipsoid """ |
---|
| 177 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
| 178 | |
---|
| 179 | radius_a = 60 |
---|
| 180 | radius_b = 10 |
---|
| 181 | density = 30 |
---|
| 182 | |
---|
| 183 | self.ana = EllipsoidModel() |
---|
| 184 | self.ana.setParam('scale', 1.0) |
---|
| 185 | self.ana.setParam('contrast', 1.0) |
---|
| 186 | self.ana.setParam('background', 0.0) |
---|
| 187 | self.ana.setParam('radius_a', radius_a) |
---|
| 188 | self.ana.setParam('radius_b', radius_b) |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | canvas = VolumeCanvas.VolumeCanvas() |
---|
| 192 | canvas.setParam('lores_density', density) |
---|
| 193 | self.handle = canvas.add('ellipsoid') |
---|
| 194 | canvas.setParam('%s.radius_x' % self.handle, radius_a) |
---|
| 195 | canvas.setParam('%s.radius_y' % self.handle, radius_b) |
---|
| 196 | canvas.setParam('%s.radius_z' % self.handle, radius_b) |
---|
| 197 | canvas.setParam('scale' , 1.0) |
---|
| 198 | canvas.setParam('%s.contrast' % self.handle, 1.0) |
---|
| 199 | canvas.setParam('background' , 0.0) |
---|
| 200 | self.canvas = canvas |
---|
| 201 | |
---|
| 202 | def testalongX(self): |
---|
| 203 | """ Testing ellipsoid along X """ |
---|
| 204 | self.ana.setParam('axis_theta', 1.57) |
---|
| 205 | self.ana.setParam('axis_phi', 0) |
---|
| 206 | |
---|
| 207 | self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) |
---|
| 208 | |
---|
| 209 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
[1b0707e1] | 210 | sim_val = self.canvas.getIq2D(0.1, 0.2) |
---|
[02cb31a] | 211 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 212 | |
---|
| 213 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 214 | |
---|
| 215 | def testalongZ(self): |
---|
| 216 | """ Testing ellipsoid along Z """ |
---|
| 217 | self.ana.setParam('axis_theta', 0) |
---|
| 218 | self.ana.setParam('axis_phi', 0) |
---|
| 219 | |
---|
| 220 | self.canvas.setParam('%s.orientation' % self.handle, [0,90,0]) |
---|
| 221 | |
---|
| 222 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
[1b0707e1] | 223 | sim_val = self.canvas.getIq2D(0.1, 0.2) |
---|
[02cb31a] | 224 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 225 | |
---|
| 226 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 227 | |
---|
| 228 | def testalongY(self): |
---|
| 229 | """ Testing ellipsoid along Y """ |
---|
| 230 | self.ana.setParam('axis_theta', math.pi/2.0) |
---|
| 231 | self.ana.setParam('axis_phi', math.pi/2.0) |
---|
| 232 | |
---|
| 233 | self.canvas.setParam('%s.orientation' % self.handle, [0,0,90]) |
---|
| 234 | |
---|
| 235 | ana_val = self.ana.runXY([0.05, 0.15]) |
---|
[1b0707e1] | 236 | sim_val = self.canvas.getIq2D(0.05, 0.15) |
---|
[02cb31a] | 237 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 238 | |
---|
| 239 | try: |
---|
| 240 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 241 | except: |
---|
| 242 | print ana_val, sim_val, sim_val/ana_val |
---|
| 243 | raise sys.exc_type, sys.exc_value |
---|
| 244 | |
---|
| 245 | class TestCoreShell(unittest.TestCase): |
---|
| 246 | """ Tests for oriented (2D) systems """ |
---|
| 247 | |
---|
| 248 | def setUp(self): |
---|
| 249 | """ Set up zero-SLD-average core-shell model """ |
---|
| 250 | from sans.models.CoreShellModel import CoreShellModel |
---|
| 251 | |
---|
| 252 | radius = 15 |
---|
| 253 | thickness = 5 |
---|
| 254 | density = 20 |
---|
| 255 | |
---|
| 256 | core_vol = 4.0/3.0*math.pi*radius*radius*radius |
---|
| 257 | self.outer_radius = radius+thickness |
---|
| 258 | shell_vol = 4.0/3.0*math.pi*self.outer_radius*self.outer_radius*self.outer_radius - core_vol |
---|
| 259 | self.shell_sld = -1.0*core_vol/shell_vol |
---|
| 260 | |
---|
| 261 | self.density = density |
---|
| 262 | |
---|
| 263 | # Core-shell |
---|
| 264 | sphere = CoreShellModel() |
---|
| 265 | # Core radius |
---|
| 266 | sphere.setParam('radius', radius) |
---|
| 267 | # Shell thickness |
---|
| 268 | sphere.setParam('thickness', thickness) |
---|
| 269 | sphere.setParam('core_sld', 1.0) |
---|
| 270 | sphere.setParam('shell_sld', self.shell_sld) |
---|
| 271 | sphere.setParam('solvent_sld', 0.0) |
---|
| 272 | sphere.setParam('background', 0.0) |
---|
| 273 | sphere.setParam('scale', 1.0) |
---|
| 274 | self.ana = sphere |
---|
| 275 | |
---|
| 276 | canvas = VolumeCanvas.VolumeCanvas() |
---|
| 277 | canvas.setParam('lores_density', self.density) |
---|
| 278 | |
---|
| 279 | handle = canvas.add('sphere') |
---|
| 280 | canvas.setParam('%s.radius' % handle, self.outer_radius) |
---|
| 281 | canvas.setParam('%s.contrast' % handle, self.shell_sld) |
---|
| 282 | |
---|
| 283 | handle2 = canvas.add('sphere') |
---|
| 284 | canvas.setParam('%s.radius' % handle2, radius) |
---|
| 285 | canvas.setParam('%s.contrast' % handle2, 1.0) |
---|
| 286 | |
---|
| 287 | canvas.setParam('scale' , 1.0) |
---|
| 288 | canvas.setParam('background' , 0.0) |
---|
| 289 | self.canvas = canvas |
---|
| 290 | |
---|
| 291 | def testdefault(self): |
---|
| 292 | """ Testing default core-shell orientation """ |
---|
| 293 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
| 294 | sim_val = self.canvas.getIq2D(0.1, 0.2) |
---|
| 295 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 296 | |
---|
| 297 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 298 | |
---|
[f98961f] | 299 | class TestRunMethods(unittest.TestCase): |
---|
| 300 | """ Tests run methods for oriented (2D) systems """ |
---|
| 301 | |
---|
| 302 | def setUp(self): |
---|
| 303 | """ Set up ellipsoid """ |
---|
| 304 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
| 305 | |
---|
| 306 | radius_a = 10 |
---|
| 307 | radius_b = 15 |
---|
| 308 | density = 1 |
---|
| 309 | |
---|
| 310 | self.ana = EllipsoidModel() |
---|
| 311 | self.ana.setParam('scale', 1.0) |
---|
| 312 | self.ana.setParam('contrast', 1.0) |
---|
| 313 | self.ana.setParam('background', 0.0) |
---|
| 314 | self.ana.setParam('radius_a', radius_a) |
---|
| 315 | self.ana.setParam('radius_b', radius_b) |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | canvas = VolumeCanvas.VolumeCanvas() |
---|
| 319 | canvas.setParam('lores_density', density) |
---|
| 320 | self.handle = canvas.add('ellipsoid') |
---|
| 321 | canvas.setParam('%s.radius_x' % self.handle, radius_a) |
---|
| 322 | canvas.setParam('%s.radius_y' % self.handle, radius_b) |
---|
| 323 | canvas.setParam('%s.radius_z' % self.handle, radius_b) |
---|
| 324 | canvas.setParam('scale' , 1.0) |
---|
| 325 | canvas.setParam('%s.contrast' % self.handle, 1.0) |
---|
| 326 | canvas.setParam('background' , 0.0) |
---|
| 327 | self.canvas = canvas |
---|
| 328 | |
---|
| 329 | self.ana.setParam('axis_theta', 1.57) |
---|
| 330 | self.ana.setParam('axis_phi', 0) |
---|
| 331 | |
---|
| 332 | self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) |
---|
| 333 | |
---|
| 334 | |
---|
| 335 | def testRunXY_List(self): |
---|
| 336 | """ Testing ellipsoid along X """ |
---|
| 337 | ana_val = self.ana.runXY([0.1, 0.2]) |
---|
| 338 | sim_val = self.canvas.runXY([0.1, 0.2]) |
---|
| 339 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 340 | |
---|
| 341 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 342 | |
---|
| 343 | def testRunXY_float(self): |
---|
| 344 | """ Testing ellipsoid along X """ |
---|
| 345 | ana_val = self.ana.runXY(0.1) |
---|
| 346 | sim_val = self.canvas.runXY(0.1) |
---|
| 347 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 348 | |
---|
| 349 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 350 | |
---|
| 351 | def testRun_float(self): |
---|
| 352 | """ Testing ellipsoid along X """ |
---|
| 353 | ana_val = self.ana.run(0.1) |
---|
| 354 | sim_val = self.canvas.run(0.1) |
---|
| 355 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 356 | |
---|
| 357 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 358 | |
---|
| 359 | def testRun_list(self): |
---|
| 360 | """ Testing ellipsoid along X """ |
---|
| 361 | ana_val = self.ana.run([0.1, 33.0]) |
---|
| 362 | sim_val = self.canvas.run([0.1, 33.0]) |
---|
| 363 | #print ana_val, sim_val, sim_val/ana_val |
---|
| 364 | |
---|
| 365 | self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | |
---|
| 369 | |
---|
[02cb31a] | 370 | if __name__ == '__main__': |
---|
| 371 | unittest.main() |
---|