trAIn.me_v3 / src /gradio /helpers /report_utils.py
AIppyDev's picture
Pareil
5683bdb
import json
import pandas as pd
from pathlib import Path
def _empty_dl_dfs():
df_summary = pd.DataFrame({"Key": [], "Value": []})
df_model = pd.DataFrame({"Key": [], "Value": []})
df_training = pd.DataFrame({"Key": [], "Value": []})
df_metrics = pd.DataFrame({"Metric": [], "Value": []})
return df_summary, df_model, df_training, df_metrics
def read_model_report(path: Path) -> dict:
"""
Lit le fichier JSON du rapport de modèle (ML).
Retourne un dict vide en cas d'erreur ou de fichier manquant.
"""
report_path = Path(path) if path is not None else None
print(report_path)
if report_path is None or not report_path.exists():
print(f"[ML REPORT] Missing report file: {report_path}")
return {} # ⬅️ dict vide, pas de tuple
try:
with report_path.open("r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
print(f"[ML REPORT] Error while reading {report_path}: {e}")
return {} # ⬅️ dict vide
print(data)
return data
def report_summary_df(rep) -> pd.DataFrame:
"""
Construit un tableau récapitulatif des informations principales du rapport.
Accepte soit un dict (nouvelle API), soit un tuple (ancienne API) et
fallback proprement dans ce cas.
"""
# --- Normalisation en dict ---
if isinstance(rep, tuple):
# Ancienne API : on ne sait pas quoi faire de ce tuple → on revient à vide
print("[ML REPORT] report_summary_df reçu un tuple, fallback sur dict vide.")
rep = {}
if rep is None:
rep = {}
if not isinstance(rep, dict) or not rep:
return pd.DataFrame({
"Informations": [
"created_at", "target", "n_features", "n_test_samples",
"selected_model.type", "selected_model.class"
],
"Value": ["-", "-", "-", "-", "-", "-"],
})
sel = rep.get("selected_model", {})
rows = [
("created_at", rep.get("created_at", "-")),
("target", rep.get("target", "-")),
("n_features", rep.get("n_features", "-")),
("n_test_samples", rep.get("n_test_samples", "-")),
("selected_model.type", sel.get("model_type", "-")),
("selected_model.class", sel.get("model_class", "-")),
]
return pd.DataFrame(rows, columns=["Informations", "Value"])
def report_metrics_df(rep) -> pd.DataFrame:
"""
Construit un tableau des métriques de performance par modèle.
Colonnes : Modèle, MAE, RMSE, R2.
Gère dict (nouvelle API) et tuple (ancienne API) en fallback.
"""
# --- Normalisation en dict ---
if isinstance(rep, tuple):
print("[ML REPORT] report_metrics_df reçu un tuple, fallback sur dict vide.")
rep = {}
if rep is None or not isinstance(rep, dict):
return pd.DataFrame(columns=["Model", "MAE", "RMSE", "R2"])
mets = rep.get("metrics_by_model", {})
if not mets:
return pd.DataFrame(columns=["Model", "MAE", "RMSE", "R2"])
df = pd.DataFrame(mets).T.reset_index().rename(columns={"index": "Model"})
# Conversion et arrondis
for c in ("MAE", "RMSE", "R2"):
if c in df.columns:
df[c] = pd.to_numeric(df[c], errors="coerce").round(3)
return df[["Model", "MAE", "RMSE", "R2"]]