source: tags/Release2.1.0b/python/asapfitter.py@ 1221

Last change on this file since 1221 was 1221, checked in by mar637, 19 years ago

Fix for ticket #65, fixed parameter issues. This was a problem with mask=!fixed in the fitter. Also, each fit reset the fixed parameters

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