MCPcopy Create free account
hub / github.com/CodeClash-ai/CodeClash / main

Function main

codeclash/analysis/viz/cdf_command_diversity.py:116–203  ·  view source on GitHub ↗

Main analysis function that processes all tournament data and generates the CDF plot. Process: 1. Scan all tournament directories for trajectory files 2. Extract commands from each trajectory and calculate diversity 3. Group diversity scores by model 4. Generate CDF visuali

()

Source from the content-addressed store, hash-verified

114
115
116def main():
117 """
118 Main analysis function that processes all tournament data and generates the CDF plot.
119
120 Process:
121 1. Scan all tournament directories for trajectory files
122 2. Extract commands from each trajectory and calculate diversity
123 3. Group diversity scores by model
124 4. Generate CDF visualization comparing models
125 """
126 model_to_diversity = {}
127
128 if not DATA_CACHE.exists():
129 # Find all tournament directories by looking for metadata.json files
130 tournaments = [x.parent for x in LOCAL_LOG_DIR.rglob("metadata.json")]
131 for game_log_folder in tqdm(tournaments):
132 # Load tournament metadata to get player-to-model mapping
133 with open(game_log_folder / "metadata.json") as f:
134 metadata = json.load(f)
135 try:
136 # Extract mapping from player name to model name
137 p2m = {
138 x["name"]: x["config"]["model"]["model_name"].strip("@").split("/")[-1]
139 for x in metadata["config"]["players"]
140 }
141 # Initialize diversity list for each model we encounter
142 for model in p2m.values():
143 if model not in model_to_diversity:
144 model_to_diversity[model] = []
145 except KeyError:
146 # Skip tournaments with malformed metadata
147 continue
148
149 # Process each player's trajectory files
150 for name in p2m.keys():
151 traj_files = (game_log_folder / "players" / name).rglob("*.traj.json")
152 for traj_file in traj_files:
153 try:
154 with open(traj_file) as f:
155 traj = json.load(f)
156
157 # Extract commands and calculate diversity for this session
158 commands = extract_commands_from_trajectory(traj)
159 if commands: # Only calculate entropy if there are commands
160 diversity = shannon_entropy(commands)
161 model_to_diversity[p2m[name]].append(diversity)
162 except (json.JSONDecodeError, KeyError):
163 # Skip malformed trajectory files
164 continue
165
166 # Remove models with no valid data
167 model_to_diversity = {k: v for k, v in model_to_diversity.items() if v}
168
169 with open(DATA_CACHE, "w") as f:
170 json.dump(model_to_diversity, f, indent=2)
171
172 with open(DATA_CACHE) as f:
173 model_to_diversity = json.load(f)

Callers 1

Calls 2

shannon_entropyFunction · 0.85

Tested by

no test coverage detected