[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 |
---|
[fd6b789] | 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 |
---|
[2eaaf1a] | 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) |
---|
[8bbab51] | 33 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
[2eaaf1a] | 34 | return fitter.fit() |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | def test_scipy(self): |
---|
| 38 | """ Simple cylinder model fit (scipy) """ |
---|
| 39 | |
---|
| 40 | result1 = self._fit("scipy") |
---|
[822f97e] | 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] ) |
---|
[2eaaf1a] | 48 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
[822f97e] | 49 | self.assertTrue( result1.fitness < 1.0 ) |
---|
| 50 | |
---|
[2eaaf1a] | 51 | |
---|
[822f97e] | 52 | def test_park(self): |
---|
| 53 | """ Simple cylinder model fit (park) """ |
---|
[2eaaf1a] | 54 | result1 = self._fit("park") |
---|
[822f97e] | 55 | |
---|
[ca6d914] | 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) |
---|
[2eaaf1a] | 59 | |
---|
[822f97e] | 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] ) |
---|
[2eaaf1a] | 62 | self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) |
---|
[822f97e] | 63 | self.assertTrue( result1.fitness < 1.0 ) |
---|
| 64 | |
---|
[2eaaf1a] | 65 | |
---|
| 66 | |
---|
| 67 | class TestSimultaneousFit(unittest.TestCase): |
---|
| 68 | """ test simultaneous fitting """ |
---|
| 69 | def setUp(self): |
---|
| 70 | """ initialize data""" |
---|
[822f97e] | 71 | |
---|
[1e3169c] | 72 | self.data1=Loader().load("cyl_400_20.txt") |
---|
| 73 | self.data2=Loader().load("cyl_400_40.txt") |
---|
[2eaaf1a] | 74 | |
---|
[822f97e] | 75 | # Receives the type of model for the fitting |
---|
| 76 | from sans.models.CylinderModel import CylinderModel |
---|
| 77 | cyl1 = CylinderModel() |
---|
| 78 | cyl1.name = "C1" |
---|
[2eaaf1a] | 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) |
---|
[822f97e] | 85 | |
---|
| 86 | cyl2 = CylinderModel() |
---|
| 87 | cyl2.name = "C2" |
---|
[2eaaf1a] | 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']) |
---|
[8bbab51] | 103 | fitter.select_problem_for_fit(Uid=1,value=1) |
---|
| 104 | fitter.select_problem_for_fit(Uid=2,value=1) |
---|
[2eaaf1a] | 105 | return fitter.fit() |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | def test_park2(self): |
---|
| 109 | """ Simultaneous cylinder model fit (park) """ |
---|
[822f97e] | 110 | |
---|
[2eaaf1a] | 111 | result1= self._fit('park') |
---|
[822f97e] | 112 | self.assert_(result1) |
---|
[2eaaf1a] | 113 | self.assertTrue(len(result1.pvec)>=0 ) |
---|
| 114 | self.assertTrue(len(result1.stderr)>= 0) |
---|
| 115 | |
---|
[822f97e] | 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 |
---|
[2eaaf1a] | 128 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
[822f97e] | 129 | elif par.name=='C2.scale': |
---|
| 130 | print par.name, par.value |
---|
[2eaaf1a] | 131 | self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) |
---|
[822f97e] | 132 | |
---|
[2eaaf1a] | 133 | |
---|
| 134 | if __name__ == '__main__': |
---|
| 135 | unittest.main() |
---|
[ca6d914] | 136 | |
---|