File size: 12,964 Bytes
60b21d3 | 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | #!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025 Stanford University, ETH Zurich, and the project authors (see CONTRIBUTORS.md)
# SPDX-FileCopyrightText: 2025 This source file is part of the OpenTSLM open-source project.
#
# SPDX-License-Identifier: MIT
"""
Parse sleep baseline evaluation results from a structured JSON file and compute
accuracy and F1 statistics. Designed for JSON files with the following shape:
{
"model_name": "...",
"dataset_name": "SleepEDFCoTQADataset",
"total_samples": 930,
"successful_inferences": 930,
"success_rate": 1.0,
"metrics": {"accuracy": 10.75},
"detailed_results": [
{
"sample_idx": 0,
"input_text": "...",
"target_answer": "... Answer: Wake",
"generated_answer": "... Answer: Wake",
"metrics": {
"accuracy": 1,
"gt_label": "wake",
"pred_label": "wake"
}
},
...
]
}
The script prioritizes labels under detailed_results[i]["metrics"]["gt_label"|"pred_label"],
falling back to extracting the trailing "Answer: <label>" from the target and
generated texts if labels are not provided.
"""
import argparse
import json
from pathlib import Path
from typing import Dict, List
# --- Inline minimal utilities (avoid importing modules that require extra packages) ---
import re
def extract_answer(text: str) -> str:
"""Extract the final answer from text by taking content after 'Answer:'
and trimming trailing special tokens.
"""
if "Answer: " not in text:
return text
answer = text.split("Answer: ")[-1].strip()
answer = re.sub(r"<\|.*?\|>$", "", answer).strip()
return answer
def calculate_f1_score(prediction: str, ground_truth: str):
"""Binary exact-match F1 on normalized strings (lower/strip/punct)."""
pred_normalized = prediction.lower().strip().rstrip(".,!?;:")
truth_normalized = ground_truth.lower().strip().rstrip(".,!?;:")
f1 = 1.0 if pred_normalized == truth_normalized else 0.0
return {
"f1_score": f1,
"precision": f1,
"recall": f1,
"prediction_normalized": pred_normalized,
"ground_truth_normalized": truth_normalized,
}
def calculate_f1_stats(data_points: List[Dict], allowed_labels=None):
"""Compute micro average and macro F1. If allowed_labels is provided, do not
create new classes outside this set when accumulating FP counts.
"""
if not data_points:
return {}
f1_scores = [point.get("f1_score", 0) for point in data_points]
average_f1 = sum(f1_scores) / len(f1_scores) if f1_scores else 0.0
class_predictions: Dict[str, Dict[str, int]] = {}
if allowed_labels:
for label in allowed_labels:
class_predictions[label] = {"tp": 0, "fp": 0, "fn": 0}
for point in data_points:
gt_class = point.get("ground_truth_normalized", "")
pred_class = point.get("prediction_normalized", "")
if gt_class not in class_predictions:
class_predictions[gt_class] = {"tp": 0, "fp": 0, "fn": 0}
if pred_class == gt_class:
class_predictions[gt_class]["tp"] += 1
else:
class_predictions[gt_class]["fn"] += 1
if (allowed_labels is None) or (pred_class in (allowed_labels or set())):
if pred_class in class_predictions:
class_predictions[pred_class]["fp"] += 1
else:
class_predictions[pred_class] = {"tp": 0, "fp": 1, "fn": 0}
class_f1_scores: Dict[str, Dict[str, float]] = {}
total_f1 = 0.0
valid_classes = 0
for class_name, counts in class_predictions.items():
tp, fp, fn = counts["tp"], counts["fp"], counts["fn"]
precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
recall = tp / (tp + fn) if (tp + fn) > 0 else 0.0
f1 = (
2 * (precision * recall) / (precision + recall)
if (precision + recall) > 0
else 0.0
)
class_f1_scores[class_name] = {
"f1": f1,
"precision": precision,
"recall": recall,
"tp": tp,
"fp": fp,
"fn": fn,
}
total_f1 += f1
valid_classes += 1
macro_f1 = total_f1 / valid_classes if valid_classes > 0 else 0.0
return {
"average_f1": average_f1,
"macro_f1": macro_f1,
"class_f1_scores": class_f1_scores,
"total_classes": valid_classes,
}
def calculate_accuracy_stats(data_points: List[Dict]):
if not data_points:
return {}
total = len(data_points)
correct = sum(1 for p in data_points if p.get("accuracy", False))
return {
"total_samples": total,
"correct_predictions": correct,
"incorrect_predictions": total - correct,
"accuracy_percentage": (correct / total) * 100 if total else 0.0,
}
def normalize_label(s: str) -> str:
"""Basic whitespace trim; further canonicalization is done in canonicalize_sleep_label."""
if s is None:
return ""
return s.strip()
def canonicalize_sleep_label(s: str) -> str:
"""Map various aliases to the canonical SleepEDF labels (lowercased).
Canonical target set (lowercased):
- wake
- non-rem stage 1
- non-rem stage 2
- non-rem stage 3
- rem sleep
- movement
Also handle legacy/alias forms like: n1/s1/1, n2/s2/2, n3/s3/3, n4/s4/4 -> map 4 to stage 3.
"""
if not s:
return ""
t = s.strip().lower()
# Remove trailing punctuation common in generations
while t and t[-1] in ".,;:!?":
t = t[:-1].strip()
# If it's an option-like artifact (e.g., "(a) wake"), keep only the label part
# but since SleepEDF doesn't use options, just strip leading option markers if present
if len(t) > 3 and t[0] == "(" and ")" in t[:4]:
t = t.split(")", 1)[-1].strip()
# Short aliases
if t in {"w"}:
return "wake"
if t in {"rem", "rapid eye movement"}:
return "rem sleep"
if t in {"artifact", "artifacts", "movement time"}:
return "movement"
# Normalize common stage forms like "stage 2", "s2", "n2", "2"
# Map to non-rem stage X; AASM stage 4 -> stage 3
stage_map = {
"1": "non-rem stage 1",
"s1": "non-rem stage 1",
"n1": "non-rem stage 1",
"stage 1": "non-rem stage 1",
"2": "non-rem stage 2",
"s2": "non-rem stage 2",
"n2": "non-rem stage 2",
"stage 2": "non-rem stage 2",
"3": "non-rem stage 3",
"s3": "non-rem stage 3",
"n3": "non-rem stage 3",
"stage 3": "non-rem stage 3",
"4": "non-rem stage 3",
"s4": "non-rem stage 3",
"n4": "non-rem stage 3",
"stage 4": "non-rem stage 3",
}
if t in stage_map:
return stage_map[t]
# Normalize explicit non-rem phrasings
t = t.replace("non rem", "non-rem").replace("nrem", "non-rem")
# Handle patterns like "non-rem stage x"
# Already canonical if it matches exactly
canonical = {
"wake",
"non-rem stage 1",
"non-rem stage 2",
"non-rem stage 3",
"rem sleep",
"movement",
# Some variants to map
"non-rem stage 4",
}
if t in canonical:
if t == "non-rem stage 4":
return "non-rem stage 3"
return t
# If phrase contains key tokens, attempt heuristic mapping
if "wake" in t:
return "wake"
if "rem" in t and "non-rem" not in t:
return "rem sleep"
if "movement" in t or "artifact" in t:
return "movement"
if "stage 4" in t:
return "non-rem stage 3"
if "stage 3" in t:
return "non-rem stage 3"
if "stage 2" in t or "spindle" in t or "k-complex" in t:
return "non-rem stage 2"
if "stage 1" in t:
return "non-rem stage 1"
# Fallback: return as-is (lowercased); this may be marked OOV by allowed label set
return t
def extract_structured_data(obj: Dict) -> List[Dict]:
"""Extract structured per-sample data points from the Sleep JSON results object.
Returns a list of dicts with keys:
- generated
- model_prediction
- ground_truth
- accuracy (bool)
- f1_score, precision, recall
- prediction_normalized, ground_truth_normalized
"""
items = obj.get("detailed_results", [])
data_points: List[Dict] = []
for it in items:
metrics = it.get("metrics", {}) or {}
gt_label = metrics.get("gt_label")
pred_label = metrics.get("pred_label")
# Fallback to parsing the textual answers if labels are missing
if not gt_label:
gt_label = extract_answer(it.get("target_answer", ""))
if not pred_label:
pred_label = extract_answer(it.get("generated_answer", ""))
ground_truth = canonicalize_sleep_label(normalize_label(gt_label))
model_prediction = canonicalize_sleep_label(normalize_label(pred_label))
generated = it.get("generated_answer", "")
# Binary exact-match accuracy on normalized labels handled in calculate_f1_score, but keep explicit flag
f1_result = calculate_f1_score(model_prediction, ground_truth)
accuracy = f1_result["f1_score"] == 1.0
data_point = {
"generated": generated,
"model_prediction": model_prediction,
"ground_truth": ground_truth,
"accuracy": accuracy,
"f1_score": f1_result["f1_score"],
"precision": f1_result["precision"],
"recall": f1_result["recall"],
"prediction_normalized": f1_result["prediction_normalized"],
"ground_truth_normalized": f1_result["ground_truth_normalized"],
}
data_points.append(data_point)
return data_points
def main():
ap = argparse.ArgumentParser(
description="Compute accuracy and F1 from a Sleep baseline results JSON (with detailed_results)."
)
ap.add_argument(
"--detailed-json",
type=Path,
required=True,
help="Path to a single results JSON file containing 'detailed_results'",
)
ap.add_argument(
"--clean-out",
type=Path,
help="Optional path to write clean JSONL of parsed per-sample points",
)
args = ap.parse_args()
with args.detailed_json.open("r", encoding="utf-8") as f:
obj = json.load(f)
# Extract per-sample points
data_points = extract_structured_data(obj)
# Print high-level info if available
model_name = obj.get("model_name")
dataset_name = obj.get("dataset_name")
total_samples = obj.get("total_samples")
top_metrics = obj.get("metrics", {}) or {}
if model_name or dataset_name or total_samples is not None:
print("\nRun Metadata:")
if model_name:
print(f"Model: {model_name}")
if dataset_name:
print(f"Dataset: {dataset_name}")
if total_samples is not None:
print(f"Total samples (reported): {total_samples}")
if "accuracy" in top_metrics:
print(f"Reported accuracy: {top_metrics['accuracy']}")
# Accuracy stats (computed from per-sample)
accuracy_stats = calculate_accuracy_stats(data_points)
print(f"\nAccuracy Statistics:")
print(f"Total samples: {accuracy_stats.get('total_samples', 0)}")
print(f"Correct predictions: {accuracy_stats.get('correct_predictions', 0)}")
print(f"Incorrect predictions: {accuracy_stats.get('incorrect_predictions', 0)}")
print(f"Accuracy: {accuracy_stats.get('accuracy_percentage', 0.0):.2f}%")
# Build allowed label set from canonical SleepEDF labels (lowercased)
ALLOWED_SLEEP_LABELS = {
"wake",
"non-rem stage 1",
"non-rem stage 2",
"non-rem stage 3",
"rem sleep",
"movement",
}
allowed_labels = ALLOWED_SLEEP_LABELS
# F1 stats with allowed labels to prevent OOV classes from polluting per-class metrics
f1_stats = calculate_f1_stats(data_points, allowed_labels=allowed_labels)
print(f"\nF1 Score Statistics:")
print(f"Average F1 Score: {f1_stats.get('average_f1', 0.0):.4f}")
print(f"Macro-F1 Score: {f1_stats.get('macro_f1', 0.0):.4f}")
print(f"Total Classes: {f1_stats.get('total_classes', 0)}")
if f1_stats.get("class_f1_scores"):
print(f"\nPer-Class F1 Scores:")
for class_name, scores in f1_stats["class_f1_scores"].items():
print(
f" {class_name}: F1={scores['f1']:.4f}, "
f"P={scores['precision']:.4f}, R={scores['recall']:.4f}"
)
# Optional clean JSONL output
if args.clean_out:
with args.clean_out.open("w", encoding="utf-8") as f:
for item in data_points:
f.write(json.dumps(item, indent=2) + "\n")
print(f"\nData saved to {args.clean_out}")
if __name__ == "__main__":
main()
|