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

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

small fix to _format_pars to test for length. Ticket #44 - fit can be saved as ASCII via fitter.store_fit.

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