|
10 | 10 | except ImportError:
|
11 | 11 | HAS_NUMDIFFTOOLS = False
|
12 | 12 |
|
13 |
| -from .parameter import Parameters |
14 |
| - |
15 | 13 |
|
16 | 14 | def alphanumeric_sort(s, _nsre=re.compile('([0-9]+)')):
|
17 | 15 | """Sort alphanumeric string."""
|
@@ -111,6 +109,7 @@ def fit_report(inpars, modelpars=None, show_correl=True, min_correl=0.1,
|
111 | 109 | Multi-line text of fit report.
|
112 | 110 |
|
113 | 111 | """
|
| 112 | + from .parameter import Parameters |
114 | 113 | if isinstance(inpars, Parameters):
|
115 | 114 | result, params = None, inpars
|
116 | 115 | if hasattr(inpars, 'params'):
|
@@ -210,6 +209,105 @@ def fit_report(inpars, modelpars=None, show_correl=True, min_correl=0.1,
|
210 | 209 | return '\n'.join(buff)
|
211 | 210 |
|
212 | 211 |
|
| 212 | +def fitreport_html_table(result, show_correl=True, min_correl=0.1): |
| 213 | + """Report minimizer result as an html table""" |
| 214 | + html = [] |
| 215 | + add = html.append |
| 216 | + |
| 217 | + def stat_row(label, val, val2=''): |
| 218 | + add('<tr><td>%s</td><td>%s</td><td>%s</td></tr>' % (label, val, val2)) |
| 219 | + add('<h2>Fit Statistics</h2>') |
| 220 | + add('<table>') |
| 221 | + stat_row('fitting method', result.method) |
| 222 | + stat_row('# function evals', result.nfev) |
| 223 | + stat_row('# data points', result.ndata) |
| 224 | + stat_row('# variables', result.nvarys) |
| 225 | + stat_row('chi-square', gformat(result.chisqr)) |
| 226 | + stat_row('reduced chi-square', gformat(result.redchi)) |
| 227 | + stat_row('Akaike info crit.', gformat(result.aic)) |
| 228 | + stat_row('Bayesian info crit.', gformat(result.bic)) |
| 229 | + add('</table>') |
| 230 | + add('<h2>Variables</h2>') |
| 231 | + add(result.params._repr_html_()) |
| 232 | + if show_correl: |
| 233 | + correls = [] |
| 234 | + parnames = list(result.params.keys()) |
| 235 | + for i, name in enumerate(result.params): |
| 236 | + par = result.params[name] |
| 237 | + if not par.vary: |
| 238 | + continue |
| 239 | + if hasattr(par, 'correl') and par.correl is not None: |
| 240 | + for name2 in parnames[i+1:]: |
| 241 | + if (name != name2 and name2 in par.correl and |
| 242 | + abs(par.correl[name2]) > min_correl): |
| 243 | + correls.append((name, name2, par.correl[name2])) |
| 244 | + if len(correls) > 0: |
| 245 | + sort_correls = sorted(correls, key=lambda val: abs(val[2])) |
| 246 | + sort_correls.reverse() |
| 247 | + extra = '(unreported correlations are < %.3f)' % (min_correl) |
| 248 | + add('<h2>Correlations %s</h2>' % extra) |
| 249 | + add('<table>') |
| 250 | + for name1, name2, val in sort_correls: |
| 251 | + stat_row(name1, name2, "%.4f" % val) |
| 252 | + add('</table>') |
| 253 | + return ''.join(html) |
| 254 | + |
| 255 | + |
| 256 | +def params_html_table(params): |
| 257 | + """Returns a HTML representation of parameters data.""" |
| 258 | + has_err = any([p.stderr is not None for p in params.values()]) |
| 259 | + has_expr = any([p.expr is not None for p in params.values()]) |
| 260 | + has_brute = any([p.brute_step is not None for p in params.values()]) |
| 261 | + |
| 262 | + html = [] |
| 263 | + add = html.append |
| 264 | + |
| 265 | + def cell(x, cat='td'): |
| 266 | + return add('<%s> %s </%s>' % (cat, x, cat)) |
| 267 | + |
| 268 | + add('<table><tr>') |
| 269 | + headers = ['name', 'value'] |
| 270 | + if has_err: |
| 271 | + headers.extend(['standard error', 'relative error']) |
| 272 | + headers.extend(['initial value', 'min', 'max', 'vary']) |
| 273 | + if has_expr: |
| 274 | + headers.append('expression') |
| 275 | + if has_brute: |
| 276 | + headers.append('brute step') |
| 277 | + for h in headers: |
| 278 | + cell(h, cat='th') |
| 279 | + add('</tr>') |
| 280 | + |
| 281 | + for p in params.values(): |
| 282 | + rows = [p.name, gformat(p.value)] |
| 283 | + if has_err: |
| 284 | + serr, perr = '', '' |
| 285 | + if p.stderr is not None: |
| 286 | + serr = gformat(p.stderr) |
| 287 | + perr = '%.2f%%' % (100 * p.stderr / p.value) |
| 288 | + rows.extend([serr, perr]) |
| 289 | + rows.extend((p.init_value, gformat(p.min), gformat(p.max), |
| 290 | + '%s' % p.vary)) |
| 291 | + if has_expr: |
| 292 | + expr = '' |
| 293 | + if p.expr is not None: |
| 294 | + expr = p.expr |
| 295 | + rows.append(expr) |
| 296 | + |
| 297 | + if has_brute: |
| 298 | + brute_step = 'None' |
| 299 | + if p.brute_step is not None: |
| 300 | + brute_step = gformat(p.brute_step) |
| 301 | + rows.append(brute_step) |
| 302 | + |
| 303 | + add('<tr>') |
| 304 | + for r in rows: |
| 305 | + cell(r) |
| 306 | + add('</tr>') |
| 307 | + add('</table>') |
| 308 | + return ''.join(html) |
| 309 | + |
| 310 | + |
213 | 311 | def report_errors(params, **kws):
|
214 | 312 | """Print a report for fitted params: see error_report()."""
|
215 | 313 | print(fit_report(params, **kws))
|
|
0 commit comments