Ignore:
Timestamp:
08/05/10 14:40:38 (14 years ago)
Author:
Malte Marquarding
Message:

Ticket #193: the rcParamsverbose? flag is only used in standard asap cli mode. Otherwise log messages are always send to the logger and one needs to call asaplog.disable()/asaplog.enable() to controls this. I have also added the function name as the log origin.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapfitter.py

    r1826 r1859  
    4949        return
    5050
     51    @print_log_dec
    5152    def set_scan(self, thescan=None, mask=None):
    5253        """
     
    5859        if not thescan:
    5960            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)
    6762        self.fitted = False
    6863        self.data = thescan
     
    7469        return
    7570
     71    @print_log_dec
    7672    def set_function(self, **kwargs):
    7773        """
     
    114110        else:
    115111            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)
    123113
    124114        self.fitter.setexpression(self.fitfunc,n)
     
    146136               or self.fitfunc is None:
    147137            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)
    155139
    156140        else:
     
    172156            if len(ps) == 0 or estimate:
    173157                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."
    192167        self._fittedrow = row
    193168        self.fitted = True
    194         print_log()
    195169        return
    196170
     
    244218        if self.fitfunc is None:
    245219            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)
    253221        if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None:
    254222            if not self.fitted and sum(self.fitter.getparameters()) == 0:
     
    266234        if fixed is not None:
    267235            self.fitter.setfixedparameters(fixed)
    268         print_log()
    269236        return
    270237
     238    @print_log_dec
    271239    def set_gauss_parameters(self, peak, centre, fwhm,
    272240                             peakfixed=0, centrefixed=0,
     
    288256        if self.fitfunc != "gauss":
    289257            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)
    297259        if 0 <= component < len(self.components):
    298260            d = {'params':[peak, centre, fwhm],
     
    301263        else:
    302264            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
    311268    def set_lorentz_parameters(self, peak, centre, fwhm,
    312269                             peakfixed=0, centrefixed=0,
     
    328285        if self.fitfunc != "lorentz":
    329286            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)
    337288        if 0 <= component < len(self.components):
    338289            d = {'params':[peak, centre, fwhm],
     
    341292        else:
    342293            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)
    350295
    351296    def get_area(self, component=None):
     
    378323            return sum(areas)
    379324
     325    @print_log_dec
    380326    def get_errors(self, component=None):
    381327        """
     
    387333        if not self.fitted:
    388334            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)
    396336        errs = list(self.fitter.geterrors())
    397337        cerrs = errs
     
    403343        return cerrs
    404344
     345
     346    @print_log_dec
    405347    def get_parameters(self, component=None, errors=False):
    406348        """
     
    412354        if not self.fitted:
    413355            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)
    421357        pars = list(self.fitter.getparameters())
    422358        fixed = list(self.fitter.getfixedparameters())
     
    444380                  area += [a for i in range(3)]
    445381        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)
    450383        return {'params':cpars, 'fixed':cfixed, 'formatted': fpars,
    451384                'errors':cerrs}
     
    481414        return out
    482415
     416
     417    @print_log_dec
    483418    def get_estimate(self):
    484419        """
     
    487422        pars = self.fitter.getestimate()
    488423        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))
    493425        return pars
    494426
     427    @print_log_dec
    495428    def get_residual(self):
    496429        """
     
    499432        if not self.fitted:
    500433            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)
    508435        return self.fitter.getresidual()
    509436
     437    @print_log_dec
    510438    def get_chi2(self):
    511439        """
     
    514442        if not self.fitted:
    515443            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)
    523445        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) )
    528447        return ch2
    529448
     449    @print_log_dec
    530450    def get_fit(self):
    531451        """
     
    534454        if not self.fitted:
    535455            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)
    543457        return self.fitter.getfit()
    544458
     
    550464        if not self.fitted:
    551465            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)
    559467        from asap import scantable
    560468        if not isinstance(self.data, scantable):
    561469            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)
    569471        scan = self.data.copy()
    570472        scan._setspectrum(self.fitter.getresidual())
    571         print_log()
    572473        return scan
    573474
     
    671572        if (not rcParams['plotter.gui']):
    672573            self._p.save(filename)
    673         print_log()
    674574
    675575    @print_log_dec
     
    683583        if not isinstance(self.data, scantable) :
    684584            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)
    692586        if insitu is None: insitu = rcParams['insitu']
    693587        if not insitu:
     
    725619            self._p.unmap()
    726620            self._p = None
    727         print_log()
    728621        return scan
Note: See TracChangeset for help on using the changeset viewer.