[72c7d31] | 1 | import logging, sys |
---|
[54c21f50] | 2 | import park,numpy,math, copy |
---|
[1e3169c] | 3 | from DataLoader.data_info import Data1D |
---|
| 4 | from DataLoader.data_info import Data2D |
---|
[48882d1] | 5 | class SansParameter(park.Parameter): |
---|
| 6 | """ |
---|
| 7 | SANS model parameters for use in the PARK fitting service. |
---|
| 8 | The parameter attribute value is redirected to the underlying |
---|
| 9 | parameter value in the SANS model. |
---|
| 10 | """ |
---|
| 11 | def __init__(self, name, model): |
---|
[ca6d914] | 12 | """ |
---|
| 13 | @param name: the name of the model parameter |
---|
| 14 | @param model: the sans model to wrap as a park model |
---|
| 15 | """ |
---|
| 16 | self._model, self._name = model,name |
---|
| 17 | #set the value for the parameter of the given name |
---|
| 18 | self.set(model.getParam(name)) |
---|
[48882d1] | 19 | |
---|
[ca6d914] | 20 | def _getvalue(self): |
---|
| 21 | """ |
---|
| 22 | override the _getvalue of park parameter |
---|
| 23 | @return value the parameter associates with self.name |
---|
| 24 | """ |
---|
| 25 | return self._model.getParam(self.name) |
---|
[48882d1] | 26 | |
---|
[ca6d914] | 27 | def _setvalue(self,value): |
---|
| 28 | """ |
---|
| 29 | override the _setvalue pf park parameter |
---|
| 30 | @param value: the value to set on a given parameter |
---|
| 31 | """ |
---|
[48882d1] | 32 | self._model.setParam(self.name, value) |
---|
| 33 | |
---|
| 34 | value = property(_getvalue,_setvalue) |
---|
| 35 | |
---|
| 36 | def _getrange(self): |
---|
[ca6d914] | 37 | """ |
---|
| 38 | Override _getrange of park parameter |
---|
| 39 | return the range of parameter |
---|
| 40 | """ |
---|
[920a6e5] | 41 | #if not self.name in self._model.getDispParamList(): |
---|
[12b76cf] | 42 | lo,hi = self._model.details[self.name][1:3] |
---|
[920a6e5] | 43 | if lo is None: lo = -numpy.inf |
---|
| 44 | if hi is None: hi = numpy.inf |
---|
| 45 | #else: |
---|
| 46 | #lo,hi = self._model.details[self.name][1:] |
---|
| 47 | #if lo is None: lo = -numpy.inf |
---|
| 48 | #if hi is None: hi = numpy.inf |
---|
[05f14dd] | 49 | if lo >= hi: |
---|
| 50 | raise ValueError,"wrong fit range for parameters" |
---|
| 51 | |
---|
[48882d1] | 52 | return lo,hi |
---|
| 53 | |
---|
| 54 | def _setrange(self,r): |
---|
[ca6d914] | 55 | """ |
---|
| 56 | override _setrange of park parameter |
---|
| 57 | @param r: the value of the range to set |
---|
| 58 | """ |
---|
[12b76cf] | 59 | self._model.details[self.name][1:3] = r |
---|
[48882d1] | 60 | range = property(_getrange,_setrange) |
---|
[a9e04aa] | 61 | |
---|
| 62 | class Model(park.Model): |
---|
[48882d1] | 63 | """ |
---|
| 64 | PARK wrapper for SANS models. |
---|
| 65 | """ |
---|
[388309d] | 66 | def __init__(self, sans_model, **kw): |
---|
[ca6d914] | 67 | """ |
---|
| 68 | @param sans_model: the sans model to wrap using park interface |
---|
| 69 | """ |
---|
[a9e04aa] | 70 | park.Model.__init__(self, **kw) |
---|
[48882d1] | 71 | self.model = sans_model |
---|
[ca6d914] | 72 | self.name = sans_model.name |
---|
| 73 | #list of parameters names |
---|
[48882d1] | 74 | self.sansp = sans_model.getParamList() |
---|
[ca6d914] | 75 | #list of park parameter |
---|
[48882d1] | 76 | self.parkp = [SansParameter(p,sans_model) for p in self.sansp] |
---|
[ca6d914] | 77 | #list of parameterset |
---|
[48882d1] | 78 | self.parameterset = park.ParameterSet(sans_model.name,pars=self.parkp) |
---|
| 79 | self.pars=[] |
---|
[ca6d914] | 80 | |
---|
| 81 | |
---|
[48882d1] | 82 | def getParams(self,fitparams): |
---|
[ca6d914] | 83 | """ |
---|
| 84 | return a list of value of paramter to fit |
---|
| 85 | @param fitparams: list of paramaters name to fit |
---|
| 86 | """ |
---|
[48882d1] | 87 | list=[] |
---|
| 88 | self.pars=[] |
---|
| 89 | self.pars=fitparams |
---|
| 90 | for item in fitparams: |
---|
| 91 | for element in self.parkp: |
---|
| 92 | if element.name ==str(item): |
---|
| 93 | list.append(element.value) |
---|
| 94 | return list |
---|
| 95 | |
---|
[ca6d914] | 96 | |
---|
[e71440c] | 97 | def setParams(self,paramlist, params): |
---|
[ca6d914] | 98 | """ |
---|
| 99 | Set value for parameters to fit |
---|
| 100 | @param params: list of value for parameters to fit |
---|
| 101 | """ |
---|
[e71440c] | 102 | try: |
---|
| 103 | for i in range(len(self.parkp)): |
---|
| 104 | for j in range(len(paramlist)): |
---|
| 105 | if self.parkp[i].name==paramlist[j]: |
---|
| 106 | self.parkp[i].value = params[j] |
---|
| 107 | self.model.setParam(self.parkp[i].name,params[j]) |
---|
| 108 | except: |
---|
| 109 | raise |
---|
[ca6d914] | 110 | |
---|
[48882d1] | 111 | def eval(self,x): |
---|
[ca6d914] | 112 | """ |
---|
| 113 | override eval method of park model. |
---|
| 114 | @param x: the x value used to compute a function |
---|
| 115 | """ |
---|
[d8a2e31] | 116 | try: |
---|
[fd0d30fd] | 117 | return self.model.evalDistribution(x) |
---|
[d8a2e31] | 118 | except: |
---|
[fd0d30fd] | 119 | raise |
---|
[a9e04aa] | 120 | |
---|
[b64fa56] | 121 | |
---|
[1e3169c] | 122 | class FitData1D(Data1D): |
---|
| 123 | """ |
---|
| 124 | Wrapper class for SANS data |
---|
| 125 | FitData1D inherits from DataLoader.data_info.Data1D. Implements |
---|
| 126 | a way to get residuals from data. |
---|
| 127 | """ |
---|
| 128 | def __init__(self,x, y,dx= None, dy=None, smearer=None): |
---|
[ac3041b] | 129 | Data1D.__init__(self, x=numpy.array(x), y=numpy.array(y), dx=dx, dy=dy) |
---|
[7d0c1a8] | 130 | """ |
---|
[1e3169c] | 131 | @param smearer: is an object of class QSmearer or SlitSmearer |
---|
[109e60ab] | 132 | that will smear the theory data (slit smearing or resolution |
---|
| 133 | smearing) when set. |
---|
| 134 | |
---|
| 135 | The proper way to set the smearing object would be to |
---|
| 136 | do the following: |
---|
| 137 | |
---|
| 138 | from DataLoader.qsmearing import smear_selection |
---|
[1e3169c] | 139 | smearer = smear_selection(some_data) |
---|
| 140 | fitdata1d = FitData1D( x= [1,3,..,], |
---|
| 141 | y= [3,4,..,8], |
---|
| 142 | dx=None, |
---|
| 143 | dy=[1,2...], smearer= smearer) |
---|
| 144 | |
---|
[109e60ab] | 145 | Note that some_data _HAS_ to be of class DataLoader.data_info.Data1D |
---|
| 146 | |
---|
| 147 | Setting it back to None will turn smearing off. |
---|
| 148 | |
---|
[7d0c1a8] | 149 | """ |
---|
[b461b6d7] | 150 | |
---|
| 151 | self.smearer = smearer |
---|
[1e3169c] | 152 | if dy ==None or dy==[]: |
---|
[2eaaf1a] | 153 | self.dy= numpy.zeros(len(self.y)) |
---|
[fd0d30fd] | 154 | else: |
---|
[1e3169c] | 155 | self.dy= numpy.asarray(dy) |
---|
[fd0d30fd] | 156 | |
---|
| 157 | # For fitting purposes, replace zero errors by 1 |
---|
| 158 | #TODO: check validity for the rare case where only |
---|
| 159 | # a few points have zero errors |
---|
| 160 | self.dy[self.dy==0]=1 |
---|
[109e60ab] | 161 | |
---|
| 162 | ## Min Q-value |
---|
[4bd557d] | 163 | #Skip the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
[1e3169c] | 164 | if min (self.x) ==0.0 and self.x[0]==0 and not numpy.isfinite(self.y[0]): |
---|
| 165 | self.qmin = min(self.x[self.x!=0]) |
---|
[773806e] | 166 | else: |
---|
[1e3169c] | 167 | self.qmin= min (self.x) |
---|
[109e60ab] | 168 | ## Max Q-value |
---|
[1e3169c] | 169 | self.qmax = max (self.x) |
---|
[058b2d7] | 170 | |
---|
[72c7d31] | 171 | # Range used for input to smearing |
---|
| 172 | self._qmin_unsmeared = self.qmin |
---|
| 173 | self._qmax_unsmeared = self.qmax |
---|
[fd0d30fd] | 174 | # Identify the bin range for the unsmeared and smeared spaces |
---|
| 175 | self.idx = (self.x>=self.qmin) & (self.x <= self.qmax) |
---|
| 176 | self.idx_unsmeared = (self.x>=self._qmin_unsmeared) & (self.x <= self._qmax_unsmeared) |
---|
| 177 | |
---|
[72c7d31] | 178 | |
---|
| 179 | |
---|
[20d30e9] | 180 | def setFitRange(self,qmin=None,qmax=None): |
---|
[7d0c1a8] | 181 | """ to set the fit range""" |
---|
[09975cbb] | 182 | # Skip Q=0 point, (especially for y(q=0)=None at x[0]). |
---|
[773806e] | 183 | #ToDo: Fix this. |
---|
[1e3169c] | 184 | if qmin==0.0 and not numpy.isfinite(self.y[qmin]): |
---|
| 185 | self.qmin = min(self.x[self.x!=0]) |
---|
[773806e] | 186 | elif qmin!=None: |
---|
| 187 | self.qmin = qmin |
---|
| 188 | |
---|
[eef2e0ed] | 189 | if qmax !=None: |
---|
| 190 | self.qmax = qmax |
---|
[72c7d31] | 191 | |
---|
[4bb2917] | 192 | # Determine the range needed in unsmeared-Q to cover |
---|
| 193 | # the smeared Q range |
---|
[72c7d31] | 194 | self._qmin_unsmeared = self.qmin |
---|
| 195 | self._qmax_unsmeared = self.qmax |
---|
| 196 | |
---|
[4bb2917] | 197 | self._first_unsmeared_bin = 0 |
---|
[1e3169c] | 198 | self._last_unsmeared_bin = len(self.x)-1 |
---|
[4bb2917] | 199 | |
---|
| 200 | if self.smearer!=None: |
---|
| 201 | self._first_unsmeared_bin, self._last_unsmeared_bin = self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
[1e3169c] | 202 | self._qmin_unsmeared = self.x[self._first_unsmeared_bin] |
---|
| 203 | self._qmax_unsmeared = self.x[self._last_unsmeared_bin] |
---|
[4bb2917] | 204 | |
---|
[fd0d30fd] | 205 | # Identify the bin range for the unsmeared and smeared spaces |
---|
| 206 | self.idx = (self.x>=self.qmin) & (self.x <= self.qmax) |
---|
| 207 | self.idx_unsmeared = (self.x>=self._qmin_unsmeared) & (self.x <= self._qmax_unsmeared) |
---|
| 208 | |
---|
[7d0c1a8] | 209 | |
---|
| 210 | def getFitRange(self): |
---|
| 211 | """ |
---|
| 212 | @return the range of data.x to fit |
---|
| 213 | """ |
---|
| 214 | return self.qmin, self.qmax |
---|
[72c7d31] | 215 | |
---|
[7d0c1a8] | 216 | def residuals(self, fn): |
---|
[72c7d31] | 217 | """ |
---|
| 218 | Compute residuals. |
---|
| 219 | |
---|
| 220 | If self.smearer has been set, use if to smear |
---|
| 221 | the data before computing chi squared. |
---|
| 222 | |
---|
| 223 | @param fn: function that return model value |
---|
| 224 | @return residuals |
---|
[109e60ab] | 225 | """ |
---|
| 226 | # Compute theory data f(x) |
---|
[fd0d30fd] | 227 | fx= numpy.zeros(len(self.x)) |
---|
[7e752fe] | 228 | fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) |
---|
[fd0d30fd] | 229 | |
---|
[d5b488b] | 230 | ## Smear theory data |
---|
[109e60ab] | 231 | if self.smearer is not None: |
---|
[4bb2917] | 232 | fx = self.smearer(fx, self._first_unsmeared_bin, self._last_unsmeared_bin) |
---|
[72c7d31] | 233 | |
---|
[d5b488b] | 234 | ## Sanity check |
---|
[fd0d30fd] | 235 | if numpy.size(self.dy)!= numpy.size(fx): |
---|
| 236 | raise RuntimeError, "FitData1D: invalid error array %d <> %d" % (numpy.shape(self.dy), |
---|
| 237 | numpy.size(fx)) |
---|
| 238 | |
---|
| 239 | return (self.y[self.idx]-fx[self.idx])/self.dy[self.idx] |
---|
[72c7d31] | 240 | |
---|
| 241 | |
---|
| 242 | |
---|
[7d0c1a8] | 243 | def residuals_deriv(self, model, pars=[]): |
---|
| 244 | """ |
---|
| 245 | @return residuals derivatives . |
---|
| 246 | @note: in this case just return empty array |
---|
| 247 | """ |
---|
| 248 | return [] |
---|
| 249 | |
---|
| 250 | |
---|
[1e3169c] | 251 | class FitData2D(Data2D): |
---|
[7d0c1a8] | 252 | """ Wrapper class for SANS data """ |
---|
[150144d] | 253 | def __init__(self,sans_data2d ,data=None, err_data=None): |
---|
[1e3169c] | 254 | Data2D.__init__(self, data= data, err_data= err_data) |
---|
[7d0c1a8] | 255 | """ |
---|
| 256 | Data can be initital with a data (sans plottable) |
---|
| 257 | or with vectors. |
---|
| 258 | """ |
---|
[1e3169c] | 259 | self.x_bins_array = [] |
---|
| 260 | self.y_bins_array = [] |
---|
| 261 | self.res_err_image=[] |
---|
| 262 | self.index_model=[] |
---|
| 263 | self.qmin= None |
---|
| 264 | self.qmax= None |
---|
| 265 | self.set_data(sans_data2d ) |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | def set_data(self, sans_data2d ): |
---|
| 269 | """ |
---|
| 270 | Determine the correct x_bin and y_bin to fit |
---|
| 271 | """ |
---|
[d8a2e31] | 272 | self.x_bins_array= numpy.reshape(sans_data2d.x_bins, |
---|
[f1c79d2] | 273 | [1,len(sans_data2d.x_bins)]) |
---|
[d8a2e31] | 274 | self.y_bins_array = numpy.reshape(sans_data2d.y_bins, |
---|
[f1c79d2] | 275 | [len(sans_data2d.y_bins),1]) |
---|
[d8a2e31] | 276 | |
---|
[da58fcc] | 277 | x_max = max(sans_data2d.xmin, sans_data2d.xmax) |
---|
| 278 | y_max = max(sans_data2d.ymin, sans_data2d.ymax) |
---|
[20d30e9] | 279 | |
---|
| 280 | ## fitting range |
---|
[773806e] | 281 | self.qmin = 1e-16 |
---|
[1e3169c] | 282 | self.qmax = math.sqrt(x_max*x_max +y_max*y_max) |
---|
[70bf68c] | 283 | ## new error image for fitting purpose |
---|
[da58fcc] | 284 | if self.err_data== None or self.err_data ==[]: |
---|
| 285 | self.res_err_data= numpy.zeros(len(self.y_bins),len(self.x_bins)) |
---|
[70bf68c] | 286 | else: |
---|
[da58fcc] | 287 | self.res_err_data = copy.deepcopy(self.err_data) |
---|
| 288 | self.res_err_data[self.res_err_data==0]=1 |
---|
[d8a2e31] | 289 | |
---|
| 290 | self.radius= numpy.sqrt(self.x_bins_array**2 + self.y_bins_array**2) |
---|
| 291 | self.index_model = (self.qmin <= self.radius)&(self.radius<= self.qmax) |
---|
[7d0c1a8] | 292 | |
---|
[20d30e9] | 293 | |
---|
| 294 | def setFitRange(self,qmin=None,qmax=None): |
---|
[7d0c1a8] | 295 | """ to set the fit range""" |
---|
[773806e] | 296 | if qmin==0.0: |
---|
| 297 | self.qmin = 1e-16 |
---|
| 298 | elif qmin!=None: |
---|
| 299 | self.qmin = qmin |
---|
[eef2e0ed] | 300 | if qmax!=None: |
---|
| 301 | self.qmax= qmax |
---|
[20d30e9] | 302 | |
---|
[7d0c1a8] | 303 | |
---|
| 304 | def getFitRange(self): |
---|
| 305 | """ |
---|
| 306 | @return the range of data.x to fit |
---|
| 307 | """ |
---|
[20d30e9] | 308 | return self.qmin, self.qmax |
---|
[7d0c1a8] | 309 | |
---|
[d8a2e31] | 310 | def residuals(self, fn): |
---|
[fd0d30fd] | 311 | |
---|
[da58fcc] | 312 | res=self.index_model*(self.data - fn([self.x_bins_array, |
---|
| 313 | self.y_bins_array]))/self.res_err_data |
---|
[7f81665] | 314 | return res.ravel() |
---|
[0e51519] | 315 | |
---|
[fd0d30fd] | 316 | |
---|
[7d0c1a8] | 317 | def residuals_deriv(self, model, pars=[]): |
---|
| 318 | """ |
---|
| 319 | @return residuals derivatives . |
---|
| 320 | @note: in this case just return empty array |
---|
| 321 | """ |
---|
| 322 | return [] |
---|
[48882d1] | 323 | |
---|
[4bd557d] | 324 | class FitAbort(Exception): |
---|
| 325 | """ |
---|
| 326 | Exception raise to stop the fit |
---|
| 327 | """ |
---|
| 328 | print"Creating fit abort Exception" |
---|
| 329 | |
---|
| 330 | |
---|
[70bf68c] | 331 | class SansAssembly: |
---|
[ca6d914] | 332 | """ |
---|
| 333 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
| 334 | """ |
---|
[4bd557d] | 335 | def __init__(self,paramlist,Model=None , Data=None, curr_thread= None): |
---|
[ca6d914] | 336 | """ |
---|
| 337 | @param Model: the model wrapper fro sans -model |
---|
| 338 | @param Data: the data wrapper for sans data |
---|
| 339 | """ |
---|
| 340 | self.model = Model |
---|
| 341 | self.data = Data |
---|
[e71440c] | 342 | self.paramlist=paramlist |
---|
[4bd557d] | 343 | self.curr_thread= curr_thread |
---|
[ca6d914] | 344 | self.res=[] |
---|
[4bd557d] | 345 | self.func_name="Functor" |
---|
[48882d1] | 346 | def chisq(self, params): |
---|
| 347 | """ |
---|
| 348 | Calculates chi^2 |
---|
| 349 | @param params: list of parameter values |
---|
| 350 | @return: chi^2 |
---|
| 351 | """ |
---|
| 352 | sum = 0 |
---|
| 353 | for item in self.res: |
---|
| 354 | sum += item*item |
---|
[4bd557d] | 355 | if len(self.res)==0: |
---|
| 356 | return None |
---|
[26cb768] | 357 | return sum/ len(self.res) |
---|
[20d30e9] | 358 | |
---|
[48882d1] | 359 | def __call__(self,params): |
---|
[ca6d914] | 360 | """ |
---|
| 361 | Compute residuals |
---|
| 362 | @param params: value of parameters to fit |
---|
| 363 | """ |
---|
[681f0dc] | 364 | #import thread |
---|
[e71440c] | 365 | self.model.setParams(self.paramlist,params) |
---|
[48882d1] | 366 | self.res= self.data.residuals(self.model.eval) |
---|
[fd6b789] | 367 | if self.curr_thread != None : |
---|
| 368 | try: |
---|
| 369 | self.curr_thread.isquit() |
---|
| 370 | except: |
---|
| 371 | raise FitAbort,"stop leastsqr optimizer" |
---|
[48882d1] | 372 | return self.res |
---|
| 373 | |
---|
[4c718654] | 374 | class FitEngine: |
---|
[ee5b04c] | 375 | def __init__(self): |
---|
[ca6d914] | 376 | """ |
---|
| 377 | Base class for scipy and park fit engine |
---|
| 378 | """ |
---|
| 379 | #List of parameter names to fit |
---|
[ee5b04c] | 380 | self.paramList=[] |
---|
[ca6d914] | 381 | #Dictionnary of fitArrange element (fit problems) |
---|
| 382 | self.fitArrangeDict={} |
---|
| 383 | |
---|
[4c718654] | 384 | def _concatenateData(self, listdata=[]): |
---|
| 385 | """ |
---|
| 386 | _concatenateData method concatenates each fields of all data contains ins listdata. |
---|
| 387 | @param listdata: list of data |
---|
[ca6d914] | 388 | @return Data: Data is wrapper class for sans plottable. it is created with all parameters |
---|
| 389 | of data concatenanted |
---|
[4c718654] | 390 | @raise: if listdata is empty will return None |
---|
| 391 | @raise: if data in listdata don't contain dy field ,will create an error |
---|
| 392 | during fitting |
---|
| 393 | """ |
---|
[109e60ab] | 394 | #TODO: we have to refactor the way we handle data. |
---|
| 395 | # We should move away from plottables and move towards the Data1D objects |
---|
| 396 | # defined in DataLoader. Data1D allows data manipulations, which should be |
---|
| 397 | # used to concatenate. |
---|
| 398 | # In the meantime we should switch off the concatenation. |
---|
| 399 | #if len(listdata)>1: |
---|
| 400 | # raise RuntimeError, "FitEngine._concatenateData: Multiple data files is not currently supported" |
---|
| 401 | #return listdata[0] |
---|
| 402 | |
---|
[4c718654] | 403 | if listdata==[]: |
---|
| 404 | raise ValueError, " data list missing" |
---|
| 405 | else: |
---|
| 406 | xtemp=[] |
---|
| 407 | ytemp=[] |
---|
| 408 | dytemp=[] |
---|
[48882d1] | 409 | self.mini=None |
---|
| 410 | self.maxi=None |
---|
[4c718654] | 411 | |
---|
[7d0c1a8] | 412 | for item in listdata: |
---|
| 413 | data=item.data |
---|
[48882d1] | 414 | mini,maxi=data.getFitRange() |
---|
| 415 | if self.mini==None and self.maxi==None: |
---|
| 416 | self.mini=mini |
---|
| 417 | self.maxi=maxi |
---|
| 418 | else: |
---|
| 419 | if mini < self.mini: |
---|
| 420 | self.mini=mini |
---|
| 421 | if self.maxi < maxi: |
---|
| 422 | self.maxi=maxi |
---|
| 423 | |
---|
| 424 | |
---|
[4c718654] | 425 | for i in range(len(data.x)): |
---|
| 426 | xtemp.append(data.x[i]) |
---|
| 427 | ytemp.append(data.y[i]) |
---|
| 428 | if data.dy is not None and len(data.dy)==len(data.y): |
---|
| 429 | dytemp.append(data.dy[i]) |
---|
| 430 | else: |
---|
[ee5b04c] | 431 | raise RuntimeError, "Fit._concatenateData: y-errors missing" |
---|
[20d30e9] | 432 | data= Data(x=xtemp,y=ytemp,dy=dytemp) |
---|
[48882d1] | 433 | data.setFitRange(self.mini, self.maxi) |
---|
| 434 | return data |
---|
[ca6d914] | 435 | |
---|
| 436 | |
---|
[fd6b789] | 437 | def set_model(self,model,Uid,pars=[], constraints=[]): |
---|
[ca6d914] | 438 | """ |
---|
| 439 | set a model on a given uid in the fit engine. |
---|
[fd6b789] | 440 | @param model: sans.models type |
---|
[ca6d914] | 441 | @param Uid :is the key of the fitArrange dictionnary where model is saved as a value |
---|
| 442 | @param pars: the list of parameters to fit |
---|
[fd6b789] | 443 | @param constraints: list of |
---|
| 444 | tuple (name of parameter, value of parameters) |
---|
| 445 | the value of parameter must be a string to constraint 2 different |
---|
| 446 | parameters. |
---|
| 447 | Example: |
---|
| 448 | we want to fit 2 model M1 and M2 both have parameters A and B. |
---|
| 449 | constraints can be: |
---|
| 450 | constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,] |
---|
[ca6d914] | 451 | @note : pars must contains only name of existing model's paramaters |
---|
| 452 | """ |
---|
[fd6b789] | 453 | if model == None: |
---|
| 454 | raise ValueError, "AbstractFitEngine: Need to set model to fit" |
---|
| 455 | |
---|
| 456 | new_model= model |
---|
| 457 | if not issubclass(model.__class__, Model): |
---|
| 458 | new_model= Model(model) |
---|
| 459 | |
---|
| 460 | if len(constraints)>0: |
---|
| 461 | for constraint in constraints: |
---|
| 462 | name, value = constraint |
---|
| 463 | try: |
---|
| 464 | new_model.parameterset[ str(name)].set( str(value) ) |
---|
| 465 | except: |
---|
| 466 | msg= "Fit Engine: Error occurs when setting the constraint" |
---|
| 467 | msg += " %s for parameter %s "%(value, name) |
---|
| 468 | raise ValueError, msg |
---|
| 469 | |
---|
[f44dbc7] | 470 | if len(pars) >0: |
---|
[fd6b789] | 471 | temp=[] |
---|
| 472 | for item in pars: |
---|
| 473 | if item in new_model.model.getParamList(): |
---|
| 474 | temp.append(item) |
---|
| 475 | self.paramList.append(item) |
---|
| 476 | else: |
---|
| 477 | |
---|
| 478 | msg = "wrong parameter %s used"%str(item) |
---|
| 479 | msg += "to set model %s. Choose"%str(new_model.model.name) |
---|
| 480 | msg += "parameter name within %s"%str(new_model.model.getParamList()) |
---|
| 481 | raise ValueError,msg |
---|
| 482 | |
---|
[6831a99] | 483 | #A fitArrange is already created but contains dList only at Uid |
---|
[ca6d914] | 484 | if self.fitArrangeDict.has_key(Uid): |
---|
[fd6b789] | 485 | self.fitArrangeDict[Uid].set_model(new_model) |
---|
[aed7c57] | 486 | self.fitArrangeDict[Uid].pars= pars |
---|
[6831a99] | 487 | else: |
---|
| 488 | #no fitArrange object has been create with this Uid |
---|
[48882d1] | 489 | fitproblem = FitArrange() |
---|
[fd6b789] | 490 | fitproblem.set_model(new_model) |
---|
[aed7c57] | 491 | fitproblem.pars= pars |
---|
[ca6d914] | 492 | self.fitArrangeDict[Uid] = fitproblem |
---|
[aed7c57] | 493 | |
---|
[d4b0687] | 494 | else: |
---|
[6831a99] | 495 | raise ValueError, "park_integration:missing parameters" |
---|
[48882d1] | 496 | |
---|
[20d30e9] | 497 | def set_data(self,data,Uid,smearer=None,qmin=None,qmax=None): |
---|
[d4b0687] | 498 | """ Receives plottable, creates a list of data to fit,set data |
---|
| 499 | in a FitArrange object and adds that object in a dictionary |
---|
| 500 | with key Uid. |
---|
| 501 | @param data: data added |
---|
| 502 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
[ca6d914] | 503 | """ |
---|
[f2817bb] | 504 | if data.__class__.__name__=='Data2D': |
---|
[1e3169c] | 505 | fitdata=FitData2D(sans_data2d=data, data=data.data, err_data= data.err_data) |
---|
[f8ce013] | 506 | else: |
---|
[1e3169c] | 507 | fitdata=FitData1D(x=data.x, y=data.y , dx= data.dx,dy=data.dy,smearer=smearer) |
---|
[20d30e9] | 508 | |
---|
| 509 | fitdata.setFitRange(qmin=qmin,qmax=qmax) |
---|
[d4b0687] | 510 | #A fitArrange is already created but contains model only at Uid |
---|
[ca6d914] | 511 | if self.fitArrangeDict.has_key(Uid): |
---|
[f8ce013] | 512 | self.fitArrangeDict[Uid].add_data(fitdata) |
---|
[d4b0687] | 513 | else: |
---|
| 514 | #no fitArrange object has been create with this Uid |
---|
| 515 | fitproblem= FitArrange() |
---|
[f8ce013] | 516 | fitproblem.add_data(fitdata) |
---|
[ca6d914] | 517 | self.fitArrangeDict[Uid]=fitproblem |
---|
[20d30e9] | 518 | |
---|
[d4b0687] | 519 | def get_model(self,Uid): |
---|
| 520 | """ |
---|
| 521 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
| 522 | @return a model at this uid or None if no FitArrange element was created |
---|
| 523 | with this Uid |
---|
| 524 | """ |
---|
[ca6d914] | 525 | if self.fitArrangeDict.has_key(Uid): |
---|
| 526 | return self.fitArrangeDict[Uid].get_model() |
---|
[d4b0687] | 527 | else: |
---|
| 528 | return None |
---|
| 529 | |
---|
| 530 | def remove_Fit_Problem(self,Uid): |
---|
| 531 | """remove fitarrange in Uid""" |
---|
[ca6d914] | 532 | if self.fitArrangeDict.has_key(Uid): |
---|
| 533 | del self.fitArrangeDict[Uid] |
---|
[a9e04aa] | 534 | |
---|
| 535 | def select_problem_for_fit(self,Uid,value): |
---|
| 536 | """ |
---|
| 537 | select a couple of model and data at the Uid position in dictionary |
---|
| 538 | and set in self.selected value to value |
---|
| 539 | @param value: the value to allow fitting. can only have the value one or zero |
---|
| 540 | """ |
---|
| 541 | if self.fitArrangeDict.has_key(Uid): |
---|
| 542 | self.fitArrangeDict[Uid].set_to_fit( value) |
---|
[eef2e0ed] | 543 | |
---|
| 544 | |
---|
[a9e04aa] | 545 | def get_problem_to_fit(self,Uid): |
---|
| 546 | """ |
---|
| 547 | return the self.selected value of the fit problem of Uid |
---|
| 548 | @param Uid: the Uid of the problem |
---|
| 549 | """ |
---|
| 550 | if self.fitArrangeDict.has_key(Uid): |
---|
| 551 | self.fitArrangeDict[Uid].get_to_fit() |
---|
[4c718654] | 552 | |
---|
[d4b0687] | 553 | class FitArrange: |
---|
| 554 | def __init__(self): |
---|
| 555 | """ |
---|
| 556 | Class FitArrange contains a set of data for a given model |
---|
| 557 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 558 | and at least one data for the fit to be performed. |
---|
| 559 | model: the model selected by the user |
---|
| 560 | Ldata: a list of data what the user wants to fit |
---|
| 561 | |
---|
| 562 | """ |
---|
| 563 | self.model = None |
---|
| 564 | self.dList =[] |
---|
[aed7c57] | 565 | self.pars=[] |
---|
[a9e04aa] | 566 | #self.selected is zero when this fit problem is not schedule to fit |
---|
| 567 | #self.selected is 1 when schedule to fit |
---|
| 568 | self.selected = 0 |
---|
[d4b0687] | 569 | |
---|
| 570 | def set_model(self,model): |
---|
| 571 | """ |
---|
| 572 | set_model save a copy of the model |
---|
| 573 | @param model: the model being set |
---|
| 574 | """ |
---|
| 575 | self.model = model |
---|
| 576 | |
---|
| 577 | def add_data(self,data): |
---|
| 578 | """ |
---|
| 579 | add_data fill a self.dList with data to fit |
---|
| 580 | @param data: Data to add in the list |
---|
| 581 | """ |
---|
| 582 | if not data in self.dList: |
---|
| 583 | self.dList.append(data) |
---|
| 584 | |
---|
| 585 | def get_model(self): |
---|
| 586 | """ @return: saved model """ |
---|
| 587 | return self.model |
---|
| 588 | |
---|
| 589 | def get_data(self): |
---|
| 590 | """ @return: list of data dList""" |
---|
[7d0c1a8] | 591 | #return self.dList |
---|
| 592 | return self.dList[0] |
---|
[d4b0687] | 593 | |
---|
| 594 | def remove_data(self,data): |
---|
| 595 | """ |
---|
| 596 | Remove one element from the list |
---|
| 597 | @param data: Data to remove from dList |
---|
| 598 | """ |
---|
| 599 | if data in self.dList: |
---|
| 600 | self.dList.remove(data) |
---|
[a9e04aa] | 601 | def set_to_fit (self, value=0): |
---|
| 602 | """ |
---|
| 603 | set self.selected to 0 or 1 for other values raise an exception |
---|
| 604 | @param value: integer between 0 or 1 |
---|
| 605 | """ |
---|
| 606 | self.selected= value |
---|
| 607 | |
---|
| 608 | def get_to_fit(self): |
---|
| 609 | """ |
---|
| 610 | @return self.selected value |
---|
| 611 | """ |
---|
| 612 | return self.selected |
---|