File size: 5,432 Bytes
5d68221 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | from __future__ import annotations
from datetime import datetime, timezone
import re
from typing import Iterable
import numpy as np
import pandas as pd
_CANONICAL_METRIC_MAP = {
"precision": "Precision",
"recall": "Recall",
"f1": "F1",
"meaniou": "Mean_IoU",
"meantpiou": "Mean_TP_IoU",
}
def normalize_metrics_dataframe(dataframe: pd.DataFrame) -> pd.DataFrame:
if dataframe.empty:
return pd.DataFrame()
rename_map: dict[str, str] = {}
class_column: str | None = None
for column in dataframe.columns:
normalized = _normalize_key(column)
if normalized == "class":
class_column = str(column)
rename_map[str(column)] = "Class"
continue
if normalized.endswith("std"):
metric_key = normalized[: -len("std")]
metric_name = _CANONICAL_METRIC_MAP.get(metric_key)
if metric_name:
rename_map[str(column)] = f"{metric_name}_std"
continue
metric_name = _CANONICAL_METRIC_MAP.get(normalized)
if metric_name:
rename_map[str(column)] = metric_name
if class_column is None:
return pd.DataFrame()
cleaned = dataframe.rename(columns=rename_map).copy()
cleaned["Class"] = cleaned["Class"].astype(str).str.strip()
cleaned = cleaned[cleaned["Class"] != ""]
for column in cleaned.columns:
if column == "Class":
continue
cleaned[column] = cleaned[column].apply(_parse_float)
return cleaned.reset_index(drop=True)
def table_to_long_dataframe(
dataframe: pd.DataFrame,
repo_id: str,
revision: str,
committed_at: datetime | None,
metrics: Iterable[str],
) -> pd.DataFrame:
if dataframe.empty:
return pd.DataFrame()
timestamp = pd.to_datetime(committed_at or datetime.now(timezone.utc), utc=True)
frames: list[pd.DataFrame] = []
for metric in metrics:
if metric not in dataframe.columns:
continue
metric_frame = pd.DataFrame(
{
"repo_id": repo_id,
"revision": revision,
"timestamp": timestamp,
"class": dataframe["Class"].astype(str),
"metric": metric,
"value": dataframe[metric],
"std": dataframe.get(f"{metric}_std", np.nan),
}
)
frames.append(metric_frame)
if not frames:
return pd.DataFrame()
long_df = pd.concat(frames, ignore_index=True)
long_df["value"] = pd.to_numeric(long_df["value"], errors="coerce")
long_df["std"] = pd.to_numeric(long_df["std"], errors="coerce")
long_df["timestamp"] = pd.to_datetime(long_df["timestamp"], utc=True, errors="coerce")
long_df = long_df.dropna(subset=["timestamp"])
return long_df
def add_latest_flags(history_df: pd.DataFrame) -> pd.DataFrame:
if history_df.empty:
result = history_df.copy()
result["is_latest"] = False
return result
unique_runs = history_df[["repo_id", "revision", "timestamp"]].drop_duplicates()
latest_runs = (
unique_runs.sort_values(["repo_id", "timestamp", "revision"])
.groupby("repo_id", as_index=False)
.tail(1)
.assign(is_latest=True)
)
merged = history_df.merge(
latest_runs,
on=["repo_id", "revision", "timestamp"],
how="left",
)
merged["is_latest"] = merged["is_latest"].fillna(False)
return merged
def filter_history(
history_df: pd.DataFrame,
repos: Iterable[str],
classes: Iterable[str],
metrics: Iterable[str],
) -> pd.DataFrame:
if history_df.empty:
return history_df
filtered = history_df.copy()
repos = list(repos)
classes = list(classes)
metrics = list(metrics)
if repos:
filtered = filtered[filtered["repo_id"].isin(repos)]
if classes:
filtered = filtered[filtered["class"].isin(classes)]
if metrics:
filtered = filtered[filtered["metric"].isin(metrics)]
return filtered
def build_latest_snapshot_table(history_df: pd.DataFrame) -> pd.DataFrame:
if history_df.empty:
return pd.DataFrame()
latest_df = history_df[history_df["is_latest"]].copy()
if latest_df.empty:
return pd.DataFrame()
value_pivot = latest_df.pivot_table(
index=["repo_id", "class", "revision", "timestamp"],
columns="metric",
values="value",
aggfunc="first",
)
std_pivot = latest_df.pivot_table(
index=["repo_id", "class", "revision", "timestamp"],
columns="metric",
values="std",
aggfunc="first",
)
if not std_pivot.empty:
std_pivot.columns = [f"{column}_std" for column in std_pivot.columns]
snapshot = value_pivot.join(std_pivot, how="left")
snapshot = snapshot.reset_index().sort_values(["repo_id", "class"])
return snapshot
def _normalize_key(value: str) -> str:
return re.sub(r"[^a-z0-9]", "", str(value).strip().lower())
def _parse_float(value: object) -> float:
if value is None:
return np.nan
text = str(value).strip()
if text == "":
return np.nan
lowered = text.lower()
if lowered in {"nan", "na", "n/a", "none", "null", "-"}:
return np.nan
cleaned = text.replace(",", "")
try:
return float(cleaned)
except ValueError:
return np.nan
|