Spaces:
Running
Running
Add model selector for rank history
Browse files- src/utils.py +42 -0
src/utils.py
CHANGED
|
@@ -806,6 +806,7 @@ BASELINE_RANK_HISTORY_COLUMNS = [
|
|
| 806 |
_RANK_COLS = [c for c in BASELINE_RANK_HISTORY_COLUMNS if c.endswith("_Rank")]
|
| 807 |
_DAILY_DISPLAY_COLS = ["date", "model"] + _RANK_COLS
|
| 808 |
_WEEKLY_DISPLAY_COLS = ["week", "model"] + _RANK_COLS
|
|
|
|
| 809 |
|
| 810 |
|
| 811 |
def _read_rank_history(path: Path, date_col: str) -> pd.DataFrame:
|
|
@@ -843,6 +844,47 @@ def load_baseline_rank_history_weekly(root_dir: str = "results") -> pd.DataFrame
|
|
| 843 |
return _read_rank_history(Path(root_dir) / "baseline_rank_history_weekly.csv", "week")
|
| 844 |
|
| 845 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
# ---------------------------------------------------------------------------
|
| 847 |
# Forecast snapshot utilities
|
| 848 |
# ---------------------------------------------------------------------------
|
|
|
|
| 806 |
_RANK_COLS = [c for c in BASELINE_RANK_HISTORY_COLUMNS if c.endswith("_Rank")]
|
| 807 |
_DAILY_DISPLAY_COLS = ["date", "model"] + _RANK_COLS
|
| 808 |
_WEEKLY_DISPLAY_COLS = ["week", "model"] + _RANK_COLS
|
| 809 |
+
_MONTHLY_DISPLAY_COLS = ["month", "model"] + _RANK_COLS
|
| 810 |
|
| 811 |
|
| 812 |
def _read_rank_history(path: Path, date_col: str) -> pd.DataFrame:
|
|
|
|
| 844 |
return _read_rank_history(Path(root_dir) / "baseline_rank_history_weekly.csv", "week")
|
| 845 |
|
| 846 |
|
| 847 |
+
def load_baseline_rank_history_monthly(root_dir: str = "results") -> pd.DataFrame:
|
| 848 |
+
"""Load monthly tracked-model rank history aggregated from daily history."""
|
| 849 |
+
daily_path = Path(root_dir) / "baseline_rank_history_daily.csv"
|
| 850 |
+
if not daily_path.exists():
|
| 851 |
+
daily_path = Path(root_dir) / "baseline_rank_history.csv"
|
| 852 |
+
if not daily_path.exists():
|
| 853 |
+
return pd.DataFrame(columns=_MONTHLY_DISPLAY_COLS)
|
| 854 |
+
try:
|
| 855 |
+
df = pd.read_csv(daily_path)
|
| 856 |
+
except Exception:
|
| 857 |
+
return pd.DataFrame(columns=_MONTHLY_DISPLAY_COLS)
|
| 858 |
+
if "date" not in df.columns:
|
| 859 |
+
return pd.DataFrame(columns=_MONTHLY_DISPLAY_COLS)
|
| 860 |
+
|
| 861 |
+
df["date"] = pd.to_datetime(df["date"], errors="coerce")
|
| 862 |
+
df = df.dropna(subset=["date"])
|
| 863 |
+
if df.empty:
|
| 864 |
+
return pd.DataFrame(columns=_MONTHLY_DISPLAY_COLS)
|
| 865 |
+
if "model" not in df.columns:
|
| 866 |
+
df["model"] = ""
|
| 867 |
+
df["month"] = df["date"].dt.strftime("%Y-%m")
|
| 868 |
+
for col in _RANK_COLS:
|
| 869 |
+
if col in df.columns:
|
| 870 |
+
df[col] = pd.to_numeric(df[col], errors="coerce")
|
| 871 |
+
|
| 872 |
+
present_rank_cols = [col for col in _RANK_COLS if col in df.columns]
|
| 873 |
+
if present_rank_cols:
|
| 874 |
+
monthly = (
|
| 875 |
+
df.groupby(["month", "model"], dropna=False)[present_rank_cols]
|
| 876 |
+
.mean(numeric_only=True)
|
| 877 |
+
.round(3)
|
| 878 |
+
.reset_index()
|
| 879 |
+
)
|
| 880 |
+
else:
|
| 881 |
+
monthly = df.loc[:, ["month", "model"]].drop_duplicates()
|
| 882 |
+
for col in present_rank_cols:
|
| 883 |
+
monthly[col] = monthly[col].map(format_rank_number)
|
| 884 |
+
present = [c for c in _MONTHLY_DISPLAY_COLS if c in monthly.columns]
|
| 885 |
+
return monthly[present].sort_values(["month", "model"]).reset_index(drop=True)
|
| 886 |
+
|
| 887 |
+
|
| 888 |
# ---------------------------------------------------------------------------
|
| 889 |
# Forecast snapshot utilities
|
| 890 |
# ---------------------------------------------------------------------------
|