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

Last change on this file since 1606 was 1603, checked in by TakTsutsumi, 15 years ago

New Development: No, merge with asap2.3.1

JIRA Issue: Yes CAS-1450

Ready to Release: Yes/No

Interface Changes: Yes/No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes

Module(s): single dish

Description: Upgrade of alma branch based on ASAP2.2.0

(rev.1562) to ASAP2.3.1 (rev.1561)


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