source: sasview/sansrealspace/test/utest_realspace.py @ 6d4df13

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 6d4df13 was a98171d, checked in by Gervaise Alina <gervyh@…>, 13 years ago

move test out of realpace folder

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