Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapfitter.py

    r1826 r1938  
    11import _asap
    22from asap.parameters import rcParams
    3 from asap.logging import asaplog, print_log, print_log_dec
     3from asap.logging import asaplog, asaplog_post_dec
    44from asap.utils import _n_bools, mask_and
    55
     
    99    The fitting class for ASAP.
    1010    """
    11 
    1211    def __init__(self):
    1312        """
     
    4948        return
    5049
     50    @asaplog_post_dec
    5151    def set_scan(self, thescan=None, mask=None):
    5252        """
     
    5858        if not thescan:
    5959            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)
    6761        self.fitted = False
    6862        self.data = thescan
     
    7468        return
    7569
     70    @asaplog_post_dec
    7671    def set_function(self, **kwargs):
    7772        """
     
    9287        if kwargs.has_key('poly'):
    9388            self.fitfunc = 'poly'
     89            self.fitfuncs = ['poly']
    9490            n = kwargs.get('poly')
    95             self.components = [n]
     91            self.components = [n+1]
    9692            self.uselinear = False
    9793        elif kwargs.has_key('lpoly'):
    9894            self.fitfunc = 'poly'
     95            self.fitfuncs = ['lpoly']
    9996            n = kwargs.get('lpoly')
    100             self.components = [n]
     97            self.components = [n+1]
    10198            self.uselinear = True
    10299        elif kwargs.has_key('gauss'):
     
    114111        else:
    115112            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)
    123114
    124115        self.fitter.setexpression(self.fitfunc,n)
     
    126117        return
    127118
    128     @print_log_dec
     119    @asaplog_post_dec
    129120    def fit(self, row=0, estimate=False):
    130121        """
     
    146137               or self.fitfunc is None:
    147138            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)
    155140
    156141        else:
     
    172157            if len(ps) == 0 or estimate:
    173158                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."
    192168        self._fittedrow = row
    193169        self.fitted = True
    194         print_log()
    195170        return
    196171
     
    221196                self.data._addfit(fit,self._fittedrow)
    222197
    223     @print_log_dec
     198    @asaplog_post_dec
    224199    def set_parameters(self,*args,**kwargs):
    225200        """
     
    229204              fixed:     a vector of which parameters are to be held fixed
    230205                         (default is none)
    231               component: in case of multiple gaussians, the index of the
    232                          component
     206              component: in case of multiple gaussians/lorentzians,
     207                         the index of the component
    233208        """
    234209        component = None
     
    244219        if self.fitfunc is None:
    245220            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)
    253222        if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None:
    254223            if not self.fitted and sum(self.fitter.getparameters()) == 0:
     
    266235        if fixed is not None:
    267236            self.fitter.setfixedparameters(fixed)
    268         print_log()
    269237        return
    270238
     239    @asaplog_post_dec
    271240    def set_gauss_parameters(self, peak, centre, fwhm,
    272241                             peakfixed=0, centrefixed=0,
     
    288257        if self.fitfunc != "gauss":
    289258            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)
    297260        if 0 <= component < len(self.components):
    298261            d = {'params':[peak, centre, fwhm],
     
    301264        else:
    302265            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
    311269    def set_lorentz_parameters(self, peak, centre, fwhm,
    312270                             peakfixed=0, centrefixed=0,
     
    316274        Set the Parameters of a 'Lorentzian' component, set with set_function.
    317275        Parameters:
    318             peak, centre, fwhm:  The gaussian parameters
     276            peak, centre, fwhm:  The lorentzian parameters
    319277            peakfixed,
    320278            centrefixed,
     
    328286        if self.fitfunc != "lorentz":
    329287            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)
    337289        if 0 <= component < len(self.components):
    338290            d = {'params':[peak, centre, fwhm],
     
    341293        else:
    342294            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)
    350296
    351297    def get_area(self, component=None):
     
    378324            return sum(areas)
    379325
     326    @asaplog_post_dec
    380327    def get_errors(self, component=None):
    381328        """
     
    387334        if not self.fitted:
    388335            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)
    396337        errs = list(self.fitter.geterrors())
    397338        cerrs = errs
     
    403344        return cerrs
    404345
     346
     347    @asaplog_post_dec
    405348    def get_parameters(self, component=None, errors=False):
    406349        """
     
    412355        if not self.fitted:
    413356            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)
    421358        pars = list(self.fitter.getparameters())
    422359        fixed = list(self.fitter.getfixedparameters())
     
    444381                  area += [a for i in range(3)]
    445382        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)
    450384        return {'params':cpars, 'fixed':cfixed, 'formatted': fpars,
    451385                'errors':cerrs}
     
    481415        return out
    482416
     417
     418    @asaplog_post_dec
    483419    def get_estimate(self):
    484420        """
     
    487423        pars = self.fitter.getestimate()
    488424        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))
    493426        return pars
    494427
     428    @asaplog_post_dec
    495429    def get_residual(self):
    496430        """
     
    499433        if not self.fitted:
    500434            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)
    508436        return self.fitter.getresidual()
    509437
     438    @asaplog_post_dec
    510439    def get_chi2(self):
    511440        """
     
    514443        if not self.fitted:
    515444            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)
    523446        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) )
    528448        return ch2
    529449
     450    @asaplog_post_dec
    530451    def get_fit(self):
    531452        """
     
    534455        if not self.fitted:
    535456            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)
    543458        return self.fitter.getfit()
    544459
    545     @print_log_dec
     460    @asaplog_post_dec
    546461    def commit(self):
    547462        """
     
    550465        if not self.fitted:
    551466            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)
    559468        from asap import scantable
    560469        if not isinstance(self.data, scantable):
    561470            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)
    569472        scan = self.data.copy()
    570473        scan._setspectrum(self.fitter.getresidual())
    571         print_log()
    572474        return scan
    573475
    574     @print_log_dec
     476    @asaplog_post_dec
    575477    def plot(self, residual=False, components=None, plotparms=False,
    576478             filename=None):
     
    671573        if (not rcParams['plotter.gui']):
    672574            self._p.save(filename)
    673         print_log()
    674 
    675     @print_log_dec
     575
     576    @asaplog_post_dec
    676577    def auto_fit(self, insitu=None, plot=False):
    677578        """
     
    683584        if not isinstance(self.data, scantable) :
    684585            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)
    692587        if insitu is None: insitu = rcParams['insitu']
    693588        if not insitu:
     
    725620            self._p.unmap()
    726621            self._p = None
    727         print_log()
    728622        return scan
Note: See TracChangeset for help on using the changeset viewer.