source: trunk/python/asapfitter.py@ 1374

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

Fix for ticket #96, copy keyword was passed to logical_and instead of array

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.6 KB
RevLine 
[113]1import _asap
[259]2from asap import rcParams
[723]3from asap import print_log
[1295]4from asap import _n_bools
[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:
[1295]44 self.mask = _n_bools(len(xdat), True)
[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:
[1295]67 self.mask = _n_bools(self.data.nchan(), True)
[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:
[1295]217 pars = _n_bools(len(self.components)*3, False)
218 fxd = _n_bools(len(pars), False)
[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'
[1273]495 from matplotlib.numerix import ma,logical_not,logical_and,array
496 m = self.mask
[113]497 if self.data:
[515]498 tlab = self.data._getsourcename(self._fittedrow)
499 xlab = self.data._getabcissalabel(self._fittedrow)
[1273]500 m = logical_and(self.mask,
[1306]501 array(self.data._getmask(self._fittedrow),
502 copy=False))
503
[626]504 ylab = self.data._get_ordinate_label()
[515]505
[1075]506 colours = ["#777777","#dddddd","red","orange","purple","green","magenta", "cyan"]
[652]507 self._p.palette(0,colours)
[515]508 self._p.set_line(label='Spectrum')
[1273]509 y = ma.masked_array(self.y,mask=logical_not(m))
[1088]510 self._p.plot(self.x, y)
[113]511 if residual:
[652]512 self._p.palette(1)
[515]513 self._p.set_line(label='Residual')
[1116]514 y = ma.masked_array(self.get_residual(),
[1273]515 mask=logical_not(m))
[1088]516 self._p.plot(self.x, y)
[652]517 self._p.palette(2)
[515]518 if components is not None:
519 cs = components
520 if isinstance(components,int): cs = [components]
[526]521 if plotparms:
[1031]522 self._p.text(0.15,0.15,str(self.get_parameters()['formatted']),size=8)
[515]523 n = len(self.components)
[652]524 self._p.palette(3)
[515]525 for c in cs:
526 if 0 <= c < n:
527 lab = self.fitfuncs[c]+str(c)
528 self._p.set_line(label=lab)
[1116]529 y = ma.masked_array(self.fitter.evaluate(c),
[1273]530 mask=logical_not(m))
[1088]531
532 self._p.plot(self.x, y)
[515]533 elif c == -1:
[652]534 self._p.palette(2)
[515]535 self._p.set_line(label="Total Fit")
[1116]536 y = ma.masked_array(self.fitter.getfit(),
[1273]537 mask=logical_not(m))
[1088]538 self._p.plot(self.x, y)
[515]539 else:
[652]540 self._p.palette(2)
[515]541 self._p.set_line(label='Fit')
[1116]542 y = ma.masked_array(self.fitter.getfit(),
[1273]543 mask=logical_not(m))
[1088]544 self._p.plot(self.x, y)
[723]545 xlim=[min(self.x),max(self.x)]
546 self._p.axes.set_xlim(xlim)
[113]547 self._p.set_axes('xlabel',xlab)
548 self._p.set_axes('ylabel',ylab)
549 self._p.set_axes('title',tlab)
550 self._p.release()
[723]551 if (not rcParams['plotter.gui']):
552 self._p.save(filename)
553 print_log()
[113]554
[1061]555 def auto_fit(self, insitu=None, plot=False):
[113]556 """
[515]557 Return a scan where the function is applied to all rows for
558 all Beams/IFs/Pols.
[723]559
[113]560 """
561 from asap import scantable
[515]562 if not isinstance(self.data, scantable) :
[723]563 msg = "Data is not a scantable"
564 if rcParams['verbose']:
565 print msg
566 return
567 else:
568 raise TypeError(msg)
[259]569 if insitu is None: insitu = rcParams['insitu']
570 if not insitu:
571 scan = self.data.copy()
572 else:
573 scan = self.data
[880]574 rows = xrange(scan.nrow())
[723]575 from asap import asaplog
[876]576 asaplog.push("Fitting:")
577 for r in rows:
[1031]578 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]579 asaplog.push(out, False)
[876]580 self.x = scan._getabcissa(r)
581 self.y = scan._getspectrum(r)
582 self.data = None
583 self.fit()
584 x = self.get_parameters()
[1061]585 if plot:
586 self.plot(residual=True)
587 x = raw_input("Accept fit ([y]/n): ")
588 if x.upper() == 'N':
589 continue
[880]590 scan._setspectrum(self.fitter.getresidual(), r)
[1061]591 if plot:
592 self._p.unmap()
593 self._p = None
[876]594 print_log()
595 return scan
[794]596
Note: See TracBrowser for help on using the repository browser.