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

Last change on this file since 1669 was 1632, 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.
Removed duplicated 'from asap import asaplog' lines.


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