[41517a6] | 1 | |
---|
| 2 | import copy |
---|
| 3 | #import logging |
---|
| 4 | #import sys |
---|
| 5 | import numpy |
---|
| 6 | import math |
---|
| 7 | import park |
---|
| 8 | from DataLoader.data_info import Data1D |
---|
| 9 | from DataLoader.data_info import Data2D |
---|
| 10 | import time |
---|
| 11 | _SMALLVALUE = 1.0e-10 |
---|
| 12 | |
---|
| 13 | class SansParameter(park.Parameter): |
---|
| 14 | """ |
---|
| 15 | SANS model parameters for use in the PARK fitting service. |
---|
| 16 | The parameter attribute value is redirected to the underlying |
---|
| 17 | parameter value in the SANS model. |
---|
| 18 | """ |
---|
| 19 | def __init__(self, name, model): |
---|
| 20 | """ |
---|
| 21 | :param name: the name of the model parameter |
---|
| 22 | :param model: the sans model to wrap as a park model |
---|
| 23 | |
---|
| 24 | """ |
---|
| 25 | park.Parameter.__init__(self, name) |
---|
| 26 | self._model, self._name = model, name |
---|
| 27 | #set the value for the parameter of the given name |
---|
| 28 | self.set(model.getParam(name)) |
---|
| 29 | |
---|
| 30 | def _getvalue(self): |
---|
| 31 | """ |
---|
| 32 | override the _getvalue of park parameter |
---|
| 33 | |
---|
| 34 | :return value the parameter associates with self.name |
---|
| 35 | |
---|
| 36 | """ |
---|
| 37 | return self._model.getParam(self.name) |
---|
| 38 | |
---|
| 39 | def _setvalue(self, value): |
---|
| 40 | """ |
---|
| 41 | override the _setvalue pf park parameter |
---|
| 42 | |
---|
| 43 | :param value: the value to set on a given parameter |
---|
| 44 | |
---|
| 45 | """ |
---|
| 46 | self._model.setParam(self.name, value) |
---|
| 47 | |
---|
| 48 | value = property(_getvalue, _setvalue) |
---|
| 49 | |
---|
| 50 | def _getrange(self): |
---|
| 51 | """ |
---|
| 52 | Override _getrange of park parameter |
---|
| 53 | return the range of parameter |
---|
| 54 | """ |
---|
| 55 | #if not self.name in self._model.getDispParamList(): |
---|
| 56 | lo, hi = self._model.details[self.name][1:3] |
---|
| 57 | if lo is None: lo = -numpy.inf |
---|
| 58 | if hi is None: hi = numpy.inf |
---|
| 59 | #else: |
---|
| 60 | #lo,hi = self._model.details[self.name][1:] |
---|
| 61 | #if lo is None: lo = -numpy.inf |
---|
| 62 | #if hi is None: hi = numpy.inf |
---|
| 63 | if lo > hi: |
---|
| 64 | raise ValueError,"wrong fit range for parameters" |
---|
| 65 | |
---|
| 66 | return lo, hi |
---|
| 67 | |
---|
| 68 | def get_name(self): |
---|
| 69 | """ |
---|
| 70 | """ |
---|
| 71 | return self._getname() |
---|
| 72 | |
---|
| 73 | def _setrange(self, r): |
---|
| 74 | """ |
---|
| 75 | override _setrange of park parameter |
---|
| 76 | |
---|
| 77 | :param r: the value of the range to set |
---|
| 78 | |
---|
| 79 | """ |
---|
| 80 | self._model.details[self.name][1:3] = r |
---|
| 81 | range = property(_getrange, _setrange) |
---|
| 82 | |
---|
| 83 | class Model(park.Model): |
---|
| 84 | """ |
---|
| 85 | PARK wrapper for SANS models. |
---|
| 86 | """ |
---|
| 87 | def __init__(self, sans_model, **kw): |
---|
| 88 | """ |
---|
| 89 | :param sans_model: the sans model to wrap using park interface |
---|
| 90 | |
---|
| 91 | """ |
---|
| 92 | park.Model.__init__(self, **kw) |
---|
| 93 | self.model = sans_model |
---|
| 94 | self.name = sans_model.name |
---|
| 95 | #list of parameters names |
---|
| 96 | self.sansp = sans_model.getParamList() |
---|
| 97 | #list of park parameter |
---|
| 98 | self.parkp = [SansParameter(p, sans_model) for p in self.sansp] |
---|
| 99 | #list of parameterset |
---|
| 100 | self.parameterset = park.ParameterSet(sans_model.name, pars=self.parkp) |
---|
| 101 | self.pars = [] |
---|
| 102 | |
---|
| 103 | def get_params(self, fitparams): |
---|
| 104 | """ |
---|
| 105 | return a list of value of paramter to fit |
---|
| 106 | |
---|
| 107 | :param fitparams: list of paramaters name to fit |
---|
| 108 | |
---|
| 109 | """ |
---|
| 110 | list_params = [] |
---|
| 111 | self.pars = [] |
---|
| 112 | self.pars = fitparams |
---|
| 113 | for item in fitparams: |
---|
| 114 | for element in self.parkp: |
---|
| 115 | if element.name == str(item): |
---|
| 116 | list_params.append(element.value) |
---|
| 117 | return list_params |
---|
| 118 | |
---|
| 119 | def set_params(self, paramlist, params): |
---|
| 120 | """ |
---|
| 121 | Set value for parameters to fit |
---|
| 122 | |
---|
| 123 | :param params: list of value for parameters to fit |
---|
| 124 | |
---|
| 125 | """ |
---|
| 126 | try: |
---|
| 127 | for i in range(len(self.parkp)): |
---|
| 128 | for j in range(len(paramlist)): |
---|
| 129 | if self.parkp[i].name == paramlist[j]: |
---|
| 130 | self.parkp[i].value = params[j] |
---|
| 131 | self.model.setParam(self.parkp[i].name, params[j]) |
---|
| 132 | except: |
---|
| 133 | raise |
---|
| 134 | |
---|
| 135 | def eval(self, x): |
---|
| 136 | """ |
---|
| 137 | override eval method of park model. |
---|
| 138 | |
---|
| 139 | :param x: the x value used to compute a function |
---|
| 140 | |
---|
| 141 | """ |
---|
| 142 | try: |
---|
| 143 | return self.model.evalDistribution(x) |
---|
| 144 | except: |
---|
| 145 | raise |
---|
| 146 | |
---|
| 147 | def eval_derivs(self, x, pars=[]): |
---|
| 148 | """ |
---|
| 149 | Evaluate the model and derivatives wrt pars at x. |
---|
| 150 | |
---|
| 151 | pars is a list of the names of the parameters for which derivatives |
---|
| 152 | are desired. |
---|
| 153 | |
---|
| 154 | This method needs to be specialized in the model to evaluate the |
---|
| 155 | model function. Alternatively, the model can implement is own |
---|
| 156 | version of residuals which calculates the residuals directly |
---|
| 157 | instead of calling eval. |
---|
| 158 | """ |
---|
| 159 | return [] |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | class FitData1D(Data1D): |
---|
| 164 | """ |
---|
| 165 | Wrapper class for SANS data |
---|
| 166 | FitData1D inherits from DataLoader.data_info.Data1D. Implements |
---|
| 167 | a way to get residuals from data. |
---|
| 168 | """ |
---|
| 169 | def __init__(self, x, y, dx=None, dy=None, smearer=None): |
---|
| 170 | """ |
---|
| 171 | :param smearer: is an object of class QSmearer or SlitSmearer |
---|
| 172 | that will smear the theory data (slit smearing or resolution |
---|
| 173 | smearing) when set. |
---|
| 174 | |
---|
| 175 | The proper way to set the smearing object would be to |
---|
| 176 | do the following: :: |
---|
| 177 | |
---|
| 178 | from DataLoader.qsmearing import smear_selection |
---|
| 179 | smearer = smear_selection(some_data) |
---|
| 180 | fitdata1d = FitData1D( x= [1,3,..,], |
---|
| 181 | y= [3,4,..,8], |
---|
| 182 | dx=None, |
---|
| 183 | dy=[1,2...], smearer= smearer) |
---|
| 184 | |
---|
| 185 | :Note: that some_data _HAS_ to be of class DataLoader.data_info.Data1D |
---|
| 186 | Setting it back to None will turn smearing off. |
---|
| 187 | |
---|
| 188 | """ |
---|
| 189 | Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) |
---|
| 190 | |
---|
| 191 | self.smearer = smearer |
---|
| 192 | self._first_unsmeared_bin = None |
---|
| 193 | self._last_unsmeared_bin = None |
---|
| 194 | # Check error bar; if no error bar found, set it constant(=1) |
---|
| 195 | # TODO: Should provide an option for users to set it like percent, |
---|
| 196 | # constant, or dy data |
---|
| 197 | if dy == None or dy == [] or dy.all() == 0: |
---|
| 198 | self.dy = numpy.ones(len(y)) |
---|
| 199 | else: |
---|
| 200 | self.dy = numpy.asarray(dy).copy() |
---|
| 201 | |
---|
| 202 | ## Min Q-value |
---|
| 203 | #Skip the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
| 204 | if min (self.x) == 0.0 and self.x[0] == 0 and\ |
---|
| 205 | not numpy.isfinite(self.y[0]): |
---|
| 206 | self.qmin = min(self.x[self.x!=0]) |
---|
| 207 | else: |
---|
| 208 | self.qmin = min(self.x) |
---|
| 209 | ## Max Q-value |
---|
| 210 | self.qmax = max(self.x) |
---|
| 211 | |
---|
| 212 | # Range used for input to smearing |
---|
| 213 | self._qmin_unsmeared = self.qmin |
---|
| 214 | self._qmax_unsmeared = self.qmax |
---|
| 215 | # Identify the bin range for the unsmeared and smeared spaces |
---|
| 216 | self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) |
---|
| 217 | self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ |
---|
| 218 | & (self.x <= self._qmax_unsmeared) |
---|
| 219 | |
---|
| 220 | def set_fit_range(self, qmin=None, qmax=None): |
---|
| 221 | """ to set the fit range""" |
---|
| 222 | # Skip Q=0 point, (especially for y(q=0)=None at x[0]). |
---|
| 223 | # ToDo: Find better way to do it. |
---|
| 224 | if qmin == 0.0 and not numpy.isfinite(self.y[qmin]): |
---|
| 225 | self.qmin = min(self.x[self.x != 0]) |
---|
| 226 | elif qmin != None: |
---|
| 227 | self.qmin = qmin |
---|
| 228 | if qmax != None: |
---|
| 229 | self.qmax = qmax |
---|
| 230 | # Determine the range needed in unsmeared-Q to cover |
---|
| 231 | # the smeared Q range |
---|
| 232 | self._qmin_unsmeared = self.qmin |
---|
| 233 | self._qmax_unsmeared = self.qmax |
---|
| 234 | |
---|
| 235 | self._first_unsmeared_bin = 0 |
---|
| 236 | self._last_unsmeared_bin = len(self.x) - 1 |
---|
| 237 | |
---|
| 238 | if self.smearer != None: |
---|
| 239 | self._first_unsmeared_bin, self._last_unsmeared_bin = \ |
---|
| 240 | self.smearer.get_bin_range(self.qmin, self.qmax) |
---|
| 241 | self._qmin_unsmeared = self.x[self._first_unsmeared_bin] |
---|
| 242 | self._qmax_unsmeared = self.x[self._last_unsmeared_bin] |
---|
| 243 | |
---|
| 244 | # Identify the bin range for the unsmeared and smeared spaces |
---|
| 245 | self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) |
---|
| 246 | ## zero error can not participate for fitting |
---|
| 247 | self.idx = self.idx & (self.dy != 0) |
---|
| 248 | self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ |
---|
| 249 | & (self.x <= self._qmax_unsmeared) |
---|
| 250 | |
---|
| 251 | def get_fit_range(self): |
---|
| 252 | """ |
---|
| 253 | return the range of data.x to fit |
---|
| 254 | """ |
---|
| 255 | return self.qmin, self.qmax |
---|
| 256 | |
---|
| 257 | def residuals(self, fn): |
---|
| 258 | """ |
---|
| 259 | Compute residuals. |
---|
| 260 | |
---|
| 261 | If self.smearer has been set, use if to smear |
---|
| 262 | the data before computing chi squared. |
---|
| 263 | |
---|
| 264 | :param fn: function that return model value |
---|
| 265 | |
---|
| 266 | :return: residuals |
---|
| 267 | |
---|
| 268 | """ |
---|
| 269 | # Compute theory data f(x) |
---|
| 270 | fx = numpy.zeros(len(self.x)) |
---|
| 271 | fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) |
---|
| 272 | |
---|
| 273 | ## Smear theory data |
---|
| 274 | if self.smearer is not None: |
---|
| 275 | fx = self.smearer(fx, self._first_unsmeared_bin, |
---|
| 276 | self._last_unsmeared_bin) |
---|
| 277 | ## Sanity check |
---|
| 278 | if numpy.size(self.dy) != numpy.size(fx): |
---|
| 279 | msg = "FitData1D: invalid error array " |
---|
| 280 | msg += "%d <> %d" % (numpy.shape(self.dy), numpy.size(fx)) |
---|
| 281 | raise RuntimeError, msg |
---|
| 282 | return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx] |
---|
| 283 | |
---|
| 284 | def residuals_deriv(self, model, pars=[]): |
---|
| 285 | """ |
---|
| 286 | :return: residuals derivatives . |
---|
| 287 | |
---|
| 288 | :note: in this case just return empty array |
---|
| 289 | |
---|
| 290 | """ |
---|
| 291 | return [] |
---|
| 292 | |
---|
| 293 | class FitData2D(Data2D): |
---|
| 294 | """ Wrapper class for SANS data """ |
---|
| 295 | def __init__(self, sans_data2d, data=None, err_data=None): |
---|
| 296 | Data2D.__init__(self, data=data, err_data=err_data) |
---|
| 297 | """ |
---|
| 298 | Data can be initital with a data (sans plottable) |
---|
| 299 | or with vectors. |
---|
| 300 | """ |
---|
| 301 | self.res_err_image = [] |
---|
| 302 | self.index_model = [] |
---|
| 303 | self.qmin = None |
---|
| 304 | self.qmax = None |
---|
| 305 | self.smearer = None |
---|
| 306 | self.radius = 0 |
---|
| 307 | self.res_err_data = [] |
---|
| 308 | self.set_data(sans_data2d) |
---|
| 309 | |
---|
| 310 | def set_data(self, sans_data2d, qmin=None, qmax=None): |
---|
| 311 | """ |
---|
| 312 | Determine the correct qx_data and qy_data within range to fit |
---|
| 313 | """ |
---|
| 314 | self.data = sans_data2d.data |
---|
| 315 | self.err_data = sans_data2d.err_data |
---|
| 316 | self.qx_data = sans_data2d.qx_data |
---|
| 317 | self.qy_data = sans_data2d.qy_data |
---|
| 318 | self.mask = sans_data2d.mask |
---|
| 319 | |
---|
| 320 | x_max = max(math.fabs(sans_data2d.xmin), math.fabs(sans_data2d.xmax)) |
---|
| 321 | y_max = max(math.fabs(sans_data2d.ymin), math.fabs(sans_data2d.ymax)) |
---|
| 322 | |
---|
| 323 | ## fitting range |
---|
| 324 | if qmin == None: |
---|
| 325 | self.qmin = 1e-16 |
---|
| 326 | if qmax == None: |
---|
| 327 | self.qmax = math.sqrt(x_max * x_max + y_max * y_max) |
---|
| 328 | ## new error image for fitting purpose |
---|
| 329 | if self.err_data == None or self.err_data == []: |
---|
| 330 | self.res_err_data = numpy.ones(len(self.data)) |
---|
| 331 | else: |
---|
| 332 | self.res_err_data = copy.deepcopy(self.err_data) |
---|
| 333 | #self.res_err_data[self.res_err_data==0]=1 |
---|
| 334 | |
---|
| 335 | self.radius = numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
| 336 | |
---|
| 337 | # Note: mask = True: for MASK while mask = False for NOT to mask |
---|
| 338 | self.index_model = ((self.qmin <= self.radius)&\ |
---|
| 339 | (self.radius <= self.qmax)) |
---|
| 340 | self.index_model = (self.index_model) & (self.mask) |
---|
| 341 | self.index_model = (self.index_model) & (numpy.isfinite(self.data)) |
---|
| 342 | |
---|
| 343 | def set_smearer(self, smearer): |
---|
| 344 | """ |
---|
| 345 | Set smearer |
---|
| 346 | """ |
---|
| 347 | if smearer == None: |
---|
| 348 | return |
---|
| 349 | self.smearer = smearer |
---|
| 350 | self.smearer.set_index(self.index_model) |
---|
| 351 | self.smearer.get_data() |
---|
| 352 | |
---|
| 353 | def set_fit_range(self, qmin=None, qmax=None): |
---|
| 354 | """ to set the fit range""" |
---|
| 355 | if qmin == 0.0: |
---|
| 356 | self.qmin = 1e-16 |
---|
| 357 | elif qmin != None: |
---|
| 358 | self.qmin = qmin |
---|
| 359 | if qmax != None: |
---|
| 360 | self.qmax = qmax |
---|
| 361 | self.radius = numpy.sqrt(self.qx_data**2 + self.qy_data**2) |
---|
| 362 | self.index_model = ((self.qmin <= self.radius)&\ |
---|
| 363 | (self.radius <= self.qmax)) |
---|
| 364 | self.index_model = (self.index_model) &(self.mask) |
---|
| 365 | self.index_model = (self.index_model) & (numpy.isfinite(self.data)) |
---|
| 366 | self.index_model = (self.index_model) & (self.res_err_data != 0) |
---|
| 367 | |
---|
| 368 | def get_fit_range(self): |
---|
| 369 | """ |
---|
| 370 | return the range of data.x to fit |
---|
| 371 | """ |
---|
| 372 | return self.qmin, self.qmax |
---|
| 373 | |
---|
| 374 | def residuals(self, fn): |
---|
| 375 | """ |
---|
| 376 | return the residuals |
---|
| 377 | """ |
---|
| 378 | if self.smearer != None: |
---|
| 379 | fn.set_index(self.index_model) |
---|
| 380 | # Get necessary data from self.data and set the data for smearing |
---|
| 381 | fn.get_data() |
---|
| 382 | |
---|
| 383 | gn = fn.get_value() |
---|
| 384 | else: |
---|
| 385 | gn = fn([self.qx_data[self.index_model], |
---|
| 386 | self.qy_data[self.index_model]]) |
---|
| 387 | # use only the data point within ROI range |
---|
| 388 | res = (self.data[self.index_model] - gn)/\ |
---|
| 389 | self.res_err_data[self.index_model] |
---|
| 390 | return res |
---|
| 391 | |
---|
| 392 | def residuals_deriv(self, model, pars=[]): |
---|
| 393 | """ |
---|
| 394 | :return: residuals derivatives . |
---|
| 395 | |
---|
| 396 | :note: in this case just return empty array |
---|
| 397 | |
---|
| 398 | """ |
---|
| 399 | return [] |
---|
| 400 | |
---|
| 401 | class FitAbort(Exception): |
---|
| 402 | """ |
---|
| 403 | Exception raise to stop the fit |
---|
| 404 | """ |
---|
| 405 | #pass |
---|
| 406 | #print"Creating fit abort Exception" |
---|
| 407 | |
---|
| 408 | |
---|
| 409 | class SansAssembly: |
---|
| 410 | """ |
---|
| 411 | Sans Assembly class a class wrapper to be call in optimizer.leastsq method |
---|
| 412 | """ |
---|
| 413 | def __init__(self, paramlist, model=None , data=None, fitresult=None, |
---|
| 414 | handler=None, curr_thread=None): |
---|
| 415 | """ |
---|
| 416 | :param Model: the model wrapper fro sans -model |
---|
| 417 | :param Data: the data wrapper for sans data |
---|
| 418 | |
---|
| 419 | """ |
---|
| 420 | self.model = model |
---|
| 421 | self.data = data |
---|
| 422 | self.paramlist = paramlist |
---|
| 423 | self.curr_thread = curr_thread |
---|
| 424 | self.handler = handler |
---|
| 425 | self.fitresult = fitresult |
---|
| 426 | self.res = [] |
---|
| 427 | self.true_res = [] |
---|
| 428 | self.func_name = "Functor" |
---|
| 429 | |
---|
| 430 | #def chisq(self, params): |
---|
| 431 | def chisq(self): |
---|
| 432 | """ |
---|
| 433 | Calculates chi^2 |
---|
| 434 | |
---|
| 435 | :param params: list of parameter values |
---|
| 436 | |
---|
| 437 | :return: chi^2 |
---|
| 438 | |
---|
| 439 | """ |
---|
| 440 | sum = 0 |
---|
| 441 | for item in self.true_res: |
---|
| 442 | sum += item * item |
---|
| 443 | if len(self.true_res) == 0: |
---|
| 444 | return None |
---|
| 445 | return sum / len(self.true_res) |
---|
| 446 | |
---|
| 447 | def __call__(self, params): |
---|
| 448 | """ |
---|
| 449 | Compute residuals |
---|
| 450 | |
---|
| 451 | :param params: value of parameters to fit |
---|
| 452 | |
---|
| 453 | """ |
---|
| 454 | #import thread |
---|
| 455 | self.model.set_params(self.paramlist, params) |
---|
| 456 | self.true_res = self.data.residuals(self.model.eval) |
---|
| 457 | # check parameters range |
---|
| 458 | if self.check_param_range(): |
---|
| 459 | # if the param value is outside of the bound |
---|
| 460 | # just silent return res = inf |
---|
| 461 | return self.res |
---|
| 462 | self.res = self.true_res |
---|
| 463 | if self.fitresult is not None and self.handler is not None: |
---|
| 464 | self.fitresult.set_model(model=self.model) |
---|
| 465 | #fitness = self.chisq(params=params) |
---|
| 466 | fitness = self.chisq() |
---|
| 467 | self.fitresult.pvec = params |
---|
| 468 | self.fitresult.set_fitness(fitness=fitness) |
---|
| 469 | self.handler.set_result(result=self.fitresult) |
---|
| 470 | self.handler.update_fit() |
---|
| 471 | |
---|
| 472 | if self.curr_thread != None : |
---|
| 473 | try: |
---|
| 474 | self.curr_thread.isquit() |
---|
| 475 | except: |
---|
| 476 | msg = "Fitting: Terminated... Note: Forcing to stop " |
---|
| 477 | msg += "fitting may cause a 'Functor error message' " |
---|
| 478 | msg += "being recorded in the log file....." |
---|
| 479 | self.handler.error(msg) |
---|
| 480 | raise |
---|
| 481 | #return |
---|
| 482 | |
---|
| 483 | return self.res |
---|
| 484 | |
---|
| 485 | def check_param_range(self): |
---|
| 486 | """ |
---|
| 487 | Check the lower and upper bound of the parameter value |
---|
| 488 | and set res to the inf if the value is outside of the |
---|
| 489 | range |
---|
| 490 | :limitation: the initial values must be within range. |
---|
| 491 | """ |
---|
| 492 | |
---|
| 493 | #time.sleep(0.01) |
---|
| 494 | is_outofbound = False |
---|
| 495 | # loop through the fit parameters |
---|
| 496 | for p in self.model.parameterset: |
---|
| 497 | param_name = p.get_name() |
---|
| 498 | if param_name in self.paramlist: |
---|
| 499 | |
---|
| 500 | # if the range was defined, check the range |
---|
| 501 | if numpy.isfinite(p.range[0]): |
---|
| 502 | if p.value == 0: |
---|
| 503 | # This value works on Scipy |
---|
| 504 | # Do not change numbers below |
---|
| 505 | value = _SMALLVALUE |
---|
| 506 | else: |
---|
| 507 | value = p.value |
---|
| 508 | # For leastsq, it needs a bit step back from the boundary |
---|
| 509 | val = p.range[0] - value * _SMALLVALUE |
---|
| 510 | if p.value < val: |
---|
| 511 | self.res *= 1e+6 |
---|
| 512 | |
---|
| 513 | is_outofbound = True |
---|
| 514 | break |
---|
| 515 | if numpy.isfinite(p.range[1]): |
---|
| 516 | # This value works on Scipy |
---|
| 517 | # Do not change numbers below |
---|
| 518 | if p.value == 0: |
---|
| 519 | value = _SMALLVALUE |
---|
| 520 | else: |
---|
| 521 | value = p.value |
---|
| 522 | # For leastsq, it needs a bit step back from the boundary |
---|
| 523 | val = p.range[1] + value * _SMALLVALUE |
---|
| 524 | if p.value > val: |
---|
| 525 | self.res *= 1e+6 |
---|
| 526 | is_outofbound = True |
---|
| 527 | break |
---|
| 528 | |
---|
| 529 | return is_outofbound |
---|
| 530 | |
---|
| 531 | |
---|
| 532 | class FitEngine: |
---|
| 533 | def __init__(self): |
---|
| 534 | """ |
---|
| 535 | Base class for scipy and park fit engine |
---|
| 536 | """ |
---|
| 537 | #List of parameter names to fit |
---|
| 538 | self.param_list = [] |
---|
| 539 | #Dictionnary of fitArrange element (fit problems) |
---|
| 540 | self.fit_arrange_dict = {} |
---|
| 541 | |
---|
| 542 | def set_model(self, model, id, pars=[], constraints=[]): |
---|
| 543 | """ |
---|
| 544 | set a model on a given in the fit engine. |
---|
| 545 | |
---|
| 546 | :param model: sans.models type |
---|
| 547 | :param : is the key of the fitArrange dictionary where model is |
---|
| 548 | saved as a value |
---|
| 549 | :param pars: the list of parameters to fit |
---|
| 550 | :param constraints: list of |
---|
| 551 | tuple (name of parameter, value of parameters) |
---|
| 552 | the value of parameter must be a string to constraint 2 different |
---|
| 553 | parameters. |
---|
| 554 | Example: |
---|
| 555 | we want to fit 2 model M1 and M2 both have parameters A and B. |
---|
| 556 | constraints can be: |
---|
| 557 | constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,] |
---|
| 558 | |
---|
| 559 | |
---|
| 560 | :note: pars must contains only name of existing model's parameters |
---|
| 561 | |
---|
| 562 | """ |
---|
| 563 | if model == None: |
---|
| 564 | raise ValueError, "AbstractFitEngine: Need to set model to fit" |
---|
| 565 | |
---|
| 566 | new_model = model |
---|
| 567 | if not issubclass(model.__class__, Model): |
---|
| 568 | new_model = Model(model) |
---|
| 569 | |
---|
| 570 | if len(constraints) > 0: |
---|
| 571 | for constraint in constraints: |
---|
| 572 | name, value = constraint |
---|
| 573 | try: |
---|
| 574 | new_model.parameterset[str(name)].set(str(value)) |
---|
| 575 | except: |
---|
| 576 | msg = "Fit Engine: Error occurs when setting the constraint" |
---|
| 577 | msg += " %s for parameter %s " % (value, name) |
---|
| 578 | raise ValueError, msg |
---|
| 579 | |
---|
| 580 | if len(pars) > 0: |
---|
| 581 | temp = [] |
---|
| 582 | for item in pars: |
---|
| 583 | if item in new_model.model.getParamList(): |
---|
| 584 | temp.append(item) |
---|
| 585 | self.param_list.append(item) |
---|
| 586 | else: |
---|
| 587 | |
---|
| 588 | msg = "wrong parameter %s used" % str(item) |
---|
| 589 | msg += "to set model %s. Choose" % str(new_model.model.name) |
---|
| 590 | msg += "parameter name within %s" % \ |
---|
| 591 | str(new_model.model.getParamList()) |
---|
| 592 | raise ValueError, msg |
---|
| 593 | |
---|
| 594 | #A fitArrange is already created but contains data_list only at id |
---|
| 595 | if self.fit_arrange_dict.has_key(id): |
---|
| 596 | self.fit_arrange_dict[id].set_model(new_model) |
---|
| 597 | self.fit_arrange_dict[id].pars = pars |
---|
| 598 | else: |
---|
| 599 | #no fitArrange object has been create with this id |
---|
| 600 | fitproblem = FitArrange() |
---|
| 601 | fitproblem.set_model(new_model) |
---|
| 602 | fitproblem.pars = pars |
---|
| 603 | self.fit_arrange_dict[id] = fitproblem |
---|
| 604 | |
---|
| 605 | else: |
---|
| 606 | raise ValueError, "park_integration:missing parameters" |
---|
| 607 | |
---|
| 608 | def set_data(self, data, id, smearer=None, qmin=None, qmax=None): |
---|
| 609 | """ |
---|
| 610 | Receives plottable, creates a list of data to fit,set data |
---|
| 611 | in a FitArrange object and adds that object in a dictionary |
---|
| 612 | with key id. |
---|
| 613 | |
---|
| 614 | :param data: data added |
---|
| 615 | :param id: unique key corresponding to a fitArrange object with data |
---|
| 616 | |
---|
| 617 | """ |
---|
| 618 | if data.__class__.__name__ == 'Data2D': |
---|
| 619 | fitdata = FitData2D(sans_data2d=data, data=data.data, |
---|
| 620 | err_data=data.err_data) |
---|
| 621 | else: |
---|
| 622 | fitdata = FitData1D(x=data.x, y=data.y , |
---|
| 623 | dx=data.dx, dy=data.dy, smearer=smearer) |
---|
| 624 | |
---|
| 625 | fitdata.set_fit_range(qmin=qmin, qmax=qmax) |
---|
| 626 | #A fitArrange is already created but contains model only at id |
---|
| 627 | if self.fit_arrange_dict.has_key(id): |
---|
| 628 | self.fit_arrange_dict[id].add_data(fitdata) |
---|
| 629 | else: |
---|
| 630 | #no fitArrange object has been create with this id |
---|
| 631 | fitproblem = FitArrange() |
---|
| 632 | fitproblem.add_data(fitdata) |
---|
| 633 | self.fit_arrange_dict[id] = fitproblem |
---|
| 634 | |
---|
| 635 | def get_model(self, id): |
---|
| 636 | """ |
---|
| 637 | |
---|
| 638 | :param id: id is key in the dictionary containing the model to return |
---|
| 639 | |
---|
| 640 | :return: a model at this id or None if no FitArrange element was |
---|
| 641 | created with this id |
---|
| 642 | |
---|
| 643 | """ |
---|
| 644 | if self.fit_arrange_dict.has_key(id): |
---|
| 645 | return self.fit_arrange_dict[id].get_model() |
---|
| 646 | else: |
---|
| 647 | return None |
---|
| 648 | |
---|
| 649 | def remove_fit_problem(self, id): |
---|
| 650 | """remove fitarrange in id""" |
---|
| 651 | if self.fit_arrange_dict.has_key(id): |
---|
| 652 | del self.fit_arrange_dict[id] |
---|
| 653 | |
---|
| 654 | def select_problem_for_fit(self, id, value): |
---|
| 655 | """ |
---|
| 656 | select a couple of model and data at the id position in dictionary |
---|
| 657 | and set in self.selected value to value |
---|
| 658 | |
---|
| 659 | :param value: the value to allow fitting. |
---|
| 660 | can only have the value one or zero |
---|
| 661 | |
---|
| 662 | """ |
---|
| 663 | if self.fit_arrange_dict.has_key(id): |
---|
| 664 | self.fit_arrange_dict[id].set_to_fit(value) |
---|
| 665 | |
---|
| 666 | def get_problem_to_fit(self, id): |
---|
| 667 | """ |
---|
| 668 | return the self.selected value of the fit problem of id |
---|
| 669 | |
---|
| 670 | :param id: the id of the problem |
---|
| 671 | |
---|
| 672 | """ |
---|
| 673 | if self.fit_arrange_dict.has_key(id): |
---|
| 674 | self.fit_arrange_dict[id].get_to_fit() |
---|
| 675 | |
---|
| 676 | class FitArrange: |
---|
| 677 | def __init__(self): |
---|
| 678 | """ |
---|
| 679 | Class FitArrange contains a set of data for a given model |
---|
| 680 | to perform the Fit.FitArrange must contain exactly one model |
---|
| 681 | and at least one data for the fit to be performed. |
---|
| 682 | |
---|
| 683 | model: the model selected by the user |
---|
| 684 | Ldata: a list of data what the user wants to fit |
---|
| 685 | |
---|
| 686 | """ |
---|
| 687 | self.model = None |
---|
| 688 | self.data_list = [] |
---|
| 689 | self.pars = [] |
---|
| 690 | #self.selected is zero when this fit problem is not schedule to fit |
---|
| 691 | #self.selected is 1 when schedule to fit |
---|
| 692 | self.selected = 0 |
---|
| 693 | |
---|
| 694 | def set_model(self, model): |
---|
| 695 | """ |
---|
| 696 | set_model save a copy of the model |
---|
| 697 | |
---|
| 698 | :param model: the model being set |
---|
| 699 | |
---|
| 700 | """ |
---|
| 701 | self.model = model |
---|
| 702 | |
---|
| 703 | def add_data(self, data): |
---|
| 704 | """ |
---|
| 705 | add_data fill a self.data_list with data to fit |
---|
| 706 | |
---|
| 707 | :param data: Data to add in the list |
---|
| 708 | |
---|
| 709 | """ |
---|
| 710 | if not data in self.data_list: |
---|
| 711 | self.data_list.append(data) |
---|
| 712 | |
---|
| 713 | def get_model(self): |
---|
| 714 | """ |
---|
| 715 | |
---|
| 716 | :return: saved model |
---|
| 717 | |
---|
| 718 | """ |
---|
| 719 | return self.model |
---|
| 720 | |
---|
| 721 | def get_data(self): |
---|
| 722 | """ |
---|
| 723 | |
---|
| 724 | :return: list of data data_list |
---|
| 725 | |
---|
| 726 | """ |
---|
| 727 | #return self.data_list |
---|
| 728 | return self.data_list[0] |
---|
| 729 | |
---|
| 730 | def remove_data(self, data): |
---|
| 731 | """ |
---|
| 732 | Remove one element from the list |
---|
| 733 | |
---|
| 734 | :param data: Data to remove from data_list |
---|
| 735 | |
---|
| 736 | """ |
---|
| 737 | if data in self.data_list: |
---|
| 738 | self.data_list.remove(data) |
---|
| 739 | |
---|
| 740 | def set_to_fit (self, value=0): |
---|
| 741 | """ |
---|
| 742 | set self.selected to 0 or 1 for other values raise an exception |
---|
| 743 | |
---|
| 744 | :param value: integer between 0 or 1 |
---|
| 745 | |
---|
| 746 | """ |
---|
| 747 | self.selected = value |
---|
| 748 | |
---|
| 749 | def get_to_fit(self): |
---|
| 750 | """ |
---|
| 751 | return self.selected value |
---|
| 752 | """ |
---|
| 753 | return self.selected |
---|