source: sasview/sansmodels/src/sans/models/test/utest_models.py @ d6513cd

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 d6513cd was af03ddd, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Model C extension update

  • Property mode set to 100644
File size: 8.3 KB
Line 
1"""
2    Unit tests for specific models
3    @author: Mathieu Doucet / UTK
4"""
5
6import unittest, time, math
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
15
16       
17class TestSphere(unittest.TestCase):
18    """ Unit tests for sphere model """
19   
20    def setUp(self):
21        from sans.models.SphereModel import SphereModel
22        self.comp = SphereModel()
23       
24    def test1D(self):
25        """ Test 1D model for a sphere """
26        self.assertAlmostEqual(self.comp.run(1.0), 56.3878, 4)
27       
28    def test1D_2(self):
29        """ Test 2D model for a sphere """
30        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878, 4)
31
32class TestCyl(unittest.TestCase):
33    """Unit tests for cylinder"""
34   
35    def setUp(self):
36
37        from sans.models.CylinderModel import CylinderModel
38        self.comp = CylinderModel()
39       
40    def test1D(self):
41        """ Test 1D model of a cylinder """ 
42        self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4)
43       
44    def testTime(self):
45        """ Time profiling """
46        self.comp.run(2.0)
47        t0 = time.clock()
48        self.assertTrue(time.clock()-t0<1e-5)
49     
50    def test2D(self):
51        """ Test 2D model of a cylinder """ 
52        self.comp.setParam('cyl_theta', 1.0)
53        self.comp.setParam('cyl_phi', 1.0)
54        self.assertAlmostEqual(self.comp.run([0.2, 2.5]), 
55                               0.038176446608393366, 4)
56       
57    def testIO(self):
58        from sans.models.ModelFactory import ModelFactory
59        from sans.models.ModelIO import ModelIO
60        factory = ModelFactory()
61        io = ModelIO(factory)
62        io.save(self.comp,"myModel.xml")
63        value = self.comp.run(1)
64        loaded = io.load("myModel.xml")
65        self.assertEqual(value, loaded.run(1))
66       
67    def testIO_add(self):
68        from sans.models.ModelFactory import ModelFactory
69        from sans.models.ModelIO import ModelIO
70        factory = ModelFactory()
71        io = ModelIO(factory)
72        sph = factory.getModel("SphereModel")
73        cyl = factory.getModel("CylinderModel")
74        combo = sph - cyl
75        io.save(combo,"myModel.xml")
76        value = combo.run(1)
77        loaded = io.load("myModel.xml")
78        self.assertEqual(value, loaded.run(1))
79       
80    def testIO_add2(self):
81        from sans.models.ModelFactory import ModelFactory
82        #from sans.models.ModelIO import ModelIO
83        factory = ModelFactory()
84        #io = ModelIO(factory)
85        sph = factory.getModel("SphereModel")
86        cyl = factory.getModel("CylinderModel")
87        sph2 = factory.getModel("SphereModel")
88        combo1 = cyl - sph
89        combo = combo1 / sph2
90        #combo1 = sph
91        #io.save(combo,"myModel.xml")
92        # Just check that we have some output
93        self.assertTrue(math.fabs(combo.run(1))>0)
94        #loaded = io.load("myModel.xml")
95        #self.assertEqual(value, loaded.run(1))
96       
97       
98     
99       
100class TestFactory(unittest.TestCase):
101    """Unit tests for Model Factory"""
102   
103    def setUp(self):
104        from sans.models.ModelFactory import ModelFactory
105        self.comp = ModelFactory().getModel('CylinderModel')
106       
107    def test1D(self):
108        """ Test 1D model of a cylinder """ 
109        self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4)
110       
111class TestAddition(unittest.TestCase):
112    """Unit tests for Model Factory"""
113   
114    def setUp(self):
115        from sans.models.ModelFactory import ModelFactory
116        self.comp1 = ModelFactory().getModel('CylinderModel')
117        self.comp2 = ModelFactory().getModel('CylinderModel')
118        self.comp3 = self.comp1+self.comp2
119       
120    def test1D(self):
121        """ Test 1D model of a cylinder """
122        self.assertAlmostEqual(self.comp3.run(0.2), 2*0.041761386790780453, 4)
123       
124 
125class TestGaussian(unittest.TestCase):
126    """Unit tests for Gaussian function"""
127   
128    def setUp(self):
129        from sans.models.Gaussian import Gaussian
130        self.gauss= Gaussian()
131       
132    def test1D(self):
133        self.gauss.setParam('scale', 2.0)
134        self.gauss.setParam('center', 1.0)
135        self.gauss.setParam('sigma', 1.0)
136        self.assertEqual(self.gauss.run(1.0), 2.0)
137        value = math.exp(-0.5)
138        self.assertEqual(self.gauss.run(2.0), 2.0*value)
139       
140    def test2D(self):
141        self.gauss.setParam('scale', 2.0)
142        self.gauss.setParam('center', 1.0)
143        self.gauss.setParam('sigma', 1.0)
144        self.assertEqual(self.gauss.runXY([1.0,1.0]), 2.0*2.0)
145        value = math.exp(-0.5)
146        self.assertEqual(self.gauss.runXY([1.0,2.0]), 2.0*2.0*value)
147        self.assertEqual(self.gauss.runXY([2.0,2.0]), 2.0*2.0*value*value)
148       
149    def test2Dphi(self):
150        self.gauss.setParam('scale', 2.0)
151        self.gauss.setParam('center', 1.0)
152        self.gauss.setParam('sigma', 1.0)
153        value = math.exp(-0.5)
154        self.assertEqual(self.gauss.run([math.sqrt(8.0), math.pi/4.0]), 2.0*2.0*value*value)
155       
156class TestLorentzian(unittest.TestCase):
157    """Unit tests for Lorentzian function"""
158   
159    def setUp(self):
160        from sans.models.Lorentzian import Lorentzian
161        self.lor= Lorentzian()
162       
163    def test1D(self):
164        self.lor.setParam('scale', 2.0)
165        self.lor.setParam('center', 1.0)
166        self.lor.setParam('gamma', 1.0)
167        self.assertEqual(self.lor.run(1.0), 4.0/math.pi)
168        value = 1/math.pi*0.5/(1+.25)
169        self.assertEqual(self.lor.run(2.0), 2.0*value)
170       
171    def test2D(self):
172        self.lor.setParam('scale', 0.5*math.pi)
173        self.lor.setParam('center', 1.0)
174        self.lor.setParam('gamma', 1.0)
175        self.assertEqual(self.lor.run(1.0), 1.0)
176        value = 0.25/(1+.25)
177        self.assertEqual(self.lor.runXY([1.0,2.0]), value)
178        self.assertEqual(self.lor.runXY([2.0,2.0]), value*value)
179       
180    def test2Dphi(self):
181        self.lor.setParam('scale', 0.5*math.pi)
182        self.lor.setParam('center', 1.0)
183        self.lor.setParam('gamma', 1.0)
184        self.assertEqual(self.lor.run(1.0), 1.0)
185        value = 0.25/(1+.25)
186        self.assertEqual(self.lor.run([math.sqrt(8.0), math.pi/4.0]), value*value)
187       
188class TestSin(unittest.TestCase):
189    """Unit tests for Sin(x) function"""
190   
191    def setUp(self):
192        from sans.models.Sin import Sin
193        self.sin = Sin()
194
195    def test1D(self):
196        self.assertEqual(self.sin.run(1.13), math.sin(1.13))
197       
198    def test2D(self):
199        self.assertEqual(self.sin.run([1.13,0.56]), math.sin(1.13*math.cos(0.56))*math.sin(1.13*math.sin(0.56)))
200        self.assertEqual(self.sin.runXY([1.13,0.56]), math.sin(1.13)*math.sin(0.56))
201       
202class TestCos(unittest.TestCase):
203    """Unit tests for Cos(x) function"""
204   
205    def setUp(self):
206        from sans.models.Cos import Cos
207        self.cos = Cos()
208
209    def test1D(self):
210        self.assertEqual(self.cos.run(1.13), math.cos(1.13))
211       
212    def test2D(self):
213        self.assertEqual(self.cos.run([1.13,0.56]), math.cos(1.13*math.cos(0.56))*math.cos(1.13*math.sin(0.56)))
214        self.assertEqual(self.cos.runXY([1.13,0.56]), math.cos(1.13)*math.cos(0.56))
215       
216class TestConstant(unittest.TestCase):
217    """Unit tests for Cos(x) function"""
218   
219    def setUp(self):
220        from sans.models.Constant import Constant
221        self.const = Constant()
222        self.const.setParam('value',56.1)
223
224    def test1D(self):
225        self.assertEqual(self.const.run(1.13), 56.1)
226       
227    def test2D(self):
228        self.assertEqual(self.const.run([1.13,0.56]), 56.1)
229        self.assertEqual(self.const.runXY([1.13,0.56]), 56.1)
230       
231    def testFunction(self):
232        from sans.models.Sin import Sin
233        s = Sin()
234        A = self.const
235        A.setParam('Value',1.5)
236        B = self.const.clone()
237        B.setParam('value',2.0)
238        C = self.const.clone()
239        C.setParam('value',3.0)
240       
241        f = A+B*s*s+C
242        answer = 1.5+2.0*math.sin(1.1)**2+3.0
243        self.assertEqual(f.run(1.1), answer)
244   
245       
246
247if __name__ == '__main__':
248    unittest.main()
Note: See TracBrowser for help on using the repository browser.