Changeset 2eaaf1a in sasview for park_integration/test
- Timestamp:
- Oct 19, 2009 4:57:35 PM (15 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- eb575b0
- Parents:
- b72b595
- Location:
- park_integration/test
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/test/test_fit_cylinder.py
r1ea3488 r2eaaf1a 3 3 """ 4 4 import unittest 5 #from sans.guitools.plottables import Theory1D 6 #from sans.guitools.plottables import Data1D 5 7 6 from danse.common.plottools.plottables import Data1D,Theory1D 8 7 from sans.fit.AbstractFitEngine import Model,FitData1D … … 11 10 from DataLoader.loader import Loader 12 11 13 class testFitModule(unittest.TestCase): 14 """ test fitting """ 15 12 class TestSingleFit(unittest.TestCase): 13 """ test single fitting """ 14 def setUp(self): 15 """ initialize data""" 16 out = Loader().load("cyl_400_20.txt") 17 #Create data that fitting engine understands 18 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) 19 self.data = FitData1D(data1) 20 # Create model that fitting engine understands 21 from sans.models.CylinderModel import CylinderModel 22 model1 = CylinderModel() 23 model1.setParam("scale", 1.0) 24 model1.setParam("radius",18) 25 model1.setParam("length", 397) 26 model1.setParam("contrast",3e-006 ) 27 model1.setParam("background", 0.0) 28 29 self.model = Model(model1) 30 31 self.pars1 =['length','radius','scale'] 32 33 def _fit(self, name="scipy"): 34 """ return fit result """ 35 fitter = Fit(name) 36 fitter.set_data(self.data,1) 37 38 fitter.set_model(self.model,1,self.pars1) 39 fitter.select_problem_for_fit(Uid=1,value=1) 40 return fitter.fit() 41 42 16 43 def test_scipy(self): 17 44 """ Simple cylinder model fit (scipy) """ 18 45 19 out=Loader().load("cyl_400_20.txt") 20 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 21 22 fitter = Fit('scipy') 23 # Receives the type of model for the fitting 24 from sans.models.CylinderModel import CylinderModel 25 model1 = CylinderModel() 26 model1.setParam('contrast', 1) 27 #data = Data(sans_data=data1) 28 data = FitData1D(data1) 29 model = Model(model1) 30 31 pars1 =['length','radius','scale'] 32 fitter.set_data(data,1) 33 model.set(scale=1e-10) 34 fitter.set_model(model,1,pars1) 35 fitter.select_problem_for_fit(Uid=1,value=1) 36 result1 = fitter.fit() 46 result1 = self._fit("scipy") 37 47 38 48 self.assert_(result1) … … 42 52 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 43 53 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 44 self.assertTrue( math.fabs(result1.pvec[2]- 9.0e-12)/3.0 < result1.stderr[2] )54 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 45 55 self.assertTrue( result1.fitness < 1.0 ) 56 46 57 47 58 def test_park(self): 48 59 """ Simple cylinder model fit (park) """ 49 50 out=Loader().load("cyl_400_20.txt") 51 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 52 53 fitter = Fit('park') 54 # Receives the type of model for the fitting 55 from sans.models.CylinderModel import CylinderModel 56 model1 = CylinderModel() 57 58 #data = Data(sans_data=data1) 59 data = FitData1D(data1) 60 model = Model(model1) 61 62 pars1 =['length','radius','scale'] 63 fitter.set_data(data,1) 64 model.set(contrast= 1) 65 model.set(scale=1e-10) 66 fitter.set_model(model,1,pars1) 67 fitter.select_problem_for_fit(Uid=1,value=1) 68 result1 = fitter.fit() 60 result1 = self._fit("park") 69 61 70 62 self.assert_(result1) 71 63 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 72 64 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 73 74 print result1.pvec[0]-400.0, result1.pvec[0] 75 print math.fabs(result1.pvec[0]-400.0)/3.0 65 76 66 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 77 67 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 78 self.assertTrue( math.fabs(result1.pvec[2]- 9.0e-12)/3.0 < result1.stderr[2] )68 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 79 69 self.assertTrue( result1.fitness < 1.0 ) 80 70 81 def test_park2(self): 82 """ Simultaneous cylinder model fit (park) """ 71 72 73 class TestSimultaneousFit(unittest.TestCase): 74 """ test simultaneous fitting """ 75 def setUp(self): 76 """ initialize data""" 83 77 84 78 out=Loader().load("cyl_400_20.txt") 85 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 79 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) 80 self.data1 = FitData1D(data1) 86 81 87 82 out2=Loader().load("cyl_400_40.txt") 88 data2 = Data1D(x=out2.x, y=out2.y, dx=out2.dx, dy=out2. y)89 90 fitter = Fit('park')83 data2 = Data1D(x=out2.x, y=out2.y, dx=out2.dx, dy=out2.dy) 84 self.data2 = FitData1D(data2) 85 91 86 # Receives the type of model for the fitting 92 87 from sans.models.CylinderModel import CylinderModel 93 88 cyl1 = CylinderModel() 94 89 cyl1.name = "C1" 95 96 #data1 = Data(sans_data=data1) 97 data1 = FitData1D(data1) 98 model1 = Model(cyl1) 99 model1.set(contrast=1) 100 model1.set(scale= 1e-10) 101 fitter.set_data(data1,1) 102 fitter.set_model(model1, 1, ['length','radius','scale']) 90 self.model1 = Model(cyl1) 91 self.model1.set(scale= 1.0) 92 self.model1.set(radius=18) 93 self.model1.set(length=396) 94 self.model1.set(contrast=3e-006 ) 95 self.model1.set(background=0.0) 103 96 104 97 cyl2 = CylinderModel() 105 98 cyl2.name = "C2" 99 self.model2 = Model(cyl2) 100 self.model2.set(scale= 1.0) 101 self.model2.set(radius=37) 102 self.model2.set(length='C1.length') 103 self.model2.set(contrast=3e-006 ) 104 self.model2.set(background=0.0) 105 106 def _fit(self, name="park"): 107 """ return fit result """ 108 fitter = Fit(name) 109 fitter.set_data(self.data1,1) 110 fitter.set_model(self.model1, 1, ['length','radius','scale']) 106 111 107 #data2 = Data(sans_data=data2) 108 data2 = FitData1D(data2) 109 # This is wrong. We should not store string as 110 # parameter values 111 # Why not inherit our AbstracFitEngine.Model from Park.Model? 112 113 #cyl2.setParam('length', 'C1.length') 114 #print "read back:", cyl2.getParam('length') 115 116 model2 = Model(cyl2) 117 model2.set(length='C1.length') 118 model2.set(contrast=1) 119 model2.set(scale= 1e-10) 120 fitter.set_data(data2,2) 121 fitter.set_model(model2, 2, ['radius','scale']) 112 fitter.set_data(self.data2,2) 113 fitter.set_model(self.model2, 2, ['radius','scale']) 122 114 fitter.select_problem_for_fit(Uid=1,value=1) 123 115 fitter.select_problem_for_fit(Uid=2,value=1) 124 result1 = fitter.fit() 116 return fitter.fit() 117 118 119 def test_park2(self): 120 """ Simultaneous cylinder model fit (park) """ 125 121 122 result1= self._fit('park') 126 123 self.assert_(result1) 127 self.assertTrue(len(result1.pvec)> 0 or len(result1.pvec)==0)128 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0)129 124 self.assertTrue(len(result1.pvec)>=0 ) 125 self.assertTrue(len(result1.stderr)>= 0) 126 130 127 for par in result1.parameters: 131 128 if par.name=='C1.length': … … 140 137 elif par.name=='C1.scale': 141 138 print par.name, par.value 142 self.assertTrue( math.fabs(par.value- 9.0e-12)/3.0 < par.stderr )139 self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) 143 140 elif par.name=='C2.scale': 144 141 print par.name, par.value 145 self.assertTrue( math.fabs(par.value- 9.0e-12)/3.0 < par.stderr )142 self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) 146 143 147 144 145 if __name__ == '__main__': 146 unittest.main() 148 147 -
park_integration/test/test_fit_line.py
r1ea3488 r2eaaf1a 1 1 """ 2 2 Unit tests for fitting module 3 @author Gervaise Alina 3 4 """ 4 5 import unittest 5 #from sans.guitools.plottables import Theory1D 6 #from sans.guitools.plottables import Data1D 6 7 7 from danse.common.plottools.plottables import Data1D,Theory1D 8 8 from sans.fit.AbstractFitEngine import Model,FitData1D 9 9 import math 10 10 class testFitModule(unittest.TestCase): 11 def smalltest(self):12 13 import numpy14 x=[1,22,3]15 y=[5,6,7,8]16 array= numpy.zeros(len(x), len(y))17 18 11 """ test fitting """ 12 19 13 def test1(self): 20 14 """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """ … … 48 42 self.assert_(result1) 49 43 50 self.assertTrue( ( math.fabs(result1.pvec[0]-4)/3 == result1.stderr[0] ) or 51 ( math.fabs(result1.pvec[0]-4)/3 < result1.stderr[0]) ) 52 53 self.assertTrue( ( math.fabs(result1.pvec[1]-2.5)/3 == result1.stderr[1]) or 54 ( math.fabs(result1.pvec[1]-2.5)/3 < result1.stderr[1] ) ) 55 self.assertTrue( result1.fitness/49 < 2 ) 44 self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] ) 45 self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 46 self.assertTrue( result1.fitness/len(data.x) < 2 ) 56 47 57 48 #fit with park test … … 63 54 64 55 self.assert_(result2) 65 66 self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or 67 ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) 68 69 self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or 70 ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) 71 self.assertTrue(result2.fitness/49 < 2) 56 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 57 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 58 self.assertTrue( result2.fitness/len(data.x) < 2) 72 59 # compare fit result result for scipy and park 73 60 self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) … … 75 62 self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 76 63 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 77 self.assertAlmostEquals( result1.fitness,result2.fitness ) 64 self.assertAlmostEquals( result1.fitness, 65 result2.fitness/len(data.x),1 ) 66 78 67 79 68 def test2(self): … … 133 122 134 123 self.assert_(result2) 135 self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or 136 ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) 137 138 self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or 139 ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) 140 self.assertTrue(result2.fitness/49 < 2) 124 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 125 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 126 self.assertTrue( result2.fitness/len(data2.x) < 2) 141 127 142 128 … … 158 144 model11 = LineModel() 159 145 model11.name= "line" 160 146 model11.setParam("A", 1.0) 147 model11.setParam("B",1.0) 148 161 149 model22 = Constant() 162 150 model22.name= "cst" 151 model22.setParam("value", 1.0) 152 163 153 # Constrain the constant value to be equal to parameter B (the real value is 2.5) 164 165 #data1 = Data(sans_data=data11 ) 166 #data2 = Data(sans_data=data22 ) 154 167 155 data11.smearer= None 168 156 data22.smearer= None … … 187 175 result2 = fitter.fit() 188 176 self.assert_(result2) 189 self.assertTrue( ( math.fabs(result2.pvec[0]-4.0)/3. < result2.stderr[0]) ) 190 191 self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3. < result2.stderr[1]) ) 192 self.assertTrue(result2.fitness/49 < 2) 177 self.assertTrue( math.fabs(result2.pvec[0]-4.0)/3. <= result2.stderr[0]) 178 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3. <= result2.stderr[1]) 179 self.assertTrue( result2.fitness/len(data2.x) < 2) 193 180 194 181 … … 209 196 model1 = LineModel() 210 197 model1.name= "M1" 211 212 data11.smearer=None 213 data22.smearer=None 198 model1.setParam("A", 1.0) 199 model1.setParam("B",1.0) 200 model = Model(model1) 201 202 data11.smearer= None 203 data22.smearer= None 214 204 data1 = FitData1D(data11 ) 215 205 data2 = FitData1D(data22 ) 216 model = Model(model1)217 206 218 207 #fit with scipy test … … 226 215 self.assert_(result1) 227 216 228 self.assertTrue( ( math.fabs(result1.pvec[0]-4)/3 == result1.stderr[0] ) or 229 ( math.fabs(result1.pvec[0]-4)/3 < result1.stderr[0]) ) 230 231 self.assertTrue( ( math.fabs(result1.pvec[1]-2.5)/3 == result1.stderr[1]) or 232 ( math.fabs(result1.pvec[1]-2.5)/3 < result1.stderr[1] ) ) 233 self.assertTrue( result1.fitness/49 < 2 ) 234 217 self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] ) 218 self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 219 self.assertTrue( result1.fitness/len(data1.x) < 2 ) 235 220 236 221 #fit with park test … … 243 228 244 229 self.assert_(result2) 245 self.assertTrue( ( math.fabs(result2.pvec[0]-4)/3 == result2.stderr[0] ) or 246 ( math.fabs(result2.pvec[0]-4)/3 < result2.stderr[0]) ) 247 248 self.assertTrue( ( math.fabs(result2.pvec[1]-2.5)/3 == result2.stderr[1] ) or 249 ( math.fabs(result2.pvec[1]-2.5)/3 < result2.stderr[1]) ) 250 self.assertTrue(result2.fitness/49 < 2) 230 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 231 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 232 self.assertTrue( result2.fitness/len(data1.x) < 2) 251 233 # compare fit result result for scipy and park 252 234 self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) … … 254 236 self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 255 237 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 256 self.assertAlmostEquals( result1.fitness,result2.fitness ) 238 #self.assertAlmostEquals( result1.fitness,result2.fitness ) 239 self.assertTrue( result1.fitness/len(data1.x) < 2 ) 240 self.assertTrue( result2.fitness/len(data2.x) < 2 ) 241 242 257 243 244 -
park_integration/test/test_park_scipy.py
r1ea3488 r2eaaf1a 17 17 """ Simple cylinder model fit (scipy) """ 18 18 19 out=Loader().load("cyl_testdata1.txt") 20 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.y) 19 out=Loader().load("cyl_400_20.txt") 20 data1 = Data1D(x=out.x, y=out.y, dx=out.dx, dy=out.dy) 21 data = FitData1D(data1) 21 22 22 fitter = Fit('scipy')23 23 # Receives the type of model for the fitting 24 24 from sans.models.MultiplicationModel import MultiplicationModel … … 26 26 from sans.models.SquareWellStructure import SquareWellStructure 27 27 model1 = MultiplicationModel(CylinderModel(),SquareWellStructure()) 28 model1.setParam('contrast', 1) 29 #data = Data(sans_data=data1) 30 data = FitData1D(data1) 28 model1.setParam('background', 0.0) 29 model1.setParam('contrast', 3e-006) 30 model1.setParam('length', 600) 31 model1.setParam('radius', 20) 32 model1.setParam('scale', 10) 33 model1.setParam('volfraction', 0.04) 34 model1.setParam('welldepth', 1.5) 35 model1.setParam('wellwidth', 1.2) 36 31 37 model = Model(model1) 32 38 33 39 pars1 =['length','radius','scale'] 40 fitter = Fit('scipy') 34 41 fitter.set_data(data,1) 35 model.set(scale=1e-10)36 42 fitter.set_model(model,1,pars1) 37 43 fitter.select_problem_for_fit(Uid=1,value=1) 38 44 result1 = fitter.fit() 39 print "result ",result1.fitness,result1.pvec,result1.stderr45 40 46 self.assert_(result1) 41 self.assertTrue(len(result1.pvec)> 0 or len(result1.pvec)==0 )42 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0)47 self.assertTrue(len(result1.pvec)>=0 ) 48 self.assertTrue(len(result1.stderr)>= 0) 43 49 44 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 45 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 46 self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) 47 self.assertTrue( result1.fitness < 1.0 ) 50 self.assertTrue( math.fabs(result1.pvec[0]-605)/3.0 <= result1.stderr[0] ) 51 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 <= result1.stderr[1] ) 52 self.assertTrue( math.fabs(result1.pvec[2]-1)/3.0 <= result1.stderr[2] ) 53 54 self.assertTrue( result1.fitness/len(data.x) < 1.0 ) 48 55 49 56
Note: See TracChangeset
for help on using the changeset viewer.