Changeset ca6d914 in sasview for park_integration
- Timestamp:
- Sep 16, 2008 4:34:55 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:
- fadea71
- Parents:
- 3b19ac9
- Location:
- park_integration
- Files:
-
- 3 added
- 4 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/AbstractFitEngine.py
r388309d rca6d914 9 9 """ 10 10 def __init__(self, name, model): 11 self._model, self._name = model,name 12 self.set(model.getParam(name)) 11 """ 12 @param name: the name of the model parameter 13 @param model: the sans model to wrap as a park model 14 """ 15 self._model, self._name = model,name 16 #set the value for the parameter of the given name 17 self.set(model.getParam(name)) 13 18 14 def _getvalue(self): return self._model.getParam(self.name) 15 16 def _setvalue(self,value): 19 def _getvalue(self): 20 """ 21 override the _getvalue of park parameter 22 @return value the parameter associates with self.name 23 """ 24 return self._model.getParam(self.name) 25 26 def _setvalue(self,value): 27 """ 28 override the _setvalue pf park parameter 29 @param value: the value to set on a given parameter 30 """ 17 31 self._model.setParam(self.name, value) 18 32 … … 20 34 21 35 def _getrange(self): 36 """ 37 Override _getrange of park parameter 38 return the range of parameter 39 """ 22 40 lo,hi = self._model.details[self.name][1:] 23 41 if lo is None: lo = -numpy.inf … … 26 44 27 45 def _setrange(self,r): 46 """ 47 override _setrange of park parameter 48 @param r: the value of the range to set 49 """ 28 50 self._model.details[self.name][1:] = r 29 51 range = property(_getrange,_setrange) … … 35 57 """ 36 58 def __init__(self, sans_model, **kw): 59 """ 60 @param sans_model: the sans model to wrap using park interface 61 """ 37 62 self.model = sans_model 38 self.name =sans_model.name39 # print "ParkFitting:sans model",self.model63 self.name = sans_model.name 64 #list of parameters names 40 65 self.sansp = sans_model.getParamList() 41 # print "ParkFitting: sans model parameter list",sansp66 #list of park parameter 42 67 self.parkp = [SansParameter(p,sans_model) for p in self.sansp] 43 # print "ParkFitting: park model parameter ",self.parkp68 #list of parameterset 44 69 self.parameterset = park.ParameterSet(sans_model.name,pars=self.parkp) 45 70 self.pars=[] 46 47 #def __call__(self, x, pars=[]): 48 # return self.eval(x) 71 72 49 73 def getParams(self,fitparams): 74 """ 75 return a list of value of paramter to fit 76 @param fitparams: list of paramaters name to fit 77 """ 50 78 list=[] 51 79 self.pars=[] … … 55 83 if element.name ==str(item): 56 84 list.append(element.value) 57 #print "abstractfitengine: getparams",list58 85 return list 59 86 87 60 88 def setParams(self, params): 89 """ 90 Set value for parameters to fit 91 @param params: list of value for parameters to fit 92 """ 61 93 list=[] 62 94 for item in self.parkp: … … 65 97 for i in range(len(params)): 66 98 self.parkp[i].value = params[i] 67 print "abstractfitengine: set-params",list[i],params[i]68 69 99 self.model.setParam(list[i],params[i]) 70 100 101 71 102 def eval(self,x): 72 103 """ 104 override eval method of park model. 105 @param x: the x value used to compute a function 106 """ 73 107 return self.model.runXY(x) 74 108 75 #def set(self, **kw):76 #"""77 #Set the initial value for a set of parameters.78 #E.g., model.set(width=3,center=5)79 #"""80 #print "Abstractfitting : set called"81 # This is a convenience funciton for the user.82 #83 #for k,v in kw.items():84 # self.parameterset[k].set(v)85 109 86 110 class Data(object): 87 111 """ Wrapper class for SANS data """ 88 112 def __init__(self,x=None,y=None,dy=None,dx=None,sans_data=None): 89 113 """ 114 Data can be initital with a data (sans plottable) 115 or with vectors. 116 """ 90 117 if sans_data !=None: 91 118 self.x= sans_data.x … … 105 132 self.qmax=None 106 133 134 107 135 def setFitRange(self,mini=None,maxi=None): 108 136 """ to set the fit range""" 109 137 self.qmin=mini 110 138 self.qmax=maxi 139 140 111 141 def getFitRange(self): 112 return self.qmin, self.qmax 142 """ 143 @return the range of data.x to fit 144 """ 145 return self.qmin, self.qmax 146 147 113 148 def residuals(self, fn): 114 149 """ @param fn: function that return model value … … 117 152 x,y,dy = [numpy.asarray(v) for v in (self.x,self.y,self.dy)] 118 153 if self.qmin==None and self.qmax==None: 119 fx = [fn(v) for v in x]154 fx =numpy.asarray([fn(v) for v in x]) 120 155 return (y - fx)/dy 121 156 else: 122 157 idx = (x>=self.qmin) & (x <= self.qmax) 123 fx = [fn(item)for item in x[idx ]]158 fx = numpy.asarray([fn(item)for item in x[idx ]]) 124 159 return (y[idx] - fx)/dy[idx] 125 160 … … 134 169 135 170 class sansAssembly: 171 """ 172 Sans Assembly class a class wrapper to be call in optimizer.leastsq method 173 """ 136 174 def __init__(self,Model=None , Data=None): 137 self.model = Model 138 self.data = Data 139 self.res=[] 175 """ 176 @param Model: the model wrapper fro sans -model 177 @param Data: the data wrapper for sans data 178 """ 179 self.model = Model 180 self.data = Data 181 self.res=[] 140 182 def chisq(self, params): 141 183 """ … … 149 191 return sum 150 192 def __call__(self,params): 193 """ 194 Compute residuals 195 @param params: value of parameters to fit 196 """ 151 197 self.model.setParams(params) 152 198 self.res= self.data.residuals(self.model.eval) … … 155 201 class FitEngine: 156 202 def __init__(self): 203 """ 204 Base class for scipy and park fit engine 205 """ 206 #List of parameter names to fit 157 207 self.paramList=[] 208 #Dictionnary of fitArrange element (fit problems) 209 self.fitArrangeDict={} 210 158 211 def _concatenateData(self, listdata=[]): 159 212 """ 160 213 _concatenateData method concatenates each fields of all data contains ins listdata. 161 214 @param listdata: list of data 162 163 @return Data: 164 215 @return Data: Data is wrapper class for sans plottable. it is created with all parameters 216 of data concatenanted 165 217 @raise: if listdata is empty will return None 166 218 @raise: if data in listdata don't contain dy field ,will create an error … … 195 247 else: 196 248 raise RuntimeError, "Fit._concatenateData: y-errors missing" 197 #return xtemp, ytemp,dytemp198 249 data= Data(x=xtemp,y=ytemp,dy=dytemp) 199 250 data.setFitRange(self.mini, self.maxi) 200 251 return data 201 def set_model(self,model,name,Uid,pars=[]): 252 253 254 def set_model(self,model,Uid,pars=[]): 255 """ 256 set a model on a given uid in the fit engine. 257 @param model: the model to fit 258 @param Uid :is the key of the fitArrange dictionnary where model is saved as a value 259 @param pars: the list of parameters to fit 260 @note : pars must contains only name of existing model's paramaters 261 """ 202 262 if len(pars) >0: 203 self.paramList = []204 263 if model==None: 205 264 raise ValueError, "AbstractFitEngine: Specify parameters to fit" 206 265 else: 207 model.model.name = name 208 model.name = name 209 self.paramList=pars 266 for item in pars: 267 if item in model.model.getParamList(): 268 self.paramList.append(item) 269 else: 270 raise ValueError,"wrong paramter %s used to set model %s. Choose\ 271 parameter name within %s"%(item, model.model.name,str(model.model.getParamList())) 272 return 210 273 #A fitArrange is already created but contains dList only at Uid 211 if self.fitArrange List.has_key(Uid):212 self.fitArrange List[Uid].set_model(model)274 if self.fitArrangeDict.has_key(Uid): 275 self.fitArrangeDict[Uid].set_model(model) 213 276 else: 214 277 #no fitArrange object has been create with this Uid 215 278 fitproblem = FitArrange() 216 279 fitproblem.set_model(model) 217 self.fitArrange List[Uid] = fitproblem280 self.fitArrangeDict[Uid] = fitproblem 218 281 else: 219 282 raise ValueError, "park_integration:missing parameters" … … 225 288 @param data: data added 226 289 @param Uid: unique key corresponding to a fitArrange object with data 227 290 """ 228 291 if qmin !=None and qmax !=None: 229 292 data.setFitRange(mini=qmin,maxi=qmax) 230 293 #A fitArrange is already created but contains model only at Uid 231 if self.fitArrange List.has_key(Uid):232 self.fitArrange List[Uid].add_data(data)294 if self.fitArrangeDict.has_key(Uid): 295 self.fitArrangeDict[Uid].add_data(data) 233 296 else: 234 297 #no fitArrange object has been create with this Uid 235 298 fitproblem= FitArrange() 236 299 fitproblem.add_data(data) 237 self.fitArrange List[Uid]=fitproblem300 self.fitArrangeDict[Uid]=fitproblem 238 301 239 302 def get_model(self,Uid): … … 243 306 with this Uid 244 307 """ 245 if self.fitArrange List.has_key(Uid):246 return self.fitArrange List[Uid].get_model()308 if self.fitArrangeDict.has_key(Uid): 309 return self.fitArrangeDict[Uid].get_model() 247 310 else: 248 311 return None … … 250 313 def remove_Fit_Problem(self,Uid): 251 314 """remove fitarrange in Uid""" 252 if self.fitArrange List.has_key(Uid):253 del self.fitArrange List[Uid]315 if self.fitArrangeDict.has_key(Uid): 316 del self.fitArrangeDict[Uid] 254 317 255 318 -
park_integration/Fitting.py
r48882d1 rca6d914 46 46 else: 47 47 raise ValueError, "enter the keyword scipy or park" 48 48 49 def returnEngine(self): 49 50 """ @return self._engine""" … … 53 54 """Perform the fit """ 54 55 return self._engine.fit(qmin,qmax) 55 def set_model(self,model,name,Uid,pars=[]): 56 self._engine.set_model(model,name,Uid,pars) 56 57 def set_model(self,model,Uid,pars=[]): 58 self._engine.set_model(model,Uid,pars) 57 59 58 60 def set_data(self,data,Uid,qmin=None, qmax=None): 59 61 self._engine.set_data(data,Uid,qmin,qmax) 62 60 63 def get_model(self,Uid): 61 64 """ return list of data""" … … 65 68 """remove fitarrange in Uid""" 66 69 self._engine.remove_Fit_Problem(Uid) 70 71 -
park_integration/ParkFitting.py
r388309d rca6d914 45 45 with Uid as keys 46 46 """ 47 self.fitArrange List={}47 self.fitArrangeDict={} 48 48 self.paramList=[] 49 49 50 def create Problem(self):50 def createAssembly(self): 51 51 """ 52 52 Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange} … … 54 54 create an assembly self.problem= park.Assembly([(parkmodel,parkdata)]) 55 55 """ 56 print "ParkFitting: In createproblem"57 56 mylist=[] 58 57 listmodel=[] 59 58 i=0 60 for k,value in self.fitArrangeList.iteritems(): 61 #sansmodel=value.get_model() 62 #wrap sans model 63 #parkmodel = Model(sansmodel) 59 for k,value in self.fitArrangeDict.iteritems(): 64 60 parkmodel = value.get_model() 65 #print "ParkFitting: createproblem: just create a model",parkmodel.parameterset66 61 for p in parkmodel.parameterset: 67 #self.param_list.append(p._getname())68 #if p.isfixed():69 #print 'parameters',p.name70 #print "parkfitting: self.paramList",self.paramList71 62 if p.isfixed() and p._getname()in self.paramList: 72 #if p.isfixed():73 63 p.set([-numpy.inf,numpy.inf]) 74 64 i+=1 … … 76 66 parkdata=self._concatenateData(Ldata) 77 67 78 couple=(parkmodel,parkdata) 79 #print "Parkfitting: fitness",couple 80 mylist.append(couple) 81 #print "mylist",mylist 68 fitness=(parkmodel,parkdata) 69 mylist.append(fitness) 70 82 71 self.problem = park.Assembly(mylist) 83 72 … … 99 88 @return result.cov: Covariance matrix 100 89 """ 101 #from numpy.linalg.linalg.LinAlgError import LinAlgError 102 #print "Parkfitting: fit method probably breaking just right before \ 103 #call fit" 104 self.createProblem() 90 91 self.createAssembly() 105 92 pars=self.problem.fit_parameters() 106 93 self.problem.eval() 107 #print "M0.B",self.problem[1].parameterset['B'].value,self.problem[0].parameterset['B'].value108 109 94 localfit = FitSimplex() 110 95 localfit.ftol = 1e-8 111 #localfit.ftol = 1e-6112 96 fitter = FitMC(localfit=localfit) 113 print "ParkFitting: result1",pars114 print "Parkfitting: in fit function fitness resid",self.problem[0].residuals()115 116 97 list=self.problem[0]._parameterset() 117 print "Parkfitting: in fit function fitness paramset",list118 for item in list:119 print "Parkfitting: in fit function fitness",item.name, item.value,item.path,item.range120 98 result = fit.fit(self.problem, 121 99 fitter=fitter, 122 100 handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) 123 #result = fit.fit(self.problem) 124 print "ParkFitting: result",result.fitness,result.pvec,result.cov 101 125 102 if result !=None: 126 #for p in result.parameters:127 # print "fit in park fitting", p.name, p.value,p.stderr128 #return result.fitness,result.pvec,result.cov,result129 103 return result 130 104 else: -
park_integration/ScipyFitting.py
r48882d1 rca6d914 35 35 Use a sans model 36 36 37 Add data with a dictionnary of FitArrange List where Uid is a key and data37 Add data with a dictionnary of FitArrangeDict where Uid is a key and data 38 38 is saved in FitArrange object. 39 39 engine.set_data(data,Uid) … … 45 45 engine.set_param( model,"M1", {'A':2,'B':4}) 46 46 47 Add model with a dictionnary of FitArrange List{} where Uid is a key and model47 Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model 48 48 is save in FitArrange object. 49 49 engine.set_model(model,Uid) … … 54 54 def __init__(self): 55 55 """ 56 Creates a dictionary (self.fitArrange List={})of FitArrange elements56 Creates a dictionary (self.fitArrangeDict={})of FitArrange elements 57 57 with Uid as keys 58 58 """ 59 self.fitArrange List={}59 self.fitArrangeDict={} 60 60 self.paramList=[] 61 61 def fit(self,qmin=None, qmax=None): 62 62 # Protect against simultanous fitting attempts 63 if len(self.fitArrange List)>1:63 if len(self.fitArrangeDict)>1: 64 64 raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." 65 65 66 66 # fitproblem contains first fitArrange object(one model and a list of data) 67 fitproblem=self.fitArrange List.values()[0]67 fitproblem=self.fitArrangeDict.values()[0] 68 68 listdata=[] 69 69 model = fitproblem.get_model() … … 77 77 qmax= max(data.x) 78 78 functor= sansAssembly(model,data) 79 print "scipyfitting:param list",model.getParams(self.paramList) 80 print "scipyfitting:functor",functor(model.getParams(self.paramList)) 81 79 82 80 out, cov_x, info, mesg, success = optimize.leastsq(functor,model.getParams(self.paramList), full_output=1, warning=True) 83 81 chisqr = functor.chisq(out) 82 if cov_x is not None and numpy.isfinite(cov_x).all(): 83 stderr = numpy.sqrt(numpy.diag(cov_x)) 84 84 85 print "scipyfitting: info",mesg 86 print"scipyfitting : success",success 87 print "scipyfitting: out", out 88 print "scipyfitting: cov_x", cov_x 89 print "scipyfitting: chisqr", chisqr 90 91 if not (numpy.isnan(out).any()): 85 if not (numpy.isnan(out).any()) or ( cov_x !=None) : 92 86 result = fitresult() 93 87 result.fitness = chisqr 94 result.cov = cov_x 95 88 result.stderr = stderr 96 89 result.pvec = out 97 90 result.success =success … … 99 92 return result 100 93 else: 101 raise ValueError, "SVD did not converge" 94 raise ValueError, "SVD did not converge"+str(success) 102 95 103 96 -
park_integration/docs/use-cases.txt
r103a0b0 rca6d914 23 23 1.1.3.1. The user does not choose a data set before starting the fit. 24 24 1.1.3.2. The user does not choose a model before starting the fit. 25 1.1.3.3. the user does not specify the list of parameter to fit 26 1.1.3.4. the user enters a list of parameters that do not belong to the model 25 27 26 28 … … 55 57 56 58 2.1.2.1. The client does not enter initial parameters 59 2.1.2.2. the user enters a list of parameters not belonging to the model 57 60 58 61 -
park_integration/test/cyl_testdata.txt
rcfe97ea rca6d914 1 1 0.007778 1.78E+14 13335629 2 3 2 0.023333 5.32E+13 7293888 4 3 0.031111 3.40E+13 5833464 … … 12 11 0.093333 1.47E+11 383624.8 13 12 0.101111 1.19E+11 344493.8 14 15 13 0.124444 2.58E+11 507633.7 16 14 0.132222 2.44E+11 493743.9 … … 22 20 0.178889 4.41E+10 209959 23 21 0.186667 4.02E+10 200480.7 24 25 22 0.202222 5.33E+10 230891.5 26 23 0.21 4.99E+10 223441.9 … … 37 34 0.295556 1.05E+10 102300 38 35 0.303333 7.95E+09 89146.17 39 40 36 0.334444 7.92E+09 89019.55 41 37 0.342222 9.75E+09 98739.25 -
park_integration/test/testdata_line.txt
r6b126e8 rca6d914 1 #y=A *x+B2 # A=2.53 # B=41 #y=A+x*B 2 #B=2.5 3 #A=4 4 4 0 2.83954 0.6 5 5 0.204082 3.44938 0.676531
Note: See TracChangeset
for help on using the changeset viewer.