[4c718654] | 1 | |
---|
[48882d1] | 2 | import park,numpy |
---|
| 3 | |
---|
| 4 | class SansParameter(park.Parameter): |
---|
| 5 | """ |
---|
| 6 | SANS model parameters for use in the PARK fitting service. |
---|
| 7 | The parameter attribute value is redirected to the underlying |
---|
| 8 | parameter value in the SANS model. |
---|
| 9 | """ |
---|
| 10 | def __init__(self, name, model): |
---|
[ca6d914] | 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)) |
---|
[48882d1] | 18 | |
---|
[ca6d914] | 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) |
---|
[48882d1] | 25 | |
---|
[ca6d914] | 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 | """ |
---|
[48882d1] | 31 | self._model.setParam(self.name, value) |
---|
| 32 | |
---|
| 33 | value = property(_getvalue,_setvalue) |
---|
| 34 | |
---|
| 35 | def _getrange(self): |
---|
[ca6d914] | 36 | """ |
---|
| 37 | Override _getrange of park parameter |
---|
| 38 | return the range of parameter |
---|
| 39 | """ |
---|
[48882d1] | 40 | lo,hi = self._model.details[self.name][1:] |
---|
| 41 | if lo is None: lo = -numpy.inf |
---|
| 42 | if hi is None: hi = numpy.inf |
---|
| 43 | return lo,hi |
---|
| 44 | |
---|
| 45 | def _setrange(self,r): |
---|
[ca6d914] | 46 | """ |
---|
| 47 | override _setrange of park parameter |
---|
| 48 | @param r: the value of the range to set |
---|
| 49 | """ |
---|
[48882d1] | 50 | self._model.details[self.name][1:] = r |
---|
| 51 | range = property(_getrange,_setrange) |
---|
[a9e04aa] | 52 | |
---|
| 53 | class Model(park.Model): |
---|
[48882d1] | 54 | """ |
---|
| 55 | PARK wrapper for SANS models. |
---|
| 56 | """ |
---|
[388309d] | 57 | def __init__(self, sans_model, **kw): |
---|
[ca6d914] | 58 | """ |
---|
| 59 | @param sans_model: the sans model to wrap using park interface |
---|
| 60 | """ |
---|
[a9e04aa] | 61 | park.Model.__init__(self, **kw) |
---|
[48882d1] | 62 | self.model = sans_model |
---|
[ca6d914] | 63 | self.name = sans_model.name |
---|
| 64 | #list of parameters names |
---|
[48882d1] | 65 | self.sansp = sans_model.getParamList() |
---|
[ca6d914] | 66 | #list of park parameter |
---|
[48882d1] | 67 | self.parkp = [SansParameter(p,sans_model) for p in self.sansp] |
---|
[ca6d914] | 68 | #list of parameterset |
---|
[48882d1] | 69 | self.parameterset = park.ParameterSet(sans_model.name,pars=self.parkp) |
---|
| 70 | self.pars=[] |
---|
[ca6d914] | 71 | |
---|
| 72 | |
---|
[48882d1] | 73 | def getParams(self,fitparams): |
---|
[ca6d914] | 74 | """ |
---|
| 75 | return a list of value of paramter to fit |
---|
| 76 | @param fitparams: list of paramaters name to fit |
---|
| 77 | """ |
---|
[48882d1] | 78 | list=[] |
---|
| 79 | self.pars=[] |
---|
| 80 | self.pars=fitparams |
---|
| 81 | for item in fitparams: |
---|
| 82 | for element in self.parkp: |
---|
| 83 | if element.name ==str(item): |
---|
| 84 | list.append(element.value) |
---|
| 85 | return list |
---|
| 86 | |
---|
[ca6d914] | 87 | |
---|
[e71440c] | 88 | def setParams(self,paramlist, params): |
---|
[ca6d914] | 89 | """ |
---|
| 90 | Set value for parameters to fit |
---|
| 91 | @param params: list of value for parameters to fit |
---|
| 92 | """ |
---|
[e71440c] | 93 | try: |
---|
| 94 | for i in range(len(self.parkp)): |
---|
| 95 | for j in range(len(paramlist)): |
---|
| 96 | if self.parkp[i].name==paramlist[j]: |
---|
| 97 | self.parkp[i].value = params[j] |
---|
| 98 | self.model.setParam(self.parkp[i].name,params[j]) |
---|
| 99 | except: |
---|
| 100 | raise |
---|
[ca6d914] | 101 | |
---|
[48882d1] | 102 | def eval(self,x): |
---|
[ca6d914] | 103 | """ |
---|
| 104 | override eval method of park model. |
---|
| 105 | @param x: the x value used to compute a function |
---|
| 106 | """ |
---|
[48882d1] | 107 | return self.model.runXY(x) |
---|
[388309d] | 108 | |
---|
| 109 | |
---|
[a9e04aa] | 110 | |
---|
| 111 | |
---|
[48882d1] | 112 | class Data(object): |
---|
| 113 | """ Wrapper class for SANS data """ |
---|
| 114 | def __init__(self,x=None,y=None,dy=None,dx=None,sans_data=None): |
---|
[ca6d914] | 115 | """ |
---|
| 116 | Data can be initital with a data (sans plottable) |
---|
| 117 | or with vectors. |
---|
| 118 | """ |
---|
[48882d1] | 119 | if sans_data !=None: |
---|
| 120 | self.x= sans_data.x |
---|
| 121 | self.y= sans_data.y |
---|
| 122 | self.dx= sans_data.dx |
---|
| 123 | self.dy= sans_data.dy |
---|
| 124 | |
---|
| 125 | elif (x!=None and y!=None and dy!=None): |
---|
| 126 | self.x=x |
---|
| 127 | self.y=y |
---|
| 128 | self.dx=dx |
---|
| 129 | self.dy=dy |
---|
| 130 | else: |
---|
| 131 | raise ValueError,\ |
---|
| 132 | "Data is missing x, y or dy, impossible to compute residuals later on" |
---|
| 133 | self.qmin=None |
---|
| 134 | self.qmax=None |
---|
| 135 | |
---|
[ca6d914] | 136 | |
---|
[48882d1] | 137 | def setFitRange(self,mini=None,maxi=None): |
---|
| 138 | """ to set the fit range""" |
---|
| 139 | self.qmin=mini |
---|
| 140 | self.qmax=maxi |
---|
[ca6d914] | 141 | |
---|
| 142 | |
---|
[48882d1] | 143 | def getFitRange(self): |
---|
[ca6d914] | 144 | """ |
---|
| 145 | @return the range of data.x to fit |
---|
| 146 | """ |
---|
| 147 | return self.qmin, self.qmax |
---|
| 148 | |
---|
| 149 | |
---|
[48882d1] | 150 | def residuals(self, fn): |
---|
| 151 | """ @param fn: function that return model value |
---|
| 152 | @return residuals |
---|
| 153 | """ |
---|
| 154 | x,y,dy = [numpy.asarray(v) for v in (self.x,self.y,self.dy)] |
---|
| 155 | if self.qmin==None and self.qmax==None: |
---|
[ca6d914] | 156 | fx =numpy.asarray([fn(v) for v in x]) |
---|
[48882d1] | 157 | return (y - fx)/dy |
---|
| 158 | else: |
---|
| 159 | idx = (x>=self.qmin) & (x <= self.qmax) |
---|
[ca6d914] | 160 | fx = numpy.asarray([fn(item)for item in x[idx ]]) |
---|
[48882d1] | 161 | return (y[idx] - fx)/dy[idx] |
---|
[e71440c] | 162 | |
---|
[48882d1] | 163 | def residuals_deriv(self, model, pars=[]): |
---|
| 164 | """ |
---|
| 165 | @return residuals derivatives . |
---|
| 166 | @note: in this case just return empty array |
---|
| 167 | """ |
---|
| 168 | return [] |
---|
| 169 | |
---|
| 170 | class sansAssembly: |
---|
[ca6d914] | 171 | """ |
---|
| 172 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
| 173 | """ |
---|
[e71440c] | 174 | def __init__(self,paramlist,Model=None , Data=None): |
---|
[ca6d914] | 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 |
---|
[e71440c] | 181 | self.paramlist=paramlist |
---|
[ca6d914] | 182 | self.res=[] |
---|
[48882d1] | 183 | def chisq(self, params): |
---|
| 184 | """ |
---|
| 185 | Calculates chi^2 |
---|
| 186 | @param params: list of parameter values |
---|
| 187 | @return: chi^2 |
---|
| 188 | """ |
---|
| 189 | sum = 0 |
---|
| 190 | for item in self.res: |
---|
| 191 | sum += item*item |
---|
| 192 | return sum |
---|
| 193 | def __call__(self,params): |
---|
[ca6d914] | 194 | """ |
---|
| 195 | Compute residuals |
---|
| 196 | @param params: value of parameters to fit |
---|
| 197 | """ |
---|
[e71440c] | 198 | self.model.setParams(self.paramlist,params) |
---|
[48882d1] | 199 | self.res= self.data.residuals(self.model.eval) |
---|
| 200 | return self.res |
---|
| 201 | |
---|
[4c718654] | 202 | class FitEngine: |
---|
[ee5b04c] | 203 | def __init__(self): |
---|
[ca6d914] | 204 | """ |
---|
| 205 | Base class for scipy and park fit engine |
---|
| 206 | """ |
---|
| 207 | #List of parameter names to fit |
---|
[ee5b04c] | 208 | self.paramList=[] |
---|
[ca6d914] | 209 | #Dictionnary of fitArrange element (fit problems) |
---|
| 210 | self.fitArrangeDict={} |
---|
| 211 | |
---|
[4c718654] | 212 | def _concatenateData(self, listdata=[]): |
---|
| 213 | """ |
---|
| 214 | _concatenateData method concatenates each fields of all data contains ins listdata. |
---|
| 215 | @param listdata: list of data |
---|
[ca6d914] | 216 | @return Data: Data is wrapper class for sans plottable. it is created with all parameters |
---|
| 217 | of data concatenanted |
---|
[4c718654] | 218 | @raise: if listdata is empty will return None |
---|
| 219 | @raise: if data in listdata don't contain dy field ,will create an error |
---|
| 220 | during fitting |
---|
| 221 | """ |
---|
| 222 | if listdata==[]: |
---|
| 223 | raise ValueError, " data list missing" |
---|
| 224 | else: |
---|
| 225 | xtemp=[] |
---|
| 226 | ytemp=[] |
---|
| 227 | dytemp=[] |
---|
[48882d1] | 228 | self.mini=None |
---|
| 229 | self.maxi=None |
---|
[4c718654] | 230 | |
---|
| 231 | for data in listdata: |
---|
[48882d1] | 232 | mini,maxi=data.getFitRange() |
---|
| 233 | if self.mini==None and self.maxi==None: |
---|
| 234 | self.mini=mini |
---|
| 235 | self.maxi=maxi |
---|
| 236 | else: |
---|
| 237 | if mini < self.mini: |
---|
| 238 | self.mini=mini |
---|
| 239 | if self.maxi < maxi: |
---|
| 240 | self.maxi=maxi |
---|
| 241 | |
---|
| 242 | |
---|
[4c718654] | 243 | for i in range(len(data.x)): |
---|
| 244 | xtemp.append(data.x[i]) |
---|
| 245 | ytemp.append(data.y[i]) |
---|
| 246 | if data.dy is not None and len(data.dy)==len(data.y): |
---|
| 247 | dytemp.append(data.dy[i]) |
---|
| 248 | else: |
---|
[ee5b04c] | 249 | raise RuntimeError, "Fit._concatenateData: y-errors missing" |
---|
[48882d1] | 250 | data= Data(x=xtemp,y=ytemp,dy=dytemp) |
---|
| 251 | data.setFitRange(self.mini, self.maxi) |
---|
| 252 | return data |
---|
[ca6d914] | 253 | |
---|
| 254 | |
---|
| 255 | def set_model(self,model,Uid,pars=[]): |
---|
| 256 | """ |
---|
| 257 | set a model on a given uid in the fit engine. |
---|
| 258 | @param model: the model to fit |
---|
| 259 | @param Uid :is the key of the fitArrange dictionnary where model is saved as a value |
---|
| 260 | @param pars: the list of parameters to fit |
---|
| 261 | @note : pars must contains only name of existing model's paramaters |
---|
| 262 | """ |
---|
[f44dbc7] | 263 | if len(pars) >0: |
---|
[6831a99] | 264 | if model==None: |
---|
[f44dbc7] | 265 | raise ValueError, "AbstractFitEngine: Specify parameters to fit" |
---|
[6831a99] | 266 | else: |
---|
[ca6d914] | 267 | for item in pars: |
---|
| 268 | if item in model.model.getParamList(): |
---|
| 269 | self.paramList.append(item) |
---|
| 270 | else: |
---|
| 271 | raise ValueError,"wrong paramter %s used to set model %s. Choose\ |
---|
| 272 | parameter name within %s"%(item, model.model.name,str(model.model.getParamList())) |
---|
| 273 | return |
---|
[6831a99] | 274 | #A fitArrange is already created but contains dList only at Uid |
---|
[ca6d914] | 275 | if self.fitArrangeDict.has_key(Uid): |
---|
| 276 | self.fitArrangeDict[Uid].set_model(model) |
---|
[6831a99] | 277 | else: |
---|
| 278 | #no fitArrange object has been create with this Uid |
---|
[48882d1] | 279 | fitproblem = FitArrange() |
---|
[6831a99] | 280 | fitproblem.set_model(model) |
---|
[ca6d914] | 281 | self.fitArrangeDict[Uid] = fitproblem |
---|
[d4b0687] | 282 | else: |
---|
[6831a99] | 283 | raise ValueError, "park_integration:missing parameters" |
---|
[48882d1] | 284 | |
---|
| 285 | def set_data(self,data,Uid,qmin=None,qmax=None): |
---|
[d4b0687] | 286 | """ Receives plottable, creates a list of data to fit,set data |
---|
| 287 | in a FitArrange object and adds that object in a dictionary |
---|
| 288 | with key Uid. |
---|
| 289 | @param data: data added |
---|
| 290 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
[ca6d914] | 291 | """ |
---|
[48882d1] | 292 | if qmin !=None and qmax !=None: |
---|
| 293 | data.setFitRange(mini=qmin,maxi=qmax) |
---|
[d4b0687] | 294 | #A fitArrange is already created but contains model only at Uid |
---|
[ca6d914] | 295 | if self.fitArrangeDict.has_key(Uid): |
---|
| 296 | self.fitArrangeDict[Uid].add_data(data) |
---|
[d4b0687] | 297 | else: |
---|
| 298 | #no fitArrange object has been create with this Uid |
---|
| 299 | fitproblem= FitArrange() |
---|
| 300 | fitproblem.add_data(data) |
---|
[ca6d914] | 301 | self.fitArrangeDict[Uid]=fitproblem |
---|
[48882d1] | 302 | |
---|
[d4b0687] | 303 | def get_model(self,Uid): |
---|
| 304 | """ |
---|
| 305 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
| 306 | @return a model at this uid or None if no FitArrange element was created |
---|
| 307 | with this Uid |
---|
| 308 | """ |
---|
[ca6d914] | 309 | if self.fitArrangeDict.has_key(Uid): |
---|
| 310 | return self.fitArrangeDict[Uid].get_model() |
---|
[d4b0687] | 311 | else: |
---|
| 312 | return None |
---|
| 313 | |
---|
| 314 | def remove_Fit_Problem(self,Uid): |
---|
| 315 | """remove fitarrange in Uid""" |
---|
[ca6d914] | 316 | if self.fitArrangeDict.has_key(Uid): |
---|
| 317 | del self.fitArrangeDict[Uid] |
---|
[a9e04aa] | 318 | |
---|
| 319 | def select_problem_for_fit(self,Uid,value): |
---|
| 320 | """ |
---|
| 321 | select a couple of model and data at the Uid position in dictionary |
---|
| 322 | and set in self.selected value to value |
---|
| 323 | @param value: the value to allow fitting. can only have the value one or zero |
---|
| 324 | """ |
---|
| 325 | if self.fitArrangeDict.has_key(Uid): |
---|
| 326 | self.fitArrangeDict[Uid].set_to_fit( value) |
---|
| 327 | def get_problem_to_fit(self,Uid): |
---|
| 328 | """ |
---|
| 329 | return the self.selected value of the fit problem of Uid |
---|
| 330 | @param Uid: the Uid of the problem |
---|
| 331 | """ |
---|
| 332 | if self.fitArrangeDict.has_key(Uid): |
---|
| 333 | self.fitArrangeDict[Uid].get_to_fit() |
---|
[4c718654] | 334 | |
---|
[d4b0687] | 335 | class FitArrange: |
---|
| 336 | def __init__(self): |
---|
| 337 | """ |
---|
| 338 | Class FitArrange contains a set of data for a given model |
---|
| 339 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 340 | and at least one data for the fit to be performed. |
---|
| 341 | model: the model selected by the user |
---|
| 342 | Ldata: a list of data what the user wants to fit |
---|
| 343 | |
---|
| 344 | """ |
---|
| 345 | self.model = None |
---|
| 346 | self.dList =[] |
---|
[a9e04aa] | 347 | #self.selected is zero when this fit problem is not schedule to fit |
---|
| 348 | #self.selected is 1 when schedule to fit |
---|
| 349 | self.selected = 0 |
---|
[d4b0687] | 350 | |
---|
| 351 | def set_model(self,model): |
---|
| 352 | """ |
---|
| 353 | set_model save a copy of the model |
---|
| 354 | @param model: the model being set |
---|
| 355 | """ |
---|
| 356 | self.model = model |
---|
| 357 | |
---|
| 358 | def add_data(self,data): |
---|
| 359 | """ |
---|
| 360 | add_data fill a self.dList with data to fit |
---|
| 361 | @param data: Data to add in the list |
---|
| 362 | """ |
---|
| 363 | if not data in self.dList: |
---|
| 364 | self.dList.append(data) |
---|
| 365 | |
---|
| 366 | def get_model(self): |
---|
| 367 | """ @return: saved model """ |
---|
| 368 | return self.model |
---|
| 369 | |
---|
| 370 | def get_data(self): |
---|
| 371 | """ @return: list of data dList""" |
---|
| 372 | return self.dList |
---|
| 373 | |
---|
| 374 | def remove_data(self,data): |
---|
| 375 | """ |
---|
| 376 | Remove one element from the list |
---|
| 377 | @param data: Data to remove from dList |
---|
| 378 | """ |
---|
| 379 | if data in self.dList: |
---|
| 380 | self.dList.remove(data) |
---|
[a9e04aa] | 381 | def set_to_fit (self, value=0): |
---|
| 382 | """ |
---|
| 383 | set self.selected to 0 or 1 for other values raise an exception |
---|
| 384 | @param value: integer between 0 or 1 |
---|
| 385 | """ |
---|
| 386 | self.selected= value |
---|
| 387 | |
---|
| 388 | def get_to_fit(self): |
---|
| 389 | """ |
---|
| 390 | @return self.selected value |
---|
| 391 | """ |
---|
| 392 | return self.selected |
---|
[94b44293] | 393 | |
---|
[4c718654] | 394 | |
---|
| 395 | |
---|
| 396 | |
---|