Haseeb949/fluenta-backend
0
1# evaluate.py2import pandas as pd3from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report4from pathlib import Path5 6RESULTS = Path("C:/Users/X/Downloads/models/backend/results.csv")7LABELS = Path("C:/Users/X/Downloads/models/Dataset/clips/labels.csv") # your labels.csv path8OUT_CM = Path("C:/Users/X/Downloads/models/backend/confusion_matrix.csv")9 10df_r = pd.read_csv(RESULTS)11df_l = pd.read_csv(LABELS)12 13# labels.csv format: filepath + binary columns for each label; need to create a single label per file: stutter vs non-stutter14# We treat any clip that has any dysfluency label >0 as Stutter, else Non-Stutter15def is_stutter_row(row):16 # adapt: find your SEP-28k label columns; typical: Block, Prolongation, SoundRep, WordRep, Interjection, NoStutteredWords17 label_cols = [c for c in df_l.columns if c.lower() not in ("filepath","filepath")]18 # ensure numeric19 s = row[label_cols].sum()20 return 0 if row.get("NoStutteredWords",0)==1 else 121 22# Build ground truth map: filename -> stutter/nonstutter23df_l['filename'] = df_l['filepath'].apply(lambda p: Path(p).name)24# Create binary truth: 1 = Stutter, 0 = Non-Stutter25def truth_from_row(r):26 # If NoStutteredWords == 1 => fluent27 if 'NoStutteredWords' in r and int(r['NoStutteredWords'])==1:28 return 029 # if any other label is 1 => stutter30 others = [c for c in r.index if c not in ('filepath','filename') and c!='NoStutteredWords']31 for c in others:32 try:33 if int(r[c])==1:34 return 135 except Exception:36 pass37 return 038 39df_l['truth'] = df_l.apply(truth_from_row, axis=1)40 41# Merge42df = pd.merge(df_r, df_l[['filename','truth']], left_on='filename', right_on='filename', how='inner')43# map predictions to binary44df['pred_bin'] = df['prediction'].map(lambda x: 1 if str(x).lower().startswith('stutter') else 0)45 46y_true = df['truth']47y_pred = df['pred_bin']48 49acc = accuracy_score(y_true, y_pred)50prec = precision_score(y_true, y_pred, zero_division=0)51rec = recall_score(y_true, y_pred, zero_division=0)52f1 = f1_score(y_true, y_pred, zero_division=0)53cm = confusion_matrix(y_true, y_pred)54 55print("Samples used:", len(df))56print("Accuracy:", acc)57print("Precision:", prec)58print("Recall:", rec)59print("F1:", f1)60print("Confusion matrix:\n", cm)61print("\nClassification report:\n", classification_report(y_true, y_pred, zero_division=0))62# save confusion matrix63import csv64with OUT_CM.open("w",newline="") as f:65 w = csv.writer(f)66 w.writerow(["","Pred=NonStutter","Pred=Stutter"])67 w.writerow(["True=NonStutter", cm[0,0], cm[0,1]])68 w.writerow(["True=Stutter", cm[1,0], cm[1,1]])69print("Saved confusion matrix to", OUT_CM)70 