source: trunk/python/asapfitter.py@ 1895

Last change on this file since 1895 was 1862, checked in by Malte Marquarding, 14 years ago

renamed print_log_dec to more explicit asaplog_post_dec

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