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