1212from codeclash .analysis .viz .utils import ASSETS_DIR , FONT_BOLD , MODEL_TO_DISPLAY_NAME
1313from codeclash .constants import LOCAL_LOG_DIR
1414
15- OUTPUT_FILE = ASSETS_DIR / "heatmap_win_rates.png"
1615
17-
18- def main (log_dir : Path ):
16+ def main (log_dir : Path , unit : str = "rounds" , output_file : Path = ASSETS_DIR / "heatmap_win_rates.png" ):
1917 print (f"Creating win rate heatmap from logs in { log_dir } ..." )
2018
2119 # Track round-level wins: (model1, model2) -> [wins, total_rounds]
@@ -33,6 +31,7 @@ def main(log_dir: Path):
3331 continue
3432
3533 # Process each round (skip round 0)
34+ tracker = defaultdict (int )
3635 for round_id , round_data in metadata ["round_stats" ].items ():
3736 if round_id == "0" :
3837 continue
@@ -42,9 +41,19 @@ def main(log_dir: Path):
4241 winner_model = p2m [winner ]
4342 loser_model = next (m for m in p2m .values () if m != winner_model )
4443
45- results [(winner_model , loser_model )][0 ] += 1 # win
46- results [(winner_model , loser_model )][1 ] += 1 # total
47- results [(loser_model , winner_model )][1 ] += 1 # total for loser
44+ if unit == "rounds" :
45+ results [(winner_model , loser_model )][0 ] += 1 # win
46+ results [(winner_model , loser_model )][1 ] += 1 # total
47+ results [(loser_model , winner_model )][1 ] += 1 # total for loser
48+ elif unit == "tournaments" :
49+ tracker [winner_model ] += 1
50+
51+ if unit == "tournaments" :
52+ winner = max (tracker , key = tracker .get )
53+ loser = min (tracker , key = tracker .get )
54+ results [(winner , loser )][0 ] += 1 # win
55+ results [(winner , loser )][1 ] += 1 # total
56+ results [(loser , winner )][1 ] += 1 # total for loser
4857 except :
4958 continue
5059
@@ -89,12 +98,19 @@ def main(log_dir: Path):
8998
9099 # plt.colorbar(im, label="Win Rate")
91100 plt .tight_layout ()
92- plt .savefig (OUTPUT_FILE , dpi = 300 , bbox_inches = "tight" )
93- print (f"Heatmap saved to { OUTPUT_FILE } " )
101+ if unit == "tournaments" :
102+ suffix = output_file .suffix or ".png"
103+ output_file = output_file .with_name (f"{ output_file .stem } _tournaments{ suffix } " )
104+ plt .savefig (output_file , dpi = 300 , bbox_inches = "tight" )
105+ print (f"Heatmap saved to { output_file } " )
94106
95107
96108if __name__ == "__main__" :
97109 parser = argparse .ArgumentParser (description = "Create model win rate heatmap" )
98110 parser .add_argument ("-d" , "--log_dir" , type = Path , default = LOCAL_LOG_DIR , help = "Path to logs" )
111+ parser .add_argument ("-u" , "--unit" , type = str , default = "rounds" , help = "Unit of analysis: rounds or tournaments" )
112+ parser .add_argument (
113+ "-o" , "--output_file" , type = Path , default = ASSETS_DIR / "heatmap_win_rates.png" , help = "Output file path"
114+ )
99115 args = parser .parse_args ()
100- main (args . log_dir )
116+ main (** vars ( args ) )
0 commit comments