source: sasview/sansmodels/test/utest_models.py @ a6d1676

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 a6d1676 was 0abf7bf, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Re #5 Get rid of pyre models, odb creation, and modelfactory, all of which haven't been used since 2007.

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[ae3ce4e]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
[a1b2471]15class TestSpherew(unittest.TestCase):
16    """ Unit tests for sphere model """
17   
18    def setUp(self):
19        from sans.models.CoreFourShellModel import CoreFourShellModel
20        from sans.models.CoreMultiShellModel import CoreMultiShellModel
21        self.comp = CoreMultiShellModel(4)
22       
23    def test1D(self):
24        """ Test 1D model for a sphere """
25        self.comp._set_dispersion()
[ae3ce4e]26       
27class TestSphere(unittest.TestCase):
28    """ Unit tests for sphere model """
29   
30    def setUp(self):
31        from sans.models.SphereModel import SphereModel
32        self.comp = SphereModel()
33       
34    def test1D(self):
35        """ Test 1D model for a sphere """
[9ce41c6]36        self.assertAlmostEqual(self.comp.run(1.0), 5.6387e-5, 4)
[ae3ce4e]37       
38    def test1D_2(self):
39        """ Test 2D model for a sphere """
[9ce41c6]40        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 5.6387e-5, 4)
[ae3ce4e]41
42class TestCyl(unittest.TestCase):
43    """Unit tests for cylinder"""
44   
45    def setUp(self):
46
47        from sans.models.CylinderModel import CylinderModel
48        self.comp = CylinderModel()
49       
50    def test1D(self):
51        """ Test 1D model of a cylinder """ 
52        self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4)
53       
54    def testTime(self):
55        """ Time profiling """
56        self.comp.run(2.0)
57        t0 = time.clock()
58        self.assertTrue(time.clock()-t0<1e-5)
59     
60    def test2D(self):
61        """ Test 2D model of a cylinder """ 
[f1abc57]62        self.comp.setParam('cyl_theta', 1*180/math.pi)
63        self.comp.setParam('cyl_phi', 1*180/math.pi)
[ae3ce4e]64        self.assertAlmostEqual(self.comp.run([0.2, 2.5]), 
[0abf7bf]65                               0.038176446608393366, 4) 
[ae3ce4e]66 
67class TestGaussian(unittest.TestCase):
68    """Unit tests for Gaussian function"""
69   
70    def setUp(self):
71        from sans.models.Gaussian import Gaussian
72        self.gauss= Gaussian()
73       
74    def test1D(self):
75        self.gauss.setParam('scale', 2.0)
76        self.gauss.setParam('center', 1.0)
77        self.gauss.setParam('sigma', 1.0)
78        self.assertEqual(self.gauss.run(1.0), 2.0)
79        value = math.exp(-0.5)
80        self.assertEqual(self.gauss.run(2.0), 2.0*value)
81       
82    def test2D(self):
83        self.gauss.setParam('scale', 2.0)
84        self.gauss.setParam('center', 1.0)
85        self.gauss.setParam('sigma', 1.0)
86        self.assertEqual(self.gauss.runXY([1.0,1.0]), 2.0*2.0)
87        value = math.exp(-0.5)
88        self.assertEqual(self.gauss.runXY([1.0,2.0]), 2.0*2.0*value)
89        self.assertEqual(self.gauss.runXY([2.0,2.0]), 2.0*2.0*value*value)
90       
91    def test2Dphi(self):
92        self.gauss.setParam('scale', 2.0)
93        self.gauss.setParam('center', 1.0)
94        self.gauss.setParam('sigma', 1.0)
95        value = math.exp(-0.5)
[e52eeea]96        self.assertAlmostEqual(self.gauss.run([math.sqrt(8.0), math.pi/4.0]), 2.0*2.0*value*value, 5)
[ae3ce4e]97       
98class TestLorentzian(unittest.TestCase):
99    """Unit tests for Lorentzian function"""
100   
101    def setUp(self):
102        from sans.models.Lorentzian import Lorentzian
103        self.lor= Lorentzian()
104       
105    def test1D(self):
106        self.lor.setParam('scale', 2.0)
107        self.lor.setParam('center', 1.0)
108        self.lor.setParam('gamma', 1.0)
109        self.assertEqual(self.lor.run(1.0), 4.0/math.pi)
110        value = 1/math.pi*0.5/(1+.25)
111        self.assertEqual(self.lor.run(2.0), 2.0*value)
112       
113    def test2D(self):
114        self.lor.setParam('scale', 0.5*math.pi)
115        self.lor.setParam('center', 1.0)
116        self.lor.setParam('gamma', 1.0)
117        self.assertEqual(self.lor.run(1.0), 1.0)
118        value = 0.25/(1+.25)
119        self.assertEqual(self.lor.runXY([1.0,2.0]), value)
120        self.assertEqual(self.lor.runXY([2.0,2.0]), value*value)
121       
122    def test2Dphi(self):
123        self.lor.setParam('scale', 0.5*math.pi)
124        self.lor.setParam('center', 1.0)
125        self.lor.setParam('gamma', 1.0)
126        self.assertEqual(self.lor.run(1.0), 1.0)
127        value = 0.25/(1+.25)
[e52eeea]128        self.assertAlmostEqual(self.lor.run([math.sqrt(8.0), math.pi/4.0]), value*value, 5)
[ae3ce4e]129       
130class TestSin(unittest.TestCase):
131    """Unit tests for Sin(x) function"""
132   
133    def setUp(self):
134        from sans.models.Sin import Sin
135        self.sin = Sin()
136
137    def test1D(self):
138        self.assertEqual(self.sin.run(1.13), math.sin(1.13))
139       
140    def test2D(self):
141        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)))
142        self.assertEqual(self.sin.runXY([1.13,0.56]), math.sin(1.13)*math.sin(0.56))
143       
144class TestCos(unittest.TestCase):
145    """Unit tests for Cos(x) function"""
146   
147    def setUp(self):
148        from sans.models.Cos import Cos
149        self.cos = Cos()
150
151    def test1D(self):
152        self.assertEqual(self.cos.run(1.13), math.cos(1.13))
153       
154    def test2D(self):
155        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)))
156        self.assertEqual(self.cos.runXY([1.13,0.56]), math.cos(1.13)*math.cos(0.56))
157       
158class TestConstant(unittest.TestCase):
159    """Unit tests for Cos(x) function"""
160   
161    def setUp(self):
162        from sans.models.Constant import Constant
163        self.const = Constant()
164        self.const.setParam('value',56.1)
165
166    def test1D(self):
167        self.assertEqual(self.const.run(1.13), 56.1)
168       
169    def test2D(self):
170        self.assertEqual(self.const.run([1.13,0.56]), 56.1)
171        self.assertEqual(self.const.runXY([1.13,0.56]), 56.1)
172       
173    def testFunction(self):
[ae60f86]174        # version 0.5.0: No longer supported
175        return
[ae3ce4e]176        from sans.models.Sin import Sin
177        s = Sin()
178        A = self.const
179        A.setParam('Value',1.5)
180        B = self.const.clone()
181        B.setParam('value',2.0)
182        C = self.const.clone()
183        C.setParam('value',3.0)
184       
185        f = A+B*s*s+C
186        answer = 1.5+2.0*math.sin(1.1)**2+3.0
187        self.assertEqual(f.run(1.1), answer)
188   
189       
190
191if __name__ == '__main__':
192    unittest.main()
Note: See TracBrowser for help on using the repository browser.