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