source: sasview/sansmodels/src/sans/models/test/utest_modelmultiplication.py @ c9a4377

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 c9a4377 was 8cfdd5e, checked in by Gervaise Alina <gervyh@…>, 16 years ago

fix setParam for modelMulplication, add 1 unittest

  • Property mode set to 100644
File size: 3.4 KB
Line 
1"""
2    Unit tests for specific models
3    @author: Gervaise Alina / UTK
4"""
5
6import unittest, numpy,math
7
8from sans.models.MultiplicationModel import MultiplicationModel
9from sans.models.SphereModel import SphereModel
10from sans.models.SquareWellStructure import SquareWellStructure
11     
12class TestDisperser(unittest.TestCase):
13    """ Unit tests for sphere model * SquareWellStructure"""
14    model1= SphereModel()
15    model2= SquareWellStructure()
16    model3= MultiplicationModel(model1, model2)
17    details={}
18    details['scale'] = ['', None, None]
19    details['radius'] = ['A', None, None]
20    details['contrast'] = ['A-2', None, None]
21    details['background'] = ['cm-1', None, None]
22    details['volfraction'] = ['', None, None]
23    details['welldepth'] = ['kT', None, None]
24    details['wellwidth'] = ['', None, None]
25   
26    ## fittable parameters
27    fixed=[]
28    fixed=['radius.width']
29   
30   
31    def test_multiplication(self):
32        """ test multiplication model"""
33        from sans.models.MultiplicationModel import MultiplicationModel
34        from sans.models.SphereModel import SphereModel
35        from sans.models.NoStructure import NoStructure
36        model1  =  MultiplicationModel(SphereModel(),NoStructure())
37        model2 = SphereModel()
38        x= 2
39        a = model1.run(x)
40       
41        b=  model2.run(x)
42        self.assertEqual(a, b)
43        model2.setParam("scale", 10)
44        c=  model2.run(x)
45        self.assertEqual(c, 10*b)
46        model1.setParam("scale", 10)
47        d=  model1.run(x)
48        self.assertEqual(d, 10*a)
49        self.assertEqual(model1.getParam("scale"), 10)
50       
51       
52    def testMultiplicationModel(self):
53        """ Test Multiplication  sphere with SquareWellStructure"""
54        ## test details dictionary
55        self.assertEqual(self.model3.details, self.details)
56       
57        ## test parameters list
58        list3= self.model3.getParamList()
59        for item in self.model1.getParamList():
60            self.assert_(item in list3)
61        for item in self.model2.getParamList():
62            self.assert_(item in list3)
63           
64        ## test set value for parameters and get paramaters
65        self.model3.setParam("scale", 15)
66        self.assertEqual(self.model3.getParam("scale"), 15)
67        self.model3.setParam("radius", 20)
68        self.assertEqual(self.model3.getParam("radius"), 20)
69        self.model3.setParam("radius.width", 15)
70        self.assertEqual(self.model3.getParam("radius.width"), 15)
71       
72        ## Dispersity
73        list3= self.model3.getDispParamList()
74        self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width'])
75       
76        from sans.models.dispersion_models import ArrayDispersion
77        disp_th = ArrayDispersion()
78       
79        values_th = numpy.zeros(100)
80        weights   = numpy.zeros(100)
81        for i in range(100):
82            values_th[i]=(math.pi/99.0*i)
83            weights[i]=(1.0)
84   
85        disp_th.set_weights(values_th, weights)
86       
87        self.model3.set_dispersion('radius', disp_th)
88       
89        val_1d = self.model3.run(math.sqrt(0.0002))
90        val_2d = self.model3.runXY([0.01,0.01]) 
91       
92        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
93        model4= self.model3.clone()
94        self.assertEqual(model4.getParam("radius"), 20)
95       
96if __name__ == '__main__':
97    unittest.main()
Note: See TracBrowser for help on using the repository browser.