source: trunk/python/asapfitter.py @ 1017

Last change on this file since 1017 was 1017, checked in by mar637, 18 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
RevLine 
[113]1import _asap
[259]2from asap import rcParams
[723]3from asap import print_log
[113]4
5class fitter:
6    """
7    The fitting class for ASAP.
8    """
[723]9
[113]10    def __init__(self):
11        """
12        Create a fitter object. No state is set.
13        """
14        self.fitter = _asap.fitter()
15        self.x = None
16        self.y = None
17        self.mask = None
18        self.fitfunc = None
[515]19        self.fitfuncs = None
[113]20        self.fitted = False
21        self.data = None
[515]22        self.components = 0
23        self._fittedrow = 0
[113]24        self._p = None
[515]25        self._selection = None
[113]26
27    def set_data(self, xdat, ydat, mask=None):
28        """
[158]29        Set the absissa and ordinate for the fit. Also set the mask
[113]30        indicationg valid points.
31        This can be used for data vectors retrieved from a scantable.
32        For scantable fitting use 'fitter.set_scan(scan, mask)'.
33        Parameters:
[158]34            xdat:    the abcissa values
[113]35            ydat:    the ordinate values
36            mask:    an optional mask
[723]37
[113]38        """
39        self.fitted = False
40        self.x = xdat
41        self.y = ydat
42        if mask == None:
43            from numarray import ones
44            self.mask = ones(len(xdat))
45        else:
46            self.mask = mask
47        return
48
49    def set_scan(self, thescan=None, mask=None):
50        """
51        Set the 'data' (a scantable) of the fitter.
52        Parameters:
53            thescan:     a scantable
54            mask:        a msk retireved from the scantable
55        """
56        if not thescan:
[723]57            msg = "Please give a correct scan"
58            if rcParams['verbose']:
59                print msg
60                return
61            else:
62                raise TypeError(msg)
[113]63        self.fitted = False
64        self.data = thescan
65        if mask is None:
66            from numarray import ones
67            self.mask = ones(self.data.nchan())
68        else:
69            self.mask = mask
70        return
71
72    def set_function(self, **kwargs):
73        """
74        Set the function to be fit.
75        Parameters:
76            poly:    use a polynomial of the order given
77            gauss:   fit the number of gaussian specified
78        Example:
79            fitter.set_function(gauss=2) # will fit two gaussians
80            fitter.set_function(poly=3)  # will fit a 3rd order polynomial
81        """
[723]82        #default poly order 0
[515]83        n=0
[113]84        if kwargs.has_key('poly'):
85            self.fitfunc = 'poly'
86            n = kwargs.get('poly')
[515]87            self.components = [n]
[113]88        elif kwargs.has_key('gauss'):
89            n = kwargs.get('gauss')
90            self.fitfunc = 'gauss'
[515]91            self.fitfuncs = [ 'gauss' for i in range(n) ]
92            self.components = [ 3 for i in range(n) ]
93        else:
[723]94            msg = "Invalid function type."
95            if rcParams['verbose']:
96                print msg
97                return
98            else:
99                raise TypeError(msg)
100
[113]101        self.fitter.setexpression(self.fitfunc,n)
102        return
[723]103
[515]104    def fit(self, row=0):
[113]105        """
106        Execute the actual fitting process. All the state has to be set.
107        Parameters:
[526]108            row:    specify the row in the scantable
[113]109        Example:
[515]110            s = scantable('myscan.asap')
111            s.set_cursor(thepol=1)        # select second pol
[113]112            f = fitter()
113            f.set_scan(s)
114            f.set_function(poly=0)
[723]115            f.fit(row=0)                  # fit first row
[113]116        """
117        if ((self.x is None or self.y is None) and self.data is None) \
118               or self.fitfunc is None:
[723]119            msg = "Fitter not yet initialised. Please set data & fit function"
120            if rcParams['verbose']:
121                print msg
122                return
123            else:
124                raise RuntimeError(msg)
125
[113]126        else:
127            if self.data is not None:
[515]128                self.x = self.data._getabcissa(row)
129                self.y = self.data._getspectrum(row)
[723]130                from asap import asaplog
131                asaplog.push("Fitting:")
[943]132                i = row
133                out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (self.data.getscan(i),self.data.getbeam(i),self.data.getif(i),self.data.getpol(i), self.data.getcycle(i))
[876]134                asaplog.push(out)
[515]135        self.fitter.setdata(self.x, self.y, self.mask)
[113]136        if self.fitfunc == 'gauss':
137            ps = self.fitter.getparameters()
138            if len(ps) == 0:
139                self.fitter.estimate()
[626]140        try:
141            self.fitter.fit()
142        except RuntimeError, msg:
[723]143            if rcParams['verbose']:
144                print msg
145            else:
146                raise
[515]147        self._fittedrow = row
[113]148        self.fitted = True
[723]149        print_log()
[113]150        return
151
[515]152    def store_fit(self):
[526]153        """
154        Store the fit parameters in the scantable.
155        """
[515]156        if self.fitted and self.data is not None:
157            pars = list(self.fitter.getparameters())
158            fixed = list(self.fitter.getfixedparameters())
[975]159            from asap.asapfit import asapfit
160            fit = asapfit()
161            fit.setparameters(pars)
162            fit.setfixedparameters(fixed)
163            fit.setfunctions(self.fitfuncs)
164            fit.setcomponents(self.components)
165            fit.setframeinfo(self.data._getcoordinfo())
166            self.data._addfit(fit,self._fittedrow)
[515]167
[1017]168    #def set_parameters(self, params, fixed=None, component=None):
169    def set_parameters(self,*args,**kwargs):
[526]170        """
171        Set the parameters to be fitted.
172        Parameters:
173              params:    a vector of parameters
174              fixed:     a vector of which parameters are to be held fixed
175                         (default is none)
176              component: in case of multiple gaussians, the index of the
177                         component
[1017]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]
[515]189        if self.fitfunc is None:
[723]190            msg = "Please specify a fitting function first."
191            if rcParams['verbose']:
192                print msg
193                return
194            else:
195                raise RuntimeError(msg)
[515]196        if self.fitfunc == "gauss" and component is not None:
[1017]197            if not self.fitted and sum(self.fitter.getparameters()) == 0:
[515]198                from numarray import zeros
199                pars = list(zeros(len(self.components)*3))
200                fxd = list(zeros(len(pars)))
201            else:
[723]202                pars = list(self.fitter.getparameters())
[515]203                fxd = list(self.fitter.getfixedparameters())
204            i = 3*component
205            pars[i:i+3] = params
206            fxd[i:i+3] = fixed
207            params = pars
[723]208            fixed = fxd
[113]209        self.fitter.setparameters(params)
210        if fixed is not None:
211            self.fitter.setfixedparameters(fixed)
[723]212        print_log()
[113]213        return
[515]214
215    def set_gauss_parameters(self, peak, centre, fhwm,
[1017]216                             peakfixed=0, centerfixed=0,
217                             fhwmfixed=0,
[515]218                             component=0):
[113]219        """
[515]220        Set the Parameters of a 'Gaussian' component, set with set_function.
221        Parameters:
222            peak, centre, fhwm:  The gaussian parameters
223            peakfixed,
224            centerfixed,
225            fhwmfixed:           Optional parameters to indicate if
226                                 the paramters should be held fixed during
227                                 the fitting process. The default is to keep
228                                 all parameters flexible.
[526]229            component:           The number of the component (Default is the
230                                 component 0)
[515]231        """
232        if self.fitfunc != "gauss":
[723]233            msg = "Function only operates on Gaussian components."
234            if rcParams['verbose']:
235                print msg
236                return
237            else:
238                raise ValueError(msg)
[515]239        if 0 <= component < len(self.components):
[1017]240            d = {'params':[peak, centre, fhwm],
241                 'fixed':[peakfixed, centerfixed, fhwmfixed]}
242            self.set_parameters(d, component)
[515]243        else:
[723]244            msg = "Please select a valid  component."
245            if rcParams['verbose']:
246                print msg
247                return
248            else:
249                raise ValueError(msg)
250
[975]251    def get_area(self, component=None):
252        """
253        Return the area under the fitted gaussian component.
254        Parameters:
255              component:   the gaussian component selection,
256                           default (None) is the sum of all components
257        Note:
258              This will only work for gaussian fits.
259        """
260        if not self.fitted: return
261        if self.fitfunc == "gauss":
262            pars = list(self.fitter.getparameters())
263            from math import log,pi,sqrt
264            fac = sqrt(pi/log(16.0))
265            areas = []
266            for i in range(len(self.components)):
267                j = i*3
268                cpars = pars[j:j+3]
269                areas.append(fac * cpars[0] * cpars[2])
270        else:
271            return None
272        if component is not None:
273            return areas[component]
274        else:
275            return sum(areas)
276
[515]277    def get_parameters(self, component=None):
278        """
[113]279        Return the fit paramters.
[526]280        Parameters:
281             component:    get the parameters for the specified component
282                           only, default is all components
[113]283        """
284        if not self.fitted:
[723]285            msg = "Not yet fitted."
286            if rcParams['verbose']:
287                print msg
288                return
289            else:
290                raise RuntimeError(msg)
[113]291        pars = list(self.fitter.getparameters())
292        fixed = list(self.fitter.getfixedparameters())
[723]293        if component is not None:
[515]294            if self.fitfunc == "gauss":
295                i = 3*component
296                cpars = pars[i:i+3]
297                cfixed = fixed[i:i+3]
298            else:
299                cpars = pars
[723]300                cfixed = fixed
[515]301        else:
302            cpars = pars
303            cfixed = fixed
[975]304        fpars = self._format_pars(cpars, cfixed, self.get_area(component))
[723]305        if rcParams['verbose']:
[515]306            print fpars
[1017]307        return {'params':cpars, 'fixed':cfixed}
[723]308
[975]309    def _format_pars(self, pars, fixed, area):
[113]310        out = ''
311        if self.fitfunc == 'poly':
312            c = 0
[515]313            for i in range(len(pars)):
314                fix = ""
315                if fixed[i]: fix = "(fixed)"
316                out += '  p%d%s= %3.3f,' % (c,fix,pars[i])
[113]317                c+=1
[515]318            out = out[:-1]  # remove trailing ','
[113]319        elif self.fitfunc == 'gauss':
320            i = 0
321            c = 0
[515]322            aunit = ''
323            ounit = ''
[113]324            if self.data:
[515]325                aunit = self.data.get_unit()
326                ounit = self.data.get_fluxunit()
[113]327            while i < len(pars):
[1017]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)
[113]332                c+=1
333                i+=3
334        return out
[723]335
[113]336    def get_estimate(self):
337        """
[515]338        Return the parameter estimates (for non-linear functions).
[113]339        """
340        pars = self.fitter.getestimate()
[943]341        fixed = self.fitter.getfixedparameters()
[723]342        if rcParams['verbose']:
[1017]343            print self._format_pars(pars,fixed,None)
[113]344        return pars
345
346    def get_residual(self):
347        """
348        Return the residual of the fit.
349        """
350        if not self.fitted:
[723]351            msg = "Not yet fitted."
352            if rcParams['verbose']:
353                print msg
354                return
355            else:
356                raise RuntimeError(msg)
[113]357        return self.fitter.getresidual()
358
359    def get_chi2(self):
360        """
361        Return chi^2.
362        """
363        if not self.fitted:
[723]364            msg = "Not yet fitted."
365            if rcParams['verbose']:
366                print msg
367                return
368            else:
369                raise RuntimeError(msg)
[113]370        ch2 = self.fitter.getchi2()
[723]371        if rcParams['verbose']:
[113]372            print 'Chi^2 = %3.3f' % (ch2)
[723]373        return ch2
[113]374
375    def get_fit(self):
376        """
377        Return the fitted ordinate values.
378        """
379        if not self.fitted:
[723]380            msg = "Not yet fitted."
381            if rcParams['verbose']:
382                print msg
383                return
384            else:
385                raise RuntimeError(msg)
[113]386        return self.fitter.getfit()
387
388    def commit(self):
389        """
[526]390        Return a new scan where the fits have been commited (subtracted)
[113]391        """
392        if not self.fitted:
393            print "Not yet fitted."
[723]394            msg = "Not yet fitted."
395            if rcParams['verbose']:
396                print msg
397                return
398            else:
399                raise RuntimeError(msg)
[975]400        from asap import scantable
401        if not isinstance(self.data, scantable):
[723]402            msg = "Not a scantable"
403            if rcParams['verbose']:
404                print msg
405                return
406            else:
407                raise TypeError(msg)
[113]408        scan = self.data.copy()
[259]409        scan._setspectrum(self.fitter.getresidual())
[723]410        print_log()
[113]411
[723]412    def plot(self, residual=False, components=None, plotparms=False, filename=None):
[113]413        """
414        Plot the last fit.
415        Parameters:
416            residual:    an optional parameter indicating if the residual
417                         should be plotted (default 'False')
[526]418            components:  a list of components to plot, e.g [0,1],
419                         -1 plots the total fit. Default is to only
420                         plot the total fit.
421            plotparms:   Inidicates if the parameter values should be present
422                         on the plot
[113]423        """
424        if not self.fitted:
425            return
[723]426        if not self._p or self._p.is_dead:
427            if rcParams['plotter.gui']:
428                from asap.asaplotgui import asaplotgui as asaplot
429            else:
430                from asap.asaplot import asaplot
431            self._p = asaplot()
432        self._p.hold()
[113]433        self._p.clear()
[515]434        self._p.set_panels()
[652]435        self._p.palette(0)
[113]436        tlab = 'Spectrum'
[723]437        xlab = 'Abcissa'
[1017]438        ylab = 'Ordinate'
439        m = None
[113]440        if self.data:
[515]441            tlab = self.data._getsourcename(self._fittedrow)
442            xlab = self.data._getabcissalabel(self._fittedrow)
443            m = self.data._getmask(self._fittedrow)
[626]444            ylab = self.data._get_ordinate_label()
[515]445
[668]446        colours = ["#777777","#bbbbbb","red","orange","purple","green","magenta", "cyan"]
[652]447        self._p.palette(0,colours)
[515]448        self._p.set_line(label='Spectrum')
[113]449        self._p.plot(self.x, self.y, m)
450        if residual:
[652]451            self._p.palette(1)
[515]452            self._p.set_line(label='Residual')
[113]453            self._p.plot(self.x, self.get_residual(), m)
[652]454        self._p.palette(2)
[515]455        if components is not None:
456            cs = components
457            if isinstance(components,int): cs = [components]
[526]458            if plotparms:
459                self._p.text(0.15,0.15,str(self.get_parameters()[2]),size=8)
[515]460            n = len(self.components)
[652]461            self._p.palette(3)
[515]462            for c in cs:
463                if 0 <= c < n:
464                    lab = self.fitfuncs[c]+str(c)
465                    self._p.set_line(label=lab)
466                    self._p.plot(self.x, self.fitter.evaluate(c), m)
467                elif c == -1:
[652]468                    self._p.palette(2)
[515]469                    self._p.set_line(label="Total Fit")
[723]470                    self._p.plot(self.x, self.get_fit(), m)
[515]471        else:
[652]472            self._p.palette(2)
[515]473            self._p.set_line(label='Fit')
474            self._p.plot(self.x, self.get_fit(), m)
[723]475        xlim=[min(self.x),max(self.x)]
476        self._p.axes.set_xlim(xlim)
[113]477        self._p.set_axes('xlabel',xlab)
478        self._p.set_axes('ylabel',ylab)
479        self._p.set_axes('title',tlab)
480        self._p.release()
[723]481        if (not rcParams['plotter.gui']):
482            self._p.save(filename)
483        print_log()
[113]484
[876]485    def auto_fit(self, insitu=None):
[113]486        """
[515]487        Return a scan where the function is applied to all rows for
488        all Beams/IFs/Pols.
[723]489
[113]490        """
491        from asap import scantable
[515]492        if not isinstance(self.data, scantable) :
[723]493            msg = "Data is not a scantable"
494            if rcParams['verbose']:
495                print msg
496                return
497            else:
498                raise TypeError(msg)
[259]499        if insitu is None: insitu = rcParams['insitu']
500        if not insitu:
501            scan = self.data.copy()
502        else:
503            scan = self.data
[880]504        rows = xrange(scan.nrow())
[723]505        from asap import asaplog
[876]506        asaplog.push("Fitting:")
507        for r in rows:
[880]508            out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" %        (scan.getscan(r),scan.getbeam(r),scan.getif(r),scan.getpol(r), scan.getcycle(r))
509            asaplog.push(out, False)
[876]510            self.x = scan._getabcissa(r)
511            self.y = scan._getspectrum(r)
512            self.data = None
513            self.fit()
514            x = self.get_parameters()
[880]515            scan._setspectrum(self.fitter.getresidual(), r)
[876]516        print_log()
517        return scan
[794]518
Note: See TracBrowser for help on using the repository browser.