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 | model22 = Constant() |
---|
139 | model22.name= "cst" |
---|
140 | model22.setParam('value','line.A') |
---|
141 | |
---|
142 | data1 = Data(sans_data=data11 ) |
---|
143 | data2 = Data(sans_data=data22 ) |
---|
144 | model1 = Model(model11) |
---|
145 | model2 = Model(model22) |
---|
146 | #fit with scipy test |
---|
147 | pars1= ['A','B'] |
---|
148 | pars2= ['value'] |
---|
149 | |
---|
150 | fitter.set_data(data1,1) |
---|
151 | fitter.set_model(model1,1,pars1) |
---|
152 | fitter.set_data(data2,2) |
---|
153 | fitter.set_model(model2,2,pars2) |
---|
154 | |
---|
155 | result2 = fitter.fit() |
---|
156 | self.assert_(result2) |
---|
157 | self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or |
---|
158 | ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) |
---|
159 | |
---|
160 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or |
---|
161 | ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) |
---|
162 | self.assertTrue(result2.fitness/49 < 2) |
---|
163 | |
---|
164 | |
---|
165 | def test4(self): |
---|
166 | """ fit 2 data concatenates with limited range of x and one model """ |
---|
167 | #load data |
---|
168 | from DataLoader.loader import Loader |
---|
169 | l = Loader() |
---|
170 | out=l.load("testdata_line.txt") |
---|
171 | data11 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
172 | out=l.load("testdata_line1.txt") |
---|
173 | data22 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
174 | #Importing the Fit module |
---|
175 | from sans.fit.Fitting import Fit |
---|
176 | fitter = Fit('scipy') |
---|
177 | # Receives the type of model for the fitting |
---|
178 | from sans.guitools.LineModel import LineModel |
---|
179 | model1 = LineModel() |
---|
180 | model1.name= "M1" |
---|
181 | |
---|
182 | data1 = Data(sans_data=data11 ) |
---|
183 | data2 = Data(sans_data=data22 ) |
---|
184 | model = Model(model1) |
---|
185 | |
---|
186 | #fit with scipy test |
---|
187 | pars1= ['A','B'] |
---|
188 | fitter.set_data(data1,1,qmin=0, qmax=7) |
---|
189 | fitter.set_model(model,1,pars1) |
---|
190 | fitter.set_data(data2,1,qmin=1,qmax=10) |
---|
191 | |
---|
192 | result1 = fitter.fit() |
---|
193 | self.assert_(result1) |
---|
194 | |
---|
195 | self.assertTrue( ( math.fabs(result1.pvec[0]-4)/3 == result1.stderr[0] ) or |
---|
196 | ( math.fabs(result1.pvec[0]-4)/3 < result1.stderr[0]) ) |
---|
197 | |
---|
198 | self.assertTrue( ( math.fabs(result1.pvec[1]-2.5)/3 == result1.stderr[1]) or |
---|
199 | ( math.fabs(result1.pvec[1]-2.5)/3 < result1.stderr[1] ) ) |
---|
200 | self.assertTrue( result1.fitness/49 < 2 ) |
---|
201 | |
---|
202 | |
---|
203 | #fit with park test |
---|
204 | fitter = Fit('park') |
---|
205 | fitter.set_data(data1,1,qmin=0, qmax=7) |
---|
206 | fitter.set_model(model,1,pars1) |
---|
207 | fitter.set_data(data2,1,qmin=1,qmax=10) |
---|
208 | |
---|
209 | result2 = fitter.fit() |
---|
210 | |
---|
211 | self.assert_(result2) |
---|
212 | self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or |
---|
213 | ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) |
---|
214 | |
---|
215 | self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or |
---|
216 | ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) |
---|
217 | self.assertTrue(result2.fitness/49 < 2) |
---|
218 | # compare fit result result for scipy and park |
---|
219 | self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) |
---|
220 | self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) |
---|
221 | self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) |
---|
222 | self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) |
---|
223 | self.assertAlmostEquals( result1.fitness,result2.fitness ) |
---|
224 | |
---|