- Timestamp:
- Apr 10, 2014 8:05:28 PM (11 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:
- 90f49a8
- Parents:
- 6fe5100
- Location:
- test
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
test/park_integration/test/utest_fit_line.py
r6fe5100 r95d58d3 6 6 import math 7 7 8 from sans.fit.AbstractFitEngine import Model 8 from sans.fit.AbstractFitEngine import Model, FitHandler 9 9 from sans.dataloader.loader import Loader 10 10 from sans.fit.Fitting import Fit … … 14 14 class testFitModule(unittest.TestCase): 15 15 """ test fitting """ 16 17 def test 1(self):18 """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """19 #load data 16 17 def test_bad_pars(self): 18 fitter = Fit('bumps') 19 20 20 data = Loader().load("testdata_line.txt") 21 21 data.name = data.filename 22 #Importing the Fit module 23 from bumps import fitters 24 fitters.FIT_DEFAULT = 'dream' 25 print fitters.FIT_OPTIONS['dream'].__dict__ 26 fitter = Fit('bumps') 22 fitter.set_data(data,1) 23 24 model1 = LineModel() 25 model1.name = "M1" 26 model = Model(model1, data) 27 pars1= ['param1','param2'] 28 try: 29 fitter.set_model(model,1,pars1) 30 except ValueError,exc: 31 #print "ValueError was correctly raised: "+str(msg) 32 assert str(exc).startswith('wrong parameter') 33 else: 34 raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 35 36 def fit_single(self, fitter_name, isdream=False): 37 fitter = Fit(fitter_name) 38 39 data = Loader().load("testdata_line.txt") 40 data.name = data.filename 41 fitter.set_data(data,1) 42 27 43 # Receives the type of model for the fitting 28 44 model1 = LineModel() … … 30 46 model = Model(model1,data) 31 47 #fit with scipy test 32 33 pars1= ['param1','param2'] 34 fitter.set_data(data,1) 35 try:fitter.set_model(model,1,pars1) 36 except ValueError,exc: 37 #print "ValueError was correctly raised: "+str(msg) 38 assert str(exc).startswith('wrong parameter') 39 else: raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 48 40 49 pars1= ['A','B'] 41 50 fitter.set_model(model,1,pars1) 42 51 fitter.select_problem_for_fit(id=1,value=1) 43 result1, = fitter.fit() 44 45 self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] ) 46 self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 47 self.assertTrue( result1.fitness/len(data.x) < 2 ) 48 49 return 50 #fit with park test 51 fitter = Fit('park') 52 fitter.set_data(data,1) 53 fitter.set_model(model,1,pars1) 54 fitter.select_problem_for_fit(id=1,value=1) 55 result2, = fitter.fit() 56 57 self.assert_(result2) 58 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 59 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 60 self.assertTrue( result2.fitness/len(data.x) < 2) 61 # compare fit result result for scipy and park 62 self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) 63 self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) 64 self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 65 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 66 self.assertAlmostEquals( result1.fitness, 67 result2.fitness/len(data.x),1 ) 68 52 result1, = fitter.fit(handler=FitHandler()) 53 54 # The target values were generated from the following statements 55 p,s,fx = result1.pvec, result1.stderr, result1.fitness 56 #print "p0,p1,s0,s1,fx_ = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx) 57 p0,p1,s0,s1,fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189 58 59 if isdream: 60 # Dream is not a minimizer: just check that the fit is within 61 # uncertainty 62 self.assertTrue( abs(p[0]-p0) <= s0 ) 63 self.assertTrue( abs(p[1]-p1) <= s1 ) 64 else: 65 self.assertTrue( abs(p[0]-p0) <= 1e-5 ) 66 self.assertTrue( abs(p[1]-p1) <= 1e-5 ) 67 self.assertTrue( abs(fx-fx_) <= 1e-2 ) 68 69 def fit_bumps(self, alg, **opts): 70 #Importing the Fit module 71 from bumps import fitters 72 fitters.FIT_DEFAULT = alg 73 fitters.FIT_OPTIONS[alg].options.update(opts) 74 fitters.FIT_OPTIONS[alg].options.update(monitors=[]) 75 #print "fitting",alg,opts 76 #kprint "options",fitters.FIT_OPTIONS[alg].__dict__ 77 self.fit_single('bumps', isdream=(alg=='dream')) 78 79 def test_bumps_de(self): 80 self.fit_bumps('de') 81 82 def test_bumps_dream(self): 83 self.fit_bumps('dream', burn=500, steps=100) 84 85 def test_bumps_amoeba(self): 86 self.fit_bumps('amoeba') 87 88 def test_bumps_newton(self): 89 self.fit_bumps('newton') 90 91 def test_scipy(self): 92 #print "fitting scipy" 93 self.fit_single('scipy') 94 95 def test_park(self): 96 #print "fitting park" 97 self.fit_single('park') 98 69 99 70 100 def test2(self): … … 97 127 fitter.select_problem_for_fit(id=2,value=0) 98 128 99 try: result1, = fitter.fit( )129 try: result1, = fitter.fit(handler=FitHandler()) 100 130 except RuntimeError,msg: 101 131 assert str(msg)=="No Assembly scheduled for Scipy fitting." … … 103 133 fitter.select_problem_for_fit(id=1,value=1) 104 134 fitter.select_problem_for_fit(id=2,value=1) 105 try: result1, = fitter.fit( )135 try: result1, = fitter.fit(handler=FitHandler()) 106 136 except RuntimeError,msg: 107 137 assert str(msg)=="Scipy can't fit more than a single fit problem at a time." 108 138 else: raise AssertionError,"No error raised for scipy fitting with more than 2 models" 109 139 110 return111 140 #fit with park test 112 141 fitter = Fit('park') … … 117 146 fitter.select_problem_for_fit(id=1,value=1) 118 147 fitter.select_problem_for_fit(id=2,value=1) 119 R1,R2 = fitter.fit( )148 R1,R2 = fitter.fit(handler=FitHandler()) 120 149 121 150 self.assertTrue( math.fabs(R1.pvec[0]-4)/3 <= R1.stderr[0] ) … … 126 155 def test3(self): 127 156 """ fit 2 data and 2 model with 1 constrainst""" 128 return129 157 #load data 130 158 l = Loader() … … 163 191 fitter.select_problem_for_fit(id=2,value=1) 164 192 165 R1,R2 = fitter.fit( )193 R1,R2 = fitter.fit(handler=FitHandler()) 166 194 self.assertTrue( math.fabs(R1.pvec[0]-4.0)/3. <= R1.stderr[0]) 167 195 self.assertTrue( math.fabs(R1.pvec[1]-2.5)/3. <= R1.stderr[1]) … … 194 222 fitter.select_problem_for_fit(id=1,value=1) 195 223 196 result1, = fitter.fit( )224 result1, = fitter.fit(handler=FitHandler()) 197 225 #print(result1) 198 226 self.assert_(result1) … … 202 230 self.assertTrue( result1.fitness/len(data1.x) < 2 ) 203 231 204 return205 232 #fit with park test 206 233 fitter = Fit('park') … … 209 236 fitter.set_data(data2,1,qmin=1,qmax=10) 210 237 fitter.select_problem_for_fit(id=1,value=1) 211 result2, = fitter.fit( )238 result2, = fitter.fit(handler=FitHandler()) 212 239 213 240 self.assert_(result2) -
test/pr_inversion/test/test_output.txt
r6fe5100 r95d58d3 3 3 #alpha=0.0007 4 4 #chi2=836.797 5 #elapsed=0.000 1680855 #elapsed=0.00041008 6 6 #qmin=None 7 7 #qmax=None
Note: See TracChangeset
for help on using the changeset viewer.