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

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

changing base model

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