[974] | 1 | from _asap import fitentry
|
---|
[1826] | 2 | from asap.parameters import rcParams
|
---|
[529] | 3 |
|
---|
[974] | 4 | class asapfit(fitentry):
|
---|
[722] | 5 |
|
---|
[974] | 6 | def __init__(self, other=None):
|
---|
| 7 | if isinstance(other, fitentry):
|
---|
| 8 | fitentry.__init__(self,other)
|
---|
| 9 | else:
|
---|
| 10 | fitentry.__init__(self)
|
---|
[529] | 11 |
|
---|
| 12 | def __str__(self):
|
---|
| 13 | out = ""
|
---|
[974] | 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)
|
---|
[529] | 34 | out += "\n"
|
---|
[974] | 35 | out += "\n"
|
---|
[529] | 36 | return out
|
---|
| 37 |
|
---|
| 38 | def as_dict(self):
|
---|
| 39 | out = []
|
---|
[974] | 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)
|
---|
[1232] | 57 | k+=1
|
---|
[974] | 58 | out.append(comp)
|
---|
[529] | 59 | return out
|
---|
| 60 |
|
---|
[1232] | 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 |
|
---|
[529] | 80 | def _format_pars(self, pars, ftype, unit):
|
---|
| 81 | out = ''
|
---|
| 82 | if ftype == 'poly':
|
---|
| 83 | i = 0
|
---|
| 84 | for p in pars:
|
---|
[1232] | 85 | out += ' p%d = %3.6f %s,' % (i,p,unit)
|
---|
[529] | 86 | i+=1
|
---|
| 87 | out = out[1:-1]
|
---|
[722] | 88 | elif ftype == 'gauss':
|
---|
[1232] | 89 | out += 'peak = %3.6f , centre = %3.6f %s, FWHM = %3.6f %s' % (pars[0],pars[1],unit,pars[2],unit)
|
---|
[529] | 90 | return out
|
---|