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