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.ScipyFitting import Parameter |
---|
8 | import math |
---|
9 | class testFitModule(unittest.TestCase): |
---|
10 | |
---|
11 | def test2models2dataonconstraint(self): |
---|
12 | """ test fitting for two set of data and one model""" |
---|
13 | from sans.fit.Loader import Load |
---|
14 | load= Load() |
---|
15 | #Load the first data |
---|
16 | load.set_filename("testdata1.txt") |
---|
17 | load.set_values() |
---|
18 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
19 | load.load_data(data1) |
---|
20 | |
---|
21 | #Load the second data |
---|
22 | load.set_filename("testdata2.txt") |
---|
23 | load.set_values() |
---|
24 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
25 | load.load_data(data2) |
---|
26 | |
---|
27 | #Load the third data |
---|
28 | load.set_filename("testdata_line.txt") |
---|
29 | load.set_values() |
---|
30 | data3 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
31 | load.load_data(data3) |
---|
32 | |
---|
33 | #Importing the Fit module |
---|
34 | from sans.fit.Fitting import Fit |
---|
35 | fitter= Fit('park') |
---|
36 | # Receives the type of model for the fitting |
---|
37 | from sans.guitools.LineModel import LineModel |
---|
38 | model1 = LineModel() |
---|
39 | model2 = LineModel() |
---|
40 | |
---|
41 | #Do the fit |
---|
42 | model1.setParam( 'A', 2.5) |
---|
43 | model1.setParam( 'B', 4) |
---|
44 | fitter.set_model(model1,"M1",1, ['A','B']) |
---|
45 | fitter.set_data(data1,1) |
---|
46 | |
---|
47 | model1.setParam( 'A', 2) |
---|
48 | model1.setParam( 'B', 3) |
---|
49 | fitter.set_model(model2,"M2",2, ['A','B']) |
---|
50 | fitter.set_data(data2,2) |
---|
51 | |
---|
52 | chisqr1, out1, cov1,result= fitter.fit() |
---|
53 | |
---|
54 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
55 | print math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) |
---|
56 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
57 | self.assert_(math.fabs(out1[3]-2.5)/math.sqrt(cov1[3][3]) < 2) |
---|
58 | self.assert_(math.fabs(out1[2]-4.0)/math.sqrt(cov1[2][2]) < 2) |
---|
59 | print chisqr1/len(data1.x) |
---|
60 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
61 | print chisqr1/len(data2.x) |
---|
62 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
63 | |
---|
64 | |
---|
65 | fitter.set_data(data3,1) |
---|
66 | chisqr2, out2, cov2, result= fitter.fit(None,None) |
---|
67 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
68 | print math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) |
---|
69 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
70 | self.assert_(math.fabs(out2[3]-2.5)/math.sqrt(cov2[3][3]) < 2) |
---|
71 | self.assert_(math.fabs(out2[2]-4.0)/math.sqrt(cov2[2][2]) < 2) |
---|
72 | print chisqr2/len(data1.x) |
---|
73 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
74 | print chisqr2/len(data2.x) |
---|
75 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
76 | |
---|
77 | fitter.remove_Fit_Problem(2) |
---|
78 | |
---|
79 | chisqr3, out3, cov3= fitter.fit() |
---|
80 | #print "park",chisqr3, out3, cov3 |
---|
81 | self.assert_(math.fabs(out1[1]-2.5)/math.sqrt(cov1[1][1]) < 2) |
---|
82 | print math.fabs(out1[0]-4.0) |
---|
83 | #self.assert_(math.fabs(out1[0]-4.0)/math.sqrt(cov1[0][0]) < 2) |
---|
84 | print chisqr1/len(data1.x) |
---|
85 | #self.assert_(chisqr1/len(data1.x) < 2) |
---|
86 | #self.assert_(chisqr1/len(data2.x) < 2) |
---|
87 | #failing at 7 place |
---|
88 | self.assertAlmostEquals(out3[1],out1[1]) |
---|
89 | self.assertAlmostEquals(out3[0],out1[0]) |
---|
90 | self.assertAlmostEquals(cov3[1][1],cov1[1][1]) |
---|
91 | self.assertAlmostEquals(cov3[0][0],cov1[0][0]) |
---|
92 | |
---|
93 | self.assertAlmostEquals(out2[1],out1[1]) |
---|
94 | self.assertAlmostEquals(out2[0],out1[0]) |
---|
95 | self.assertAlmostEquals(cov2[1][1],cov1[1][1]) |
---|
96 | self.assertAlmostEquals(cov2[0][0],cov1[0][0]) |
---|
97 | |
---|
98 | self.assertAlmostEquals(out2[1],out3[1]) |
---|
99 | self.assertAlmostEquals(out2[0],out3[0]) |
---|
100 | self.assertAlmostEquals(cov2[1][1],cov3[1][1]) |
---|
101 | self.assertAlmostEquals(cov2[0][0],cov3[0][0]) |
---|
102 | print chisqr1,chisqr2,chisqr3 |
---|
103 | #self.assertAlmostEquals(chisqr1,chisqr2) |
---|
104 | self.assert_(chisqr1) |
---|