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