Changeset 1859 for trunk/python/asapfitter.py
- Timestamp:
- 08/05/10 14:40:38 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asapfitter.py
r1826 r1859 49 49 return 50 50 51 @print_log_dec 51 52 def set_scan(self, thescan=None, mask=None): 52 53 """ … … 58 59 if not thescan: 59 60 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) 61 raise TypeError(msg) 67 62 self.fitted = False 68 63 self.data = thescan … … 74 69 return 75 70 71 @print_log_dec 76 72 def set_function(self, **kwargs): 77 73 """ … … 114 110 else: 115 111 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) 112 raise TypeError(msg) 123 113 124 114 self.fitter.setexpression(self.fitfunc,n) … … 146 136 or self.fitfunc is None: 147 137 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) 138 raise RuntimeError(msg) 155 139 156 140 else: … … 172 156 if len(ps) == 0 or estimate: 173 157 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 158 fxdpar = list(self.fitter.getfixedparameters()) 159 if len(fxdpar) and fxdpar.count(0) == 0: 160 raise RuntimeError,"No point fitting, if all parameters are fixed." 161 if self.uselinear: 162 converged = self.fitter.lfit() 163 else: 164 converged = self.fitter.fit() 165 if not converged: 166 raise RuntimeError,"Fit didn't converge." 192 167 self._fittedrow = row 193 168 self.fitted = True 194 print_log()195 169 return 196 170 … … 244 218 if self.fitfunc is None: 245 219 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) 220 raise RuntimeError(msg) 253 221 if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None: 254 222 if not self.fitted and sum(self.fitter.getparameters()) == 0: … … 266 234 if fixed is not None: 267 235 self.fitter.setfixedparameters(fixed) 268 print_log()269 236 return 270 237 238 @print_log_dec 271 239 def set_gauss_parameters(self, peak, centre, fwhm, 272 240 peakfixed=0, centrefixed=0, … … 288 256 if self.fitfunc != "gauss": 289 257 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) 258 raise ValueError(msg) 297 259 if 0 <= component < len(self.components): 298 260 d = {'params':[peak, centre, fwhm], … … 301 263 else: 302 264 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 265 raise ValueError(msg) 266 267 @print_log_dec 311 268 def set_lorentz_parameters(self, peak, centre, fwhm, 312 269 peakfixed=0, centrefixed=0, … … 328 285 if self.fitfunc != "lorentz": 329 286 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) 287 raise ValueError(msg) 337 288 if 0 <= component < len(self.components): 338 289 d = {'params':[peak, centre, fwhm], … … 341 292 else: 342 293 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) 294 raise ValueError(msg) 350 295 351 296 def get_area(self, component=None): … … 378 323 return sum(areas) 379 324 325 @print_log_dec 380 326 def get_errors(self, component=None): 381 327 """ … … 387 333 if not self.fitted: 388 334 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) 335 raise RuntimeError(msg) 396 336 errs = list(self.fitter.geterrors()) 397 337 cerrs = errs … … 403 343 return cerrs 404 344 345 346 @print_log_dec 405 347 def get_parameters(self, component=None, errors=False): 406 348 """ … … 412 354 if not self.fitted: 413 355 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) 356 raise RuntimeError(msg) 421 357 pars = list(self.fitter.getparameters()) 422 358 fixed = list(self.fitter.getfixedparameters()) … … 444 380 area += [a for i in range(3)] 445 381 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area) 446 if rcParams['verbose']: 447 #print fpars 448 asaplog.push(fpars) 449 print_log() 382 asaplog.push(fpars) 450 383 return {'params':cpars, 'fixed':cfixed, 'formatted': fpars, 451 384 'errors':cerrs} … … 481 414 return out 482 415 416 417 @print_log_dec 483 418 def get_estimate(self): 484 419 """ … … 487 422 pars = self.fitter.getestimate() 488 423 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() 424 asaplog.push(self._format_pars(pars,fixed,None)) 493 425 return pars 494 426 427 @print_log_dec 495 428 def get_residual(self): 496 429 """ … … 499 432 if not self.fitted: 500 433 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) 434 raise RuntimeError(msg) 508 435 return self.fitter.getresidual() 509 436 437 @print_log_dec 510 438 def get_chi2(self): 511 439 """ … … 514 442 if not self.fitted: 515 443 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) 444 raise RuntimeError(msg) 523 445 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() 446 asaplog.push( 'Chi^2 = %3.3f' % (ch2) ) 528 447 return ch2 529 448 449 @print_log_dec 530 450 def get_fit(self): 531 451 """ … … 534 454 if not self.fitted: 535 455 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) 456 raise RuntimeError(msg) 543 457 return self.fitter.getfit() 544 458 … … 550 464 if not self.fitted: 551 465 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) 466 raise RuntimeError(msg) 559 467 from asap import scantable 560 468 if not isinstance(self.data, scantable): 561 469 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) 470 raise TypeError(msg) 569 471 scan = self.data.copy() 570 472 scan._setspectrum(self.fitter.getresidual()) 571 print_log()572 473 return scan 573 474 … … 671 572 if (not rcParams['plotter.gui']): 672 573 self._p.save(filename) 673 print_log()674 574 675 575 @print_log_dec … … 683 583 if not isinstance(self.data, scantable) : 684 584 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) 585 raise TypeError(msg) 692 586 if insitu is None: insitu = rcParams['insitu'] 693 587 if not insitu: … … 725 619 self._p.unmap() 726 620 self._p = None 727 print_log()728 621 return scan
Note: See TracChangeset
for help on using the changeset viewer.