source: sasview/park_integration/test/testfitting.py @ c7bc3e7

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 c7bc3e7 was 48882d1, checked in by Gervaise Alina <gervyh@…>, 16 years ago

park fitting with new model and new data

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[4a0536a]1"""
2    Unit tests for fitting module
3"""
[93c2ef2]4import unittest
[4a0536a]5from sans.guitools.plottables import Theory1D
6from sans.guitools.plottables import Data1D
[48882d1]7from sans.fit.AbstractFitEngine import Data, Model
[93c2ef2]8import math
[4a0536a]9class testFitModule(unittest.TestCase):
10    """ test fitting """
11    def testLoader(self):
12        """
13            test module Load
14        """
[7705306]15        from sans.fit.Loader import Load
[4a0536a]16        load= Load()
17        load.set_filename("testdata_line.txt")
18        self.assertEqual(load.get_filename(),"testdata_line.txt")
19        load.set_values()
20        x=[]
21        y=[]
22        dx=[]
23        dy=[]
24        x,y,dx,dy = load.get_values()
25        # test that values have been loaded
26        self.assertNotEqual(x, None)
27        self.assertNotEqual(y, [])
28        self.assertNotEqual(dy, None)
29        self.assertEqual(len(x),len(y))
30        self.assertEqual(len(dy),len(y))
31       
32        # test data the two plottables contained values loaded
33        data1 = Theory1D(x=[], y=[], dy=None)
34        data2 = Data1D(x=[], y=[],dx=None, dy=None)
35        data1.name = "data1"
36        data2.name = "data2"
37        load.load_data(data1)
38        load.load_data(data2)
39       
40       
41        for i in range(len(x)):
42            self.assertEqual(data2.x[i],x[i])
43            self.assertEqual(data1.y[i],y[i])
44            self.assertEqual(data2.y[i],y[i])
45            self.assertEqual(data1.dx[i],dx[i])
46            self.assertEqual(data2.dy[i],dy[i])
47            self.assertEqual(data1.x[i],data2.x[i])
48            self.assertEqual(data2.y[i],data2.y[i])
49           
50           
[f24b8f4]51    def testfit_1Data_1Model(self):
[792db7d5]52        """ test fitting for one data and one model park vs scipy"""
[f24b8f4]53        #load data
[7705306]54        from sans.fit.Loader import Load
[4a0536a]55        load= Load()
56        load.set_filename("testdata_line.txt")
57        load.set_values()
[48882d1]58        data11 = Data1D(x=[], y=[],dx=None, dy=None)
59        load.load_data(data11)
[93c2ef2]60       
[f24b8f4]61        #Importing the Fit module
[7705306]62        from sans.fit.Fitting import Fit
[4dd63eb]63        fitter= Fit('scipy')
64       
[4a0536a]65        # Receives the type of model for the fitting
66        from sans.guitools.LineModel import LineModel
[48882d1]67        model11  = LineModel()
68        model22  = LineModel()
[93c2ef2]69       
[3701620]70        #Do the fit SCIPY
[48882d1]71        model11.setParam( 'A', 2)
72        model11.setParam( 'B', 1)
73        data1=Data(sans_data=data11)
74        model1 =Model(model11)
75        model2 =Model(model22)
76     
[4dd63eb]77        fitter.set_data(data1,1)
[985c88b]78        fitter.set_model(model1,"M1",1,['A','B'])
[f24b8f4]79       
[48882d1]80        result= fitter.fit()
81        out1=result.pvec
82        chisqr1=result.fitness
83        cov1=result.cov
84        print "scipy",chisqr1, out1, cov1
[3701620]85        """ testing SCIPy results"""
86        self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2)
87        self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2)
88        self.assert_(chisqr1/len(data1.x) < 2)
89        # PARK
[4dd63eb]90        fitter= Fit('park')
[f24b8f4]91       
[3701620]92        #Do the fit
[4dd63eb]93        fitter.set_data(data1,1)
[48882d1]94        model2.setParams( [2,1])
95   
[985c88b]96        fitter.set_model(model2,"M1",1,['A','B'])
[3701620]97       
[48882d1]98        result2=fitter.fit(None,None)
99        out2=result2.pvec
100        chisqr2=result2.fitness
101        cov2=result2.cov
[cf3b781]102        self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2)
103        self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2)
104        self.assert_(chisqr2/len(data1.x) < 2)
[792db7d5]105        print "scipy",chisqr1, out1, cov1
106        print "park",chisqr2, out2, cov2
107        self.assertAlmostEquals(out1[1], out2[1],0)
108        self.assertAlmostEquals(out1[0], out2[0],0)
109        self.assertAlmostEquals(cov1[0][0], cov2[0][0],1)
110        self.assertAlmostEquals(cov1[1][1], cov2[1][1],1)
111        self.assertAlmostEquals(chisqr1, chisqr2)
[48882d1]112         
113    def testfit_1Data_1Model(self):
114        """ test fitting for one data and one model cipy"""
115        #load data
116        from sans.fit.Loader import Load
117        load= Load()
118        load.set_filename("testdata_line.txt")
119        load.set_values()
120        data11 = Data1D(x=[], y=[],dx=None, dy=None)
121        load.load_data(data11)
122        data1=Data(sans_data=data11)
123   
124        #Importing the Fit module
125        from sans.fit.Fitting import Fit
126        fitter= Fit('scipy')
127       
128        # Receives the type of model for the fitting
129        from sans.guitools.LineModel import LineModel
130        model1  = LineModel()
131        model =Model(model1)
132       
133        #Do the fit SCIPY
134        fitter.set_data(data1,1)
135        import math
136       
137        pars1=['A','B']
138        pars1.sort()
139        fitter.set_model(model,"M1",1,pars1)
140        result=fitter.fit()
141        print "scipy",result.fitness,result.cov, result.pvec
142   
143        self.assert_(result.fitness)
144       
[f24b8f4]145       
[48882d1]146   
[792db7d5]147     
Note: See TracBrowser for help on using the repository browser.