Changeset cf3b781 in sasview
- Timestamp:
- Jul 7, 2008 6:37:27 PM (16 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:
- 1b0b3ca
- Parents:
- 672c44da
- Location:
- park_integration
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/Fitting.py
r4408fb0 rcf3b781 42 42 """ return list of data""" 43 43 44 def set_param(self,model, pars):44 def set_param(self,model,name, pars): 45 45 """ Recieve a dictionary of parameter and save it """ 46 46 47 def add_constraint(self, constraint): 48 """ User specify contraint to fit """ 47 def remove_data(self,Uid,data=None): 48 """ remove one or all data""" 49 50 def remove_model(self,Uid): 51 """ remove model """ 49 52 50 def get_constraint(self):51 """ return the contraint value """52 53 def set_constraint(self,constraint):54 """55 receive a string as a constraint56 @param constraint: a string used to constraint some parameters to get a57 specific value58 53 """ 59 if __name__ == "__main__":60 load= Load()61 # test scipy62 """test fit one data set one model with scipy """63 #load data64 load.set_filename("testdata_line.txt")65 load.set_values()66 data1 = Data1D(x=[], y=[], dx=None,dy=None)67 data1.name = "data1"68 load.load_data(data1)69 #choose a model70 from sans.guitools.LineModel import LineModel71 model = LineModel()72 #Create a Fit engine73 fitter =Fit()74 fitter.fit_engine('scipy')75 engine = fitter.returnEngine()76 77 #set the model78 engine.set_model(model,1)79 engine.set_data(data1,1)80 81 print"fit only one data SCIPY:",engine.fit({'A':2,'B':1},None,None)82 83 84 """ test fit one data set one model with park """85 fitter.fit_engine('scipy')86 engine = fitter.returnEngine()87 #set the model88 engine.set_model(model,1)89 engine.set_data(data1,1)90 91 print"fit only one data PARK:",engine.fit({'A':2,'B':1},None,None)92 93 94 """test fit with 2 data and one model SCIPY:"""95 # reinitialize the fitter96 fitter =Fit()97 #create an engine98 fitter.fit_engine("scipy")99 engine=fitter.returnEngine()100 #set the model for fit101 engine.set_model(model,2 )102 #load 1 st data103 load.set_filename("testdata1.txt")104 load.set_values()105 data2 = Data1D(x=[], y=[], dx=None,dy=None)106 data2.name = "data2"107 load.load_data(data2)108 #load 2nd data109 load.set_filename("testdata2.txt")110 load.set_values()111 data3 = Data1D(x=[], y=[], dx=None,dy=None)112 data3.name = "data2"113 load.load_data(data3)114 115 #set data in the engine116 engine.set_data(data2,2)117 engine.set_data(data3,2)118 print"fit two data SCIPY:",engine.fit({'A':2,'B':1},None,None)119 120 """ test fit with 2 data and one model PARK:"""121 fitter.fit_engine("park")122 engine=fitter.returnEngine()123 #set the model for fit124 engine.set_model(model,2 )125 #load 1 st data126 load.set_filename("testdata1.txt")127 load.set_values()128 data2 = Data1D(x=[], y=[], dx=None,dy=None)129 data2.name = "data2"130 load.load_data(data2)131 #load 2nd data132 load.set_filename("testdata2.txt")133 load.set_values()134 data3 = Data1D(x=[], y=[], dx=None,dy=None)135 data3.name = "data2"136 load.load_data(data3)137 138 #set data in the engine139 engine.set_data(data2,2)140 engine.set_data(data3,2)141 print"fit two data PARK:",engine.fit({'A':2,'B':1},None,None)142 143 -
park_integration/ParkFitting.py
r9e85792 rcf3b781 7 7 from park import fit,fitresult 8 8 from park import assembly 9 from park.fitmc import FitSimplex, FitMC 9 10 10 11 from sans.guitools.plottables import Data1D … … 67 68 x,y,dy = [numpy.asarray(v) for v in (self.x,self.y,self.dy)] 68 69 if self.qmin==None and self.qmax==None: 70 self.fx = fn(x) 69 71 return (y - fn(x))/dy 70 72 71 73 else: 74 self.fx = fn(x[idx]) 72 75 idx = x>=self.qmin & x <= self.qmax 73 76 return (y[idx] - fn(x[idx]))/dy[idx] … … 115 118 if data in self.dList: 116 119 self.dList.remove(data) 120 def remove_model(self): 121 """ Remove model """ 122 model=None 123 def remove_datalist(self): 124 self.dList=[] 117 125 118 126 class ParkFit: … … 160 168 mylist.append(couple) 161 169 #print mylist 162 return mylist170 self.problem = park.Assembly(mylist) 163 171 #return model,data 164 172 … … 167 175 Do the fit 168 176 """ 169 177 178 self.createProblem(pars) 170 179 print "starting ParkFit.fit()" 171 modelList=self.createProblem()172 problem = park.Assembly(modelList)173 pars= problem.fit_parameters()180 #problem[0].model.parameterset['A'].set([1,5]) 181 #problem[0].model.parameterset['B'].set([1,5]) 182 pars=self.problem.fit_parameters() 174 183 print "About to call eval",pars 175 184 print "initial",[p.value for p in pars] 176 problem.eval()185 self.problem.eval() 177 186 #print "M2.B",problem.parameterset['M2.B'].expression,problem.parameterset['M2.B'].value 178 187 #print "problem :",problem[0].parameterset,problem[0].parameterset.fitted … … 180 189 #problem[0].parameterset['A'].set([0,1000]) 181 190 #print "problem :",problem[0].parameterset,problem[0].parameterset.fitted 182 fit.fit(problem, handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) 183 191 192 localfit = FitSimplex() 193 localfit.ftol = 1e-8 194 fitter = FitMC(localfit=localfit) 195 196 result = fit.fit(self.problem, 197 fitter=fitter, 198 handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) 199 pvec = result.pvec 200 cov = self.problem.cov(pvec) 201 return result.fitness,pvec,numpy.sqrt(numpy.diag(cov)) 202 184 203 185 204 def set_model(self,model,Uid): … … 219 238 return parameters 220 239 221 def add_constraint(self, constraint):222 """ User specify contraint to fit"""223 self.constraint = str(constraint)224 225 def get_constraint(self):226 """ return the contraint value """227 return self.constraint228 229 def set_constraint(self,constraint):230 """231 receive a string as a constraint232 @param constraint: a string used to constraint some parameters to get a233 specific value234 """235 self.constraint= constraint240 def remove_data(self,Uid,data=None): 241 """ remove one or all data""" 242 if data==None:# remove all element in data list 243 if self.fitArrangeList.has_key(Uid): 244 self.fitArrangeList[Uid].remove_datalist() 245 else: 246 if self.fitArrangeList.has_key(Uid): 247 self.fitArrangeList[Uid].remove_data(data) 248 249 def remove_model(self,Uid): 250 """ remove model """ 251 if self.fitArrangeList.has_key(Uid): 252 self.fitArrangeList[Uid].remove_model() 253 254 236 255 def _concatenateData(self, listdata=[]): 237 256 """ concatenate each fields of all Data contains ins listdata … … 287 306 288 307 289 290 if __name__ == "__main__": 291 load= Load() 292 293 # test fit one data set one model 294 load.set_filename("testdata_line.txt") 295 load.set_values() 296 data1 = Data1D(x=[], y=[], dx=None,dy=None) 297 data1.name = "data1" 298 load.load_data(data1) 299 fitter =ParkFit() 300 301 from sans.guitools.LineModel import LineModel 302 model = LineModel() 303 fitter.set_model(model,1) 304 fitter.set_data(data1,1) 305 306 print"PARK fit result",fitter.fit({'A':2,'B':1},None,None) 308 309 307 310 308 311 309 310 -
park_integration/ScipyFitting.py
r4408fb0 rcf3b781 68 68 listdata = fitproblem.get_data() 69 69 70 parameters = self.set_param(model, pars)70 parameters = self.set_param(model,model.name,pars) 71 71 72 72 # Do the fit with data set (contains one or more data) and one model 73 73 xtemp,ytemp,dytemp=self._concatenateData( listdata) 74 print "dytemp",dytemp 74 75 if qmin==None: 75 76 qmin= min(xtemp) … … 123 124 return self.fitArrangeList[Uid] 124 125 125 def set_param(self,model, pars):126 def set_param(self,model,name, pars): 126 127 """ Recieve a dictionary of parameter and save it """ 127 128 parameters=[] … … 129 130 raise ValueError, "Cannot set parameters for empty model" 130 131 else: 131 #for key ,value in pars:132 model.name=name 132 133 for key, value in pars.iteritems(): 133 134 param = Parameter(model, key, value) … … 164 165 #print mylist 165 166 return mylist 166 167 def remove_data(self,Uid,data=None): 168 """ remove one or all data""" 169 if data==None:# remove all element in data list 170 if self.fitArrangeList.has_key(Uid): 171 self.fitArrangeList[Uid].remove_datalist() 172 else: 173 if self.fitArrangeList.has_key(Uid): 174 self.fitArrangeList[Uid].remove_data(data) 175 176 def remove_model(self,Uid): 177 """ remove model """ 178 if self.fitArrangeList.has_key(Uid): 179 self.fitArrangeList[Uid].remove_model() 167 180 168 181 … … 214 227 if x[j]>qmin and x[j]<qmax: 215 228 residuals.append( ( y[j] - model.runXY(x[j]) ) / err_y[j] ) 216 229 217 230 return residuals 218 231 … … 240 253 return chisqr, out, cov_x 241 254 242 243 if __name__ == "__main__":244 load= Load()245 246 # test fit one data set one model247 load.set_filename("testdata_line.txt")248 load.set_values()249 data1 = Data1D(x=[], y=[], dx=None,dy=None)250 data1.name = "data1"251 load.load_data(data1)252 fitter =ScipyFit()253 from sans.guitools.LineModel import LineModel254 model = LineModel()255 fitter.set_model(model,1)256 fitter.set_data(data1,1)257 258 chisqr, out, cov=fitter.fit({'A':2,'B':1},None,None)259 print "my list of param",fitter.createProblem()260 print"fit only one data",chisqr, out, cov261 print "this model list of param",model.getParamList()262 # test fit with 2 data and one model263 fitter =ScipyFit()264 265 fitter.set_model(model,2 )266 load.set_filename("testdata1.txt")267 load.set_values()268 data2 = Data1D(x=[], y=[], dx=None,dy=None)269 data2.name = "data2"270 271 load.load_data(data2)272 fitter.set_data(data2,2)273 274 load.set_filename("testdata2.txt")275 load.set_values()276 data3 = Data1D(x=[], y=[], dx=None,dy=None)277 data3.name = "data2"278 load.load_data(data3)279 fitter.set_data(data3,2)280 chisqr, out, cov=fitter.fit({'A':2,'B':1},None,None)281 print"fit two data",chisqr, out, cov282 -
park_integration/test/constrainttestpark.py
r9e85792 rcf3b781 40 40 engine.set_data(data1,1) 41 41 #print "m1 param b", M1.B 42 engine.set_param( model2,"M2", {'A': 1,'B':'2*M1.B'})42 engine.set_param( model2,"M2", {'A':'M1.A','B':'M1.B'}) 43 43 engine.set_model(model2,2) 44 44 engine.set_data(data2,2) … … 46 46 47 47 print engine.fit({'A':2,'B':1},None,None) 48 48 if True: 49 import pylab 50 x1 = engine.problem[0].data.x 51 x2 = engine.problem[1].data.x 52 y1 = engine.problem[0].data.y 53 y2 = engine.problem[1].data.y 54 fx1 = engine.problem[0].data.fx 55 fx2 = engine.problem[1].data.fx 56 pylab.plot(x1,y1,'xb',x1,fx1,'-b',x2,y2,'xr',x2,fx2,'-r') 57 pylab.show() -
park_integration/test/testfitting.py
r9e85792 rcf3b781 73 73 #Do the fit SCIPY 74 74 engine.set_data(data1,1) 75 engine.set_param( model,"M1", {'A':2,'B':4}) 75 76 engine.set_model(model,1) 76 77 … … 87 88 #Do the fit 88 89 engine.set_data(data1,1) 90 engine.set_param( model1,"M1", {'A':2,'B':4}) 89 91 engine.set_model(model,1) 90 92 91 93 engine.fit({'A':2,'B':1},None,None) 92 """ 93 94 self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) 95 self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) 96 self.assert_(chisqr2/len(data1.x) < 2) 97 98 self.assertEqual(out1[1], out2[1]) 99 self.assertEquals(out1[0], out2[0]) 100 self.assertEquals(cov1[0][0], cov2[0][0]) 101 self.assertEquals(cov1[1][1], cov2[1][1]) 102 self.assertEquals(chisqr1, chisqr2) 103 """ 94 95 self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) 96 self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) 97 self.assert_(chisqr2/len(data1.x) < 2) 98 99 self.assertEqual(out1[1], out2[1]) 100 self.assertEquals(out1[0], out2[0]) 101 self.assertEquals(cov1[0][0], cov2[0][0]) 102 self.assertEquals(cov1[1][1], cov2[1][1]) 103 self.assertEquals(chisqr1, chisqr2) 104 104 105 def testfit_2Data_1Model(self): 105 106 """ test fitting for two set of data and one model""" … … 128 129 engine = fitter.returnEngine() 129 130 #Do the fit 130 engine.set_model(model,1) 131 engine.set_param( model,"M1", {'A':2,'B':4}) 132 engine.set_model(model,1) 133 131 134 engine.set_data(data1,1) 132 135 engine.set_data(data2,1) … … 150 153 engine.set_data(data2,1) 151 154 engine.fit({'A':2,'B':1},None,None) 152 """ 153 chisqr2, out2, cov2= engine.fit({'A':2,'B':1},None,None) 154 155 156 self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) 157 self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) 158 self.assert_(chisqr2/len(data1.x) < 2) 159 self.assert_(chisqr2/len(data2.x) < 2) 160 161 self.assertEqual(out1[0],out2[0]) 162 self.assertEqual(out1[1],out2[1]) 163 self.assertEqual(chisqr1,chisqr2) 164 self.assertEqual(cov1[0][0],cov2[0][0]) 165 self.assertEqual(cov1[1][1],cov2[1][1]) 166 """ 155 156 chisqr2, out2, cov2= engine.fit({'A':2,'B':1},None,None) 157 #print "park",chisqr2, out2, cov2 158 self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) 159 self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) 160 self.assert_(chisqr2/len(data1.x) < 2) 161 self.assert_(chisqr2/len(data2.x) < 2) 162 163 self.assertEqual(out1[0],out2[0]) 164 self.assertEqual(out1[1],out2[1]) 165 self.assertEqual(chisqr1,chisqr2) 166 self.assertEqual(cov1[0][0],cov2[0][0]) 167 self.assertEqual(cov1[1][1],cov2[1][1]) 168 167 169 def test2models2dataonconstraint(self): 168 170 """ test for 2 Models two data one constraint""" … … 187 189 from sans.guitools.LineModel import LineModel 188 190 model1 = LineModel() 189 model1.name='M1'190 191 model2 = LineModel() 191 model2.name='M2'192 192 193 #set engine for scipy 193 fitter.fit_engine('scipy') 194 engine = fitter.returnEngine() 195 #Do the fit 194 """ 195 fitter.fit_engine('scipy') 196 engine = fitter.returnEngine() 197 #Do the fit 198 engine.set_param( model1,"M1", {'A':2,'B':4}) 199 engine.set_model(model1,1) 200 engine.set_data(data1,1) 201 engine.set_param( model1,"M2", {'A':2.1,'B':3}) 202 engine.set_model(model2,2) 203 engine.set_data(data2,2) 204 205 try: engine.fit({'A':2,'B':1},None,None) 206 except ValueError,msg: 207 assert str(msg)=="cannot fit more than one model",'Message: <%s>'%(msg) 208 """ 209 #set engine for park 210 fitter= Fit() 211 fitter.fit_engine('park') 212 engine = fitter.returnEngine() 213 #Do the fit 214 engine.set_data(data1,1) 215 engine.set_param(model1,"M1",{'A':2,'B':1}) 196 216 engine.set_model(model1,1) 197 engine.set_data(data1,1) 217 218 engine.set_param(model2,"M2",{'A':3,'B':'M1.B'}) 198 219 engine.set_model(model2,2) 199 220 engine.set_data(data2,2) 200 201 try: engine.fit({'A':2,'B':1},None,None) 202 except ValueError,msg: 203 assert str(msg)=="cannot fit more than one model",'Message: <%s>'%(msg) 204 205 #set engine for park 206 fitter= Fit() 207 fitter.fit_engine('park') 208 engine = fitter.returnEngine() 209 #Do the fit 210 engine.set_data(data1,1) 211 engine.set_param(model1,{'A':2,'B':1}) 212 engine.set_model(model1,1) 213 214 engine.set_param(model2,{'A':3,'B':'5*M1.B'}) 215 engine.set_model(model2,2) 216 engine.set_data(data2,2) 217 engine.fit({'A':2,'B':1},None,None) 218 221 chisqr2, out2, cov2= engine.fit({'A':2,'B':1},None,None) 222 223 224 self.assert_(math.fabs(out2[1]-2.5)/math.sqrt(cov2[1][1]) < 2) 225 self.assert_(math.fabs(out2[0]-4.0)/math.sqrt(cov2[0][0]) < 2) 226 self.assert_(chisqr2/len(data1.x) < 2) 227 self.assert_(chisqr2/len(data2.x) < 2) 228 -
park_integration/test/testpark.py
r9e85792 rcf3b781 40 40 engine.set_data(data1,1) 41 41 42 import numpy 43 #print engine.fit({'A':2,'B':1},None,None) 44 #engine.remove_data(2,data2) 45 #engine.remove_model(2) 46 42 47 engine.set_param( model2,"M2", {'A':2.5,'B':4}) 43 48 engine.set_model(model2,2) 44 49 engine.set_data(data2,2) 45 46 47 50 print engine.fit({'A':2,'B':1},None,None) 48 51 52 if True: 53 import pylab 54 x1 = engine.problem[0].data.x 55 x2 = engine.problem[1].data.x 56 y1 = engine.problem[0].data.y 57 y2 = engine.problem[1].data.y 58 fx1 = engine.problem[0].data.fx 59 fx2 = engine.problem[1].data.fx 60 pylab.plot(x1,y1,'xb',x1,fx1,'-b',x2,y2,'xr',x2,fx2,'-r') 61 pylab.show() 62 if False: 63 print "current" 64 print engine.problem.chisq 65 print engine.problem.residuals 66 print "M1.y",engine.problem[0].data.y 67 print "M1.fx",engine.problem[0].data.fx 68 print "M1 delta",numpy.asarray(engine.problem[0].data.y)-engine.problem[0].data.fx 69 print "M2.y",engine.problem[0].data.y 70 print "M2.fx",engine.problem[0].data.fx 71 print "M2 delta",numpy.asarray(engine.problem[1].data.y)-engine.problem[1].data.fx 72 print "target" 73 engine.problem(numpy.array([4,2.5,4,2.5])) 74 print engine.problem.chisq 75 print engine.problem.residuals 76 print "M1.y",engine.problem[0].data.y 77 print "M1.fx",engine.problem[0].data.fx 78 print "M1 delta",numpy.asarray(engine.problem[0].data.y)-engine.problem[0].data.fx 79 print "M2.y",engine.problem[0].data.y 80 print "M2.fx",engine.problem[0].data.fx 81 print "M2 delta",numpy.asarray(engine.problem[1].data.y)-engine.problem[1].data.fx 82
Note: See TracChangeset
for help on using the changeset viewer.