|
|
""" |
|
|
Utility functions for MUSEval Leaderboard |
|
|
""" |
|
|
|
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
from typing import Dict, List, Any, Optional |
|
|
|
|
|
def norm_sNavie(df: pd.DataFrame) -> pd.DataFrame: |
|
|
"""Normalize dataframe using naive normalization""" |
|
|
|
|
|
return df |
|
|
|
|
|
def pivot_df(file_path: str, tab_name: str) -> pd.DataFrame: |
|
|
"""Pivot dataframe from file""" |
|
|
try: |
|
|
df = pd.read_csv(file_path) |
|
|
|
|
|
return df |
|
|
except Exception as e: |
|
|
print(f"Error reading {file_path}: {e}") |
|
|
return pd.DataFrame() |
|
|
|
|
|
def get_grouped_dfs() -> Dict[str, pd.DataFrame]: |
|
|
"""Get grouped dataframes for different views""" |
|
|
from .load_results import load_results_with_metadata, create_overall_table |
|
|
|
|
|
|
|
|
results = load_results_with_metadata() |
|
|
if not results: |
|
|
return { |
|
|
'domain': pd.DataFrame(), |
|
|
'frequency': pd.DataFrame(), |
|
|
'term_length': pd.DataFrame(), |
|
|
'univariate': pd.DataFrame(), |
|
|
'overall': pd.DataFrame() |
|
|
} |
|
|
|
|
|
|
|
|
overall_df = create_overall_table() |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
'domain': overall_df.copy(), |
|
|
'frequency': overall_df.copy(), |
|
|
'term_length': overall_df.copy(), |
|
|
'univariate': overall_df.copy(), |
|
|
'overall': overall_df.copy() |
|
|
} |
|
|
|
|
|
def pivot_existed_df(df: pd.DataFrame, tab_name: str) -> pd.DataFrame: |
|
|
"""Pivot existing dataframe""" |
|
|
if df.empty: |
|
|
return df |
|
|
|
|
|
|
|
|
df_copy = df.copy() |
|
|
df_copy['tab'] = tab_name |
|
|
return df_copy |
|
|
|
|
|
def rename_metrics(df: pd.DataFrame) -> pd.DataFrame: |
|
|
"""Rename metrics columns""" |
|
|
if df.empty: |
|
|
return df |
|
|
|
|
|
|
|
|
if 'MAE' in df.columns: |
|
|
df_copy = df.copy() |
|
|
df_copy['MASE_Rank'] = df_copy['MAE'].rank(method='min') |
|
|
return df_copy |
|
|
|
|
|
return df |
|
|
|
|
|
def format_df(df: pd.DataFrame) -> pd.DataFrame: |
|
|
"""Format dataframe for display""" |
|
|
if df.empty: |
|
|
return df |
|
|
|
|
|
df_copy = df.copy() |
|
|
|
|
|
|
|
|
numeric_cols = ['MAE', 'Uni-MAE', 'RMSE', 'MAPE', 'R²', 'SMAPE', 'Uni-Multi'] |
|
|
for col in numeric_cols: |
|
|
if col in df_copy.columns: |
|
|
if col in ['MAPE', 'SMAPE']: |
|
|
df_copy[col] = df_copy[col].apply(lambda x: f"{x:.1f}%" if pd.notna(x) else "") |
|
|
else: |
|
|
df_copy[col] = df_copy[col].apply(lambda x: f"{x:.3f}" if pd.notna(x) else "") |
|
|
|
|
|
return df_copy |
|
|
|