[ca6d914] | 1 | """ |
---|
| 2 | Unit tests for fitting module |
---|
[eb575b0] | 3 | @author G.alina |
---|
[ca6d914] | 4 | """ |
---|
| 5 | import unittest |
---|
[2eaaf1a] | 6 | |
---|
[1e3169c] | 7 | from sans.fit.AbstractFitEngine import Model |
---|
[ca6d914] | 8 | import math |
---|
[822f97e] | 9 | from sans.fit.Fitting import Fit |
---|
| 10 | from DataLoader.loader import Loader |
---|
| 11 | |
---|
[2eaaf1a] | 12 | class TestSingleFit(unittest.TestCase): |
---|
| 13 | """ test single fitting """ |
---|
| 14 | def setUp(self): |
---|
| 15 | """ initialize data""" |
---|
[1e3169c] | 16 | self.data = Loader().load("cyl_400_20.txt") |
---|
[2eaaf1a] | 17 | # Create model that fitting engine understands |
---|
[ca6d914] | 18 | from sans.models.CylinderModel import CylinderModel |
---|
| 19 | model1 = CylinderModel() |
---|
[2eaaf1a] | 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) |
---|
[8bbab51] | 36 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[2eaaf1a] | 37 | return fitter.fit() |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | def test_scipy(self): |
---|
| 41 | """ Simple cylinder model fit (scipy) """ |
---|
| 42 | |
---|
| 43 | result1 = self._fit("scipy") |
---|
[822f97e] | 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] ) |
---|
[2eaaf1a] | 51 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
[822f97e] | 52 | self.assertTrue( result1.fitness < 1.0 ) |
---|
| 53 | |
---|
[2eaaf1a] | 54 | |
---|
[822f97e] | 55 | def test_park(self): |
---|
| 56 | """ Simple cylinder model fit (park) """ |
---|
[2eaaf1a] | 57 | result1 = self._fit("park") |
---|
[822f97e] | 58 | |
---|
[ca6d914] | 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) |
---|
[2eaaf1a] | 62 | |
---|
[822f97e] | 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] ) |
---|
[2eaaf1a] | 65 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
[822f97e] | 66 | self.assertTrue( result1.fitness < 1.0 ) |
---|
| 67 | |
---|
[2eaaf1a] | 68 | |
---|
| 69 | |
---|
| 70 | class TestSimultaneousFit(unittest.TestCase): |
---|
| 71 | """ test simultaneous fitting """ |
---|
| 72 | def setUp(self): |
---|
| 73 | """ initialize data""" |
---|
[822f97e] | 74 | |
---|
[1e3169c] | 75 | self.data1=Loader().load("cyl_400_20.txt") |
---|
| 76 | self.data2=Loader().load("cyl_400_40.txt") |
---|
[2eaaf1a] | 77 | |
---|
[822f97e] | 78 | # Receives the type of model for the fitting |
---|
| 79 | from sans.models.CylinderModel import CylinderModel |
---|
| 80 | cyl1 = CylinderModel() |
---|
| 81 | cyl1.name = "C1" |
---|
[2eaaf1a] | 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) |
---|
[822f97e] | 88 | |
---|
| 89 | cyl2 = CylinderModel() |
---|
| 90 | cyl2.name = "C2" |
---|
[2eaaf1a] | 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']) |
---|
[8bbab51] | 106 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 107 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
[2eaaf1a] | 108 | return fitter.fit() |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | def test_park2(self): |
---|
| 112 | """ Simultaneous cylinder model fit (park) """ |
---|
[822f97e] | 113 | |
---|
[2eaaf1a] | 114 | result1= self._fit('park') |
---|
[822f97e] | 115 | self.assert_(result1) |
---|
[2eaaf1a] | 116 | self.assertTrue(len(result1.pvec)>=0 ) |
---|
| 117 | self.assertTrue(len(result1.stderr)>= 0) |
---|
| 118 | |
---|
[822f97e] | 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 |
---|
[2eaaf1a] | 131 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
[822f97e] | 132 | elif par.name=='C2.scale': |
---|
| 133 | print par.name, par.value |
---|
[2eaaf1a] | 134 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
[822f97e] | 135 | |
---|
[2eaaf1a] | 136 | |
---|
| 137 | if __name__ == '__main__': |
---|
| 138 | unittest.main() |
---|
[ca6d914] | 139 | |
---|