[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 | |
---|
[393f0f3] | 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: |
---|
[393f0f3] | 117 | return self.model.evalDistribution(x) |
---|
[d8a2e31] | 118 | except: |
---|
[393f0f3] | 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.res_err_image=[] |
---|
| 260 | self.index_model=[] |
---|
| 261 | self.qmin= None |
---|
| 262 | self.qmax= None |
---|
| 263 | self.set_data(sans_data2d ) |
---|
| 264 | |
---|
[027e8f2] | 265 | def set_data(self, sans_data2d, qmin=None, qmax=None ): |
---|
[1e3169c] | 266 | """ |
---|
[83195f7] | 267 | Determine the correct qx_data and qy_data within range to fit |
---|
[1e3169c] | 268 | """ |
---|
[83195f7] | 269 | self.data = sans_data2d.data |
---|
| 270 | self.err_data = sans_data2d.err_data |
---|
| 271 | self.qx_data = sans_data2d.qx_data |
---|
| 272 | self.qy_data = sans_data2d.qy_data |
---|
| 273 | self.mask = sans_data2d.mask |
---|
| 274 | |
---|
| 275 | x_max = max(math.fabs(sans_data2d.xmin), math.fabs(sans_data2d.xmax)) |
---|
| 276 | y_max = max(math.fabs(sans_data2d.ymin), math.fabs(sans_data2d.ymax)) |
---|
[20d30e9] | 277 | |
---|
| 278 | ## fitting range |
---|
[027e8f2] | 279 | if qmin == None: |
---|
| 280 | self.qmin = 1e-16 |
---|
| 281 | if qmax == None: |
---|
| 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 ==[]: |
---|
[83195f7] | 285 | self.res_err_data= numpy.zeros(len(self.data)) |
---|
[70bf68c] | 286 | else: |
---|
[da58fcc] | 287 | self.res_err_data = copy.deepcopy(self.err_data) |
---|
[83195f7] | 288 | self.res_err_data[self.res_err_data==0]=numpy.sqrt(self.data[self.res_err_data==0]) |
---|
[d8a2e31] | 289 | |
---|
[83195f7] | 290 | self.radius= numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
| 291 | |
---|
| 292 | # Note: mask = True: for MASK while mask = False for NOT to mask |
---|
| 293 | self.index_model = ((self.qmin <= self.radius)&(self.radius<= self.qmax)) |
---|
| 294 | self.mask = (self.index_model) & (self.mask) |
---|
| 295 | |
---|
[20d30e9] | 296 | def setFitRange(self,qmin=None,qmax=None): |
---|
[7d0c1a8] | 297 | """ to set the fit range""" |
---|
[773806e] | 298 | if qmin==0.0: |
---|
| 299 | self.qmin = 1e-16 |
---|
| 300 | elif qmin!=None: |
---|
| 301 | self.qmin = qmin |
---|
[eef2e0ed] | 302 | if qmax!=None: |
---|
[83195f7] | 303 | self.qmax= qmax |
---|
| 304 | self.radius= numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
| 305 | self.index_model = ((self.qmin <= self.radius)&(self.radius<= self.qmax)) |
---|
| 306 | self.mask = (self.index_model) &(self.mask) |
---|
| 307 | |
---|
[7d0c1a8] | 308 | |
---|
| 309 | def getFitRange(self): |
---|
| 310 | """ |
---|
| 311 | @return the range of data.x to fit |
---|
| 312 | """ |
---|
[20d30e9] | 313 | return self.qmin, self.qmax |
---|
[7d0c1a8] | 314 | |
---|
[d8a2e31] | 315 | def residuals(self, fn): |
---|
[83195f7] | 316 | """ |
---|
| 317 | @return the residuals |
---|
| 318 | """ |
---|
| 319 | # use only the data point within ROI range |
---|
| 320 | res=(self.data[self.mask] - fn([self.qx_data[self.mask], |
---|
| 321 | self.qy_data[self.mask]]))/self.res_err_data[self.mask] |
---|
| 322 | return res |
---|
[0e51519] | 323 | |
---|
[fd0d30fd] | 324 | |
---|
[7d0c1a8] | 325 | def residuals_deriv(self, model, pars=[]): |
---|
| 326 | """ |
---|
| 327 | @return residuals derivatives . |
---|
| 328 | @note: in this case just return empty array |
---|
| 329 | """ |
---|
| 330 | return [] |
---|
[48882d1] | 331 | |
---|
[4bd557d] | 332 | class FitAbort(Exception): |
---|
| 333 | """ |
---|
| 334 | Exception raise to stop the fit |
---|
| 335 | """ |
---|
| 336 | print"Creating fit abort Exception" |
---|
| 337 | |
---|
| 338 | |
---|
[70bf68c] | 339 | class SansAssembly: |
---|
[ca6d914] | 340 | """ |
---|
| 341 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
| 342 | """ |
---|
[393f0f3] | 343 | def __init__(self,paramlist,Model=None , Data=None, curr_thread= None): |
---|
[ca6d914] | 344 | """ |
---|
[393f0f3] | 345 | @param Model: the model wrapper fro sans -model |
---|
| 346 | @param Data: the data wrapper for sans data |
---|
[ca6d914] | 347 | """ |
---|
[393f0f3] | 348 | self.model = Model |
---|
| 349 | self.data = Data |
---|
| 350 | self.paramlist=paramlist |
---|
[4bd557d] | 351 | self.curr_thread= curr_thread |
---|
[ca6d914] | 352 | self.res=[] |
---|
[4bd557d] | 353 | self.func_name="Functor" |
---|
[48882d1] | 354 | def chisq(self, params): |
---|
| 355 | """ |
---|
| 356 | Calculates chi^2 |
---|
| 357 | @param params: list of parameter values |
---|
| 358 | @return: chi^2 |
---|
| 359 | """ |
---|
| 360 | sum = 0 |
---|
| 361 | for item in self.res: |
---|
| 362 | sum += item*item |
---|
[4bd557d] | 363 | if len(self.res)==0: |
---|
| 364 | return None |
---|
[26cb768] | 365 | return sum/ len(self.res) |
---|
[20d30e9] | 366 | |
---|
[48882d1] | 367 | def __call__(self,params): |
---|
[ca6d914] | 368 | """ |
---|
| 369 | Compute residuals |
---|
| 370 | @param params: value of parameters to fit |
---|
| 371 | """ |
---|
[681f0dc] | 372 | #import thread |
---|
[e71440c] | 373 | self.model.setParams(self.paramlist,params) |
---|
[48882d1] | 374 | self.res= self.data.residuals(self.model.eval) |
---|
[255306e] | 375 | #if self.curr_thread != None : |
---|
| 376 | # try: |
---|
| 377 | # self.curr_thread.isquit() |
---|
| 378 | # except: |
---|
| 379 | # raise FitAbort,"stop leastsqr optimizer" |
---|
[48882d1] | 380 | return self.res |
---|
| 381 | |
---|
[4c718654] | 382 | class FitEngine: |
---|
[ee5b04c] | 383 | def __init__(self): |
---|
[ca6d914] | 384 | """ |
---|
| 385 | Base class for scipy and park fit engine |
---|
| 386 | """ |
---|
| 387 | #List of parameter names to fit |
---|
[ee5b04c] | 388 | self.paramList=[] |
---|
[ca6d914] | 389 | #Dictionnary of fitArrange element (fit problems) |
---|
| 390 | self.fitArrangeDict={} |
---|
| 391 | |
---|
[4c718654] | 392 | def _concatenateData(self, listdata=[]): |
---|
| 393 | """ |
---|
| 394 | _concatenateData method concatenates each fields of all data contains ins listdata. |
---|
| 395 | @param listdata: list of data |
---|
[ca6d914] | 396 | @return Data: Data is wrapper class for sans plottable. it is created with all parameters |
---|
| 397 | of data concatenanted |
---|
[4c718654] | 398 | @raise: if listdata is empty will return None |
---|
| 399 | @raise: if data in listdata don't contain dy field ,will create an error |
---|
| 400 | during fitting |
---|
| 401 | """ |
---|
[109e60ab] | 402 | #TODO: we have to refactor the way we handle data. |
---|
| 403 | # We should move away from plottables and move towards the Data1D objects |
---|
| 404 | # defined in DataLoader. Data1D allows data manipulations, which should be |
---|
| 405 | # used to concatenate. |
---|
| 406 | # In the meantime we should switch off the concatenation. |
---|
| 407 | #if len(listdata)>1: |
---|
| 408 | # raise RuntimeError, "FitEngine._concatenateData: Multiple data files is not currently supported" |
---|
| 409 | #return listdata[0] |
---|
| 410 | |
---|
[4c718654] | 411 | if listdata==[]: |
---|
| 412 | raise ValueError, " data list missing" |
---|
| 413 | else: |
---|
| 414 | xtemp=[] |
---|
| 415 | ytemp=[] |
---|
| 416 | dytemp=[] |
---|
[48882d1] | 417 | self.mini=None |
---|
| 418 | self.maxi=None |
---|
[4c718654] | 419 | |
---|
[7d0c1a8] | 420 | for item in listdata: |
---|
| 421 | data=item.data |
---|
[48882d1] | 422 | mini,maxi=data.getFitRange() |
---|
| 423 | if self.mini==None and self.maxi==None: |
---|
| 424 | self.mini=mini |
---|
| 425 | self.maxi=maxi |
---|
| 426 | else: |
---|
| 427 | if mini < self.mini: |
---|
| 428 | self.mini=mini |
---|
| 429 | if self.maxi < maxi: |
---|
| 430 | self.maxi=maxi |
---|
| 431 | |
---|
| 432 | |
---|
[4c718654] | 433 | for i in range(len(data.x)): |
---|
| 434 | xtemp.append(data.x[i]) |
---|
| 435 | ytemp.append(data.y[i]) |
---|
| 436 | if data.dy is not None and len(data.dy)==len(data.y): |
---|
| 437 | dytemp.append(data.dy[i]) |
---|
| 438 | else: |
---|
[ee5b04c] | 439 | raise RuntimeError, "Fit._concatenateData: y-errors missing" |
---|
[20d30e9] | 440 | data= Data(x=xtemp,y=ytemp,dy=dytemp) |
---|
[48882d1] | 441 | data.setFitRange(self.mini, self.maxi) |
---|
| 442 | return data |
---|
[ca6d914] | 443 | |
---|
| 444 | |
---|
[fd6b789] | 445 | def set_model(self,model,Uid,pars=[], constraints=[]): |
---|
[ca6d914] | 446 | """ |
---|
| 447 | set a model on a given uid in the fit engine. |
---|
[fd6b789] | 448 | @param model: sans.models type |
---|
[ca6d914] | 449 | @param Uid :is the key of the fitArrange dictionnary where model is saved as a value |
---|
| 450 | @param pars: the list of parameters to fit |
---|
[fd6b789] | 451 | @param constraints: list of |
---|
| 452 | tuple (name of parameter, value of parameters) |
---|
| 453 | the value of parameter must be a string to constraint 2 different |
---|
| 454 | parameters. |
---|
| 455 | Example: |
---|
| 456 | we want to fit 2 model M1 and M2 both have parameters A and B. |
---|
| 457 | constraints can be: |
---|
| 458 | constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,] |
---|
[ca6d914] | 459 | @note : pars must contains only name of existing model's paramaters |
---|
| 460 | """ |
---|
[fd6b789] | 461 | if model == None: |
---|
| 462 | raise ValueError, "AbstractFitEngine: Need to set model to fit" |
---|
[393f0f3] | 463 | |
---|
| 464 | new_model= model |
---|
| 465 | if not issubclass(model.__class__, Model): |
---|
| 466 | new_model= Model(model) |
---|
[fd6b789] | 467 | |
---|
| 468 | if len(constraints)>0: |
---|
| 469 | for constraint in constraints: |
---|
| 470 | name, value = constraint |
---|
| 471 | try: |
---|
| 472 | new_model.parameterset[ str(name)].set( str(value) ) |
---|
| 473 | except: |
---|
| 474 | msg= "Fit Engine: Error occurs when setting the constraint" |
---|
| 475 | msg += " %s for parameter %s "%(value, name) |
---|
| 476 | raise ValueError, msg |
---|
| 477 | |
---|
[f44dbc7] | 478 | if len(pars) >0: |
---|
[fd6b789] | 479 | temp=[] |
---|
| 480 | for item in pars: |
---|
| 481 | if item in new_model.model.getParamList(): |
---|
| 482 | temp.append(item) |
---|
| 483 | self.paramList.append(item) |
---|
| 484 | else: |
---|
| 485 | |
---|
| 486 | msg = "wrong parameter %s used"%str(item) |
---|
| 487 | msg += "to set model %s. Choose"%str(new_model.model.name) |
---|
| 488 | msg += "parameter name within %s"%str(new_model.model.getParamList()) |
---|
| 489 | raise ValueError,msg |
---|
| 490 | |
---|
[6831a99] | 491 | #A fitArrange is already created but contains dList only at Uid |
---|
[ca6d914] | 492 | if self.fitArrangeDict.has_key(Uid): |
---|
[fd6b789] | 493 | self.fitArrangeDict[Uid].set_model(new_model) |
---|
[aed7c57] | 494 | self.fitArrangeDict[Uid].pars= pars |
---|
[6831a99] | 495 | else: |
---|
| 496 | #no fitArrange object has been create with this Uid |
---|
[48882d1] | 497 | fitproblem = FitArrange() |
---|
[fd6b789] | 498 | fitproblem.set_model(new_model) |
---|
[aed7c57] | 499 | fitproblem.pars= pars |
---|
[ca6d914] | 500 | self.fitArrangeDict[Uid] = fitproblem |
---|
[aed7c57] | 501 | |
---|
[d4b0687] | 502 | else: |
---|
[6831a99] | 503 | raise ValueError, "park_integration:missing parameters" |
---|
[48882d1] | 504 | |
---|
[20d30e9] | 505 | def set_data(self,data,Uid,smearer=None,qmin=None,qmax=None): |
---|
[d4b0687] | 506 | """ Receives plottable, creates a list of data to fit,set data |
---|
| 507 | in a FitArrange object and adds that object in a dictionary |
---|
| 508 | with key Uid. |
---|
| 509 | @param data: data added |
---|
| 510 | @param Uid: unique key corresponding to a fitArrange object with data |
---|
[ca6d914] | 511 | """ |
---|
[f2817bb] | 512 | if data.__class__.__name__=='Data2D': |
---|
[1e3169c] | 513 | fitdata=FitData2D(sans_data2d=data, data=data.data, err_data= data.err_data) |
---|
[f8ce013] | 514 | else: |
---|
[1e3169c] | 515 | fitdata=FitData1D(x=data.x, y=data.y , dx= data.dx,dy=data.dy,smearer=smearer) |
---|
[393f0f3] | 516 | |
---|
[20d30e9] | 517 | fitdata.setFitRange(qmin=qmin,qmax=qmax) |
---|
[d4b0687] | 518 | #A fitArrange is already created but contains model only at Uid |
---|
[ca6d914] | 519 | if self.fitArrangeDict.has_key(Uid): |
---|
[f8ce013] | 520 | self.fitArrangeDict[Uid].add_data(fitdata) |
---|
[d4b0687] | 521 | else: |
---|
| 522 | #no fitArrange object has been create with this Uid |
---|
| 523 | fitproblem= FitArrange() |
---|
[f8ce013] | 524 | fitproblem.add_data(fitdata) |
---|
[ca6d914] | 525 | self.fitArrangeDict[Uid]=fitproblem |
---|
[20d30e9] | 526 | |
---|
[d4b0687] | 527 | def get_model(self,Uid): |
---|
| 528 | """ |
---|
| 529 | @param Uid: Uid is key in the dictionary containing the model to return |
---|
| 530 | @return a model at this uid or None if no FitArrange element was created |
---|
| 531 | with this Uid |
---|
| 532 | """ |
---|
[ca6d914] | 533 | if self.fitArrangeDict.has_key(Uid): |
---|
| 534 | return self.fitArrangeDict[Uid].get_model() |
---|
[d4b0687] | 535 | else: |
---|
| 536 | return None |
---|
| 537 | |
---|
| 538 | def remove_Fit_Problem(self,Uid): |
---|
| 539 | """remove fitarrange in Uid""" |
---|
[ca6d914] | 540 | if self.fitArrangeDict.has_key(Uid): |
---|
| 541 | del self.fitArrangeDict[Uid] |
---|
[a9e04aa] | 542 | |
---|
| 543 | def select_problem_for_fit(self,Uid,value): |
---|
| 544 | """ |
---|
| 545 | select a couple of model and data at the Uid position in dictionary |
---|
| 546 | and set in self.selected value to value |
---|
| 547 | @param value: the value to allow fitting. can only have the value one or zero |
---|
| 548 | """ |
---|
| 549 | if self.fitArrangeDict.has_key(Uid): |
---|
| 550 | self.fitArrangeDict[Uid].set_to_fit( value) |
---|
[eef2e0ed] | 551 | |
---|
| 552 | |
---|
[a9e04aa] | 553 | def get_problem_to_fit(self,Uid): |
---|
| 554 | """ |
---|
| 555 | return the self.selected value of the fit problem of Uid |
---|
| 556 | @param Uid: the Uid of the problem |
---|
| 557 | """ |
---|
| 558 | if self.fitArrangeDict.has_key(Uid): |
---|
| 559 | self.fitArrangeDict[Uid].get_to_fit() |
---|
[4c718654] | 560 | |
---|
[d4b0687] | 561 | class FitArrange: |
---|
| 562 | def __init__(self): |
---|
| 563 | """ |
---|
| 564 | Class FitArrange contains a set of data for a given model |
---|
| 565 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 566 | and at least one data for the fit to be performed. |
---|
| 567 | model: the model selected by the user |
---|
| 568 | Ldata: a list of data what the user wants to fit |
---|
| 569 | |
---|
| 570 | """ |
---|
| 571 | self.model = None |
---|
| 572 | self.dList =[] |
---|
[aed7c57] | 573 | self.pars=[] |
---|
[a9e04aa] | 574 | #self.selected is zero when this fit problem is not schedule to fit |
---|
| 575 | #self.selected is 1 when schedule to fit |
---|
| 576 | self.selected = 0 |
---|
[d4b0687] | 577 | |
---|
| 578 | def set_model(self,model): |
---|
| 579 | """ |
---|
| 580 | set_model save a copy of the model |
---|
| 581 | @param model: the model being set |
---|
| 582 | """ |
---|
| 583 | self.model = model |
---|
| 584 | |
---|
| 585 | def add_data(self,data): |
---|
| 586 | """ |
---|
| 587 | add_data fill a self.dList with data to fit |
---|
| 588 | @param data: Data to add in the list |
---|
| 589 | """ |
---|
| 590 | if not data in self.dList: |
---|
| 591 | self.dList.append(data) |
---|
| 592 | |
---|
| 593 | def get_model(self): |
---|
| 594 | """ @return: saved model """ |
---|
| 595 | return self.model |
---|
| 596 | |
---|
| 597 | def get_data(self): |
---|
| 598 | """ @return: list of data dList""" |
---|
[7d0c1a8] | 599 | #return self.dList |
---|
| 600 | return self.dList[0] |
---|
[d4b0687] | 601 | |
---|
| 602 | def remove_data(self,data): |
---|
| 603 | """ |
---|
| 604 | Remove one element from the list |
---|
| 605 | @param data: Data to remove from dList |
---|
| 606 | """ |
---|
| 607 | if data in self.dList: |
---|
| 608 | self.dList.remove(data) |
---|
[a9e04aa] | 609 | def set_to_fit (self, value=0): |
---|
| 610 | """ |
---|
| 611 | set self.selected to 0 or 1 for other values raise an exception |
---|
| 612 | @param value: integer between 0 or 1 |
---|
| 613 | """ |
---|
| 614 | self.selected= value |
---|
| 615 | |
---|
| 616 | def get_to_fit(self): |
---|
| 617 | """ |
---|
| 618 | @return self.selected value |
---|
| 619 | """ |
---|
| 620 | return self.selected |
---|