lukehinds's picture
Big refactor
1264ff3
raw
history blame
3.71 kB
import json
import os
import numpy as np
import pandas as pd
from src.display.formatting import has_no_nan_values, make_clickable_model
from src.leaderboard.read_evals import get_raw_eval_results
def get_leaderboard_df(results_path: str, requests_path: str, cols: list, benchmark_cols: list) -> pd.DataFrame:
"""Creates a dataframe from all the individual experiment results"""
print(f"Getting raw eval results from {results_path} and {requests_path}")
raw_data = get_raw_eval_results(results_path, requests_path)
print(f"Got {len(raw_data)} raw eval results")
if not raw_data:
print("No raw data found!")
return pd.DataFrame(columns=cols)
all_data_json = [v.to_dict() for v in raw_data]
print(f"Converted {len(all_data_json)} results to dict")
print(f"Data before DataFrame creation: {all_data_json}")
df = pd.DataFrame.from_records(all_data_json)
print(f"Created DataFrame with columns: {df.columns.tolist()}")
print(f"DataFrame before filtering:\n{df}")
# Ensure all required columns exist
for col in cols:
if col not in df.columns:
print(f"Adding missing column: {col}")
df[col] = None
# Sort by Security Score if available, otherwise don't sort
if "Security Score ⬆️" in df.columns:
df = df.sort_values(by="Security Score ⬆️", ascending=False)
print(f"DataFrame after sorting:\n{df}")
else:
print("Security Score column not found, skipping sorting")
# Select only the columns we want to display
df = df[cols]
# Round numeric columns
numeric_cols = df.select_dtypes(include=[np.number]).columns
df[numeric_cols] = df[numeric_cols].round(decimals=2)
print(f"DataFrame after column selection and rounding:\n{df}")
print(f"Final DataFrame has {len(df)} rows")
return df
def get_evaluation_queue_df(save_path: str, cols: list) -> list[pd.DataFrame]:
"""Creates the different dataframes for the evaluation queues requestes"""
print(f"Looking for eval requests in {save_path}")
all_evals = []
# Walk through all directories recursively
for root, _, files in os.walk(save_path):
for file in files:
if file.endswith('.json'):
file_path = os.path.join(root, file)
print(f"Reading JSON file: {file_path}")
with open(file_path) as fp:
data = json.load(fp)
# Create a new dict with the required column names
formatted_data = {
"model": make_clickable_model(data["model"]),
"revision": data.get("revision", "main"),
"private": data.get("private", False),
"precision": data.get("precision", ""),
"weight_type": data.get("weight_type", ""),
"status": data.get("status", "")
}
all_evals.append(formatted_data)
print(f"Found {len(all_evals)} total eval requests")
pending_list = [e for e in all_evals if e["status"] in ["PENDING", "RERUN"]]
running_list = [e for e in all_evals if e["status"] == "RUNNING"]
finished_list = [e for e in all_evals if e["status"].startswith("FINISHED") or e["status"] == "PENDING_NEW_EVAL"]
print(f"Pending: {len(pending_list)}, Running: {len(running_list)}, Finished: {len(finished_list)}")
df_pending = pd.DataFrame.from_records(pending_list, columns=cols)
df_running = pd.DataFrame.from_records(running_list, columns=cols)
df_finished = pd.DataFrame.from_records(finished_list, columns=cols)
return df_finished[cols], df_running[cols], df_pending[cols]