Spaces:
Runtime error
Runtime error
Update Down/down/exp/plot_paradox_results.py
Browse files
Down/down/exp/plot_paradox_results.py
CHANGED
|
@@ -1,15 +1,23 @@
|
|
| 1 |
-
"""
|
|
|
|
| 2 |
|
| 3 |
-
Creates PNG plots under down/graphics/.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
|
|
|
| 8 |
import csv
|
| 9 |
import os
|
| 10 |
from pathlib import Path
|
| 11 |
from typing import Dict, List
|
| 12 |
|
|
|
|
|
|
|
| 13 |
import matplotlib.pyplot as plt
|
| 14 |
|
| 15 |
|
|
@@ -18,13 +26,51 @@ def _load_csv(path: Path) -> List[Dict[str, str]]:
|
|
| 18 |
return list(csv.DictReader(f))
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def main() -> None:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
graphics_dir.mkdir(parents=True, exist_ok=True)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
if not results_csv.exists():
|
| 30 |
raise FileNotFoundError(f"Missing results CSV: {results_csv}")
|
|
@@ -33,46 +79,41 @@ def main() -> None:
|
|
| 33 |
if not rows:
|
| 34 |
raise RuntimeError(f"CSV is empty: {results_csv}")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
def to_float(x: str) -> float:
|
| 38 |
-
try:
|
| 39 |
-
return float(x)
|
| 40 |
-
except Exception:
|
| 41 |
-
return float("nan")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
agents = sorted({r.get("agent", "unknown") for r in rows})
|
| 45 |
-
xs_all = [int(r.get("global_episode", r.get("episode", 0)) or 0) for r in rows]
|
| 46 |
|
| 47 |
-
#
|
| 48 |
plt.figure()
|
| 49 |
for a in agents:
|
| 50 |
xs = [int(r.get("global_episode", r.get("episode", 0)) or 0) for r in rows if r.get("agent") == a]
|
| 51 |
-
ys = [
|
| 52 |
if xs:
|
| 53 |
plt.plot(xs, ys, label=a)
|
| 54 |
plt.xlabel("Global episode")
|
| 55 |
plt.ylabel("Episode reward")
|
| 56 |
-
plt.title("
|
| 57 |
plt.legend()
|
| 58 |
-
out1 = graphics_dir / "
|
| 59 |
-
plt.savefig(out1, dpi=
|
| 60 |
plt.close()
|
| 61 |
|
| 62 |
# Rho plot (if present)
|
| 63 |
-
|
|
|
|
| 64 |
plt.figure()
|
| 65 |
for a in agents:
|
| 66 |
xs = [int(r.get("global_episode", r.get("episode", 0)) or 0) for r in rows if r.get("agent") == a]
|
| 67 |
-
ys = [
|
| 68 |
if xs:
|
| 69 |
plt.plot(xs, ys, label=a)
|
| 70 |
plt.xlabel("Global episode")
|
| 71 |
plt.ylabel("rho")
|
| 72 |
-
plt.title("
|
| 73 |
plt.legend()
|
| 74 |
-
out2 = graphics_dir / "
|
| 75 |
-
plt.savefig(out2, dpi=
|
| 76 |
plt.close()
|
| 77 |
|
| 78 |
print(f"Saved plots to {graphics_dir}")
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Plot results produced by exp/run_paradox_tabular.py.
|
| 3 |
|
| 4 |
+
Creates PNG plots under down/graphics/ by default.
|
| 5 |
+
Now supports:
|
| 6 |
+
--csv path to results csv
|
| 7 |
+
--out output directory for pngs
|
| 8 |
+
--prefix filename prefix for plots
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
+
import argparse
|
| 14 |
import csv
|
| 15 |
import os
|
| 16 |
from pathlib import Path
|
| 17 |
from typing import Dict, List
|
| 18 |
|
| 19 |
+
import matplotlib
|
| 20 |
+
matplotlib.use("Agg")
|
| 21 |
import matplotlib.pyplot as plt
|
| 22 |
|
| 23 |
|
|
|
|
| 26 |
return list(csv.DictReader(f))
|
| 27 |
|
| 28 |
|
| 29 |
+
def _to_float(x: str) -> float:
|
| 30 |
+
try:
|
| 31 |
+
return float(x)
|
| 32 |
+
except Exception:
|
| 33 |
+
return float("nan")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _build_parser() -> argparse.ArgumentParser:
|
| 37 |
+
p = argparse.ArgumentParser(description="Plot DOWN paradox tabular results.")
|
| 38 |
+
p.add_argument(
|
| 39 |
+
"--csv",
|
| 40 |
+
type=str,
|
| 41 |
+
default="",
|
| 42 |
+
help="Path to results CSV. If omitted, uses TWOQUARKS_RESULTS_DIR/down_paradox_tabular_results.csv or down/results/...",
|
| 43 |
+
)
|
| 44 |
+
p.add_argument(
|
| 45 |
+
"--out",
|
| 46 |
+
type=str,
|
| 47 |
+
default="",
|
| 48 |
+
help="Output directory for PNGs. If omitted, uses TWOQUARKS_GRAPHICS_DIR or down/graphics.",
|
| 49 |
+
)
|
| 50 |
+
p.add_argument("--prefix", type=str, default="Down", help="Prefix for output filenames.")
|
| 51 |
+
return p
|
| 52 |
+
|
| 53 |
+
|
| 54 |
def main() -> None:
|
| 55 |
+
args = _build_parser().parse_args()
|
| 56 |
+
|
| 57 |
+
quark_dir = Path(__file__).resolve().parents[1] # .../down
|
| 58 |
+
|
| 59 |
+
# Default dirs (env overrides)
|
| 60 |
+
results_dir = Path(os.environ.get("TWOQUARKS_RESULTS_DIR", (quark_dir / "results").as_posix()))
|
| 61 |
+
graphics_dir = Path(os.environ.get("TWOQUARKS_GRAPHICS_DIR", (quark_dir / "graphics").as_posix()))
|
| 62 |
+
|
| 63 |
+
# Respect CLI overrides
|
| 64 |
+
if args.out.strip():
|
| 65 |
+
graphics_dir = Path(args.out)
|
| 66 |
+
|
| 67 |
graphics_dir.mkdir(parents=True, exist_ok=True)
|
| 68 |
+
|
| 69 |
+
if args.csv.strip():
|
| 70 |
+
results_csv = Path(args.csv)
|
| 71 |
+
else:
|
| 72 |
+
results_dir.mkdir(parents=True, exist_ok=True)
|
| 73 |
+
results_csv = results_dir / "down_paradox_tabular_results.csv"
|
| 74 |
|
| 75 |
if not results_csv.exists():
|
| 76 |
raise FileNotFoundError(f"Missing results CSV: {results_csv}")
|
|
|
|
| 79 |
if not rows:
|
| 80 |
raise RuntimeError(f"CSV is empty: {results_csv}")
|
| 81 |
|
| 82 |
+
prefix = args.prefix.strip() or "Down"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Agents present in CSV
|
| 85 |
agents = sorted({r.get("agent", "unknown") for r in rows})
|
|
|
|
| 86 |
|
| 87 |
+
# Reward plot
|
| 88 |
plt.figure()
|
| 89 |
for a in agents:
|
| 90 |
xs = [int(r.get("global_episode", r.get("episode", 0)) or 0) for r in rows if r.get("agent") == a]
|
| 91 |
+
ys = [_to_float(r.get("episode_reward", r.get("return", "nan"))) for r in rows if r.get("agent") == a]
|
| 92 |
if xs:
|
| 93 |
plt.plot(xs, ys, label=a)
|
| 94 |
plt.xlabel("Global episode")
|
| 95 |
plt.ylabel("Episode reward")
|
| 96 |
+
plt.title(f"{prefix}: reward over training")
|
| 97 |
plt.legend()
|
| 98 |
+
out1 = graphics_dir / f"{prefix}_paradox_tabular_reward.png"
|
| 99 |
+
plt.savefig(out1, dpi=160, bbox_inches="tight")
|
| 100 |
plt.close()
|
| 101 |
|
| 102 |
# Rho plot (if present)
|
| 103 |
+
has_rho = any(("rho_state" in r) for r in rows) or any(("rho" in r) for r in rows)
|
| 104 |
+
if has_rho:
|
| 105 |
plt.figure()
|
| 106 |
for a in agents:
|
| 107 |
xs = [int(r.get("global_episode", r.get("episode", 0)) or 0) for r in rows if r.get("agent") == a]
|
| 108 |
+
ys = [_to_float(r.get("rho_state", r.get("rho", "nan"))) for r in rows if r.get("agent") == a]
|
| 109 |
if xs:
|
| 110 |
plt.plot(xs, ys, label=a)
|
| 111 |
plt.xlabel("Global episode")
|
| 112 |
plt.ylabel("rho")
|
| 113 |
+
plt.title(f"{prefix}: rho over training")
|
| 114 |
plt.legend()
|
| 115 |
+
out2 = graphics_dir / f"{prefix}_paradox_tabular_rho.png"
|
| 116 |
+
plt.savefig(out2, dpi=160, bbox_inches="tight")
|
| 117 |
plt.close()
|
| 118 |
|
| 119 |
print(f"Saved plots to {graphics_dir}")
|