source: branches/polybatch/python/asapfitter.py@ 2745

Last change on this file since 2745 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
Line 
1import _asap
2from asap.parameters import rcParams
3from asap.logging import asaplog, asaplog_post_dec
4from asap.utils import _n_bools, mask_and
5
6
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
20 self.fitfuncs = None
21 self.fitted = False
22 self.data = None
23 self.components = 0
24 self._fittedrow = 0
25 self._p = None
26 self._selection = None
27 self.uselinear = False
28
29 def set_data(self, xdat, ydat, mask=None):
30 """
31 Set the absissa and ordinate for the fit. Also set the mask
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:
36 xdat: the abcissa values
37 ydat: the ordinate values
38 mask: an optional mask
39
40 """
41 self.fitted = False
42 self.x = xdat
43 self.y = ydat
44 if mask == None:
45 self.mask = _n_bools(len(xdat), True)
46 else:
47 self.mask = mask
48 return
49
50 @asaplog_post_dec
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
56 mask: a msk retrieved from the scantable
57 """
58 if not thescan:
59 msg = "Please give a correct scan"
60 raise TypeError(msg)
61 self.fitted = False
62 self.data = thescan
63 self.mask = None
64 if mask is None:
65 self.mask = _n_bools(self.data.nchan(), True)
66 else:
67 self.mask = mask
68 return
69
70 @asaplog_post_dec
71 def set_function(self, **kwargs):
72 """
73 Set the function to be fit.
74 Parameters:
75 poly: use a polynomial of the order given with nonlinear least squares fit
76 lpoly: use polynomial of the order given with linear least squares fit
77 gauss: fit the number of gaussian specified
78 lorentz: fit the number of lorentzian specified
79 Example:
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
82 fitter.set_function(gauss=2) # will fit two gaussians
83 fitter.set_function(lorentz=2) # will fit two lorentzians
84 """
85 #default poly order 0
86 n=0
87 if kwargs.has_key('poly'):
88 self.fitfunc = 'poly'
89 n = kwargs.get('poly')
90 self.components = [n]
91 self.uselinear = False
92 elif kwargs.has_key('lpoly'):
93 self.fitfunc = 'poly'
94 n = kwargs.get('lpoly')
95 self.components = [n]
96 self.uselinear = True
97 elif kwargs.has_key('gauss'):
98 n = kwargs.get('gauss')
99 self.fitfunc = 'gauss'
100 self.fitfuncs = [ 'gauss' for i in range(n) ]
101 self.components = [ 3 for i in range(n) ]
102 self.uselinear = False
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
109 else:
110 msg = "Invalid function type."
111 raise TypeError(msg)
112
113 self.fitter.setexpression(self.fitfunc,n)
114 self.fitted = False
115 return
116
117 @asaplog_post_dec
118 def fit(self, row=0, estimate=False):
119 """
120 Execute the actual fitting process. All the state has to be set.
121 Parameters:
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.
126 Example:
127 s = scantable('myscan.asap')
128 s.set_cursor(thepol=1) # select second pol
129 f = fitter()
130 f.set_scan(s)
131 f.set_function(poly=0)
132 f.fit(row=0) # fit first row
133 """
134 if ((self.x is None or self.y is None) and self.data is None) \
135 or self.fitfunc is None:
136 msg = "Fitter not yet initialised. Please set data & fit function"
137 raise RuntimeError(msg)
138
139 else:
140 if self.data is not None:
141 self.x = self.data._getabcissa(row)
142 self.y = self.data._getspectrum(row)
143 self.mask = mask_and(self.mask, self.data._getmask(row))
144 asaplog.push("Fitting:")
145 i = row
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),
149 self.data.getpol(i),
150 self.data.getcycle(i))
151 asaplog.push(out,False)
152 self.fitter.setdata(self.x, self.y, self.mask)
153 if self.fitfunc == 'gauss' or self.fitfunc == 'lorentz':
154 ps = self.fitter.getparameters()
155 if len(ps) == 0 or estimate:
156 self.fitter.estimate()
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."
166 self._fittedrow = row
167 self.fitted = True
168 return
169
170 def store_fit(self, filename=None):
171 """
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
176 """
177 if self.fitted and self.data is not None:
178 pars = list(self.fitter.getparameters())
179 fixed = list(self.fitter.getfixedparameters())
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())
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)
195
196 @asaplog_post_dec
197 def set_parameters(self,*args,**kwargs):
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
206 """
207 component = None
208 fixed = None
209 params = None
210
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]
217 if self.fitfunc is None:
218 msg = "Please specify a fitting function first."
219 raise RuntimeError(msg)
220 if (self.fitfunc == "gauss" or self.fitfunc == 'lorentz') and component is not None:
221 if not self.fitted and sum(self.fitter.getparameters()) == 0:
222 pars = _n_bools(len(self.components)*3, False)
223 fxd = _n_bools(len(pars), False)
224 else:
225 pars = list(self.fitter.getparameters())
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
231 fixed = fxd
232 self.fitter.setparameters(params)
233 if fixed is not None:
234 self.fitter.setfixedparameters(fixed)
235 return
236
237 @asaplog_post_dec
238 def set_gauss_parameters(self, peak, centre, fwhm,
239 peakfixed=0, centrefixed=0,
240 fwhmfixed=0,
241 component=0):
242 """
243 Set the Parameters of a 'Gaussian' component, set with set_function.
244 Parameters:
245 peak, centre, fwhm: The gaussian parameters
246 peakfixed,
247 centrefixed,
248 fwhmfixed: Optional parameters to indicate if
249 the paramters should be held fixed during
250 the fitting process. The default is to keep
251 all parameters flexible.
252 component: The number of the component (Default is the
253 component 0)
254 """
255 if self.fitfunc != "gauss":
256 msg = "Function only operates on Gaussian components."
257 raise ValueError(msg)
258 if 0 <= component < len(self.components):
259 d = {'params':[peak, centre, fwhm],
260 'fixed':[peakfixed, centrefixed, fwhmfixed]}
261 self.set_parameters(d, component)
262 else:
263 msg = "Please select a valid component."
264 raise ValueError(msg)
265
266 @asaplog_post_dec
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."
286 raise ValueError(msg)
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."
293 raise ValueError(msg)
294
295 def get_area(self, component=None):
296 """
297 Return the area under the fitted gaussian/lorentzian component.
298 Parameters:
299 component: the gaussian/lorentzian component selection,
300 default (None) is the sum of all components
301 Note:
302 This will only work for gaussian/lorentzian fits.
303 """
304 if not self.fitted: return
305 if self.fitfunc == "gauss" or self.fitfunc == "lorentz":
306 pars = list(self.fitter.getparameters())
307 from math import log,pi,sqrt
308 if self.fitfunc == "gauss":
309 fac = sqrt(pi/log(16.0))
310 elif self.fitfunc == "lorentz":
311 fac = pi/2.0
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
324 @asaplog_post_dec
325 def get_errors(self, component=None):
326 """
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."
334 raise RuntimeError(msg)
335 errs = list(self.fitter.geterrors())
336 cerrs = errs
337 if component is not None:
338 if self.fitfunc == "gauss" or self.fitfunc == "lorentz":
339 i = 3*component
340 if i < len(errs):
341 cerrs = errs[i:i+3]
342 return cerrs
343
344
345 @asaplog_post_dec
346 def get_parameters(self, component=None, errors=False):
347 """
348 Return the fit paramters.
349 Parameters:
350 component: get the parameters for the specified component
351 only, default is all components
352 """
353 if not self.fitted:
354 msg = "Not yet fitted."
355 raise RuntimeError(msg)
356 pars = list(self.fitter.getparameters())
357 fixed = list(self.fitter.getfixedparameters())
358 errs = list(self.fitter.geterrors())
359 area = []
360 if component is not None:
361 if self.fitfunc == "gauss" or self.fitfunc == "lorentz":
362 i = 3*component
363 cpars = pars[i:i+3]
364 cfixed = fixed[i:i+3]
365 cerrs = errs[i:i+3]
366 a = self.get_area(component)
367 area = [a for i in range(3)]
368 else:
369 cpars = pars
370 cfixed = fixed
371 cerrs = errs
372 else:
373 cpars = pars
374 cfixed = fixed
375 cerrs = errs
376 if self.fitfunc == "gauss" or self.fitfunc == "lorentz":
377 for c in range(len(self.components)):
378 a = self.get_area(c)
379 area += [a for i in range(3)]
380 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area)
381 asaplog.push(fpars)
382 return {'params':cpars, 'fixed':cfixed, 'formatted': fpars,
383 'errors':cerrs}
384
385 def _format_pars(self, pars, fixed, errors, area):
386 out = ''
387 if self.fitfunc == 'poly':
388 c = 0
389 for i in range(len(pars)):
390 fix = ""
391 if len(fixed) and fixed[i]: fix = "(fixed)"
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])
396 c+=1
397 out = out[:-1] # remove trailing ','
398 elif self.fitfunc == 'gauss' or self.fitfunc == 'lorentz':
399 i = 0
400 c = 0
401 aunit = ''
402 ounit = ''
403 if self.data:
404 aunit = self.data.get_unit()
405 ounit = self.data.get_fluxunit()
406 while i < len(pars):
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)
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)
411 c+=1
412 i+=3
413 return out
414
415
416 @asaplog_post_dec
417 def get_estimate(self):
418 """
419 Return the parameter estimates (for non-linear functions).
420 """
421 pars = self.fitter.getestimate()
422 fixed = self.fitter.getfixedparameters()
423 asaplog.push(self._format_pars(pars,fixed,None))
424 return pars
425
426 @asaplog_post_dec
427 def get_residual(self):
428 """
429 Return the residual of the fit.
430 """
431 if not self.fitted:
432 msg = "Not yet fitted."
433 raise RuntimeError(msg)
434 return self.fitter.getresidual()
435
436 @asaplog_post_dec
437 def get_chi2(self):
438 """
439 Return chi^2.
440 """
441 if not self.fitted:
442 msg = "Not yet fitted."
443 raise RuntimeError(msg)
444 ch2 = self.fitter.getchi2()
445 asaplog.push( 'Chi^2 = %3.3f' % (ch2) )
446 return ch2
447
448 @asaplog_post_dec
449 def get_fit(self):
450 """
451 Return the fitted ordinate values.
452 """
453 if not self.fitted:
454 msg = "Not yet fitted."
455 raise RuntimeError(msg)
456 return self.fitter.getfit()
457
458 @asaplog_post_dec
459 def commit(self):
460 """
461 Return a new scan where the fits have been commited (subtracted)
462 """
463 if not self.fitted:
464 msg = "Not yet fitted."
465 raise RuntimeError(msg)
466 from asap import scantable
467 if not isinstance(self.data, scantable):
468 msg = "Not a scantable"
469 raise TypeError(msg)
470 scan = self.data.copy()
471 scan._setspectrum(self.fitter.getresidual())
472 return scan
473
474 @asaplog_post_dec
475 def plot(self, residual=False, components=None, plotparms=False,
476 filename=None):
477 """
478 Plot the last fit.
479 Parameters:
480 residual: an optional parameter indicating if the residual
481 should be plotted (default 'False')
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
487 """
488 if not self.fitted:
489 return
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()
497 self._p.clear()
498 self._p.set_panels()
499 self._p.palette(0)
500 tlab = 'Spectrum'
501 xlab = 'Abcissa'
502 ylab = 'Ordinate'
503 from numpy import ma,logical_not,logical_and,array
504 m = self.mask
505 if self.data:
506 tlab = self.data._getsourcename(self._fittedrow)
507 xlab = self.data._getabcissalabel(self._fittedrow)
508 m = logical_and(self.mask,
509 array(self.data._getmask(self._fittedrow),
510 copy=False))
511
512 ylab = self.data._get_ordinate_label()
513
514 colours = ["#777777","#dddddd","red","orange","purple","green","magenta", "cyan"]
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 )
527 self._p.palette(0,colours)
528 self._p.set_line(label=label0)
529 y = ma.masked_array(self.y,mask=logical_not(m))
530 self._p.plot(self.x, y)
531 if residual:
532 self._p.palette(7)
533 self._p.set_line(label='Residual')
534 y = ma.masked_array(self.get_residual(),
535 mask=logical_not(m))
536 self._p.plot(self.x, y)
537 self._p.palette(2)
538 if components is not None:
539 cs = components
540 if isinstance(components,int): cs = [components]
541 if plotparms:
542 self._p.text(0.15,0.15,str(self.get_parameters()['formatted']),size=8)
543 n = len(self.components)
544 self._p.palette(3)
545 for c in cs:
546 if 0 <= c < n:
547 lab = self.fitfuncs[c]+str(c)
548 self._p.set_line(label=lab)
549 y = ma.masked_array(self.fitter.evaluate(c),
550 mask=logical_not(m))
551
552 self._p.plot(self.x, y)
553 elif c == -1:
554 self._p.palette(2)
555 self._p.set_line(label="Total Fit")
556 y = ma.masked_array(self.fitter.getfit(),
557 mask=logical_not(m))
558 self._p.plot(self.x, y)
559 else:
560 self._p.palette(2)
561 self._p.set_line(label='Fit')
562 y = ma.masked_array(self.fitter.getfit(),
563 mask=logical_not(m))
564 self._p.plot(self.x, y)
565 xlim=[min(self.x),max(self.x)]
566 self._p.axes.set_xlim(xlim)
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()
571 if (not rcParams['plotter.gui']):
572 self._p.save(filename)
573
574 @asaplog_post_dec
575 def auto_fit(self, insitu=None, plot=False):
576 """
577 Return a scan where the function is applied to all rows for
578 all Beams/IFs/Pols.
579
580 """
581 from asap import scantable
582 if not isinstance(self.data, scantable) :
583 msg = "Data is not a scantable"
584 raise TypeError(msg)
585 if insitu is None: insitu = rcParams['insitu']
586 if not insitu:
587 scan = self.data.copy()
588 else:
589 scan = self.data
590 rows = xrange(scan.nrow())
591 # Save parameters of baseline fits as a class attribute.
592 # NOTICE: This does not reflect changes in scantable!
593 if len(rows) > 0: self.blpars=[]
594 asaplog.push("Fitting:")
595 for r in rows:
596 out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (scan.getscan(r),
597 scan.getbeam(r),
598 scan.getif(r),
599 scan.getpol(r),
600 scan.getcycle(r))
601 asaplog.push(out, False)
602 self.x = scan._getabcissa(r)
603 self.y = scan._getspectrum(r)
604 self.mask = mask_and(self.mask, scan._getmask(r))
605 self.data = None
606 self.fit()
607 x = self.get_parameters()
608 fpar = self.get_parameters()
609 if plot:
610 self.plot(residual=True)
611 x = raw_input("Accept fit ([y]/n): ")
612 if x.upper() == 'N':
613 self.blpars.append(None)
614 continue
615 scan._setspectrum(self.fitter.getresidual(), r)
616 self.blpars.append(fpar)
617 if plot:
618 self._p.unmap()
619 self._p = None
620 return scan
Note: See TracBrowser for help on using the repository browser.