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