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 FittingModule import Parameter |
---|
8 | import math |
---|
9 | class testFitModule(unittest.TestCase): |
---|
10 | """ test fitting """ |
---|
11 | def testLoader(self): |
---|
12 | """ |
---|
13 | test module Load |
---|
14 | """ |
---|
15 | from Loader import Load |
---|
16 | load= Load() |
---|
17 | |
---|
18 | load.set_filename("testdata_line.txt") |
---|
19 | self.assertEqual(load.get_filename(),"testdata_line.txt") |
---|
20 | load.set_values() |
---|
21 | x=[] |
---|
22 | y=[] |
---|
23 | dx=[] |
---|
24 | dy=[] |
---|
25 | |
---|
26 | x,y,dx,dy = load.get_values() |
---|
27 | |
---|
28 | # test that values have been loaded |
---|
29 | self.assertNotEqual(x, None) |
---|
30 | self.assertNotEqual(y, []) |
---|
31 | self.assertNotEqual(dy, None) |
---|
32 | self.assertEqual(len(x),len(y)) |
---|
33 | self.assertEqual(len(dy),len(y)) |
---|
34 | |
---|
35 | # test data the two plottables contained values loaded |
---|
36 | data1 = Theory1D(x=[], y=[], dy=None) |
---|
37 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
38 | data1.name = "data1" |
---|
39 | data2.name = "data2" |
---|
40 | load.load_data(data1) |
---|
41 | load.load_data(data2) |
---|
42 | |
---|
43 | |
---|
44 | for i in range(len(x)): |
---|
45 | self.assertEqual(data2.x[i],x[i]) |
---|
46 | self.assertEqual(data1.y[i],y[i]) |
---|
47 | self.assertEqual(data2.y[i],y[i]) |
---|
48 | self.assertEqual(data1.dx[i],dx[i]) |
---|
49 | self.assertEqual(data2.dy[i],dy[i]) |
---|
50 | self.assertEqual(data1.x[i],data2.x[i]) |
---|
51 | self.assertEqual(data2.y[i],data2.y[i]) |
---|
52 | |
---|
53 | |
---|
54 | def testfit_1Data_1Model(self): |
---|
55 | """ test fitting for one data and one model""" |
---|
56 | #load data |
---|
57 | from Loader import Load |
---|
58 | load= Load() |
---|
59 | load.set_filename("testdata_line.txt") |
---|
60 | load.set_values() |
---|
61 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
62 | load.load_data(data1) |
---|
63 | |
---|
64 | #Importing the Fit module |
---|
65 | from FittingModule import Fitting |
---|
66 | Fit= Fitting() |
---|
67 | # Receives the type of model for the fitting |
---|
68 | from sans.guitools.LineModel import LineModel |
---|
69 | model = LineModel() |
---|
70 | |
---|
71 | #Do the fit |
---|
72 | Fit.set_data(data1,1) |
---|
73 | Fit.set_model(model,1) |
---|
74 | self.assertEqual(Fit.fit_engine("scipy"),True) |
---|
75 | chisqr, out, cov=Fit.fit({'A':2,'B':1},None,None) |
---|
76 | #print"fit only one data",chisqr, out, cov |
---|
77 | #Testing results |
---|
78 | |
---|
79 | self.assert_(math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) < 2) |
---|
80 | self.assert_(math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) < 2) |
---|
81 | self.assert_(chisqr/len(data1.x) < 2) |
---|
82 | |
---|
83 | #print "chisqr",chisqr/len(data1.x) |
---|
84 | #print "Error on A",math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) |
---|
85 | #print "Error on B",math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) |
---|
86 | |
---|
87 | def testfit_2Data_1Model(self): |
---|
88 | """ test fitting for two set of data data and one model""" |
---|
89 | from Loader import Load |
---|
90 | load= Load() |
---|
91 | #Load the first set of data |
---|
92 | load.set_filename("testdata1.txt") |
---|
93 | load.set_values() |
---|
94 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
95 | load.load_data(data1) |
---|
96 | |
---|
97 | #Load the second set of data |
---|
98 | load.set_filename("testdata2.txt") |
---|
99 | load.set_values() |
---|
100 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
101 | load.load_data(data2) |
---|
102 | |
---|
103 | #Importing the Fit module |
---|
104 | from FittingModule import Fitting |
---|
105 | Fit= Fitting() |
---|
106 | # Receives the type of model for the fitting |
---|
107 | from sans.guitools.LineModel import LineModel |
---|
108 | model = LineModel() |
---|
109 | |
---|
110 | #Do the fit |
---|
111 | Fit.set_data(data1,1) |
---|
112 | Fit.set_model(model,1) |
---|
113 | |
---|
114 | Fit.set_data(data2,2) |
---|
115 | Fit.set_model(model,2) |
---|
116 | self.assertEqual(Fit.fit_engine("scipy"),True) |
---|
117 | self.assertEqual(Fit.fit_engine("scipys"),False) |
---|
118 | chisqr, out, cov=Fit.fit({'A':2,'B':1},None,None) |
---|
119 | #print"fit only one data",chisqr, out, cov |
---|
120 | |
---|
121 | #Testing results |
---|
122 | |
---|
123 | self.assert_(math.fabs(out[1]-2.5)/math.sqrt(cov[1][1]) < 2) |
---|
124 | self.assert_(math.fabs(out[0]-4.0)/math.sqrt(cov[0][0]) < 2) |
---|
125 | self.assert_(chisqr/len(data1.x) < 2) |
---|
126 | |
---|
127 | |
---|