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

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

Moving sansmodels to trunk

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