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