source: trunk/python/asapfitter.py@ 2067

Last change on this file since 2067 was 2047, checked in by WataruKawasaki, 14 years ago

New Development: Yes

JIRA Issue: Yes CAS-2847

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: Yes

Module(s): scantable

Description: added {auto_}sinusoid_baseline() for sinusoidal baseline fitting. also minor bug fixes for asapfitter.


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