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