Changes in trunk/python/asapfitter.py [1938:1826]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asapfitter.py
r1938 r1826 1 1 import _asap 2 2 from asap.parameters import rcParams 3 from asap.logging import asaplog, asaplog_post_dec3 from asap.logging import asaplog, print_log, print_log_dec 4 4 from asap.utils import _n_bools, mask_and 5 5 … … 9 9 The fitting class for ASAP. 10 10 """ 11 11 12 def __init__(self): 12 13 """ … … 48 49 return 49 50 50 @asaplog_post_dec51 51 def set_scan(self, thescan=None, mask=None): 52 52 """ … … 58 58 if not thescan: 59 59 msg = "Please give a correct scan" 60 raise TypeError(msg) 60 if rcParams['verbose']: 61 #print msg 62 asaplog.push(msg) 63 print_log('ERROR') 64 return 65 else: 66 raise TypeError(msg) 61 67 self.fitted = False 62 68 self.data = thescan … … 68 74 return 69 75 70 @asaplog_post_dec71 76 def set_function(self, **kwargs): 72 77 """ … … 87 92 if kwargs.has_key('poly'): 88 93 self.fitfunc = 'poly' 89 self.fitfuncs = ['poly']90 94 n = kwargs.get('poly') 91 self.components = [n +1]95 self.components = [n] 92 96 self.uselinear = False 93 97 elif kwargs.has_key('lpoly'): 94 98 self.fitfunc = 'poly' 95 self.fitfuncs = ['lpoly']96 99 n = kwargs.get('lpoly') 97 self.components = [n +1]100 self.components = [n] 98 101 self.uselinear = True 99 102 elif kwargs.has_key('gauss'): … … 111 114 else: 112 115 msg = "Invalid function type." 113 raise TypeError(msg) 116 if rcParams['verbose']: 117 #print msg 118 asaplog.push(msg) 119 print_log('ERROR') 120 return 121 else: 122 raise TypeError(msg) 114 123 115 124 self.fitter.setexpression(self.fitfunc,n) … … 117 126 return 118 127 119 @ asaplog_post_dec128 @print_log_dec 120 129 def fit(self, row=0, estimate=False): 121 130 """ … … 137 146 or self.fitfunc is None: 138 147 msg = "Fitter not yet initialised. Please set data & fit function" 139 raise RuntimeError(msg) 148 if rcParams['verbose']: 149 #print msg 150 asaplog.push(msg) 151 print_log('ERROR') 152 return 153 else: 154 raise RuntimeError(msg) 140 155 141 156 else: … … 157 172 if len(ps) == 0 or estimate: 158 173 self.fitter.estimate() 159 fxdpar = list(self.fitter.getfixedparameters()) 160 if len(fxdpar) and fxdpar.count(0) == 0: 161 raise RuntimeError,"No point fitting, if all parameters are fixed." 162 if self.uselinear: 163 converged = self.fitter.lfit() 164 else: 165 converged = self.fitter.fit() 166 if not converged: 167 raise RuntimeError,"Fit didn't converge." 174 try: 175 fxdpar = list(self.fitter.getfixedparameters()) 176 if len(fxdpar) and fxdpar.count(0) == 0: 177 raise RuntimeError,"No point fitting, if all parameters are fixed." 178 if self.uselinear: 179 converged = self.fitter.lfit() 180 else: 181 converged = self.fitter.fit() 182 if not converged: 183 raise RuntimeError,"Fit didn't converge." 184 except RuntimeError, msg: 185 if rcParams['verbose']: 186 #print msg 187 print_log() 188 asaplog.push(str(msg)) 189 print_log('ERROR') 190 else: 191 raise 168 192 self._fittedrow = row 169 193 self.fitted = True 194 print_log() 170 195 return 171 196 … … 196 221 self.data._addfit(fit,self._fittedrow) 197 222 198 @ asaplog_post_dec223 @print_log_dec 199 224 def set_parameters(self,*args,**kwargs): 200 225 """ … … 204 229 fixed: a vector of which parameters are to be held fixed 205 230 (default is none) 206 component: in case of multiple gaussians /lorentzians,207 the index of thecomponent231 component: in case of multiple gaussians, the index of the 232 component 208 233 """ 209 234 component = None … … 219 244 if self.fitfunc is None: 220 245 msg = "Please specify a fitting function first." 221 raise RuntimeError(msg) 246 if rcParams['verbose']: 247 #print msg 248 asaplog.push(msg) 249 print_log('ERROR') 250 return 251 else: 252 raise RuntimeError(msg) 222 253 if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None: 223 254 if not self.fitted and sum(self.fitter.getparameters()) == 0: … … 235 266 if fixed is not None: 236 267 self.fitter.setfixedparameters(fixed) 268 print_log() 237 269 return 238 270 239 @asaplog_post_dec240 271 def set_gauss_parameters(self, peak, centre, fwhm, 241 272 peakfixed=0, centrefixed=0, … … 257 288 if self.fitfunc != "gauss": 258 289 msg = "Function only operates on Gaussian components." 259 raise ValueError(msg) 290 if rcParams['verbose']: 291 #print msg 292 asaplog.push(msg) 293 print_log('ERROR') 294 return 295 else: 296 raise ValueError(msg) 260 297 if 0 <= component < len(self.components): 261 298 d = {'params':[peak, centre, fwhm], … … 264 301 else: 265 302 msg = "Please select a valid component." 266 raise ValueError(msg) 267 268 @asaplog_post_dec 303 if rcParams['verbose']: 304 #print msg 305 asaplog.push(msg) 306 print_log('ERROR') 307 return 308 else: 309 raise ValueError(msg) 310 269 311 def set_lorentz_parameters(self, peak, centre, fwhm, 270 312 peakfixed=0, centrefixed=0, … … 274 316 Set the Parameters of a 'Lorentzian' component, set with set_function. 275 317 Parameters: 276 peak, centre, fwhm: The lorentzian parameters318 peak, centre, fwhm: The gaussian parameters 277 319 peakfixed, 278 320 centrefixed, … … 286 328 if self.fitfunc != "lorentz": 287 329 msg = "Function only operates on Lorentzian components." 288 raise ValueError(msg) 330 if rcParams['verbose']: 331 #print msg 332 asaplog.push(msg) 333 print_log('ERROR') 334 return 335 else: 336 raise ValueError(msg) 289 337 if 0 <= component < len(self.components): 290 338 d = {'params':[peak, centre, fwhm], … … 293 341 else: 294 342 msg = "Please select a valid component." 295 raise ValueError(msg) 343 if rcParams['verbose']: 344 #print msg 345 asaplog.push(msg) 346 print_log('ERROR') 347 return 348 else: 349 raise ValueError(msg) 296 350 297 351 def get_area(self, component=None): … … 324 378 return sum(areas) 325 379 326 @asaplog_post_dec327 380 def get_errors(self, component=None): 328 381 """ … … 334 387 if not self.fitted: 335 388 msg = "Not yet fitted." 336 raise RuntimeError(msg) 389 if rcParams['verbose']: 390 #print msg 391 asaplog.push(msg) 392 print_log('ERROR') 393 return 394 else: 395 raise RuntimeError(msg) 337 396 errs = list(self.fitter.geterrors()) 338 397 cerrs = errs … … 344 403 return cerrs 345 404 346 347 @asaplog_post_dec348 405 def get_parameters(self, component=None, errors=False): 349 406 """ … … 355 412 if not self.fitted: 356 413 msg = "Not yet fitted." 357 raise RuntimeError(msg) 414 if rcParams['verbose']: 415 #print msg 416 asaplog.push(msg) 417 print_log('ERROR') 418 return 419 else: 420 raise RuntimeError(msg) 358 421 pars = list(self.fitter.getparameters()) 359 422 fixed = list(self.fitter.getfixedparameters()) … … 381 444 area += [a for i in range(3)] 382 445 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area) 383 asaplog.push(fpars) 446 if rcParams['verbose']: 447 #print fpars 448 asaplog.push(fpars) 449 print_log() 384 450 return {'params':cpars, 'fixed':cfixed, 'formatted': fpars, 385 451 'errors':cerrs} … … 415 481 return out 416 482 417 418 @asaplog_post_dec419 483 def get_estimate(self): 420 484 """ … … 423 487 pars = self.fitter.getestimate() 424 488 fixed = self.fitter.getfixedparameters() 425 asaplog.push(self._format_pars(pars,fixed,None,None)) 489 if rcParams['verbose']: 490 #print self._format_pars(pars,fixed,None) 491 asaplog.push(self._format_pars(pars,fixed,None)) 492 print_log() 426 493 return pars 427 494 428 @asaplog_post_dec429 495 def get_residual(self): 430 496 """ … … 433 499 if not self.fitted: 434 500 msg = "Not yet fitted." 435 raise RuntimeError(msg) 501 if rcParams['verbose']: 502 #print msg 503 asaplog.push(msg) 504 print_log('ERROR') 505 return 506 else: 507 raise RuntimeError(msg) 436 508 return self.fitter.getresidual() 437 509 438 @asaplog_post_dec439 510 def get_chi2(self): 440 511 """ … … 443 514 if not self.fitted: 444 515 msg = "Not yet fitted." 445 raise RuntimeError(msg) 516 if rcParams['verbose']: 517 #print msg 518 asaplog.push(msg) 519 print_log('ERROR') 520 return 521 else: 522 raise RuntimeError(msg) 446 523 ch2 = self.fitter.getchi2() 447 asaplog.push( 'Chi^2 = %3.3f' % (ch2) ) 524 if rcParams['verbose']: 525 #print 'Chi^2 = %3.3f' % (ch2) 526 asaplog.push( 'Chi^2 = %3.3f' % (ch2) ) 527 print_log() 448 528 return ch2 449 529 450 @asaplog_post_dec451 530 def get_fit(self): 452 531 """ … … 455 534 if not self.fitted: 456 535 msg = "Not yet fitted." 457 raise RuntimeError(msg) 536 if rcParams['verbose']: 537 #print msg 538 asaplog.push(msg) 539 print_log('ERROR') 540 return 541 else: 542 raise RuntimeError(msg) 458 543 return self.fitter.getfit() 459 544 460 @ asaplog_post_dec545 @print_log_dec 461 546 def commit(self): 462 547 """ … … 465 550 if not self.fitted: 466 551 msg = "Not yet fitted." 467 raise RuntimeError(msg) 552 if rcParams['verbose']: 553 #print msg 554 asaplog.push(msg) 555 print_log('ERROR') 556 return 557 else: 558 raise RuntimeError(msg) 468 559 from asap import scantable 469 560 if not isinstance(self.data, scantable): 470 561 msg = "Not a scantable" 471 raise TypeError(msg) 562 if rcParams['verbose']: 563 #print msg 564 asaplog.push(msg) 565 print_log('ERROR') 566 return 567 else: 568 raise TypeError(msg) 472 569 scan = self.data.copy() 473 570 scan._setspectrum(self.fitter.getresidual()) 571 print_log() 474 572 return scan 475 573 476 @ asaplog_post_dec574 @print_log_dec 477 575 def plot(self, residual=False, components=None, plotparms=False, 478 576 filename=None): … … 573 671 if (not rcParams['plotter.gui']): 574 672 self._p.save(filename) 575 576 @asaplog_post_dec 673 print_log() 674 675 @print_log_dec 577 676 def auto_fit(self, insitu=None, plot=False): 578 677 """ … … 584 683 if not isinstance(self.data, scantable) : 585 684 msg = "Data is not a scantable" 586 raise TypeError(msg) 685 if rcParams['verbose']: 686 #print msg 687 asaplog.push(msg) 688 print_log('ERROR') 689 return 690 else: 691 raise TypeError(msg) 587 692 if insitu is None: insitu = rcParams['insitu'] 588 693 if not insitu: … … 620 725 self._p.unmap() 621 726 self._p = None 727 print_log() 622 728 return scan
Note:
See TracChangeset
for help on using the changeset viewer.