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