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