Spaces:
Sleeping
Sleeping
Fixed 'extract_leaderboard_stats'
Browse files
app.py
CHANGED
@@ -349,72 +349,84 @@ def play_game(
|
|
349 |
|
350 |
def extract_leaderboard_stats(game_name: str) -> pd.DataFrame:
|
351 |
all_stats = []
|
352 |
-
|
353 |
for db_file, agent_type, model_name in iter_agent_databases():
|
354 |
conn = sqlite3.connect(db_file)
|
355 |
try:
|
356 |
if game_name == "Aggregated Performance":
|
357 |
-
|
358 |
-
|
359 |
-
"SUM(reward) AS total_rewards
|
|
|
|
|
360 |
)
|
361 |
-
df = pd.read_sql_query(q, conn)
|
362 |
avg_time = conn.execute(
|
363 |
-
"SELECT AVG(generation_time) FROM moves
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
).fetchone()[0] or 0
|
366 |
else:
|
367 |
-
|
368 |
-
|
369 |
-
"SUM(reward) AS total_rewards "
|
370 |
-
"FROM game_results WHERE game_name = ?"
|
|
|
|
|
371 |
)
|
372 |
-
df = pd.read_sql_query(q, conn, params=(game_name,))
|
373 |
avg_time = conn.execute(
|
374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
375 |
(game_name,),
|
376 |
).fetchone()[0] or 0
|
377 |
|
378 |
-
df
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
"
|
385 |
-
|
386 |
-
).fetchone()[0] or 0
|
387 |
-
total_vs_random = conn.execute(
|
388 |
-
"SELECT COUNT(*) FROM game_results "
|
389 |
-
"WHERE opponent = 'random_None'"
|
390 |
-
).fetchone()[0] or 0
|
391 |
vs_random_rate = (
|
392 |
-
wins_vs_random / total_vs_random * 100
|
393 |
if total_vs_random > 0
|
394 |
-
else 0
|
395 |
)
|
396 |
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
|
|
|
|
406 |
finally:
|
407 |
conn.close()
|
408 |
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
)
|
414 |
-
|
415 |
-
cols = [c for c in LEADERBOARD_COLUMNS if c in leaderboard_df.columns]
|
416 |
-
leaderboard_df = leaderboard_df[cols]
|
417 |
-
return leaderboard_df
|
418 |
|
419 |
# -----------------------------------------------------------------------------
|
420 |
# Simple plotting helpers
|
|
|
349 |
|
350 |
def extract_leaderboard_stats(game_name: str) -> pd.DataFrame:
|
351 |
all_stats = []
|
|
|
352 |
for db_file, agent_type, model_name in iter_agent_databases():
|
353 |
conn = sqlite3.connect(db_file)
|
354 |
try:
|
355 |
if game_name == "Aggregated Performance":
|
356 |
+
# get totals across all games in this DB
|
357 |
+
df = pd.read_sql_query(
|
358 |
+
"SELECT COUNT(DISTINCT episode) AS games_played, SUM(reward) AS total_rewards "
|
359 |
+
"FROM game_results",
|
360 |
+
conn,
|
361 |
)
|
|
|
362 |
avg_time = conn.execute(
|
363 |
+
"SELECT AVG(generation_time) FROM moves"
|
364 |
+
).fetchone()[0] or 0
|
365 |
+
wins_vs_random = conn.execute(
|
366 |
+
"SELECT COUNT(*) FROM game_results "
|
367 |
+
"WHERE opponent = 'random_None' AND reward > 0",
|
368 |
+
).fetchone()[0] or 0
|
369 |
+
total_vs_random = conn.execute(
|
370 |
+
"SELECT COUNT(*) FROM game_results "
|
371 |
+
"WHERE opponent = 'random_None'",
|
372 |
).fetchone()[0] or 0
|
373 |
else:
|
374 |
+
# filter by the selected game
|
375 |
+
df = pd.read_sql_query(
|
376 |
+
"SELECT COUNT(DISTINCT episode) AS games_played, SUM(reward) AS total_rewards "
|
377 |
+
"FROM game_results WHERE game_name = ?",
|
378 |
+
conn,
|
379 |
+
params=(game_name,),
|
380 |
)
|
|
|
381 |
avg_time = conn.execute(
|
382 |
+
"SELECT AVG(generation_time) FROM moves WHERE game_name = ?",
|
383 |
+
(game_name,),
|
384 |
+
).fetchone()[0] or 0
|
385 |
+
wins_vs_random = conn.execute(
|
386 |
+
"SELECT COUNT(*) FROM game_results "
|
387 |
+
"WHERE opponent = 'random_None' AND reward > 0 AND game_name = ?",
|
388 |
+
(game_name,),
|
389 |
+
).fetchone()[0] or 0
|
390 |
+
total_vs_random = conn.execute(
|
391 |
+
"SELECT COUNT(*) FROM game_results "
|
392 |
+
"WHERE opponent = 'random_None' AND game_name = ?",
|
393 |
(game_name,),
|
394 |
).fetchone()[0] or 0
|
395 |
|
396 |
+
# If there were no results for this game, df will be empty or NaNs.
|
397 |
+
if df.empty or df["games_played"].iloc[0] is None:
|
398 |
+
games_played = 0
|
399 |
+
total_rewards = 0.0
|
400 |
+
else:
|
401 |
+
games_played = int(df["games_played"].iloc[0] or 0)
|
402 |
+
total_rewards = float(df["total_rewards"].iloc[0] or 0) / 2.0
|
403 |
+
|
|
|
|
|
|
|
|
|
|
|
404 |
vs_random_rate = (
|
405 |
+
(wins_vs_random / total_vs_random) * 100.0
|
406 |
if total_vs_random > 0
|
407 |
+
else 0.0
|
408 |
)
|
409 |
|
410 |
+
# Build a single-row DataFrame for this agent
|
411 |
+
row = {
|
412 |
+
"agent_name": model_name,
|
413 |
+
"agent_type": agent_type,
|
414 |
+
"# games": games_played,
|
415 |
+
"total rewards": total_rewards,
|
416 |
+
"avg_generation_time (sec)": round(float(avg_time), 3),
|
417 |
+
"win-rate": round(vs_random_rate, 2),
|
418 |
+
"win vs_random (%)": round(vs_random_rate, 2),
|
419 |
+
}
|
420 |
+
all_stats.append(pd.DataFrame([row]))
|
421 |
finally:
|
422 |
conn.close()
|
423 |
|
424 |
+
# Concatenate all rows; if all_stats is empty, return an empty DataFrame with columns.
|
425 |
+
if not all_stats:
|
426 |
+
return pd.DataFrame(columns=LEADERBOARD_COLUMNS)
|
427 |
+
|
428 |
+
leaderboard_df = pd.concat(all_stats, ignore_index=True)
|
429 |
+
return leaderboard_df[LEADERBOARD_COLUMNS]
|
|
|
|
|
|
|
430 |
|
431 |
# -----------------------------------------------------------------------------
|
432 |
# Simple plotting helpers
|