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