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