Changeset 1017


Ignore:
Timestamp:
04/18/06 15:53:48 (18 years ago)
Author:
mar637
Message:

Fix for ticket #21 - get_parameters/set_parameters inconsistent. Reworked the interface to allow this.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapfitter.py

    r975 r1017  
    166166            self.data._addfit(fit,self._fittedrow)
    167167
    168     def set_parameters(self, params, fixed=None, component=None):
     168    #def set_parameters(self, params, fixed=None, component=None):
     169    def set_parameters(self,*args,**kwargs):
    169170        """
    170171        Set the parameters to be fitted.
     
    175176              component: in case of multiple gaussians, the index of the
    176177                         component
    177              """
     178        """
     179        component = None
     180        fixed = None
     181        params = None
     182       
     183        if len(args) and isinstance(args[0],dict):
     184            kwargs = args[0]
     185        if kwargs.has_key("fixed"): fixed = kwargs["fixed"]
     186        if kwargs.has_key("params"): params = kwargs["params"]
     187        if len(args) == 2 and isinstance(args[1], int):
     188            component = args[1]
    178189        if self.fitfunc is None:
    179190            msg = "Please specify a fitting function first."
     
    184195                raise RuntimeError(msg)
    185196        if self.fitfunc == "gauss" and component is not None:
    186             if not self.fitted:
     197            if not self.fitted and sum(self.fitter.getparameters()) == 0:
    187198                from numarray import zeros
    188199                pars = list(zeros(len(self.components)*3))
     
    203214
    204215    def set_gauss_parameters(self, peak, centre, fhwm,
    205                              peakfixed=False, centerfixed=False,
    206                              fhwmfixed=False,
     216                             peakfixed=0, centerfixed=0,
     217                             fhwmfixed=0,
    207218                             component=0):
    208219        """
     
    227238                raise ValueError(msg)
    228239        if 0 <= component < len(self.components):
    229             self.set_parameters([peak, centre, fhwm],
    230                                 [peakfixed, centerfixed, fhwmfixed],
    231                                 component)
     240            d = {'params':[peak, centre, fhwm],
     241                 'fixed':[peakfixed, centerfixed, fhwmfixed]}
     242            self.set_parameters(d, component)
    232243        else:
    233244            msg = "Please select a valid  component."
     
    294305        if rcParams['verbose']:
    295306            print fpars
    296         return cpars, cfixed, fpars
     307        return {'params':cpars, 'fixed':cfixed}
    297308
    298309    def _format_pars(self, pars, fixed, area):
     
    315326                ounit = self.data.get_fluxunit()
    316327            while i < len(pars):
    317                 out += '  %2d: peak = %3.3f %s , centre = %3.3f %s, FWHM = %3.3f %s\n      area = %3.3f %s %s\n' % (c,pars[i],ounit,pars[i+1],aunit,pars[i+2],aunit, area,ounit,aunit)
     328                if area:
     329                    out += '  %2d: peak = %3.3f %s , centre = %3.3f %s, FWHM = %3.3f %s\n      area = %3.3f %s %s\n' % (c,pars[i],ounit,pars[i+1],aunit,pars[i+2],aunit, area,ounit,aunit)
     330                else:
     331                    out += '  %2d: peak = %3.3f %s , centre = %3.3f %s, FWHM = %3.3f %s\n' % (c,pars[i],ounit,pars[i+1],aunit,pars[i+2],aunit,ounit,aunit)
    318332                c+=1
    319333                i+=3
     
    327341        fixed = self.fitter.getfixedparameters()
    328342        if rcParams['verbose']:
    329             print self._format_pars(pars,fixed)
     343            print self._format_pars(pars,fixed,None)
    330344        return pars
    331345
     
    422436        tlab = 'Spectrum'
    423437        xlab = 'Abcissa'
    424         m = ()
     438        ylab = 'Ordinate'
     439        m = None
    425440        if self.data:
    426441            tlab = self.data._getsourcename(self._fittedrow)
Note: See TracChangeset for help on using the changeset viewer.