Process round results to add computed fields and sort scores
(
round_results: dict[str, Any] | None, agent_info: list[AgentInfo] | None = None
)
| 478 | |
| 479 | |
| 480 | def process_round_results( |
| 481 | round_results: dict[str, Any] | None, agent_info: list[AgentInfo] | None = None |
| 482 | ) -> dict[str, Any] | None: |
| 483 | """Process round results to add computed fields and sort scores""" |
| 484 | if not round_results: |
| 485 | return round_results |
| 486 | |
| 487 | # Create a copy to avoid modifying original data |
| 488 | processed = round_results.copy() |
| 489 | |
| 490 | # Get scores, initialize empty dict if missing |
| 491 | scores = round_results.get("scores", {}).copy() |
| 492 | |
| 493 | # Ensure all expected players are in scores, even with 0 wins |
| 494 | if agent_info: |
| 495 | expected_players = {agent.name for agent in agent_info} |
| 496 | missing_players = expected_players - set(scores.keys()) |
| 497 | if missing_players: |
| 498 | logger.warning(f"Players {sorted(missing_players)} not found in round results, adding with 0 wins") |
| 499 | for player in missing_players: |
| 500 | scores[player] = 0 |
| 501 | |
| 502 | # Sort scores alphabetically by key |
| 503 | scores = dict(sorted(scores.items())) |
| 504 | processed["scores"] = scores |
| 505 | processed["sorted_scores"] = list(scores.items()) |
| 506 | |
| 507 | # Calculate winner percentage and p-value |
| 508 | winner = round_results.get("winner") |
| 509 | if winner and scores: |
| 510 | total_games = sum(scores.values()) |
| 511 | if total_games > 0: |
| 512 | if winner != "Tie": |
| 513 | winner_wins = scores.get(winner, 0) |
| 514 | ties = scores.get("Tie", 0) |
| 515 | win_percentage = round(((winner_wins + 0.5 * ties) / total_games) * 100, 1) |
| 516 | processed["winner_percentage"] = win_percentage |
| 517 | else: |
| 518 | processed["winner_percentage"] = None # No percentage for ties |
| 519 | |
| 520 | # Calculate p-value for statistical significance |
| 521 | logger.debug(f"Calculating p-value for scores: {dict(sorted(scores.items()))}") |
| 522 | p_value = calculate_p_value(scores) |
| 523 | logger.debug(f"P-value result: {p_value} (rounded: {round(p_value, 2)})") |
| 524 | processed["p_value"] = round(p_value, 2) |
| 525 | else: |
| 526 | processed["winner_percentage"] = None |
| 527 | processed["p_value"] = None |
| 528 | else: |
| 529 | processed["winner_percentage"] = None |
| 530 | processed["p_value"] = None |
| 531 | |
| 532 | return processed |
| 533 | |
| 534 | |
| 535 | @dataclass |
no test coverage detected