1 | """ |
---|
2 | Unit tests for fitting module |
---|
3 | """ |
---|
4 | import unittest |
---|
5 | from sans.guitools.plottables import Theory1D |
---|
6 | from sans.guitools.plottables import Data1D |
---|
7 | from sans.fit.ScipyFitting import Parameter |
---|
8 | import math |
---|
9 | class testFitModule(unittest.TestCase): |
---|
10 | """ test fitting """ |
---|
11 | def testLoader(self): |
---|
12 | """ |
---|
13 | test module Load |
---|
14 | """ |
---|
15 | from sans.fit.Loader import Load |
---|
16 | load= Load() |
---|
17 | |
---|
18 | load.set_filename("testdata_line.txt") |
---|
19 | self.assertEqual(load.get_filename(),"testdata_line.txt") |
---|
20 | load.set_values() |
---|
21 | x=[] |
---|
22 | y=[] |
---|
23 | dx=[] |
---|
24 | dy=[] |
---|
25 | |
---|
26 | x,y,dx,dy = load.get_values() |
---|
27 | |
---|
28 | # test that values have been loaded |
---|
29 | self.assertNotEqual(x, None) |
---|
30 | self.assertNotEqual(y, []) |
---|
31 | self.assertNotEqual(dy, None) |
---|
32 | self.assertEqual(len(x),len(y)) |
---|
33 | self.assertEqual(len(dy),len(y)) |
---|
34 | |
---|
35 | # test data the two plottables contained values loaded |
---|
36 | data1 = Theory1D(x=[], y=[], dy=None) |
---|
37 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
38 | data1.name = "data1" |
---|
39 | data2.name = "data2" |
---|
40 | load.load_data(data1) |
---|
41 | load.load_data(data2) |
---|
42 | |
---|
43 | |
---|
44 | for i in range(len(x)): |
---|
45 | self.assertEqual(data2.x[i],x[i]) |
---|
46 | self.assertEqual(data1.y[i],y[i]) |
---|
47 | self.assertEqual(data2.y[i],y[i]) |
---|
48 | self.assertEqual(data1.dx[i],dx[i]) |
---|
49 | self.assertEqual(data2.dy[i],dy[i]) |
---|
50 | self.assertEqual(data1.x[i],data2.x[i]) |
---|
51 | self.assertEqual(data2.y[i],data2.y[i]) |
---|
52 | |
---|
53 | |
---|
54 | def testfit_1Data_1Model(self): |
---|
55 | """ test fitting for one data and one model""" |
---|
56 | #load data |
---|
57 | from sans.fit.Loader import Load |
---|
58 | load= Load() |
---|
59 | load.set_filename("testdata_line.txt") |
---|
60 | load.set_values() |
---|
61 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
62 | load.load_data(data1) |
---|
63 | |
---|
64 | #Importing the Fit module |
---|
65 | from sans.fit.Fitting import Fit |
---|
66 | fitter= Fit() |
---|
67 | fitter.fit_engine('scipy') |
---|
68 | engine = fitter.returnEngine() |
---|
69 | # Receives the type of model for the fitting |
---|
70 | from sans.guitools.LineModel import LineModel |
---|
71 | model = LineModel() |
---|
72 | |
---|
73 | #Do the fit SCIPY |
---|
74 | engine.set_data(data1,1) |
---|
75 | engine.set_model(model,1) |
---|
76 | |
---|
77 | chisqr1, out1, cov1=engine.fit({'A':2,'B':1},None,None) |
---|
78 | """ testing SCIPy results""" |
---|
79 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
80 | self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
81 | self.assert_(chisqr1/len(data1.x) < 2) |
---|
82 | # PARK |
---|
83 | fitter= Fit() |
---|
84 | fitter.fit_engine('park') |
---|
85 | engine = fitter.returnEngine() |
---|
86 | |
---|
87 | #Do the fit |
---|
88 | engine.set_data(data1,1) |
---|
89 | engine.set_model(model,1) |
---|
90 | |
---|
91 | engine.fit({'A':2,'B':1},None,None) |
---|
92 | """ |
---|
93 | |
---|
94 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
95 | self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) |
---|
96 | self.assert_(chisqr2/len(data1.x) < 2) |
---|
97 | |
---|
98 | self.assertEqual(out1[1], out2[1]) |
---|
99 | self.assertEquals(out1[0], out2[0]) |
---|
100 | self.assertEquals(cov1[0][0], cov2[0][0]) |
---|
101 | self.assertEquals(cov1[1][1], cov2[1][1]) |
---|
102 | self.assertEquals(chisqr1, chisqr2) |
---|
103 | """ |
---|
104 | def testfit_2Data_1Model(self): |
---|
105 | """ test fitting for two set of data and one model""" |
---|
106 | from sans.fit.Loader import Load |
---|
107 | load= Load() |
---|
108 | #Load the first set of data |
---|
109 | load.set_filename("testdata1.txt") |
---|
110 | load.set_values() |
---|
111 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
112 | load.load_data(data1) |
---|
113 | |
---|
114 | #Load the second set of data |
---|
115 | load.set_filename("testdata2.txt") |
---|
116 | load.set_values() |
---|
117 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
118 | load.load_data(data2) |
---|
119 | |
---|
120 | #Importing the Fit module |
---|
121 | from sans.fit.Fitting import Fit |
---|
122 | fitter= Fit() |
---|
123 | # Receives the type of model for the fitting |
---|
124 | from sans.guitools.LineModel import LineModel |
---|
125 | model = LineModel() |
---|
126 | #set engine for scipy |
---|
127 | fitter.fit_engine('scipy') |
---|
128 | engine = fitter.returnEngine() |
---|
129 | #Do the fit |
---|
130 | engine.set_model(model,1) |
---|
131 | engine.set_data(data1,1) |
---|
132 | engine.set_data(data2,1) |
---|
133 | |
---|
134 | |
---|
135 | chisqr1, out1, cov1= engine.fit({'A':2,'B':1},None,None) |
---|
136 | |
---|
137 | """ Testing results for SCIPY""" |
---|
138 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
139 | self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
140 | self.assert_(chisqr1/len(data1.x+data2.x) < 2) |
---|
141 | #self.assert_(chisqr1/len(data2.x) < 2) |
---|
142 | |
---|
143 | #set engine for park |
---|
144 | fitter= Fit() |
---|
145 | fitter.fit_engine('park') |
---|
146 | engine = fitter.returnEngine() |
---|
147 | #Do the fit |
---|
148 | engine.set_data(data1,1) |
---|
149 | engine.set_model(model,1) |
---|
150 | engine.set_data(data2,1) |
---|
151 | engine.fit({'A':2,'B':1},None,None) |
---|
152 | """ |
---|
153 | chisqr2, out2, cov2= engine.fit({'A':2,'B':1},None,None) |
---|
154 | |
---|
155 | |
---|
156 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
157 | self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) |
---|
158 | self.assert_(chisqr2/len(data1.x) < 2) |
---|
159 | self.assert_(chisqr2/len(data2.x) < 2) |
---|
160 | |
---|
161 | self.assertEqual(out1[0],out2[0]) |
---|
162 | self.assertEqual(out1[1],out2[1]) |
---|
163 | self.assertEqual(chisqr1,chisqr2) |
---|
164 | self.assertEqual(cov1[0][0],cov2[0][0]) |
---|
165 | self.assertEqual(cov1[1][1],cov2[1][1]) |
---|
166 | """ |
---|