Changeset 2047 for trunk/python/asapfitter.py
- Timestamp:
- 03/15/11 18:31:04 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asapfitter.py
r1938 r2047 73 73 Set the function to be fit. 74 74 Parameters: 75 poly: use a polynomial of the order given with nonlinear least squares fit 76 lpoly: use polynomial of the order given with linear least squares fit 77 gauss: fit the number of gaussian specified 78 lorentz: fit the number of lorentzian specified 75 poly: use a polynomial of the order given with nonlinear least squares fit 76 lpoly: use polynomial of the order given with linear least squares fit 77 gauss: fit the number of gaussian specified 78 lorentz: fit the number of lorentzian specified 79 sinusoid: fit the number of sinusoid specified 79 80 Example: 80 81 fitter.set_function(poly=3) # will fit a 3rd order polynomial via nonlinear method … … 82 83 fitter.set_function(gauss=2) # will fit two gaussians 83 84 fitter.set_function(lorentz=2) # will fit two lorentzians 85 fitter.set_function(sinusoid=3) # will fit three sinusoids 84 86 """ 85 87 #default poly order 0 … … 109 111 self.components = [ 3 for i in range(n) ] 110 112 self.uselinear = False 113 elif kwargs.has_key('sinusoid'): 114 n = kwargs.get('sinusoid') 115 self.fitfunc = 'sinusoid' 116 self.fitfuncs = [ 'sinusoid' for i in range(n) ] 117 self.components = [ 3 for i in range(n) ] 118 self.uselinear = False 111 119 else: 112 120 msg = "Invalid function type." … … 204 212 fixed: a vector of which parameters are to be held fixed 205 213 (default is none) 206 component: in case of multiple gaussians/lorentzians ,207 the index of the component214 component: in case of multiple gaussians/lorentzians/sinusoidals, 215 the index of the target component 208 216 """ 209 217 component = None … … 220 228 msg = "Please specify a fitting function first." 221 229 raise RuntimeError(msg) 222 if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None:230 if (self.fitfunc == "gauss" or self.fitfunc == "lorentz" or self.fitfunc == "sinusoid") and component is not None: 223 231 if not self.fitted and sum(self.fitter.getparameters()) == 0: 224 232 pars = _n_bools(len(self.components)*3, False) 225 fxd = _n_bools(len(pars), False)233 fxd = _n_bools(len(pars), False) 226 234 else: 227 235 pars = list(self.fitter.getparameters()) 228 fxd = list(self.fitter.getfixedparameters())236 fxd = list(self.fitter.getfixedparameters()) 229 237 i = 3*component 230 238 pars[i:i+3] = params 231 fxd[i:i+3] = fixed239 fxd[i:i+3] = fixed 232 240 params = pars 233 fixed = fxd241 fixed = fxd 234 242 self.fitter.setparameters(params) 235 243 if fixed is not None: … … 290 298 d = {'params':[peak, centre, fwhm], 291 299 'fixed':[peakfixed, centrefixed, fwhmfixed]} 300 self.set_parameters(d, component) 301 else: 302 msg = "Please select a valid component." 303 raise ValueError(msg) 304 305 @asaplog_post_dec 306 def set_sinusoid_parameters(self, ampl, period, x0, 307 amplfixed=0, periodfixed=0, 308 x0fixed=0, 309 component=0): 310 """ 311 Set the Parameters of a 'Sinusoidal' component, set with set_function. 312 Parameters: 313 ampl, period, x0: The sinusoidal parameters 314 amplfixed, 315 periodfixed, 316 x0fixed: Optional parameters to indicate if 317 the paramters should be held fixed during 318 the fitting process. The default is to keep 319 all parameters flexible. 320 component: The number of the component (Default is the 321 component 0) 322 """ 323 if self.fitfunc != "sinusoid": 324 msg = "Function only operates on Sinusoidal components." 325 raise ValueError(msg) 326 if 0 <= component < len(self.components): 327 d = {'params':[ampl, period, x0], 328 'fixed': [amplfixed, periodfixed, x0fixed]} 292 329 self.set_parameters(d, component) 293 330 else: … … 338 375 cerrs = errs 339 376 if component is not None: 340 if self.fitfunc == "gauss" or self.fitfunc == "lorentz" :377 if self.fitfunc == "gauss" or self.fitfunc == "lorentz" or self.fitfunc == "sinusoid": 341 378 i = 3*component 342 379 if i < len(errs): … … 361 398 area = [] 362 399 if component is not None: 363 if self.fitfunc == "gauss" or self.fitfunc == "lorentz": 400 if self.fitfunc == "poly" or self.fitfunc == "lpoly": 401 cpars = pars 402 cfixed = fixed 403 cerrs = errs 404 else: 364 405 i = 3*component 365 406 cpars = pars[i:i+3] 366 407 cfixed = fixed[i:i+3] 367 408 cerrs = errs[i:i+3] 368 a = self.get_area(component) 369 area = [a for i in range(3)] 370 else: 371 cpars = pars 372 cfixed = fixed 373 cerrs = errs 409 if self.fitfunc == "gauss" or self.fitfunc == "lorentz": 410 a = self.get_area(component) 411 area = [a for i in range(3)] 374 412 else: 375 413 cpars = pars … … 378 416 if self.fitfunc == "gauss" or self.fitfunc == "lorentz": 379 417 for c in range(len(self.components)): 380 a = self.get_area(c)381 area += [a for i in range(3)]418 a = self.get_area(c) 419 area += [a for i in range(3)] 382 420 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area) 383 421 asaplog.push(fpars) … … 387 425 def _format_pars(self, pars, fixed, errors, area): 388 426 out = '' 389 if self.fitfunc == 'poly':427 if self.fitfunc == "poly" or self.fitfunc == "lpoly": 390 428 c = 0 391 429 for i in range(len(pars)): 392 430 fix = "" 393 431 if len(fixed) and fixed[i]: fix = "(fixed)" 394 if errors : 395 out += ' p%d%s= %3.6f (%1.6f),' % (c,fix,pars[i], errors[i]) 396 else: 397 out += ' p%d%s= %3.6f,' % (c,fix,pars[i]) 432 out += " p%d%s= %3.6f" % (c, fix, pars[i]) 433 if errors : out += " (%1.6f)" % errors[i] 434 out += "," 398 435 c+=1 399 436 out = out[:-1] # remove trailing ',' 400 el if self.fitfunc == 'gauss' or self.fitfunc == 'lorentz':437 else: 401 438 i = 0 402 439 c = 0 403 aunit = '' 404 ounit = '' 440 if self.fitfunc == "gauss" or self.fitfunc == "lorentz": 441 pnam = ["peak", "centre", "FWHM"] 442 elif self.fitfunc == "sinusoid": 443 pnam = ["amplitude", "period", "x0"] 444 aunit = "" 445 ounit = "" 405 446 if self.data: 406 447 aunit = self.data.get_unit() 407 448 ounit = self.data.get_fluxunit() 408 449 while i < len(pars): 409 if len(area): 410 out += ' %2d: peak = %3.3f %s , centre = %3.3f %s, FWHM = %3.3f %s\n area = %3.3f %s %s\n' % (c,pars[i],ounit,pars[i+1],aunit,pars[i+2],aunit, area[i],ounit,aunit) 411 else: 412 out += ' %2d: peak = %3.3f %s , centre = %3.3f %s, FWHM = %3.3f %s\n' % (c,pars[i],ounit,pars[i+1],aunit,pars[i+2],aunit,ounit,aunit) 450 fix0 = fix1 = fix2 = "" 451 if i < len(fixed)-2: 452 if fixed[i]: fix0 = "(fixed)" 453 if fixed[i+1]: fix1 = "(fixed)" 454 if fixed[i+2]: fix2 = "(fixed)" 455 out += " %2d: " % c 456 out += "%s%s = %3.3f %s, " % (pnam[0], fix0, pars[i], ounit) 457 out += "%s%s = %3.3f %s, " % (pnam[1], fix1, pars[i+1], aunit) 458 out += "%s%s = %3.3f %s\n" % (pnam[2], fix2, pars[i+2], aunit) 459 if len(area): out += " area = %3.3f %s %s\n" % (area[i], ounit, aunit) 413 460 c+=1 414 461 i+=3
Note: See TracChangeset
for help on using the changeset viewer.