1 | """ |
---|
2 | Unit tests for fitting module |
---|
3 | @author G.alina |
---|
4 | """ |
---|
5 | import unittest |
---|
6 | |
---|
7 | from sans.fit.AbstractFitEngine import Model |
---|
8 | import math |
---|
9 | from sans.fit.Fitting import Fit |
---|
10 | from DataLoader.loader import Loader |
---|
11 | |
---|
12 | class TestSingleFit(unittest.TestCase): |
---|
13 | """ test single fitting """ |
---|
14 | def setUp(self): |
---|
15 | """ initialize data""" |
---|
16 | self.data = Loader().load("cyl_400_20.txt") |
---|
17 | # Create model that fitting engine understands |
---|
18 | from sans.models.CylinderModel import CylinderModel |
---|
19 | self.model = CylinderModel() |
---|
20 | self.model.setParam("scale", 1.0) |
---|
21 | self.model.setParam("radius",18) |
---|
22 | self.model.setParam("length", 397) |
---|
23 | self.model.setParam("contrast",3e-006 ) |
---|
24 | self.model.setParam("background", 0.0) |
---|
25 | #select parameters to fit |
---|
26 | self.pars1 =['length','radius','scale'] |
---|
27 | |
---|
28 | def _fit(self, name="scipy"): |
---|
29 | """ return fit result """ |
---|
30 | fitter = Fit(name) |
---|
31 | fitter.set_data(self.data,1) |
---|
32 | fitter.set_model(self.model,1,self.pars1) |
---|
33 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
34 | return fitter.fit() |
---|
35 | |
---|
36 | |
---|
37 | def test_scipy(self): |
---|
38 | """ Simple cylinder model fit (scipy) """ |
---|
39 | |
---|
40 | result1 = self._fit("scipy") |
---|
41 | |
---|
42 | self.assert_(result1) |
---|
43 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
44 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
45 | |
---|
46 | self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) |
---|
47 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) |
---|
48 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
49 | self.assertTrue( result1.fitness < 1.0 ) |
---|
50 | |
---|
51 | |
---|
52 | def test_park(self): |
---|
53 | """ Simple cylinder model fit (park) """ |
---|
54 | result1 = self._fit("park") |
---|
55 | |
---|
56 | self.assert_(result1) |
---|
57 | self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) |
---|
58 | self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) |
---|
59 | |
---|
60 | self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) |
---|
61 | self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) |
---|
62 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
63 | self.assertTrue( result1.fitness < 1.0 ) |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | class TestSimultaneousFit(unittest.TestCase): |
---|
68 | """ test simultaneous fitting """ |
---|
69 | def setUp(self): |
---|
70 | """ initialize data""" |
---|
71 | |
---|
72 | self.data1=Loader().load("cyl_400_20.txt") |
---|
73 | self.data2=Loader().load("cyl_400_40.txt") |
---|
74 | |
---|
75 | # Receives the type of model for the fitting |
---|
76 | from sans.models.CylinderModel import CylinderModel |
---|
77 | cyl1 = CylinderModel() |
---|
78 | cyl1.name = "C1" |
---|
79 | self.model1 = Model(cyl1) |
---|
80 | self.model1.set(scale= 1.0) |
---|
81 | self.model1.set(radius=18) |
---|
82 | self.model1.set(length=396) |
---|
83 | self.model1.set(contrast=3e-006 ) |
---|
84 | self.model1.set(background=0.0) |
---|
85 | |
---|
86 | cyl2 = CylinderModel() |
---|
87 | cyl2.name = "C2" |
---|
88 | self.model2 = Model(cyl2) |
---|
89 | self.model2.set(scale= 1.0) |
---|
90 | self.model2.set(radius=37) |
---|
91 | self.model2.set(length='C1.length') |
---|
92 | self.model2.set(contrast=3e-006 ) |
---|
93 | self.model2.set(background=0.0) |
---|
94 | |
---|
95 | def _fit(self, name="park"): |
---|
96 | """ return fit result """ |
---|
97 | fitter = Fit(name) |
---|
98 | fitter.set_data(self.data1,1) |
---|
99 | fitter.set_model(self.model1, 1, ['length','radius','scale']) |
---|
100 | |
---|
101 | fitter.set_data(self.data2,2) |
---|
102 | fitter.set_model(self.model2, 2, ['radius','scale']) |
---|
103 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
104 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
105 | return fitter.fit() |
---|
106 | |
---|
107 | |
---|
108 | def test_park2(self): |
---|
109 | """ Simultaneous cylinder model fit (park) """ |
---|
110 | |
---|
111 | result1= self._fit('park') |
---|
112 | self.assert_(result1) |
---|
113 | self.assertTrue(len(result1.pvec)>=0 ) |
---|
114 | self.assertTrue(len(result1.stderr)>= 0) |
---|
115 | |
---|
116 | for par in result1.parameters: |
---|
117 | if par.name=='C1.length': |
---|
118 | print par.name, par.value |
---|
119 | self.assertTrue( math.fabs(par.value-400.0)/3.0 < par.stderr ) |
---|
120 | elif par.name=='C1.radius': |
---|
121 | print par.name, par.value |
---|
122 | self.assertTrue( math.fabs(par.value-20.0)/3.0 < par.stderr ) |
---|
123 | elif par.name=='C2.radius': |
---|
124 | print par.name, par.value |
---|
125 | self.assertTrue( math.fabs(par.value-40.0)/3.0 < par.stderr ) |
---|
126 | elif par.name=='C1.scale': |
---|
127 | print par.name, par.value |
---|
128 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
129 | elif par.name=='C2.scale': |
---|
130 | print par.name, par.value |
---|
131 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
132 | |
---|
133 | |
---|
134 | if __name__ == '__main__': |
---|
135 | unittest.main() |
---|
136 | |
---|