source: sasview/park_integration/test/test_fit_line.py @ 7683f33a

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 7683f33a was dd8ee14, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on sansview with processing module

  • Property mode set to 100644
File size: 8.6 KB
Line 
1"""
2    Unit tests for fitting module
3    @author Gervaise Alina
4"""
5import unittest
6
7from sans.fit.AbstractFitEngine import Model
8import math
9class testFitModule(unittest.TestCase):
10    """ test fitting """
11   
12    def test1(self):
13        """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """
14        #load data
15        from DataLoader.loader import Loader
16        data = Loader().load("testdata_line.txt")
17        #Importing the Fit module
18        from sans.fit.Fitting import Fit
19        fitter = Fit('scipy')
20        # Receives the type of model for the fitting
21        from sans.models.LineModel import LineModel
22        model1  = LineModel()
23        model1.name = "M1"
24        model = Model(model1)
25        #fit with scipy test
26       
27        pars1= ['param1','param2']
28        fitter.set_data(data,1)
29        try:fitter.set_model(model,1,pars1)
30        except ValueError,msg:
31            print "ValueError was raised: "+str(msg)
32            #assert str(msg)=="wrong paramter %s used to set model %s. Choose\
33            #                 parameter name within %s"%('param1', model.model.name,str(model.model.getParamList()))
34        else: raise AssertError,"No error raised for scipy fitting with wrong parameters name to fit"
35        pars1= ['A','B']
36        fitter.set_model(model,1,pars1)
37        fitter.select_problem_for_fit(id=1,value=1)
38        result1 = fitter.fit()
39        self.assert_(result1)
40       
41        self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] )
42        self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1])
43        self.assertTrue( result1.fitness/len(data.x) < 2 )
44       
45        #fit with park test
46        fitter = Fit('park')
47        fitter.set_data(data,1)
48        fitter.set_model(model,1,pars1)
49        fitter.select_problem_for_fit(id=1,value=1)
50        result2 = fitter.fit()
51       
52        self.assert_(result2)
53        self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 
54        self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] )
55        self.assertTrue( result2.fitness/len(data.x) < 2)
56        # compare fit result result for scipy and park
57        self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] )
58        self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] )
59        self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] )
60        self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] )
61        self.assertAlmostEquals( result1.fitness,
62                                 result2.fitness/len(data.x),1 )
63       
64       
65    def test2(self):
66        """ fit 2 data and 2 model with no constrainst"""
67        #load data
68        from DataLoader.loader import Loader
69        l = Loader()
70        data1=l.load("testdata_line.txt")
71     
72        data2=l.load("testdata_line1.txt")
73     
74        #Importing the Fit module
75        from sans.fit.Fitting import Fit
76        fitter = Fit('scipy')
77        # Receives the type of model for the fitting
78        from sans.models.LineModel import LineModel
79        model11  = LineModel()
80        model11.name= "M1"
81        model22  = LineModel()
82        model11.name= "M2"
83     
84        model1 = Model(model11)
85        model2 = Model(model22)
86        #fit with scipy test
87        pars1= ['A','B']
88        fitter.set_data(data1,1)
89        fitter.set_model(model1,1,pars1)
90        fitter.select_problem_for_fit(id=1,value=0)
91        fitter.set_data(data2,2)
92        fitter.set_model(model2,2,pars1)
93        fitter.select_problem_for_fit(id=2,value=0)
94       
95        try: result1 = fitter.fit()
96        except RuntimeError,msg:
97           assert str(msg)=="No Assembly scheduled for Scipy fitting."
98        else: raise AssertError,"No error raised for scipy fitting with no model"
99        fitter.select_problem_for_fit(id=1,value=1)
100        fitter.select_problem_for_fit(id=2,value=1)
101        try: result1 = fitter.fit()
102        except RuntimeError,msg:
103           assert str(msg)=="Scipy can't fit more than a single fit problem at a time."
104        else: raise AssertError,"No error raised for scipy fitting with more than 2 models"
105       
106        #fit with park test
107        fitter = Fit('park')
108        fitter.set_data(data1,1)
109        fitter.set_model(model1,1,pars1)
110        fitter.set_data(data2,2)
111        fitter.set_model(model2,2,pars1)
112        fitter.select_problem_for_fit(id=1,value=1)
113        fitter.select_problem_for_fit(id=2,value=1)
114        result2 = fitter.fit()
115       
116        self.assert_(result2)
117        self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] )
118        self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] )
119        self.assertTrue( result2.fitness/(len(data1.x)+len(data2.x)) < 2)
120       
121       
122    def test3(self):
123        """ fit 2 data and 2 model with 1 constrainst"""
124        #load data
125        from DataLoader.loader import Loader
126        l = Loader()
127        data1= l.load("testdata_line.txt")
128        data2= l.load("testdata_cst.txt")
129       
130        # Receives the type of model for the fitting
131        from sans.models.LineModel import LineModel
132        model11  = LineModel()
133        model11.name= "line"
134        model11.setParam("A", 1.0)
135        model11.setParam("B",1.0)
136       
137        from sans.models.Constant import Constant
138        model22  = Constant()
139        model22.name= "cst"
140        model22.setParam("value", 1.0)
141       
142        model1 = Model(model11)
143        model2 = Model(model22)
144        model1.set(A=4)
145        model1.set(B=3)
146        # Constraint the constant value to be equal to parameter B (the real value is 2.5)
147        model2.set(value='line.B')
148        #fit with scipy test
149        pars1= ['A','B']
150        pars2= ['value']
151       
152        #Importing the Fit module
153        from sans.fit.Fitting import Fit
154        fitter = Fit('park')
155        fitter.set_data(data1,1)
156        fitter.set_model(model1,1,pars1)
157        fitter.set_data(data2,2,smearer=None)
158        fitter.set_model(model2,2,pars2)
159        fitter.select_problem_for_fit(id=1,value=1)
160        fitter.select_problem_for_fit(id=2,value=1)
161       
162        result2 = fitter.fit()
163        self.assert_(result2)
164        self.assertTrue( math.fabs(result2.pvec[0]-4.0)/3. <= result2.stderr[0]) 
165        self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3. <= result2.stderr[1])
166        self.assertTrue( result2.fitness/(len(data1.x)+len(data2.x)) < 2)
167       
168       
169    def test4(self):
170        """ fit 2 data concatenates with limited range of x and  one model """
171            #load data
172        from DataLoader.loader import Loader
173        l = Loader()
174        data1 = l.load("testdata_line.txt")
175        data2 = l.load("testdata_line1.txt")
176       
177       
178        # Receives the type of model for the fitting
179        from sans.models.LineModel import LineModel
180        model1  = LineModel()
181        model1.name= "M1"
182        model1.setParam("A", 1.0)
183        model1.setParam("B",1.0)
184        model = Model(model1)
185     
186        #fit with scipy test
187        pars1= ['A','B']
188        #Importing the Fit module
189        from sans.fit.Fitting import Fit
190        fitter = Fit('scipy')
191        fitter.set_data(data1,1,qmin=0, qmax=7)
192        fitter.set_model(model,1,pars1)
193        fitter.set_data(data2,1,qmin=1,qmax=10)
194        fitter.select_problem_for_fit(id=1,value=1)
195       
196        result1 = fitter.fit()
197        self.assert_(result1)
198
199        self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] )
200        self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1])
201        self.assertTrue( result1.fitness/len(data1.x) < 2 )
202       
203        #fit with park test
204        fitter = Fit('park')
205        fitter.set_data(data1,1,qmin=0, qmax=7)
206        fitter.set_model(model,1,pars1)
207        fitter.set_data(data2,1,qmin=1,qmax=10)
208        fitter.select_problem_for_fit(id=1,value=1)
209        result2 = fitter.fit()
210       
211        self.assert_(result2)
212        self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] )
213        self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] )
214        self.assertTrue( result2.fitness/len(data1.x) < 2)
215        # compare fit result result for scipy and park
216        self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] )
217        self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] )
218        self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] )
219        self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] )
220        self.assertTrue( result2.fitness/(len(data2.x)+len(data1.x)) < 2 )
221       
222       
223   
224   
Note: See TracBrowser for help on using the repository browser.