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