Spaces:
Configuration error
Configuration error
Upload stratego\benchmarking\analysis\analyze_csv.py with huggingface_hub
Browse files
stratego//benchmarking//analysis//analyze_csv.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# stratego/benchmarking/analysis/analyze_csv.py
|
| 2 |
+
|
| 3 |
+
import csv
|
| 4 |
+
import os
|
| 5 |
+
from collections import Counter
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
OUTPUT_DIR = "stratego/benchmarking/output"
|
| 9 |
+
SUMMARY_DIR = "stratego/benchmarking/summaries"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def find_latest_benchmark_csv():
|
| 13 |
+
files = [
|
| 14 |
+
f for f in os.listdir(OUTPUT_DIR)
|
| 15 |
+
if f.startswith("benchmark_") and f.endswith(".csv")
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
if not files:
|
| 19 |
+
raise FileNotFoundError("No benchmark CSV files found in output/")
|
| 20 |
+
|
| 21 |
+
files.sort()
|
| 22 |
+
return os.path.join(OUTPUT_DIR, files[-1])
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def analyze_benchmark_csv(csv_path: str):
|
| 26 |
+
games = []
|
| 27 |
+
|
| 28 |
+
with open(csv_path, newline="", encoding="utf-8") as f:
|
| 29 |
+
reader = csv.DictReader(f)
|
| 30 |
+
for row in reader:
|
| 31 |
+
games.append(row)
|
| 32 |
+
|
| 33 |
+
total_games = len(games)
|
| 34 |
+
|
| 35 |
+
wins = Counter()
|
| 36 |
+
terminations = Counter()
|
| 37 |
+
|
| 38 |
+
turns = []
|
| 39 |
+
invalid_p0 = 0
|
| 40 |
+
invalid_p1 = 0
|
| 41 |
+
repetitions = []
|
| 42 |
+
|
| 43 |
+
for g in games:
|
| 44 |
+
winner_raw = g["winner"]
|
| 45 |
+
try:
|
| 46 |
+
winner = int(winner_raw)
|
| 47 |
+
except (TypeError, ValueError):
|
| 48 |
+
winner = -1
|
| 49 |
+
reason = g["game_end_reason"]
|
| 50 |
+
|
| 51 |
+
if winner == 0:
|
| 52 |
+
wins["p0"] += 1
|
| 53 |
+
elif winner == 1:
|
| 54 |
+
wins["p1"] += 1
|
| 55 |
+
else:
|
| 56 |
+
wins["draw"] += 1
|
| 57 |
+
|
| 58 |
+
terminations[reason] += 1
|
| 59 |
+
|
| 60 |
+
turns.append(int(g["turns"]))
|
| 61 |
+
invalid_p0 += int(g["invalid_moves_p0"])
|
| 62 |
+
invalid_p1 += int(g["invalid_moves_p1"])
|
| 63 |
+
repetitions.append(int(g["repetitions"]))
|
| 64 |
+
|
| 65 |
+
summary = {
|
| 66 |
+
"total_games": total_games,
|
| 67 |
+
"wins_p0": wins["p0"],
|
| 68 |
+
"wins_p1": wins["p1"],
|
| 69 |
+
"draws": wins["draw"],
|
| 70 |
+
"win_rate_p0": wins["p0"] / total_games if total_games else 0,
|
| 71 |
+
"win_rate_p1": wins["p1"] / total_games if total_games else 0,
|
| 72 |
+
"avg_game_length": sum(turns) / total_games if total_games else 0,
|
| 73 |
+
"avg_invalid_moves_p0": invalid_p0 / total_games if total_games else 0,
|
| 74 |
+
"avg_invalid_moves_p1": invalid_p1 / total_games if total_games else 0,
|
| 75 |
+
"avg_repetitions": sum(repetitions) / total_games if total_games else 0,
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
return summary, terminations
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def write_summary_csv(summary, terminations, source_csv):
|
| 82 |
+
os.makedirs(SUMMARY_DIR, exist_ok=True)
|
| 83 |
+
|
| 84 |
+
base = os.path.basename(source_csv).replace("benchmark_", "").replace(".csv", "")
|
| 85 |
+
out_path = os.path.join(SUMMARY_DIR, f"summary_{base}.csv")
|
| 86 |
+
|
| 87 |
+
with open(out_path, "w", newline="", encoding="utf-8") as f:
|
| 88 |
+
writer = csv.writer(f)
|
| 89 |
+
|
| 90 |
+
writer.writerow(["metric", "value"])
|
| 91 |
+
for k, v in summary.items():
|
| 92 |
+
writer.writerow([k, v])
|
| 93 |
+
|
| 94 |
+
writer.writerow([])
|
| 95 |
+
writer.writerow(["termination_reason", "count"])
|
| 96 |
+
for reason, count in terminations.items():
|
| 97 |
+
writer.writerow([reason, count])
|
| 98 |
+
|
| 99 |
+
print(f"[OK] Summary written to {out_path}")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
latest_csv = find_latest_benchmark_csv()
|
| 104 |
+
print(f"[INFO] Analyzing {latest_csv}")
|
| 105 |
+
|
| 106 |
+
summary, terminations = analyze_benchmark_csv(latest_csv)
|
| 107 |
+
write_summary_csv(summary, terminations, latest_csv)
|