source: trunk/python/asapfitter.py@ 1027

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

Fix for ticket #21 - get_parameters/set_parameters inconsistent. Reworked the interface to allow this.

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