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.AbstractFitEngine import Data, Model,FitData1D |
---|
8 | import math |
---|
9 | from sans.fit.Fitting import Fit |
---|
10 | from DataLoader.loader import Loader |
---|
11 | |
---|
12 | class testFitModule(unittest.TestCase): |
---|
13 | """ test fitting """ |
---|
14 | |
---|
15 | def test_scipy(self): |
---|
16 | """ Simple cylinder model fit (scipy) """ |
---|
17 | |
---|
18 | out=Loader().load("cyl_400_20.txt") |
---|
19 | data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
20 | |
---|
21 | fitter = Fit('scipy') |
---|
22 | # Receives the type of model for the fitting |
---|
23 | from sans.models.CylinderModel import CylinderModel |
---|
24 | model1 = CylinderModel() |
---|
25 | model1.setParam('contrast', 1) |
---|
26 | #data = Data(sans_data=data1) |
---|
27 | data = FitData1D(data1) |
---|
28 | model = Model(model1) |
---|
29 | |
---|
30 | pars1 =['length','radius','scale'] |
---|
31 | fitter.set_data(data,1) |
---|
32 | model.set(scale=1e-10) |
---|
33 | fitter.set_model(model,1,pars1) |
---|
34 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
35 | result1 = fitter.fit() |
---|
36 | |
---|
37 | self.assert_(result1) |
---|
38 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
39 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
40 | |
---|
41 | self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) |
---|
42 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) |
---|
43 | self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) |
---|
44 | self.assertTrue( result1.fitness < 1.0 ) |
---|
45 | |
---|
46 | def test_park(self): |
---|
47 | """ Simple cylinder model fit (park) """ |
---|
48 | |
---|
49 | out=Loader().load("cyl_400_20.txt") |
---|
50 | data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
51 | |
---|
52 | fitter = Fit('park') |
---|
53 | # Receives the type of model for the fitting |
---|
54 | from sans.models.CylinderModel import CylinderModel |
---|
55 | model1 = CylinderModel() |
---|
56 | |
---|
57 | #data = Data(sans_data=data1) |
---|
58 | data = FitData1D(data1) |
---|
59 | model = Model(model1) |
---|
60 | |
---|
61 | pars1 =['length','radius','scale'] |
---|
62 | fitter.set_data(data,1) |
---|
63 | model.set(contrast= 1) |
---|
64 | model.set(scale=1e-10) |
---|
65 | fitter.set_model(model,1,pars1) |
---|
66 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
67 | result1 = fitter.fit() |
---|
68 | |
---|
69 | self.assert_(result1) |
---|
70 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
71 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
72 | |
---|
73 | print result1.pvec[0]-400.0, result1.pvec[0] |
---|
74 | print math.fabs(result1.pvec[0]-400.0)/3.0 |
---|
75 | self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) |
---|
76 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) |
---|
77 | self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) |
---|
78 | self.assertTrue( result1.fitness < 1.0 ) |
---|
79 | |
---|
80 | def test_park2(self): |
---|
81 | """ Simultaneous cylinder model fit (park) """ |
---|
82 | |
---|
83 | out=Loader().load("cyl_400_20.txt") |
---|
84 | data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) |
---|
85 | |
---|
86 | out2=Loader().load("cyl_400_40.txt") |
---|
87 | data2 = Data1D(x=out2.x, y=out2.y, dx=out2.dx, dy=out2.y) |
---|
88 | |
---|
89 | fitter = Fit('park') |
---|
90 | # Receives the type of model for the fitting |
---|
91 | from sans.models.CylinderModel import CylinderModel |
---|
92 | cyl1 = CylinderModel() |
---|
93 | cyl1.name = "C1" |
---|
94 | |
---|
95 | #data1 = Data(sans_data=data1) |
---|
96 | data1 = FitData1D(data1) |
---|
97 | model1 = Model(cyl1) |
---|
98 | model1.set(contrast=1) |
---|
99 | model1.set(scale= 1e-10) |
---|
100 | fitter.set_data(data1,1) |
---|
101 | fitter.set_model(model1, 1, ['length','radius','scale']) |
---|
102 | |
---|
103 | cyl2 = CylinderModel() |
---|
104 | cyl2.name = "C2" |
---|
105 | |
---|
106 | #data2 = Data(sans_data=data2) |
---|
107 | data2 = FitData1D(data2) |
---|
108 | # This is wrong. We should not store string as |
---|
109 | # parameter values |
---|
110 | # Why not inherit our AbstracFitEngine.Model from Park.Model? |
---|
111 | |
---|
112 | #cyl2.setParam('length', 'C1.length') |
---|
113 | #print "read back:", cyl2.getParam('length') |
---|
114 | |
---|
115 | model2 = Model(cyl2) |
---|
116 | model2.set(length='C1.length') |
---|
117 | model2.set(contrast=1) |
---|
118 | model2.set(scale= 1e-10) |
---|
119 | fitter.set_data(data2,2) |
---|
120 | fitter.set_model(model2, 2, ['radius','scale']) |
---|
121 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
122 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
123 | result1 = fitter.fit() |
---|
124 | |
---|
125 | self.assert_(result1) |
---|
126 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
127 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
128 | |
---|
129 | for par in result1.parameters: |
---|
130 | if par.name=='C1.length': |
---|
131 | print par.name, par.value |
---|
132 | self.assertTrue( math.fabs(par.value-400.0)/3.0 < par.stderr ) |
---|
133 | elif par.name=='C1.radius': |
---|
134 | print par.name, par.value |
---|
135 | self.assertTrue( math.fabs(par.value-20.0)/3.0 < par.stderr ) |
---|
136 | elif par.name=='C2.radius': |
---|
137 | print par.name, par.value |
---|
138 | self.assertTrue( math.fabs(par.value-40.0)/3.0 < par.stderr ) |
---|
139 | elif par.name=='C1.scale': |
---|
140 | print par.name, par.value |
---|
141 | self.assertTrue( math.fabs(par.value-9.0e-12)/3.0 < par.stderr ) |
---|
142 | elif par.name=='C2.scale': |
---|
143 | print par.name, par.value |
---|
144 | self.assertTrue( math.fabs(par.value-9.0e-12)/3.0 < par.stderr ) |
---|
145 | |
---|
146 | |
---|
147 | |
---|