[959eb01] | 1 | """ |
---|
| 2 | Unit tests for specific models |
---|
| 3 | @author: Mathieu Doucet / UTK |
---|
| 4 | """ |
---|
[aaf5e49] | 5 | from __future__ import print_function |
---|
[959eb01] | 6 | |
---|
| 7 | import unittest, math, time |
---|
| 8 | |
---|
| 9 | # Disable "missing docstring" complaint |
---|
| 10 | # pylint: disable-msg=C0111 |
---|
| 11 | # Disable "too many methods" complaint |
---|
| 12 | # pylint: disable-msg=R0904 |
---|
| 13 | # Disable "could be a function" complaint |
---|
| 14 | # pylint: disable-msg=R0201 |
---|
| 15 | |
---|
| 16 | try: |
---|
| 17 | import VolumeCanvas |
---|
[aaf5e49] | 18 | print("Testing local version") |
---|
[959eb01] | 19 | except: |
---|
| 20 | import sys |
---|
[aaf5e49] | 21 | print(sys.exc_value) |
---|
[959eb01] | 22 | #testing the version that is working on |
---|
[aaf5e49] | 23 | print("Testing installed version") |
---|
[959eb01] | 24 | import sas.sascalc.realspace.VolumeCanvas as VolumeCanvas |
---|
| 25 | |
---|
| 26 | class TestRealSpaceModel(unittest.TestCase): |
---|
| 27 | """ Unit tests for sphere model """ |
---|
| 28 | |
---|
| 29 | def setUp(self): |
---|
| 30 | self.model = VolumeCanvas.VolumeCanvas() |
---|
| 31 | self.model.add('cylinder', 'cyl') |
---|
| 32 | self.model.add('sphere', 'sph') |
---|
| 33 | self.model.add('ellipsoid', 'elli') |
---|
| 34 | self.model.add('singlehelix', 'shelix') |
---|
| 35 | |
---|
| 36 | def testAdding(self): |
---|
| 37 | self.assertEqual('cyl', self.model.add('cylinder', 'cyl')) |
---|
| 38 | |
---|
| 39 | def testDeleting(self): |
---|
| 40 | self.model.add('ellipsoid','elli2') |
---|
| 41 | self.model.delete('elli2') |
---|
| 42 | self.assert_('elli2' not in self.model.getShapeList()) |
---|
| 43 | |
---|
| 44 | def testsetParam(self): |
---|
| 45 | self.model.setParam('q_max', 0.2) |
---|
| 46 | self.model.setParam('shelix.radius_helix', 12) |
---|
| 47 | |
---|
| 48 | def testgetParamList(self): |
---|
| 49 | #print self.model.getParamList() |
---|
| 50 | #print self.model.getParamList('shelix') |
---|
| 51 | pass |
---|
| 52 | |
---|
| 53 | def testPr_Iq(self): |
---|
| 54 | self.model.getPr() |
---|
| 55 | #print "pr is calculated", self.model.hasPr |
---|
| 56 | result = self.model.getIq(0.1) |
---|
| 57 | #print "I(0.1) is calculated: ", result |
---|
| 58 | |
---|
| 59 | class TestSphere(unittest.TestCase): |
---|
| 60 | """ Unit tests for sphere model """ |
---|
| 61 | |
---|
| 62 | def setUp(self): |
---|
| 63 | self.canvas = VolumeCanvas.VolumeCanvas() |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def testSetQmax(self): |
---|
| 67 | old_value = self.canvas.getParam('q_max') |
---|
| 68 | new_value = old_value + 0.1 |
---|
| 69 | self.canvas.setParam('q_max', new_value) |
---|
| 70 | self.assertEqual(self.canvas.getParam("Q_MAx"), new_value) |
---|
| 71 | |
---|
| 72 | def testSetDensity(self): |
---|
| 73 | self.canvas.setParam('lores_density', 0.1) |
---|
| 74 | handle = self.canvas.add('sphere') |
---|
| 75 | self.canvas.setParam("%s.radius" % handle, 5.0) |
---|
| 76 | vol = 4/3*math.pi*5*5*5 |
---|
| 77 | npts_1 = vol/0.1 |
---|
| 78 | value_1 = self.canvas.getIq(0.001) |
---|
| 79 | |
---|
| 80 | # Change density, the answer should be the same |
---|
| 81 | self.canvas.setParam('lores_density', 0.2) |
---|
| 82 | npts_2 = vol/0.2 |
---|
| 83 | value_2 = self.canvas.getIq(0.001) |
---|
| 84 | |
---|
| 85 | self.assert_( (value_1-value_2)/value_1 < 0.1) |
---|
| 86 | |
---|
| 87 | def testSetDensityTiming(self): |
---|
| 88 | """Testing change in computation time with density""" |
---|
| 89 | handle = self.canvas.add('sphere') |
---|
| 90 | self.canvas.setParam("%s.radius" % handle, 15.0) |
---|
| 91 | |
---|
| 92 | self.canvas.setParam('lores_density', 0.6) |
---|
| 93 | t_0 = time.time() |
---|
| 94 | self.canvas.getIq(0.001) |
---|
| 95 | t_1 = time.time()-t_0 |
---|
| 96 | |
---|
| 97 | # Change density, the answer should be the same |
---|
| 98 | self.canvas.setParam('lores_density', 0.1) |
---|
| 99 | t_0 = time.time() |
---|
| 100 | self.canvas.getIq(0.001) |
---|
| 101 | t_2 = time.time()-t_0 |
---|
| 102 | |
---|
| 103 | self.assert_( t_2 < t_1 and (t_1-t_2)/t_2 > 2) |
---|
| 104 | |
---|
| 105 | def testGetParamList(self): |
---|
| 106 | """ Test GetParamList on empty canvas""" |
---|
| 107 | self.assert_('lores_density' in self.canvas.getParamList()) |
---|
| 108 | handle = self.canvas.add('sphere') |
---|
| 109 | |
---|
| 110 | def testGetParamListWithShape(self): |
---|
| 111 | """ Test GetParamList on filled canvas""" |
---|
| 112 | self.canvas.add('sphere') |
---|
| 113 | self.assert_('lores_density' in self.canvas.getParamList()) |
---|
| 114 | |
---|
| 115 | def testAdd(self): |
---|
| 116 | handle = "s1" |
---|
| 117 | self.assertEqual(handle, self.canvas.add('sphere', handle)) |
---|
| 118 | |
---|
| 119 | #TODO: test for current list of shape |
---|
| 120 | self.assertEqual( [handle] , self.canvas.getShapeList()) |
---|
| 121 | |
---|
| 122 | def testSetRadius(self): |
---|
| 123 | 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) |
---|
| 126 | |
---|
| 127 | def testGetIq(self): |
---|
| 128 | """ Test the output of I(q) to the analytical solution |
---|
| 129 | If the normalization is wrong, we will have to fix it. |
---|
| 130 | |
---|
| 131 | getIq() should call getPr() behind the scenes so that |
---|
| 132 | the user doesnt have to do it if he doesn't need to. |
---|
| 133 | """ |
---|
| 134 | from sas.models.SphereModel import SphereModel |
---|
| 135 | sphere = SphereModel() |
---|
| 136 | sphere.setParam('radius', 10.0) |
---|
| 137 | sphere.setParam('contrast', 1.0) |
---|
| 138 | sphere.setParam('background', 0.0) |
---|
| 139 | sphere.setParam('scale', 1.0) |
---|
| 140 | |
---|
| 141 | handle = self.canvas.add('sphere') |
---|
| 142 | self.canvas.setParam('%s.radius' % handle, 10.0) |
---|
| 143 | self.canvas.setParam('%s.contrast' % handle, 1.0) |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | sim_1 = self.canvas.getIq(0.001) |
---|
| 147 | ana_1 = sphere.run(0.001) |
---|
| 148 | sim_2 = self.canvas.getIq(0.01) |
---|
| 149 | ana_2 = sphere.run(0.01) |
---|
| 150 | |
---|
| 151 | # test the shape of the curve (calculate relative error |
---|
| 152 | # on the output and it should be compatible with zero |
---|
| 153 | # THIS WILL DEPEND ON THE NUMBER OF SPACE POINTS: |
---|
| 154 | # that why we need some error analysis. |
---|
| 155 | self.assert_( (sim_2*ana_1/sim_1 - ana_2)/ana_2 < 0.1) |
---|
| 156 | |
---|
| 157 | # test the absolute amplitude |
---|
| 158 | self.assert_( math.fabs(sim_2-ana_2)/ana_2 < 0.1) |
---|
| 159 | |
---|
| 160 | def testGetIq2(self): |
---|
| 161 | """ Test two different q values |
---|
| 162 | """ |
---|
| 163 | handle = self.canvas.add('sphere') |
---|
| 164 | self.canvas.setParam('%s.radius' % handle, 10.0) |
---|
| 165 | |
---|
| 166 | sim_1 = self.canvas.getIq(0.001) |
---|
| 167 | sim_2 = self.canvas.getIq(0.01) |
---|
| 168 | |
---|
| 169 | self.assertNotAlmostEqual(sim_2, sim_1, 3) |
---|
| 170 | |
---|
| 171 | def testGetIq_Identical(self): |
---|
| 172 | """ Test for identical model / no param change |
---|
| 173 | """ |
---|
| 174 | handle = self.canvas.add('sphere') |
---|
| 175 | self.canvas.setParam('%s.radius' % handle, 10.0) |
---|
| 176 | |
---|
| 177 | sim_1 = self.canvas.getIq(0.01) |
---|
| 178 | sim_2 = self.canvas.getIq(0.01) |
---|
| 179 | |
---|
| 180 | self.assertEqual(sim_2, sim_1) |
---|
| 181 | |
---|
| 182 | def testGetIq_Identical2(self): |
---|
| 183 | """ Test for identical model after a parameter change |
---|
| 184 | Should be different only of the space points |
---|
| 185 | are regenerated and the random seed is different |
---|
| 186 | """ |
---|
| 187 | handle = self.canvas.add('sphere') |
---|
| 188 | self.canvas.setParam('%s.radius' % handle, 10.0) |
---|
| 189 | |
---|
| 190 | self.canvas.setParam('lores_density', 0.1) |
---|
| 191 | sim_1 = self.canvas.getIq(0.01) |
---|
| 192 | |
---|
| 193 | # Try to fool the code by changing to a different value |
---|
| 194 | self.canvas.setParam('lores_density', 0.2) |
---|
| 195 | self.canvas.getIq(0.01) |
---|
| 196 | |
---|
| 197 | self.canvas.setParam('lores_density', 0.1) |
---|
| 198 | sim_2 = self.canvas.getIq(0.01) |
---|
| 199 | |
---|
| 200 | self.assert_((sim_2-sim_1)/sim_1<0.05) |
---|
| 201 | |
---|
| 202 | def testGetIq_time(self): |
---|
| 203 | """ Time profile |
---|
| 204 | """ |
---|
| 205 | handle = self.canvas.add('sphere') |
---|
| 206 | self.canvas.setParam('%s.radius' % handle, 15.0) |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | self.canvas.setParam('lores_density', 0.1) |
---|
| 210 | t_0 = time.time() |
---|
| 211 | sim_1 = self.canvas.getIq(0.01) |
---|
| 212 | delta_1 = time.time()-t_0 |
---|
| 213 | |
---|
| 214 | self.canvas.setParam('lores_density', 0.1) |
---|
| 215 | |
---|
| 216 | t_0 = time.time() |
---|
| 217 | sim_2 = self.canvas.getIq(0.01) |
---|
| 218 | delta_2 = time.time()-t_0 |
---|
| 219 | |
---|
| 220 | self.assert_((delta_2-delta_1)/delta_1<0.05) |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | def testGetPr(self): |
---|
| 224 | """Compare the output of P(r) to the theoretical value""" |
---|
| 225 | #TODO: find a way to compare you P(r) to the known |
---|
| 226 | # analytical value. |
---|
| 227 | pass |
---|
| 228 | |
---|
| 229 | def testLogic1(self): |
---|
| 230 | """ Test that the internal logic is set so that the user |
---|
| 231 | get the right output after changing a parameter |
---|
| 232 | """ |
---|
| 233 | |
---|
| 234 | handle = self.canvas.add('sphere') |
---|
| 235 | self.canvas.setParam('%s.radius' % handle, 10.0) |
---|
| 236 | result_1 = self.canvas.getIq(0.1) |
---|
| 237 | self.canvas.setParam('%s.radius' % handle, 20.0) |
---|
| 238 | result_2 = self.canvas.getIq(0.1) |
---|
| 239 | self.assertNotAlmostEqual(result_1, result_2, 2) |
---|
| 240 | |
---|
| 241 | class TestCanvas(unittest.TestCase): |
---|
| 242 | """ Unit tests for all shapes in canvas model """ |
---|
| 243 | |
---|
| 244 | def setUp(self): |
---|
| 245 | self.canvas = VolumeCanvas.VolumeCanvas() |
---|
| 246 | self.canvas.params['lores_density'] = 0.05 |
---|
| 247 | |
---|
| 248 | def testGetIq_cylinder(self): |
---|
| 249 | handle = self.canvas.add('cylinder','cyl') |
---|
| 250 | self.canvas.setParam('%s.radius' % handle, 15.0) |
---|
| 251 | self.canvas.setParam('%s.length' % handle, 50.0) |
---|
| 252 | self.assertEqual(50,self.canvas.getParam('%s.length'%handle)) |
---|
| 253 | result_1 = self.canvas.getIq(0.1) |
---|
| 254 | result_2 = self.canvas.getIq(0.1) |
---|
| 255 | self.assertEqual(result_1,result_2) |
---|
| 256 | |
---|
| 257 | self.canvas.delete(handle) |
---|
| 258 | handle2 = self.canvas.add('cylinder','cyl2') |
---|
| 259 | self.assertEqual(40,self.canvas.getParam('%s.length'%handle2)) |
---|
| 260 | result_3 = self.canvas.getIq(0.1) |
---|
| 261 | self.assertNotEqual(result_1, result_3) |
---|
| 262 | |
---|
| 263 | def testGetIq_ellipsoid(self): |
---|
| 264 | handle = self.canvas.add('ellipsoid','elli') |
---|
| 265 | self.canvas.setParam('%s.radius_x' % handle, 35) |
---|
| 266 | self.canvas.setParam('%s.radius_y' % handle, 20) |
---|
| 267 | self.canvas.setParam('%s.radius_z' % handle, 10) |
---|
| 268 | result_1 = self.canvas.getIq(0.1) |
---|
| 269 | result_2 = self.canvas.getIq(0.1) |
---|
| 270 | self.assertEqual(result_1,result_2) |
---|
| 271 | |
---|
| 272 | self.canvas.delete(handle) |
---|
| 273 | self.assertEqual(False,self.canvas.hasPr) |
---|
| 274 | handle2 = self.canvas.add('ellipsoid','elli2') |
---|
| 275 | result_3 = self.canvas.getIq(0.1) |
---|
| 276 | self.assertNotEqual(result_1, result_3) |
---|
| 277 | |
---|
| 278 | def testGetIq_singlehelix(self): |
---|
| 279 | handle = self.canvas.add('singlehelix','shelix') |
---|
| 280 | self.canvas.setParam('%s.radius_helix' % handle, 11) |
---|
| 281 | self.canvas.setParam('%s.radius_tube' % handle, 4) |
---|
| 282 | self.canvas.setParam('%s.pitch' % handle, 30) |
---|
| 283 | self.canvas.setParam('%s.turns' % handle, 3.2) |
---|
| 284 | result_1 = self.canvas.getIq(0.1) |
---|
| 285 | result_2 = self.canvas.getIq(0.1) |
---|
| 286 | self.assertEqual(result_1,result_2) |
---|
| 287 | |
---|
| 288 | self.canvas.delete(handle) |
---|
| 289 | self.assertEqual(False,self.canvas.hasPr) |
---|
| 290 | handle2 = self.canvas.add('singlehelix','shelix2') |
---|
| 291 | result_3 = self.canvas.getIq(0.1) |
---|
| 292 | self.assertNotEqual(result_1, result_3) |
---|
| 293 | |
---|
| 294 | class TestOrdering(unittest.TestCase): |
---|
| 295 | """ Unit tests for all shapes in canvas model """ |
---|
| 296 | |
---|
| 297 | def setUp(self): |
---|
| 298 | from sas.models.CoreShellModel import CoreShellModel |
---|
| 299 | radius = 15 |
---|
| 300 | thickness = 5 |
---|
| 301 | core_vol = 4.0/3.0*math.pi*radius*radius*radius |
---|
| 302 | outer_radius = radius+thickness |
---|
| 303 | shell_vol = 4.0/3.0*math.pi*outer_radius*outer_radius*outer_radius - core_vol |
---|
| 304 | self.shell_sld = -1.0*core_vol/shell_vol |
---|
| 305 | |
---|
| 306 | self.canvas = VolumeCanvas.VolumeCanvas() |
---|
| 307 | self.canvas.params['lores_density'] = 0.1 |
---|
| 308 | |
---|
| 309 | # Core shell model |
---|
| 310 | sphere = CoreShellModel() |
---|
| 311 | # Core radius |
---|
| 312 | sphere.setParam('radius', radius) |
---|
| 313 | # Shell thickness |
---|
| 314 | 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 | self.sphere = sphere |
---|
| 321 | self.radius = radius |
---|
| 322 | self.outer_radius = outer_radius |
---|
| 323 | |
---|
| 324 | def set_coreshell_on_canvas(self, order1=None, order2=None): |
---|
| 325 | |
---|
| 326 | handle = self.canvas.add('sphere') |
---|
| 327 | self.canvas.setParam('%s.radius' % handle, self.outer_radius) |
---|
| 328 | self.canvas.setParam('%s.contrast' % handle, self.shell_sld) |
---|
[ac07a3a] | 329 | if order1 is not None: |
---|
[959eb01] | 330 | self.canvas.setParam('%s.order' % handle, order1) |
---|
| 331 | |
---|
| 332 | handle2 = self.canvas.add('sphere') |
---|
| 333 | self.canvas.setParam('%s.radius' % handle2, self.radius) |
---|
| 334 | self.canvas.setParam('%s.contrast' % handle2, 1.0) |
---|
[45dffa69] | 335 | if order2 is not None: |
---|
[959eb01] | 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 | |
---|
| 342 | def testDefaultOrder(self): |
---|
| 343 | self.set_coreshell_on_canvas() |
---|
| 344 | |
---|
| 345 | ana = self.sphere.run(0.05) |
---|
| 346 | val, err = self.canvas.getIqError(0.05) |
---|
| 347 | self.assert_(math.fabs(ana-val)<2.0*err) |
---|
| 348 | |
---|
| 349 | def testRightOrder(self): |
---|
| 350 | self.set_coreshell_on_canvas(3.0, 6.0) |
---|
| 351 | |
---|
| 352 | ana = self.sphere.run(0.05) |
---|
| 353 | val, err = self.canvas.getIqError(0.05) |
---|
| 354 | #print 'right', ana, val, err |
---|
| 355 | self.assert_(math.fabs(ana-val)/ana < 1.1) |
---|
| 356 | |
---|
| 357 | def testWrongOrder(self): |
---|
| 358 | from sas.models.SphereModel import SphereModel |
---|
| 359 | self.set_coreshell_on_canvas(1, 0) |
---|
| 360 | |
---|
| 361 | # Core shell model |
---|
| 362 | sphere = SphereModel() |
---|
| 363 | # Core radius |
---|
| 364 | 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 | |
---|
| 370 | ana = sphere.run(0.05) |
---|
| 371 | val, err = self.canvas.getIqError(0.05) |
---|
| 372 | #print 'wrong', ana, val, err |
---|
| 373 | self.assert_(math.fabs(ana-val)/ana < 1.1) |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | if __name__ == '__main__': |
---|
| 377 | unittest.main() |
---|