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