File size: 11,926 Bytes
90c137b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | #!/usr/bin/env python3
"""
Render a static, single-file HTML report comparing the SHIPPED heart model
against one retraining CANDIDATE — for a room screen, not a dashboard.
python scripts/render_candidate_report.py <candidate_dir> [--out reports/x.html]
Reads only `candidate_card.json` from the given candidate directory (never the
pkl, never the merged training CSV — no patient row is read or shown). Writes
one self-contained HTML file: no external CSS/JS, no network calls, so it opens
identically offline and on a projector.
Nothing here writes to the shipped bundle, `reference_scores.json`, or MLflow.
This is a read-only view of a `candidate_card.json` that a retraining run
already produced.
"""
from __future__ import annotations
import argparse
import html
import json
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
DECISIONS = ("referral", "no_referral", "uncertain")
DECISION_LABEL = {
"referral": "Referral",
"no_referral": "No referral",
"uncertain": "Uncertain",
}
CELLS = ("F0", "F1", "M0", "M1")
CELL_LABEL = {
"F0": "Female, no disease",
"F1": "Female, disease",
"M0": "Male, no disease",
"M1": "Male, disease",
}
def esc(x) -> str:
return html.escape(str(x))
def pct(x: float) -> str:
return f"{x * 100:.2f}%"
def render_transition_table(matrix: dict) -> str:
counts = matrix["counts"]
rows = []
for before in DECISIONS:
cells = []
for after in DECISIONS:
n = counts[f"{before}__to__{after}"]
cls = "diag" if before == after else ("moved" if n else "zero")
cells.append(f'<td class="num {cls}">{n}</td>')
rows.append(
f'<tr><th class="rowhead">{DECISION_LABEL[before]}</th>{"".join(cells)}</tr>'
)
header = "".join(f"<th>{DECISION_LABEL[d]}</th>" for d in DECISIONS)
return f"""
<table class="matrix">
<caption>Decision transition — shipped model (rows) → candidate (columns), n = {matrix['n']}</caption>
<thead><tr><th class="corner">shipped ↓ / candidate →</th>{header}</tr></thead>
<tbody>{"".join(rows)}</tbody>
</table>
"""
def render_coverage_table(coverage: dict) -> str:
target = coverage["declared_target"]
def row(label: str, kind_key: str, badge: str, badge_cls: str) -> str:
c = coverage[kind_key]
cells = "".join(
f'<td class="num">{pct(c[cell])}<span class="n">n={c[f"n_{cell}"]}</span></td>'
for cell in CELLS
)
below = " below-target" if c["marginal"] < target else ""
return (
f'<tr><th class="rowhead">{label} '
f'<span class="badge {badge_cls}">{badge}</span></th>'
f'<td class="num marginal{below}">{pct(c["marginal"])}</td>{cells}</tr>'
)
header = "".join(f"<th>{CELL_LABEL[c]}</th>" for c in CELLS)
return f"""
<table class="coverage">
<caption>Conformal coverage vs. declared target 1−α = {pct(target)}
— <span class="badge by-construction">by construction</span> = measured on the same
rows the quantile was taken from (an identity check, not an estimate);
<span class="badge cross-conformal">cross-conformal</span> = measured on rows whose
Mondrian cell was computed WITHOUT them (5-fold)</caption>
<thead><tr><th class="corner"></th><th>Marginal</th>{header}</tr></thead>
<tbody>
{row("Shipped", "shipped_in_sample", "by construction", "by-construction")}
{row("Shipped", "shipped_cross_conformal", "cross-conformal", "cross-conformal")}
{row("Candidate", "candidate_in_sample", "by construction", "by-construction")}
{row("Candidate", "candidate_cross_conformal", "cross-conformal", "cross-conformal")}
</tbody>
</table>
"""
def render_provenance_table(card: dict) -> str:
def sha_row(label: str, value: str) -> str:
# label is a static string we author, already valid HTML (may carry an
# entity like —); only the value, which comes off the card, is
# escaped. Escaping the label too turned "—" into the literal
# text "—" on the page instead of an em dash.
return f'<tr><th class="rowhead">{label}</th><td class="mono">{esc(value)}</td></tr>'
def val_row(label: str, value) -> str:
return f'<tr><th class="rowhead">{label}</th><td>{esc(value)}</td></tr>'
return f"""
<table class="provenance">
<caption>Identifiers, row counts, and sha256 hashes for this run</caption>
<tbody>
{val_row("MLflow run id", card["mlflow_run_id"])}
{val_row("Created at (UTC)", card["created_at"])}
{val_row("Config version", card["config_version"])}
{val_row("Conformal alpha", card["conformal_alpha"])}
{val_row("Rows — base UCI / from review queue / rejected / total",
f'{card["rows_base_uci"]} / {card["rows_from_review_queue"]} / '
f'{card["rows_rejected"]} / {card["rows_total"]}')}
{sha_row("Candidate bundle sha256", card["bundle_sha256"])}
{sha_row("Merged training data sha256", card["training_data_sha256"])}
{sha_row("Base training CSV sha256", card["base_training_csv_sha256"])}
{sha_row("Shipped bundle sha256 (unchanged by this run)", card["shipped_bundle_sha256"])}
{sha_row("reference_scores.json sha256 (unchanged by this run)", card["reference_scores_sha256"])}
</tbody>
</table>
"""
TEMPLATE = """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Heart candidate vs. shipped — {run_id}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
:root {{
--bg: #0b0f14; --panel: #121822; --border: #263041; --text: #eef2f7;
--muted: #9fb0c3; --accent: #4fb0ff; --good: #35c07a; --warn: #f0a63a;
--diag: #16324d; --moved: #5a2a1f; --zero: #0f1520;
}}
* {{ box-sizing: border-box; }}
body {{
background: var(--bg); color: var(--text);
font-family: "Segoe UI", -apple-system, Helvetica, Arial, sans-serif;
font-size: 22px; line-height: 1.45; margin: 0; padding: 48px 64px 96px;
}}
h1 {{ font-size: 40px; margin: 0 0 6px; }}
h2 {{ font-size: 28px; margin: 56px 0 18px; border-bottom: 2px solid var(--border); padding-bottom: 10px; }}
.subtitle {{ color: var(--muted); font-size: 24px; margin: 0 0 8px; }}
.badges {{ margin: 20px 0 0; display: flex; gap: 14px; flex-wrap: wrap; }}
.pill {{
display: inline-block; padding: 8px 18px; border-radius: 999px;
font-size: 20px; font-weight: 600; border: 2px solid var(--border);
}}
.pill.candidate {{ background: #12240f; border-color: var(--good); color: var(--good); }}
.pill.not-promoted {{ background: #241512; border-color: var(--warn); color: var(--warn); }}
.pill.readonly {{ background: #0f1a24; border-color: var(--accent); color: var(--accent); }}
table {{ border-collapse: collapse; width: 100%; margin: 0 0 12px; }}
caption {{
text-align: left; caption-side: top; color: var(--muted);
font-size: 20px; padding: 0 0 14px; font-weight: 500;
}}
th, td {{ border: 1px solid var(--border); padding: 12px 16px; text-align: center; }}
th.rowhead, th.corner {{ text-align: left; background: var(--panel); font-weight: 600; }}
thead th {{ background: var(--panel); font-weight: 700; }}
td.num {{ font-variant-numeric: tabular-nums; font-size: 24px; }}
td.mono {{ font-family: "JetBrains Mono", Consolas, monospace; font-size: 16px; text-align: left; word-break: break-all; color: var(--muted); }}
.matrix td.diag {{ background: var(--diag); font-weight: 700; }}
.matrix td.moved {{ background: var(--moved); font-weight: 700; color: #ffb199; }}
.matrix td.zero {{ color: #4a5568; }}
.coverage td.marginal {{ font-weight: 700; }}
.coverage td.below-target {{ color: var(--warn); }}
.coverage .n {{ display: block; font-size: 14px; color: var(--muted); font-weight: 400; }}
.badge {{ font-size: 14px; padding: 3px 10px; border-radius: 6px; font-weight: 600; margin-left: 8px; }}
.badge.by-construction {{ background: #241a0c; color: var(--warn); border: 1px solid var(--warn); }}
.badge.cross-conformal {{ background: #0c2418; color: var(--good); border: 1px solid var(--good); }}
.movement {{ display: flex; gap: 24px; margin: 18px 0 40px; flex-wrap: wrap; }}
.stat {{ background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 20px 28px; min-width: 220px; }}
.stat .n {{ font-size: 44px; font-weight: 800; color: var(--accent); }}
.stat .label {{ color: var(--muted); font-size: 18px; margin-top: 4px; }}
.limit {{
background: #1a1206; border: 2px solid var(--warn); border-radius: 12px;
padding: 24px 28px; font-size: 19px; line-height: 1.6; color: #f4d9ab;
}}
.limit strong {{ color: var(--warn); }}
footer {{ margin-top: 64px; color: var(--muted); font-size: 15px; border-top: 1px solid var(--border); padding-top: 20px; }}
</style>
</head>
<body>
<h1>Heart CAD retraining — candidate vs. shipped</h1>
<p class="subtitle">Spline-GLM + IVAP + Mondrian conformal · comparison over the 920-patient UCI cohort</p>
<div class="badges">
<span class="pill candidate">CANDIDATE, NOT a replacement</span>
<span class="pill not-promoted">promoted: no</span>
<span class="pill readonly">shipped bundle & reference_scores.json: read-only, unchanged</span>
</div>
<h2>Decision movement, shipped → candidate</h2>
<div class="movement">
<div class="stat"><div class="n">{decisions_changed}</div><div class="label">decisions changed / {n_total}</div></div>
<div class="stat"><div class="n">{moved_out}</div><div class="label">moved OUT of uncertain<br>(candidate committed)</div></div>
<div class="stat"><div class="n">{moved_in}</div><div class="label">moved INTO uncertain<br>(candidate withdrew)</div></div>
</div>
{transition_table}
<h2>Conformal coverage</h2>
{coverage_table}
<h2>Provenance</h2>
{provenance_table}
<h2>Scientific limit</h2>
<div class="limit"><strong>Not a validated model — evidence for a human decision.</strong><br><br>{limit_text}</div>
<footer>
Generated {generated_at} by <code>scripts/render_candidate_report.py</code> from
<code>{card_path}</code> only. No patient row, no pkl, and no training CSV was read to build this page.
</footer>
</body>
</html>
"""
def render(card: dict, card_path: Path) -> str:
import datetime
matrix = card["transition_matrix"]
return TEMPLATE.format(
run_id=esc(card["mlflow_run_id"]),
decisions_changed=matrix["decisions_changed_total"],
n_total=matrix["n"],
moved_out=matrix["moved_out_of_uncertain"],
moved_in=matrix["moved_into_uncertain"],
transition_table=render_transition_table(matrix),
coverage_table=render_coverage_table(card["conformal_coverage"]),
provenance_table=render_provenance_table(card),
limit_text=esc(card["limit"]),
generated_at=datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M UTC"),
card_path=esc(card_path.relative_to(PROJECT_ROOT)),
)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("candidate_dir", type=Path, help="models/heart_disease/candidates/<run_id>/")
parser.add_argument("--out", type=Path, default=None)
args = parser.parse_args()
card_path = (args.candidate_dir / "candidate_card.json").resolve()
card = json.loads(card_path.read_text())
out = args.out or (PROJECT_ROOT / "reports" / f"heart_candidate_{card['mlflow_run_id'][:12]}.html")
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(render(card, card_path))
print(f"wrote {out}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|