source: branches/alma/python/asapfitter.py @ 1628

Last change on this file since 1628 was 1628, checked in by Takeshi Nakazato, 15 years ago

New Development: No

JIRA Issue: No

Ready to Release: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Bug fix.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.8 KB
Line 
1import _asap
2from asap import rcParams
3from asap import print_log
4from asap import _n_bools
5from asap import mask_and
6from asap import asaplog
7
8class fitter:
9    """
10    The fitting class for ASAP.
11    """
12
13    def __init__(self):
14        """
15        Create a fitter object. No state is set.
16        """
17        self.fitter = _asap.fitter()
18        self.x = None
19        self.y = None
20        self.mask = None
21        self.fitfunc = None
22        self.fitfuncs = None
23        self.fitted = False
24        self.data = None
25        self.components = 0
26        self._fittedrow = 0
27        self._p = None
28        self._selection = None
29        self.uselinear = False
30
31    def set_data(self, xdat, ydat, mask=None):
32        """
33        Set the absissa and ordinate for the fit. Also set the mask
34        indicationg valid points.
35        This can be used for data vectors retrieved from a scantable.
36        For scantable fitting use 'fitter.set_scan(scan, mask)'.
37        Parameters:
38            xdat:    the abcissa values
39            ydat:    the ordinate values
40            mask:    an optional mask
41
42        """
43        self.fitted = False
44        self.x = xdat
45        self.y = ydat
46        if mask == None:
47            self.mask = _n_bools(len(xdat), True)
48        else:
49            self.mask = mask
50        return
51
52    def set_scan(self, thescan=None, mask=None):
53        """
54        Set the 'data' (a scantable) of the fitter.
55        Parameters:
56            thescan:     a scantable
57            mask:        a msk retrieved from the scantable
58        """
59        if not thescan:
60            msg = "Please give a correct scan"
61            if rcParams['verbose']:
62                #print msg
63                asaplog.push(msg)
64                print_log('ERROR')
65                return
66            else:
67                raise TypeError(msg)
68        self.fitted = False
69        self.data = thescan
70        self.mask = None
71        if mask is None:
72            self.mask = _n_bools(self.data.nchan(), True)
73        else:
74            self.mask = mask
75        return
76
77    def set_function(self, **kwargs):
78        """
79        Set the function to be fit.
80        Parameters:
81            poly:    use a polynomial of the order given with nonlinear least squares fit
82            lpoly:   use polynomial of the order given with linear least squares fit
83            gauss:   fit the number of gaussian specified
84        Example:
85            fitter.set_function(gauss=2) # will fit two gaussians
86            fitter.set_function(poly=3)  # will fit a 3rd order polynomial via nonlinear method
87            fitter.set_function(lpoly=3)  # will fit a 3rd order polynomial via linear method
88        """
89        #default poly order 0
90        n=0
91        if kwargs.has_key('poly'):
92            self.fitfunc = 'poly'
93            n = kwargs.get('poly')
94            self.components = [n]
95            self.uselinear = False
96        elif kwargs.has_key('lpoly'):
97            self.fitfunc = 'poly'
98            n = kwargs.get('lpoly')
99            self.components = [n]
100            self.uselinear = True
101        elif kwargs.has_key('gauss'):
102            n = kwargs.get('gauss')
103            self.fitfunc = 'gauss'
104            self.fitfuncs = [ 'gauss' for i in range(n) ]
105            self.components = [ 3 for i in range(n) ]
106            self.uselinear = False
107        else:
108            msg = "Invalid function type."
109            if rcParams['verbose']:
110                #print msg
111                asaplog.push(msg)
112                print_log('ERROR')
113                return
114            else:
115                raise TypeError(msg)
116
117        self.fitter.setexpression(self.fitfunc,n)
118        self.fitted = False
119        return
120
121    def fit(self, row=0, estimate=False):
122        """
123        Execute the actual fitting process. All the state has to be set.
124        Parameters:
125            row:        specify the row in the scantable
126            estimate:   auto-compute an initial parameter set (default False)
127                        This can be used to compute estimates even if fit was
128                        called before.
129        Example:
130            s = scantable('myscan.asap')
131            s.set_cursor(thepol=1)        # select second pol
132            f = fitter()
133            f.set_scan(s)
134            f.set_function(poly=0)
135            f.fit(row=0)                  # fit first row
136        """
137        if ((self.x is None or self.y is None) and self.data is None) \
138               or self.fitfunc is None:
139            msg = "Fitter not yet initialised. Please set data & fit function"
140            if rcParams['verbose']:
141                #print msg
142                asaplog.push(msg)
143                print_log('ERROR')
144                return
145            else:
146                raise RuntimeError(msg)
147
148        else:
149            if self.data is not None:
150                self.x = self.data._getabcissa(row)
151                self.y = self.data._getspectrum(row)
152                self.mask = mask_and(self.mask, self.data._getmask(row))
153                from asap import asaplog
154                asaplog.push("Fitting:")
155                i = row
156                out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (self.data.getscan(i),
157                                                                      self.data.getbeam(i),
158                                                                      self.data.getif(i),
159                                                                      self.data.getpol(i),
160                                                                      self.data.getcycle(i))
161                asaplog.push(out,False)
162        self.fitter.setdata(self.x, self.y, self.mask)
163        if self.fitfunc == 'gauss':
164            ps = self.fitter.getparameters()
165            if len(ps) == 0 or estimate:
166                self.fitter.estimate()
167        try:
168            fxdpar = list(self.fitter.getfixedparameters())
169            if len(fxdpar) and fxdpar.count(0) == 0:
170                 raise RuntimeError,"No point fitting, if all parameters are fixed."
171            if self.uselinear:
172                converged = self.fitter.lfit()
173            else:
174                converged = self.fitter.fit()
175            if not converged:
176                raise RuntimeError,"Fit didn't converge."
177        except RuntimeError, msg:
178            if rcParams['verbose']:
179                #print msg
180                print_log()
181                asaplog.push(msg.message)
182                print_log('ERROR')
183            else:
184                raise
185        self._fittedrow = row
186        self.fitted = True
187        print_log()
188        return
189
190    def store_fit(self, filename=None):
191        """
192        Save the fit parameters.
193        Parameters:
194            filename:    if specified save as an ASCII file, if None (default)
195                         store it in the scnatable
196        """
197        if self.fitted and self.data is not None:
198            pars = list(self.fitter.getparameters())
199            fixed = list(self.fitter.getfixedparameters())
200            from asap.asapfit import asapfit
201            fit = asapfit()
202            fit.setparameters(pars)
203            fit.setfixedparameters(fixed)
204            fit.setfunctions(self.fitfuncs)
205            fit.setcomponents(self.components)
206            fit.setframeinfo(self.data._getcoordinfo())
207            if filename is not None:
208                import os
209                filename = os.path.expandvars(os.path.expanduser(filename))
210                if os.path.exists(filename):
211                    raise IOError("File '%s' exists." % filename)
212                fit.save(filename)
213            else:
214                self.data._addfit(fit,self._fittedrow)
215
216    #def set_parameters(self, params, fixed=None, component=None):
217    def set_parameters(self,*args,**kwargs):
218        """
219        Set the parameters to be fitted.
220        Parameters:
221              params:    a vector of parameters
222              fixed:     a vector of which parameters are to be held fixed
223                         (default is none)
224              component: in case of multiple gaussians, the index of the
225                         component
226        """
227        component = None
228        fixed = None
229        params = None
230
231        if len(args) and isinstance(args[0],dict):
232            kwargs = args[0]
233        if kwargs.has_key("fixed"): fixed = kwargs["fixed"]
234        if kwargs.has_key("params"): params = kwargs["params"]
235        if len(args) == 2 and isinstance(args[1], int):
236            component = args[1]
237        if self.fitfunc is None:
238            msg = "Please specify a fitting function first."
239            if rcParams['verbose']:
240                #print msg
241                asaplog.push(msg)
242                print_log('ERROR')
243                return
244            else:
245                raise RuntimeError(msg)
246        if self.fitfunc == "gauss" and component is not None:
247            if not self.fitted and sum(self.fitter.getparameters()) == 0:
248                pars = _n_bools(len(self.components)*3, False)
249                fxd = _n_bools(len(pars), False)
250            else:
251                pars = list(self.fitter.getparameters())
252                fxd = list(self.fitter.getfixedparameters())
253            i = 3*component
254            pars[i:i+3] = params
255            fxd[i:i+3] = fixed
256            params = pars
257            fixed = fxd
258        self.fitter.setparameters(params)
259        if fixed is not None:
260            self.fitter.setfixedparameters(fixed)
261        print_log()
262        return
263
264    def set_gauss_parameters(self, peak, centre, fwhm,
265                             peakfixed=0, centrefixed=0,
266                             fwhmfixed=0,
267                             component=0):
268        """
269        Set the Parameters of a 'Gaussian' component, set with set_function.
270        Parameters:
271            peak, centre, fwhm:  The gaussian parameters
272            peakfixed,
273            centrefixed,
274            fwhmfixed:           Optional parameters to indicate if
275                                 the paramters should be held fixed during
276                                 the fitting process. The default is to keep
277                                 all parameters flexible.
278            component:           The number of the component (Default is the
279                                 component 0)
280        """
281        if self.fitfunc != "gauss":
282            msg = "Function only operates on Gaussian components."
283            if rcParams['verbose']:
284                #print msg
285                asaplog.push(msg)
286                print_log('ERROR')
287                return
288            else:
289                raise ValueError(msg)
290        if 0 <= component < len(self.components):
291            d = {'params':[peak, centre, fwhm],
292                 'fixed':[peakfixed, centrefixed, fwhmfixed]}
293            self.set_parameters(d, component)
294        else:
295            msg = "Please select a valid  component."
296            if rcParams['verbose']:
297                #print msg
298                asaplog.push(msg)
299                print_log('ERROR')
300                return
301            else:
302                raise ValueError(msg)
303
304    def get_area(self, component=None):
305        """
306        Return the area under the fitted gaussian component.
307        Parameters:
308              component:   the gaussian component selection,
309                           default (None) is the sum of all components
310        Note:
311              This will only work for gaussian fits.
312        """
313        if not self.fitted: return
314        if self.fitfunc == "gauss":
315            pars = list(self.fitter.getparameters())
316            from math import log,pi,sqrt
317            fac = sqrt(pi/log(16.0))
318            areas = []
319            for i in range(len(self.components)):
320                j = i*3
321                cpars = pars[j:j+3]
322                areas.append(fac * cpars[0] * cpars[2])
323        else:
324            return None
325        if component is not None:
326            return areas[component]
327        else:
328            return sum(areas)
329
330    def get_errors(self, component=None):
331        """
332        Return the errors in the parameters.
333        Parameters:
334            component:    get the errors for the specified component
335                          only, default is all components
336        """
337        if not self.fitted:
338            msg = "Not yet fitted."
339            if rcParams['verbose']:
340                #print msg
341                asaplog.push(msg)
342                print_log('ERROR')
343                return
344            else:
345                raise RuntimeError(msg)
346        errs = list(self.fitter.geterrors())
347        cerrs = errs
348        if component is not None:
349            if self.fitfunc == "gauss":
350                i = 3*component
351                if i < len(errs):
352                    cerrs = errs[i:i+3]
353        return cerrs
354
355    def get_parameters(self, component=None, errors=False):
356        """
357        Return the fit paramters.
358        Parameters:
359             component:    get the parameters for the specified component
360                           only, default is all components
361        """
362        if not self.fitted:
363            msg = "Not yet fitted."
364            if rcParams['verbose']:
365                #print msg
366                asaplog.push(msg)
367                print_log('ERROR')
368                return
369            else:
370                raise RuntimeError(msg)
371        pars = list(self.fitter.getparameters())
372        fixed = list(self.fitter.getfixedparameters())
373        errs = list(self.fitter.geterrors())
374        area = []
375        if component is not None:
376            if self.fitfunc == "gauss":
377                i = 3*component
378                cpars = pars[i:i+3]
379                cfixed = fixed[i:i+3]
380                cerrs = errs[i:i+3]
381                a = self.get_area(component)
382                area = [a for i in range(3)]
383            else:
384                cpars = pars
385                cfixed = fixed
386                cerrs = errs
387        else:
388            cpars = pars
389            cfixed = fixed
390            cerrs = errs
391            if self.fitfunc == "gauss":
392                for c in range(len(self.components)):
393                  a = self.get_area(c)
394                  area += [a for i in range(3)]
395        fpars = self._format_pars(cpars, cfixed, errors and cerrs, area)
396        if rcParams['verbose']:
397            #print fpars
398            asaplog.push(fpars)
399            print_log()
400        return {'params':cpars, 'fixed':cfixed, 'formatted': fpars,
401                'errors':cerrs}
402
403    def _format_pars(self, pars, fixed, errors, area):
404        out = ''
405        if self.fitfunc == 'poly':
406            c = 0
407            for i in range(len(pars)):
408                fix = ""
409                if len(fixed) and fixed[i]: fix = "(fixed)"
410                if errors :
411                    out += '  p%d%s= %3.6f (%1.6f),' % (c,fix,pars[i], errors[i])
412                else:
413                    out += '  p%d%s= %3.6f,' % (c,fix,pars[i])
414                c+=1
415            out = out[:-1]  # remove trailing ','
416        elif self.fitfunc == 'gauss':
417            i = 0
418            c = 0
419            aunit = ''
420            ounit = ''
421            if self.data:
422                aunit = self.data.get_unit()
423                ounit = self.data.get_fluxunit()
424            while i < len(pars):
425                if len(area):
426                    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[i],ounit,aunit)
427                else:
428                    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)
429                c+=1
430                i+=3
431        return out
432
433    def get_estimate(self):
434        """
435        Return the parameter estimates (for non-linear functions).
436        """
437        pars = self.fitter.getestimate()
438        fixed = self.fitter.getfixedparameters()
439        if rcParams['verbose']:
440            #print self._format_pars(pars,fixed,None)
441            asaplog.push(self._format_pars(pars,fixed,None))
442            print_log()
443        return pars
444
445    def get_residual(self):
446        """
447        Return the residual of the fit.
448        """
449        if not self.fitted:
450            msg = "Not yet fitted."
451            if rcParams['verbose']:
452                #print msg
453                asaplog.push(msg)
454                print_log('ERROR')
455                return
456            else:
457                raise RuntimeError(msg)
458        return self.fitter.getresidual()
459
460    def get_chi2(self):
461        """
462        Return chi^2.
463        """
464        if not self.fitted:
465            msg = "Not yet fitted."
466            if rcParams['verbose']:
467                #print msg
468                asaplog.push(msg)
469                print_log('ERROR')
470                return
471            else:
472                raise RuntimeError(msg)
473        ch2 = self.fitter.getchi2()
474        if rcParams['verbose']:
475            #print 'Chi^2 = %3.3f' % (ch2)
476            asaplog.push( 'Chi^2 = %3.3f' % (ch2) )
477            print_log()
478        return ch2
479
480    def get_fit(self):
481        """
482        Return the fitted ordinate values.
483        """
484        if not self.fitted:
485            msg = "Not yet fitted."
486            if rcParams['verbose']:
487                #print msg
488                asaplog.push(msg)
489                print_log('ERROR')
490                return
491            else:
492                raise RuntimeError(msg)
493        return self.fitter.getfit()
494
495    def commit(self):
496        """
497        Return a new scan where the fits have been commited (subtracted)
498        """
499        if not self.fitted:
500            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)
508        from asap import scantable
509        if not isinstance(self.data, scantable):
510            msg = "Not a scantable"
511            if rcParams['verbose']:
512                #print msg
513                asaplog.push(msg)
514                print_log('ERROR')
515                return
516            else:
517                raise TypeError(msg)
518        scan = self.data.copy()
519        scan._setspectrum(self.fitter.getresidual())
520        print_log()
521        return scan
522
523    def plot(self, residual=False, components=None, plotparms=False, filename=None):
524        """
525        Plot the last fit.
526        Parameters:
527            residual:    an optional parameter indicating if the residual
528                         should be plotted (default 'False')
529            components:  a list of components to plot, e.g [0,1],
530                         -1 plots the total fit. Default is to only
531                         plot the total fit.
532            plotparms:   Inidicates if the parameter values should be present
533                         on the plot
534        """
535        if not self.fitted:
536            return
537        if not self._p or self._p.is_dead:
538            if rcParams['plotter.gui']:
539                from asap.asaplotgui import asaplotgui as asaplot
540            else:
541                from asap.asaplot import asaplot
542            self._p = asaplot()
543        self._p.hold()
544        self._p.clear()
545        self._p.set_panels()
546        self._p.palette(0)
547        tlab = 'Spectrum'
548        xlab = 'Abcissa'
549        ylab = 'Ordinate'
550        from matplotlib.numerix import ma,logical_not,logical_and,array
551        m = self.mask
552        if self.data:
553            tlab = self.data._getsourcename(self._fittedrow)
554            xlab = self.data._getabcissalabel(self._fittedrow)
555            m =  logical_and(self.mask,
556                             array(self.data._getmask(self._fittedrow),
557                                   copy=False))
558                             
559            ylab = self.data._get_ordinate_label()
560
561        colours = ["#777777","#dddddd","red","orange","purple","green","magenta", "cyan"]
562        nomask=True
563        for i in range(len(m)):
564            nomask = nomask and m[i]
565        label0='Masked Region'
566        label1='Spectrum'
567        if ( nomask ):
568            label0=label1
569        else:
570            y = ma.masked_array( self.y, mask = m )
571            self._p.palette(1,colours)
572            self._p.set_line( label = label1 )
573            self._p.plot( self.x, y )
574        self._p.palette(0,colours)
575        self._p.set_line(label=label0)
576        y = ma.masked_array(self.y,mask=logical_not(m))
577        self._p.plot(self.x, y)
578        if residual:
579            self._p.palette(7)
580            self._p.set_line(label='Residual')
581            y = ma.masked_array(self.get_residual(),
582                                  mask=logical_not(m))
583            self._p.plot(self.x, y)
584        self._p.palette(2)
585        if components is not None:
586            cs = components
587            if isinstance(components,int): cs = [components]
588            if plotparms:
589                self._p.text(0.15,0.15,str(self.get_parameters()['formatted']),size=8)
590            n = len(self.components)
591            self._p.palette(3)
592            for c in cs:
593                if 0 <= c < n:
594                    lab = self.fitfuncs[c]+str(c)
595                    self._p.set_line(label=lab)
596                    y = ma.masked_array(self.fitter.evaluate(c),
597                                          mask=logical_not(m))
598
599                    self._p.plot(self.x, y)
600                elif c == -1:
601                    self._p.palette(2)
602                    self._p.set_line(label="Total Fit")
603                    y = ma.masked_array(self.fitter.getfit(),
604                                          mask=logical_not(m))
605                    self._p.plot(self.x, y)
606        else:
607            self._p.palette(2)
608            self._p.set_line(label='Fit')
609            y = ma.masked_array(self.fitter.getfit(),
610                                  mask=logical_not(m))
611            self._p.plot(self.x, y)
612        xlim=[min(self.x),max(self.x)]
613        self._p.axes.set_xlim(xlim)
614        self._p.set_axes('xlabel',xlab)
615        self._p.set_axes('ylabel',ylab)
616        self._p.set_axes('title',tlab)
617        self._p.release()
618        if (not rcParams['plotter.gui']):
619            self._p.save(filename)
620        print_log()
621
622    def auto_fit(self, insitu=None, plot=False):
623        """
624        Return a scan where the function is applied to all rows for
625        all Beams/IFs/Pols.
626
627        """
628        from asap import scantable
629        if not isinstance(self.data, scantable) :
630            msg = "Data is not a scantable"
631            if rcParams['verbose']:
632                #print msg
633                asaplog.push(msg)
634                print_log('ERROR')
635                return
636            else:
637                raise TypeError(msg)
638        if insitu is None: insitu = rcParams['insitu']
639        if not insitu:
640            scan = self.data.copy()
641        else:
642            scan = self.data
643        rows = xrange(scan.nrow())
644        # Save parameters of baseline fits as a class attribute.
645        # NOTICE: This does not reflect changes in scantable!
646        if len(rows) > 0: self.blpars=[]
647        from asap import asaplog
648        asaplog.push("Fitting:")
649        for r in rows:
650            out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (scan.getscan(r),
651                                                                   scan.getbeam(r),
652                                                                   scan.getif(r),
653                                                                   scan.getpol(r),
654                                                                   scan.getcycle(r))
655            asaplog.push(out, False)
656            self.x = scan._getabcissa(r)
657            self.y = scan._getspectrum(r)
658            self.mask = mask_and(self.mask, scan._getmask(r))
659            self.data = None
660            self.fit()
661            x = self.get_parameters()
662            fpar = self.get_parameters()
663            if plot:
664                self.plot(residual=True)
665                x = raw_input("Accept fit ([y]/n): ")
666                if x.upper() == 'N':
667                    self.blpars.append(None)
668                    continue
669            scan._setspectrum(self.fitter.getresidual(), r)
670            self.blpars.append(fpar)
671        if plot:
672            self._p.unmap()
673            self._p = None
674        print_log()
675        return scan
676
Note: See TracBrowser for help on using the repository browser.