| 1 | from _asap import fitentry | 
|---|
| 2 | from asap import rcParams | 
|---|
| 3 |  | 
|---|
| 4 | class asapfit(fitentry): | 
|---|
| 5 |  | 
|---|
| 6 | def __init__(self, other=None): | 
|---|
| 7 | if isinstance(other, fitentry): | 
|---|
| 8 | fitentry.__init__(self,other) | 
|---|
| 9 | else: | 
|---|
| 10 | fitentry.__init__(self) | 
|---|
| 11 |  | 
|---|
| 12 | def __str__(self): | 
|---|
| 13 | out = "" | 
|---|
| 14 | out += "Fit:" | 
|---|
| 15 | pars = self.getparameters() | 
|---|
| 16 | mask = self.getfixedparameters() | 
|---|
| 17 | funcs = self.getfunctions() | 
|---|
| 18 | comps = self.getcomponents() | 
|---|
| 19 | finfo = self.getframeinfo() | 
|---|
| 20 | pos=0 | 
|---|
| 21 | k = 0 | 
|---|
| 22 | for f in funcs: | 
|---|
| 23 | out += "\n Type:         " | 
|---|
| 24 | out += f | 
|---|
| 25 | s = pos | 
|---|
| 26 | pos += comps[k] | 
|---|
| 27 | ps = pars[s:pos] | 
|---|
| 28 | out += "\n  Parameters:  " | 
|---|
| 29 | out += self._format_pars(pars[s:pos],f, finfo[0]) | 
|---|
| 30 | out += "\n  Fixed Parms: " | 
|---|
| 31 | out += str(mask[s:pos]) | 
|---|
| 32 | out += "\n  Frame:       " | 
|---|
| 33 | out += str(finfo) | 
|---|
| 34 | out += "\n" | 
|---|
| 35 | out += "\n" | 
|---|
| 36 | return out | 
|---|
| 37 |  | 
|---|
| 38 | def as_dict(self): | 
|---|
| 39 | out = [] | 
|---|
| 40 | pars = self.getparameters() | 
|---|
| 41 | mask = self.getfixedparameters() | 
|---|
| 42 | funcs = self.getfunctions() | 
|---|
| 43 | comps = self.getcomponents() | 
|---|
| 44 | pos=0 | 
|---|
| 45 | k=0 | 
|---|
| 46 | comp = [] | 
|---|
| 47 | for f in funcs: | 
|---|
| 48 | s = pos | 
|---|
| 49 | pos += comps[k] | 
|---|
| 50 | ps = pars[s:pos] | 
|---|
| 51 | d = {'function' : f, | 
|---|
| 52 | 'parameters' : pars[s:pos], | 
|---|
| 53 | 'fixed' : mask[s:pos], | 
|---|
| 54 | 'frame' : self.getframeinfo() | 
|---|
| 55 | } | 
|---|
| 56 | comp.append(d) | 
|---|
| 57 | k+=1 | 
|---|
| 58 | out.append(comp) | 
|---|
| 59 | return out | 
|---|
| 60 |  | 
|---|
| 61 | def save(self, filename): | 
|---|
| 62 | f = file(filename, 'w') | 
|---|
| 63 | parstr = "" | 
|---|
| 64 | for i in xrange(self.getcomponents()[0]): | 
|---|
| 65 | pname = "P%d"% (i) | 
|---|
| 66 | parstr += "%-12s " % (pname) | 
|---|
| 67 | header = "#%-12s %s\n" % ("Function", parstr) | 
|---|
| 68 | f.write(header) | 
|---|
| 69 | i = 0 | 
|---|
| 70 | funcs=self.getfunctions() | 
|---|
| 71 | pars=self.getparameters() | 
|---|
| 72 | for c in self.getcomponents(): | 
|---|
| 73 | line = " %-12s " % (funcs[i]) | 
|---|
| 74 | for j in xrange(c): | 
|---|
| 75 | line += "%3.8f " % (pars[i*c+j]) | 
|---|
| 76 | i+=1 | 
|---|
| 77 | f.write(line+"\n") | 
|---|
| 78 | f.close() | 
|---|
| 79 |  | 
|---|
| 80 | def _format_pars(self, pars, ftype, unit): | 
|---|
| 81 | out = '' | 
|---|
| 82 | if ftype == 'poly': | 
|---|
| 83 | i = 0 | 
|---|
| 84 | for p in pars: | 
|---|
| 85 | out += ' p%d = %3.6f %s,' % (i,p,unit) | 
|---|
| 86 | i+=1 | 
|---|
| 87 | out = out[1:-1] | 
|---|
| 88 | elif ftype == 'gauss': | 
|---|
| 89 | out += 'peak = %3.6f , centre = %3.6f %s, FWHM = %3.6f %s' % (pars[0],pars[1],unit,pars[2],unit) | 
|---|
| 90 | return out | 
|---|