|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +from dataclasses import dataclass |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Dict, List, Optional, Tuple |
| 10 | + |
| 11 | + |
| 12 | +def _repo_root() -> Path: |
| 13 | + return Path(__file__).resolve().parents[1] |
| 14 | + |
| 15 | + |
| 16 | +def _read_json(path: Path) -> Any: |
| 17 | + return json.loads(path.read_text(encoding="utf-8", errors="ignore")) |
| 18 | + |
| 19 | + |
| 20 | +def _find_first_png(case_dir: Path) -> Optional[Path]: |
| 21 | + pngs = sorted(case_dir.glob("*.png")) |
| 22 | + return pngs[0] if pngs else None |
| 23 | + |
| 24 | + |
| 25 | +def _fmt_num(x: Optional[float]) -> str: |
| 26 | + if x is None: |
| 27 | + return "" |
| 28 | + return f"{x:.3f}" |
| 29 | + |
| 30 | + |
| 31 | +def _fmt_runtime(x: Optional[float]) -> str: |
| 32 | + if x is None: |
| 33 | + return "" |
| 34 | + # Keep short while still readable. |
| 35 | + if x >= 1000.0: |
| 36 | + return f"{x:.0f}" |
| 37 | + return f"{x:.3f}" |
| 38 | + |
| 39 | + |
| 40 | +@dataclass(frozen=True) |
| 41 | +class Row: |
| 42 | + test_id: str |
| 43 | + purpose: str |
| 44 | + case_id: str |
| 45 | + bullet: int |
| 46 | + |
| 47 | + |
| 48 | +def _case_dir(root: Path, bullet: int, case_id: str) -> Path: |
| 49 | + return root / f"bullet-point-{bullet}" / case_id |
| 50 | + |
| 51 | + |
| 52 | +def _load_openroad_metrics(case_dir: Path) -> Tuple[Optional[float], Optional[float]]: |
| 53 | + metrics_path = case_dir / "metrics.json" |
| 54 | + if not metrics_path.exists(): |
| 55 | + return None, None |
| 56 | + data = _read_json(metrics_path) |
| 57 | + if isinstance(data, list) and data: |
| 58 | + m = data[0] |
| 59 | + if isinstance(m, dict): |
| 60 | + return ( |
| 61 | + float(m["total_um"]) if m.get("total_um") is not None else None, |
| 62 | + float(m["openroad_runtime_s"]) |
| 63 | + if m.get("openroad_runtime_s") is not None |
| 64 | + else None, |
| 65 | + ) |
| 66 | + return None, None |
| 67 | + |
| 68 | + |
| 69 | +def _load_ortools_metrics(case_dir: Path) -> Tuple[Optional[float], Optional[float]]: |
| 70 | + metrics_path = case_dir / "metrics.json" |
| 71 | + if not metrics_path.exists(): |
| 72 | + return None, None |
| 73 | + data = _read_json(metrics_path) |
| 74 | + if isinstance(data, dict) and "total_cost_um" in data: |
| 75 | + return ( |
| 76 | + float(data["total_cost_um"]) if data.get("total_cost_um") is not None else None, |
| 77 | + float(data["wall_s"]) if data.get("wall_s") is not None else None, |
| 78 | + ) |
| 79 | + return None, None |
| 80 | + |
| 81 | + |
| 82 | +def _load_status_wall(case_dir: Path) -> Optional[float]: |
| 83 | + status_path = case_dir / "status.json" |
| 84 | + if not status_path.exists(): |
| 85 | + return None |
| 86 | + data = _read_json(status_path) |
| 87 | + if isinstance(data, dict) and data.get("wall_s") is not None: |
| 88 | + return float(data["wall_s"]) |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +def _format_link(root: Path, path: Path, label: str) -> str: |
| 93 | + rel = path.resolve().relative_to(root.resolve()) |
| 94 | + return f"[{label}]({rel.as_posix()})" |
| 95 | + |
| 96 | + |
| 97 | +def main(argv: Optional[List[str]] = None) -> int: |
| 98 | + ap = argparse.ArgumentParser( |
| 99 | + description="Fill the AMUR TABLE for bullet-point-3..7 runs (writes markdown)." |
| 100 | + ) |
| 101 | + ap.add_argument( |
| 102 | + "--out", |
| 103 | + type=Path, |
| 104 | + default=Path("amur_table_filled.md"), |
| 105 | + help="Markdown output path (repo-relative by default).", |
| 106 | + ) |
| 107 | + args = ap.parse_args(argv) |
| 108 | + |
| 109 | + root = _repo_root() |
| 110 | + out_path = args.out |
| 111 | + if not out_path.is_absolute(): |
| 112 | + out_path = (root / out_path).resolve() |
| 113 | + |
| 114 | + # Google-doc table rows, plus an explicit 5c_mid/5c_strict appendix. |
| 115 | + rows: List[Row] = [ |
| 116 | + Row("3a", "Optimizers", "3a", 3), |
| 117 | + Row("3b", "Optimizers", "3b", 3), |
| 118 | + Row("3c", "Optimizers", "3c", 3), |
| 119 | + Row("3d", "Optimizers", "3d", 3), |
| 120 | + Row("4a", "Ports/chains", "4a", 4), |
| 121 | + Row("4b", "Ports/chains", "4b", 4), |
| 122 | + Row("4c", "Ports/chains", "4c", 4), |
| 123 | + Row("4d", "Ports/chains", "4d", 4), |
| 124 | + # 5c in the doc is the "should error?" case; by default map to strict. |
| 125 | + Row("5c", "Polarity", "5c_strict", 5), |
| 126 | + Row("5d", "Polarity", "5d", 5), |
| 127 | + Row("5e", "Polarity", "5e", 5), |
| 128 | + Row("6c", "Clocks", "6c", 6), |
| 129 | + Row("6d", "Clocks", "6d", 6), |
| 130 | + Row("6e", "Clocks", "6e", 6), |
| 131 | + Row("6f", "Clocks", "6f", 6), |
| 132 | + Row("6g", "Clocks", "6g", 6), |
| 133 | + Row("6h", "Clocks", "6h", 6), |
| 134 | + Row("7c", "Grouping", "7c", 7), |
| 135 | + Row("7d", "Grouping", "7d", 7), |
| 136 | + Row("7e", "Grouping", "7e", 7), |
| 137 | + ] |
| 138 | + |
| 139 | + lines: List[str] = [] |
| 140 | + lines.append("| Test ID | Purpose | Total chain length (um) | Runtime (sec) | Link to visualization |") |
| 141 | + lines.append("| ------: | ------------ | ----------------------- | ------------- | --------------------- |") |
| 142 | + |
| 143 | + for row in rows: |
| 144 | + case_dir = _case_dir(root, row.bullet, row.case_id) |
| 145 | + total_um: Optional[float] = None |
| 146 | + runtime_s: Optional[float] = None |
| 147 | + link: str = "" |
| 148 | + |
| 149 | + if row.case_id in {"3c", "3d"}: |
| 150 | + total_um, runtime_s = _load_ortools_metrics(case_dir) |
| 151 | + png = _find_first_png(case_dir) |
| 152 | + if png is not None: |
| 153 | + link = _format_link(root, png, "plot") |
| 154 | + else: |
| 155 | + total_um, runtime_s = _load_openroad_metrics(case_dir) |
| 156 | + if runtime_s is None: |
| 157 | + runtime_s = _load_status_wall(case_dir) |
| 158 | + png = _find_first_png(case_dir) |
| 159 | + if png is not None: |
| 160 | + link = _format_link(root, png, "plot") |
| 161 | + else: |
| 162 | + stdout_log = case_dir / "stdout.log" |
| 163 | + if stdout_log.exists(): |
| 164 | + link = _format_link(root, stdout_log, "log") |
| 165 | + |
| 166 | + lines.append( |
| 167 | + f"| {row.test_id} | {row.purpose} | {_fmt_num(total_um)} | {_fmt_runtime(runtime_s)} | {link} |" |
| 168 | + ) |
| 169 | + |
| 170 | + # Appendix: explicitly show the polarity-mode delta for 5c. |
| 171 | + lines.append("") |
| 172 | + lines.append("## Polarity mode delta (5c)") |
| 173 | + lines.append( |
| 174 | + "This section is not part of the original AMUR TABLE, but shows how `mid` vs `strict` affects the `5c` expectation." |
| 175 | + ) |
| 176 | + lines.append("") |
| 177 | + lines.append("| Case | Polarity mode | Result | Total chain length (um) | Runtime (sec) | Link |") |
| 178 | + lines.append("| ---- | ------------- | ------ | ----------------------- | ------------- | ---- |") |
| 179 | + |
| 180 | + for case_id, mode in [("5c_mid", "mid"), ("5c_strict", "strict")]: |
| 181 | + case_dir = _case_dir(root, 5, case_id) |
| 182 | + total_um, runtime_s = _load_openroad_metrics(case_dir) |
| 183 | + if runtime_s is None: |
| 184 | + runtime_s = _load_status_wall(case_dir) |
| 185 | + png = _find_first_png(case_dir) |
| 186 | + link = "" |
| 187 | + if png is not None: |
| 188 | + link = _format_link(root, png, "plot") |
| 189 | + else: |
| 190 | + stdout_log = case_dir / "stdout.log" |
| 191 | + if stdout_log.exists(): |
| 192 | + link = _format_link(root, stdout_log, "log") |
| 193 | + result = "OK" if (case_dir / "metrics.json").exists() else "ERROR" |
| 194 | + lines.append( |
| 195 | + f"| {case_id} | {mode} | {result} | {_fmt_num(total_um)} | {_fmt_runtime(runtime_s)} | {link} |" |
| 196 | + ) |
| 197 | + |
| 198 | + out_path.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 199 | + print(f"WROTE: {out_path}") |
| 200 | + return 0 |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + raise SystemExit(main()) |
| 205 | + |
0 commit comments