source: sasview/park_integration/test/test_fit_line.py @ cf76ca74

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 cf76ca74 was 1e3169c, checked in by Gervaise Alina <gervyh@…>, 15 years ago

change fitdata1D to inheritates from dataloader.data1D

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