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 with 2 constraint""" |
---|
13 | from sans.fit.Loader import Load |
---|
14 | load= Load() |
---|
15 | #Load the first set of 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 set of 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 | #Importing the Fit module |
---|
28 | from sans.fit.Fitting import Fit |
---|
29 | fitter= Fit('park') |
---|
30 | # Receives the type of model for the fitting |
---|
31 | from sans.guitools.LineModel import LineModel |
---|
32 | model1 = LineModel() |
---|
33 | model2 = LineModel() |
---|
34 | |
---|
35 | #Do the fit |
---|
36 | model1.setParam( 'A', 1) |
---|
37 | model1.setParam( 'B', 2) |
---|
38 | fitter.set_model(model1,"M1",1, ['A','B']) |
---|
39 | fitter.set_data(data1,1) |
---|
40 | |
---|
41 | model2.setParam( 'A', 'M1.A') |
---|
42 | model2.setParam( 'B','M1.B') |
---|
43 | fitter.set_model(model2,"M2",2, ['A','B']) |
---|
44 | fitter.set_data(data2,2) |
---|
45 | |
---|
46 | |
---|
47 | chisqr2, out2, cov2,result= fitter.fit() |
---|
48 | print "chisqr2",chisqr2 |
---|
49 | print "out2", out2 |
---|
50 | print " cov2", cov2 |
---|
51 | print chisqr2/len(data1.x) |
---|
52 | |
---|
53 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
54 | self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) |
---|
55 | #self.assert_(chisqr2/len(data1.x) < 2) |
---|
56 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
57 | def testmodel1data1param1(self): |
---|
58 | """ test fitting for two set of data and one model with 2 constraint""" |
---|
59 | from sans.fit.Loader import Load |
---|
60 | load= Load() |
---|
61 | #Load the first set of data |
---|
62 | load.set_filename("testdata1.txt") |
---|
63 | load.set_values() |
---|
64 | data1 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
65 | load.load_data(data1) |
---|
66 | |
---|
67 | #Load the second set of data |
---|
68 | load.set_filename("testdata2.txt") |
---|
69 | load.set_values() |
---|
70 | data2 = Data1D(x=[], y=[],dx=None, dy=None) |
---|
71 | load.load_data(data2) |
---|
72 | |
---|
73 | #Importing the Fit module |
---|
74 | from sans.fit.Fitting import Fit |
---|
75 | fitter= Fit('park') |
---|
76 | # Receives the type of model for the fitting |
---|
77 | from sans.guitools.LineModel import LineModel |
---|
78 | model1 = LineModel() |
---|
79 | |
---|
80 | |
---|
81 | #Do the fit |
---|
82 | model1.setParam( 'A', 1) |
---|
83 | fitter.set_model(model1,"M1",1, ['A']) |
---|
84 | fitter.set_data(data1,1) |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | chisqr2, out2, cov2,result = fitter.fit() |
---|
89 | print "chisqr2",chisqr2 |
---|
90 | print "out2", out2 |
---|
91 | print " cov2", cov2 |
---|
92 | print chisqr2/len(data1.x) |
---|
93 | |
---|
94 | self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) |
---|
95 | self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) |
---|
96 | #self.assert_(chisqr2/len(data1.x) < 2) |
---|
97 | #self.assert_(chisqr2/len(data2.x) < 2) |
---|
98 | |
---|