source: trunk/python/asapfitter.py@ 1256

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

Merge from Release2.1.0b tag

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