source: branches/asap-3.x/python/asapfitter.py@ 1957

Last change on this file since 1957 was 1739, checked in by Malte Marquarding, 15 years ago

Replace matplotlib.numerix with numpy

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.8 KB
Line 
1import _asap
2from asap import rcParams
3from asap import print_log_dec
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 @print_log_dec
117 def fit(self, row=0, estimate=False):
118 """
119 Execute the actual fitting process. All the state has to be set.
120 Parameters:
121 row: specify the row in the scantable
122 estimate: auto-compute an initial parameter set (default False)
123 This can be used to compute estimates even if fit was
124 called before.
125 Example:
126 s = scantable('myscan.asap')
127 s.set_cursor(thepol=1) # select second pol
128 f = fitter()
129 f.set_scan(s)
130 f.set_function(poly=0)
131 f.fit(row=0) # fit first row
132 """
133 if ((self.x is None or self.y is None) and self.data is None) \
134 or self.fitfunc is None:
135 msg = "Fitter not yet initialised. Please set data & fit function"
136 if rcParams['verbose']:
137 print msg
138 return
139 else:
140 raise RuntimeError(msg)
141
142 else:
143 if self.data is not None:
144 self.x = self.data._getabcissa(row)
145 self.y = self.data._getspectrum(row)
146 self.mask = mask_and(self.mask, self.data._getmask(row))
147 from asap import asaplog
148 asaplog.push("Fitting:")
149 i = row
150 out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (self.data.getscan(i),
151 self.data.getbeam(i),
152 self.data.getif(i),
153 self.data.getpol(i),
154 self.data.getcycle(i))
155 asaplog.push(out,False)
156 self.fitter.setdata(self.x, self.y, self.mask)
157 if self.fitfunc == 'gauss':
158 ps = self.fitter.getparameters()
159 if len(ps) == 0 or estimate:
160 self.fitter.estimate()
161 try:
162 fxdpar = list(self.fitter.getfixedparameters())
163 if len(fxdpar) and fxdpar.count(0) == 0:
164 raise RuntimeError,"No point fitting, if all parameters are fixed."
165 if self.uselinear:
166 converged = self.fitter.lfit()
167 else:
168 converged = self.fitter.fit()
169 if not converged:
170 raise RuntimeError,"Fit didn't converge."
171 except RuntimeError, msg:
172 if rcParams['verbose']:
173 print msg
174 else:
175 raise
176 self._fittedrow = row
177 self.fitted = True
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 @print_log_dec
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 return
250
251 def set_gauss_parameters(self, peak, centre, fwhm,
252 peakfixed=0, centrefixed=0,
253 fwhmfixed=0,
254 component=0):
255 """
256 Set the Parameters of a 'Gaussian' component, set with set_function.
257 Parameters:
258 peak, centre, fwhm: The gaussian parameters
259 peakfixed,
260 centrefixed,
261 fwhmfixed: Optional parameters to indicate if
262 the paramters should be held fixed during
263 the fitting process. The default is to keep
264 all parameters flexible.
265 component: The number of the component (Default is the
266 component 0)
267 """
268 if self.fitfunc != "gauss":
269 msg = "Function only operates on Gaussian components."
270 if rcParams['verbose']:
271 print msg
272 return
273 else:
274 raise ValueError(msg)
275 if 0 <= component < len(self.components):
276 d = {'params':[peak, centre, fwhm],
277 'fixed':[peakfixed, centrefixed, fwhmfixed]}
278 self.set_parameters(d, component)
279 else:
280 msg = "Please select a valid component."
281 if rcParams['verbose']:
282 print msg
283 return
284 else:
285 raise ValueError(msg)
286
287 def get_area(self, component=None):
288 """
289 Return the area under the fitted gaussian component.
290 Parameters:
291 component: the gaussian component selection,
292 default (None) is the sum of all components
293 Note:
294 This will only work for gaussian fits.
295 """
296 if not self.fitted: return
297 if self.fitfunc == "gauss":
298 pars = list(self.fitter.getparameters())
299 from math import log,pi,sqrt
300 fac = sqrt(pi/log(16.0))
301 areas = []
302 for i in range(len(self.components)):
303 j = i*3
304 cpars = pars[j:j+3]
305 areas.append(fac * cpars[0] * cpars[2])
306 else:
307 return None
308 if component is not None:
309 return areas[component]
310 else:
311 return sum(areas)
312
313 def get_errors(self, component=None):
314 """
315 Return the errors in the parameters.
316 Parameters:
317 component: get the errors for the specified component
318 only, default is all components
319 """
320 if not self.fitted:
321 msg = "Not yet fitted."
322 if rcParams['verbose']:
323 print msg
324 return
325 else:
326 raise RuntimeError(msg)
327 errs = list(self.fitter.geterrors())
328 cerrs = errs
329 if component is not None:
330 if self.fitfunc == "gauss":
331 i = 3*component
332 if i < len(errs):
333 cerrs = errs[i:i+3]
334 return cerrs
335
336 def get_parameters(self, component=None, errors=False):
337 """
338 Return the fit paramters.
339 Parameters:
340 component: get the parameters for the specified component
341 only, default is all components
342 """
343 if not self.fitted:
344 msg = "Not yet fitted."
345 if rcParams['verbose']:
346 print msg
347 return
348 else:
349 raise RuntimeError(msg)
350 pars = list(self.fitter.getparameters())
351 fixed = list(self.fitter.getfixedparameters())
352 errs = list(self.fitter.geterrors())
353 area = []
354 if component is not None:
355 if self.fitfunc == "gauss":
356 i = 3*component
357 cpars = pars[i:i+3]
358 cfixed = fixed[i:i+3]
359 cerrs = errs[i:i+3]
360 a = self.get_area(component)
361 area = [a for i in range(3)]
362 else:
363 cpars = pars
364 cfixed = fixed
365 cerrs = errs
366 else:
367 cpars = pars
368 cfixed = fixed
369 cerrs = errs
370 if self.fitfunc == "gauss":
371 for c in range(len(self.components)):
372 a = self.get_area(c)
373 area += [a for i in range(3)]
374 fpars = self._format_pars(cpars, cfixed, errors and cerrs, area)
375 if rcParams['verbose']:
376 print fpars
377 return {'params':cpars, 'fixed':cfixed, 'formatted': fpars,
378 'errors':cerrs}
379
380 def _format_pars(self, pars, fixed, errors, area):
381 out = ''
382 if self.fitfunc == 'poly':
383 c = 0
384 for i in range(len(pars)):
385 fix = ""
386 if len(fixed) and fixed[i]: fix = "(fixed)"
387 if errors :
388 out += ' p%d%s= %3.6f (%1.6f),' % (c,fix,pars[i], errors[i])
389 else:
390 out += ' p%d%s= %3.6f,' % (c,fix,pars[i])
391 c+=1
392 out = out[:-1] # remove trailing ','
393 elif self.fitfunc == 'gauss':
394 i = 0
395 c = 0
396 aunit = ''
397 ounit = ''
398 if self.data:
399 aunit = self.data.get_unit()
400 ounit = self.data.get_fluxunit()
401 while i < len(pars):
402 if len(area):
403 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)
404 else:
405 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)
406 c+=1
407 i+=3
408 return out
409
410 def get_estimate(self):
411 """
412 Return the parameter estimates (for non-linear functions).
413 """
414 pars = self.fitter.getestimate()
415 fixed = self.fitter.getfixedparameters()
416 if rcParams['verbose']:
417 print self._format_pars(pars,fixed,None)
418 return pars
419
420 def get_residual(self):
421 """
422 Return the residual of the fit.
423 """
424 if not self.fitted:
425 msg = "Not yet fitted."
426 if rcParams['verbose']:
427 print msg
428 return
429 else:
430 raise RuntimeError(msg)
431 return self.fitter.getresidual()
432
433 def get_chi2(self):
434 """
435 Return chi^2.
436 """
437 if not self.fitted:
438 msg = "Not yet fitted."
439 if rcParams['verbose']:
440 print msg
441 return
442 else:
443 raise RuntimeError(msg)
444 ch2 = self.fitter.getchi2()
445 if rcParams['verbose']:
446 print 'Chi^2 = %3.3f' % (ch2)
447 return ch2
448
449 def get_fit(self):
450 """
451 Return the fitted ordinate values.
452 """
453 if not self.fitted:
454 msg = "Not yet fitted."
455 if rcParams['verbose']:
456 print msg
457 return
458 else:
459 raise RuntimeError(msg)
460 return self.fitter.getfit()
461
462 @print_log_dec
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 return scan
485
486 @print_log_dec
487 def plot(self, residual=False, components=None, plotparms=False,
488 filename=None):
489 """
490 Plot the last fit.
491 Parameters:
492 residual: an optional parameter indicating if the residual
493 should be plotted (default 'False')
494 components: a list of components to plot, e.g [0,1],
495 -1 plots the total fit. Default is to only
496 plot the total fit.
497 plotparms: Inidicates if the parameter values should be present
498 on the plot
499 """
500 if not self.fitted:
501 return
502 if not self._p or self._p.is_dead:
503 if rcParams['plotter.gui']:
504 from asap.asaplotgui import asaplotgui as asaplot
505 else:
506 from asap.asaplot import asaplot
507 self._p = asaplot()
508 self._p.hold()
509 self._p.clear()
510 self._p.set_panels()
511 self._p.palette(0)
512 tlab = 'Spectrum'
513 xlab = 'Abcissa'
514 ylab = 'Ordinate'
515 from numpy import ma,logical_not,logical_and,array
516 m = self.mask
517 if self.data:
518 tlab = self.data._getsourcename(self._fittedrow)
519 xlab = self.data._getabcissalabel(self._fittedrow)
520 m = logical_and(self.mask,
521 array(self.data._getmask(self._fittedrow),
522 copy=False))
523
524 ylab = self.data._get_ordinate_label()
525
526 colours = ["#777777","#dddddd","red","orange","purple","green","magenta", "cyan"]
527 self._p.palette(0,colours)
528 self._p.set_line(label='Spectrum')
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(1)
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 @print_log_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 if rcParams['verbose']:
585 print msg
586 return
587 else:
588 raise TypeError(msg)
589 if insitu is None: insitu = rcParams['insitu']
590 if not insitu:
591 scan = self.data.copy()
592 else:
593 scan = self.data
594 rows = xrange(scan.nrow())
595 from asap import asaplog
596 asaplog.push("Fitting:")
597 for r in rows:
598 out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (scan.getscan(r),
599 scan.getbeam(r),
600 scan.getif(r),
601 scan.getpol(r),
602 scan.getcycle(r))
603 asaplog.push(out, False)
604 self.x = scan._getabcissa(r)
605 self.y = scan._getspectrum(r)
606 self.mask = mask_and(self.mask, scan._getmask(r))
607 self.data = None
608 self.fit()
609 x = self.get_parameters()
610 if plot:
611 self.plot(residual=True)
612 x = raw_input("Accept fit ([y]/n): ")
613 if x.upper() == 'N':
614 continue
615 scan._setspectrum(self.fitter.getresidual(), r)
616 if plot:
617 self._p.unmap()
618 self._p = None
619 return scan
Note: See TracBrowser for help on using the repository browser.