1 | """ |
---|
2 | Unit tests for fitting module using park integration |
---|
3 | fitting 2 data with 2 model and one constraint on only one parameter is not working |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | from sans.guitools.plottables import Theory1D |
---|
7 | from sans.guitools.plottables import Data1D |
---|
8 | from sans.fit.AbstractFitEngine import Model,Data |
---|
9 | import math |
---|
10 | class testFitModule(unittest.TestCase): |
---|
11 | |
---|
12 | def test2models2data2constraints(self): |
---|
13 | """ test fitting for two data , 2 model , 2 constraints""" |
---|
14 | from sans.fit.Loader import Load |
---|
15 | load= Load() |
---|
16 | #Load the first data |
---|
17 | load.set_filename("testdata1.txt") |
---|
18 | load.set_values() |
---|
19 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
20 | load.load_data(data1) |
---|
21 | |
---|
22 | #Load the second data |
---|
23 | load.set_filename("testdata2.txt") |
---|
24 | load.set_values() |
---|
25 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
26 | load.load_data(data2) |
---|
27 | |
---|
28 | #Load the third data |
---|
29 | load.set_filename("testdata_line.txt") |
---|
30 | load.set_values() |
---|
31 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
32 | load.load_data(data3) |
---|
33 | |
---|
34 | #Importing the Fit module |
---|
35 | from sans.fit.Fitting import Fit |
---|
36 | fitter= Fit('park') |
---|
37 | # Receives the type of model for the fitting |
---|
38 | from sans.guitools.LineModel import LineModel |
---|
39 | model1 = LineModel() |
---|
40 | model2 = LineModel() |
---|
41 | |
---|
42 | #Do the fit |
---|
43 | model1.setParam( 'A', 2.5) |
---|
44 | model1.setParam( 'B', 4) |
---|
45 | model1.name="M1" |
---|
46 | fitter.set_model(Model(model1),"M1",1, ['A','B']) |
---|
47 | fitter.set_data(Data(sans_data=data1),1) |
---|
48 | model2.name="M2" |
---|
49 | model2.setParam( 'A', "M1.A+1") |
---|
50 | model2.setParam( 'B', 'M1.B*2') |
---|
51 | |
---|
52 | fitter.set_model(Model(model2),"M2",2, ['A','B']) |
---|
53 | fitter.set_data(Data(sans_data=data2),2) |
---|
54 | |
---|
55 | result = fitter.fit() |
---|
56 | chisqr1 = result.fitness |
---|
57 | out1 = result.pvec |
---|
58 | cov1 = result.cov |
---|
59 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
60 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
61 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
62 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
63 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
64 | print chisqr1/len(data1.x) |
---|
65 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
66 | print chisqr1/len(data2.x) |
---|
67 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
68 | |
---|
69 | def test2models2data1constraint(self): |
---|
70 | """ test fitting for two data , 2 model ,1 constraint""" |
---|
71 | from sans.fit.Loader import Load |
---|
72 | load= Load() |
---|
73 | #Load the first data |
---|
74 | load.set_filename("testdata1.txt") |
---|
75 | load.set_values() |
---|
76 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
77 | load.load_data(data1) |
---|
78 | |
---|
79 | #Load the second data |
---|
80 | load.set_filename("testdata2.txt") |
---|
81 | load.set_values() |
---|
82 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
83 | load.load_data(data2) |
---|
84 | |
---|
85 | #Load the third data |
---|
86 | load.set_filename("testdata_line.txt") |
---|
87 | load.set_values() |
---|
88 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
89 | load.load_data(data3) |
---|
90 | |
---|
91 | #Importing the Fit module |
---|
92 | from sans.fit.Fitting import Fit |
---|
93 | fitter= Fit('park') |
---|
94 | # Receives the type of model for the fitting |
---|
95 | from sans.guitools.LineModel import LineModel |
---|
96 | model1 = LineModel() |
---|
97 | model2 = LineModel() |
---|
98 | |
---|
99 | #Do the fit |
---|
100 | model1.setParam( 'A', 2.5) |
---|
101 | model1.setParam( 'B', 4) |
---|
102 | model1.name="M1" |
---|
103 | fitter.set_model(Model(model1),"M1",1, ['A','B']) |
---|
104 | fitter.set_data(Data(sans_data=data1),1) |
---|
105 | model2.name="M2" |
---|
106 | model2.setParam( 'A', 2) |
---|
107 | model2.setParam( 'B', 'M1.B*2') |
---|
108 | |
---|
109 | fitter.set_model(Model(model2),"M2",2, ['A','B']) |
---|
110 | fitter.set_data(Data(sans_data=data2),2) |
---|
111 | |
---|
112 | result = fitter.fit() |
---|
113 | chisqr1 = result.fitness |
---|
114 | out1 = result.pvec |
---|
115 | cov1 = result.cov |
---|
116 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
117 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
118 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
119 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
120 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
121 | print chisqr1/len(data1.x) |
---|
122 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
123 | print chisqr1/len(data2.x) |
---|
124 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
125 | |
---|
126 | |
---|
127 | def test2models2dataNoconstraint(self): |
---|
128 | """ test fitting for two data and 2 models no cosntrainst""" |
---|
129 | from sans.fit.Loader import Load |
---|
130 | load= Load() |
---|
131 | #Load the first data |
---|
132 | load.set_filename("testdata1.txt") |
---|
133 | load.set_values() |
---|
134 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
135 | load.load_data(data1) |
---|
136 | |
---|
137 | #Load the second data |
---|
138 | load.set_filename("testdata2.txt") |
---|
139 | load.set_values() |
---|
140 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
141 | load.load_data(data2) |
---|
142 | |
---|
143 | #Load the third data |
---|
144 | load.set_filename("testdata_line.txt") |
---|
145 | load.set_values() |
---|
146 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
147 | load.load_data(data3) |
---|
148 | |
---|
149 | #Importing the Fit module |
---|
150 | from sans.fit.Fitting import Fit |
---|
151 | fitter= Fit('park') |
---|
152 | # Receives the type of model for the fitting |
---|
153 | from sans.guitools.LineModel import LineModel |
---|
154 | model1 = LineModel() |
---|
155 | model2 = LineModel() |
---|
156 | |
---|
157 | #Do the fit |
---|
158 | model1.setParam( 'A', 2.5) |
---|
159 | model1.setParam( 'B', 4) |
---|
160 | model1.name="M1" |
---|
161 | fitter.set_model(Model(model1),"M1",1, ['A','B']) |
---|
162 | fitter.set_data(Data(sans_data=data1),1) |
---|
163 | model2.name="M2" |
---|
164 | model2.setParam( 'A', 1) |
---|
165 | model2.setParam( 'B', 2) |
---|
166 | |
---|
167 | fitter.set_model(Model(model2),"M2",2, ['A','B']) |
---|
168 | fitter.set_data(Data(sans_data=data2),2) |
---|
169 | |
---|
170 | result = fitter.fit() |
---|
171 | chisqr1 = result.fitness |
---|
172 | out1 = result.pvec |
---|
173 | cov1 = result.cov |
---|
174 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
175 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
176 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
177 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
178 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
179 | print chisqr1/len(data1.x) |
---|
180 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
181 | print chisqr1/len(data2.x) |
---|
182 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
183 | |
---|
184 | |
---|
185 | |
---|