1
2 """
3 Created on Fri Apr 20 19:24:21 2012
4
5 @author: Tillsten
6
7 Changes:
8 - 13-Feb-2013 M Newville
9 complemented "report_errors" and "report_ci" with
10 "error_report" and "ci_report" (respectively) which
11 return the text of the report. Thus report_errors()
12 is simply:
13 def report_errors(params, modelpars=None, show_correl=True):
14 print error_report(params, modelpars=modelpars,
15 show_correl=show_correl)
16 and similar for report_ci() / ci_report()
17
18 """
19
20 from __future__ import print_function
21
22
23 -def fit_report(params, modelpars=None, show_correl=True, min_correl=0.1):
24 """return text of a report for fitted params best-fit values,
25 uncertainties and correlations
26
27 arguments
28 ----------
29 params Parameters from fit
30 modelpars Optional Known Model Parameters [None]
31 show_correl whether to show list of sorted correlations [True]
32 min_correl smallest correlation absolute value to show [0.1]
33
34 """
35 parnames = sorted(params)
36 buff = []
37 add = buff.append
38 namelen = max([len(n) for n in parnames])
39 add("[[Variables]]")
40 for name in parnames:
41 par = params[name]
42 space = ' '*(namelen+2 - len(name))
43 nout = " %s: %s" % (name, space)
44 initval = 'inital = ?'
45 if par.init_value is not None:
46 initval = 'initial = % .6f' % par.init_value
47 if modelpars is not None and name in modelpars:
48 initval = '%s, model_value =% .6f' % (initval, modelpars[name].value)
49
50 try:
51 sval = '% .6f' % par.value
52 except (TypeError, ValueError):
53 sval = 'Non Numeric Value?'
54
55 if par.stderr is not None:
56 sval = '% .6f +/- %.6f' % (par.value, par.stderr)
57 try:
58 sval = '%s (%.2f%%)' % (sval, abs(par.stderr/par.value)*100)
59 except ZeroDivisionError:
60 pass
61
62 if par.vary:
63 add(" %s %s %s" % (nout, sval, initval))
64 elif par.expr is not None:
65 add(" %s %s == '%s'" % (nout, sval, par.expr))
66 else:
67 add(" %s fixed" % (nout))
68
69 if show_correl:
70 add('[[Correlations]] (unreported correlations are < % .3f)' % min_correl)
71 correls = {}
72 for i, name in enumerate(parnames):
73 par = params[name]
74 if not par.vary:
75 continue
76 if hasattr(par, 'correl') and par.correl is not None:
77 for name2 in parnames[i+1:]:
78 if name != name2 and name2 in par.correl:
79 correls["%s, %s" % (name, name2)] = par.correl[name2]
80
81 sort_correl = sorted(correls.items(), key=lambda it: abs(it[1]))
82 sort_correl.reverse()
83 for name, val in sort_correl:
84 if abs(val) < min_correl:
85 break
86 lspace = max(1, 25 - len(name))
87 add(' C(%s)%s = % .3f ' % (name, (' '*30)[:lspace], val))
88 return '\n'.join(buff)
89
91 """print a report for fitted params: see error_report()"""
92 print(fit_report(params, **kws))
93
95 """print a report for fitted params: see error_report()"""
96 print(fit_report(params, **kws))
97
99 """return text of a report for confidence intervals"""
100 maxlen = max([len(i) for i in ci])
101 buff = []
102 add = buff.append
103 convp = lambda x: ("%.2f" % (x[0]*100))+'%'
104 conv = lambda x: "%.5f" % x[1]
105 title_shown = False
106 for name, row in ci.items():
107 if not title_shown:
108 add("".join([''.rjust(maxlen)]+[i.rjust(10) for i in map(convp, row)]))
109 title_shown = True
110 add("".join([name.rjust(maxlen)]+[i.rjust(10) for i in map(conv, row)]))
111 return '\n'.join(buff)
112
114 """print a report for confidence intervals"""
115 print(ci_report(ci))
116