-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
358 lines (305 loc) · 11.1 KB
/
evaluate.py
File metadata and controls
358 lines (305 loc) · 11.1 KB
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
import argparse
import asyncio
import json
import statistics
import time
from dataclasses import dataclass
from pathlib import Path
import httpx
from rich.console import Console
from rich.panel import Panel
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
SpinnerColumn,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
)
from rich.table import Table
from rich.text import Text
@dataclass
class EvaluationResult:
protocol_id: str
accuracy_at_1: int # 1 or 0
recall_at_3: int # 1 or 0
latency_s: float
ground_truth: str
top_prediction: str
top_3_predictions: list[str]
response_json: dict
async def evaluate_single(
client: httpx.AsyncClient,
endpoint: str,
json_file: Path,
semaphore: asyncio.Semaphore,
) -> EvaluationResult:
"""Evaluate a single protocol against the endpoint."""
async with semaphore:
with open(json_file, "r") as f:
data = json.load(f)
protocol_id = data["protocol_id"]
query = data["query"]
ground_truth = data["gt"]
valid_icd_codes = set(data["icd_codes"])
if ground_truth not in valid_icd_codes:
raise ValueError(
f"Dataset error in {json_file.name}: gt '{ground_truth}' not in icd_codes"
)
start_time = time.perf_counter()
response = await client.post(endpoint, json={"symptoms": query})
latency_s = time.perf_counter() - start_time
response.raise_for_status()
result = response.json()
diagnoses = sorted(result["diagnoses"], key=lambda x: x["rank"])
top_3 = diagnoses[:3]
top_prediction = diagnoses[0]["icd10_code"] if diagnoses else ""
top_3_predictions = [d["icd10_code"] for d in top_3]
# Accuracy@1: does the rank 1 prediction match ground truth?
accuracy_at_1 = 1 if top_prediction == ground_truth else 0
# Recall@3: are any of the top 3 predictions in the valid icd_codes list?
recall_at_3 = (
1 if any(code in valid_icd_codes for code in top_3_predictions) else 0
)
return EvaluationResult(
protocol_id=protocol_id,
accuracy_at_1=accuracy_at_1,
recall_at_3=recall_at_3,
latency_s=latency_s,
ground_truth=ground_truth,
top_prediction=top_prediction,
top_3_predictions=top_3_predictions,
response_json=result,
)
async def run_evaluation(
endpoint: str,
dataset_dir: Path,
parallelism: int,
) -> list[EvaluationResult]:
"""Run evaluation on all JSON files in the dataset directory."""
console = Console()
json_files = list(dataset_dir.glob("*.json"))
if not json_files:
console.print(f"[red]No JSON files found in {dataset_dir}[/red]")
return []
console.print(
Panel(
f"[bold cyan]Diagnostic Accuracy Evaluation[/bold cyan]\n\n"
f"Endpoint: [yellow]{endpoint}[/yellow]\n"
f"Dataset: [yellow]{dataset_dir}[/yellow]\n"
f"Files: [yellow]{len(json_files)}[/yellow]\n"
f"Parallelism: [yellow]{parallelism}[/yellow]",
title="[bold white]Configuration[/bold white]",
border_style="cyan",
)
)
semaphore = asyncio.Semaphore(parallelism)
results: list[EvaluationResult] = []
errors: list[tuple[Path, Exception]] = []
async with httpx.AsyncClient(timeout=120.0) as client:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(bar_width=40),
TaskProgressColumn(),
MofNCompleteColumn(),
TimeElapsedColumn(),
console=console,
) as progress:
task = progress.add_task(
"[cyan]Evaluating protocols...", total=len(json_files)
)
async def process_file(json_file: Path):
try:
result = await evaluate_single(
client, endpoint, json_file, semaphore
)
results.append(result)
except Exception as e:
errors.append((json_file, e))
finally:
progress.advance(task)
await asyncio.gather(*[process_file(f) for f in json_files])
if errors:
console.print(
f"\n[red]Encountered {len(errors)} errors during evaluation[/red]"
)
for path, err in errors[:5]:
console.print(f" [dim]• {path.name}: {err}[/dim]")
if len(errors) > 5:
console.print(f" [dim]... and {len(errors) - 5} more[/dim]")
return results
def compute_metrics(results: list[EvaluationResult]) -> dict:
"""Compute aggregated metrics from evaluation results."""
if not results:
return {}
total = len(results)
accuracy_at_1 = sum(r.accuracy_at_1 for r in results) / total * 100
recall_at_3 = sum(r.recall_at_3 for r in results) / total * 100
latencies = [r.latency_s for r in results]
avg_latency = statistics.mean(latencies)
min_latency = min(latencies)
max_latency = max(latencies)
p50_latency = statistics.median(latencies)
if total >= 4:
quantiles = statistics.quantiles(latencies, n=20)
p95_latency = quantiles[-1]
else:
p95_latency = max_latency
return {
"total_protocols": total,
"accuracy_at_1_percent": round(accuracy_at_1, 2),
"recall_at_3_percent": round(recall_at_3, 2),
"latency_avg_s": round(avg_latency, 3),
"latency_min_s": round(min_latency, 3),
"latency_max_s": round(max_latency, 3),
"latency_p50_s": round(p50_latency, 3),
"latency_p95_s": round(p95_latency, 3),
}
def write_jsonl(results: list[EvaluationResult], output_path: Path):
"""Write results to JSONL file."""
with open(output_path, "w") as f:
for r in results:
line = {
"protocol_id": r.protocol_id,
"response": r.response_json,
"scores": {
"accuracy_at_1": r.accuracy_at_1,
"recall_at_3": r.recall_at_3,
"latency_s": round(r.latency_s, 3),
"ground_truth": r.ground_truth,
"top_prediction": r.top_prediction,
"top_3_predictions": r.top_3_predictions,
},
}
f.write(json.dumps(line, ensure_ascii=False) + "\n")
def write_metrics_json(submission_name: str, metrics: dict, output_path: Path):
"""Write aggregated metrics to JSON file."""
output_data = {
"submission_name": submission_name,
**metrics,
}
with open(output_path, "w") as f:
json.dump(output_data, f, indent=2)
def display_summary(
results: list[EvaluationResult],
metrics: dict,
output_jsonl: Path,
output_json: Path,
console: Console,
):
"""Display a beautiful summary of the evaluation results."""
if not results:
console.print("[red]No results to display[/red]")
return
# Metrics table
metrics_table = Table(
title="[bold]Evaluation Metrics[/bold]",
show_header=True,
header_style="bold magenta",
border_style="cyan",
)
metrics_table.add_column("Metric", style="cyan", width=20)
metrics_table.add_column("Value", style="green", justify="right", width=15)
metrics_table.add_row("Accuracy@1", f"{metrics['accuracy_at_1_percent']:.2f}%")
metrics_table.add_row("Recall@3", f"{metrics['recall_at_3_percent']:.2f}%")
metrics_table.add_row(
"Total Protocols", f"[bold white]{metrics['total_protocols']}[/bold white]"
)
# Latency table
latency_table = Table(
title="[bold]Latency Statistics[/bold]",
show_header=True,
header_style="bold magenta",
border_style="cyan",
)
latency_table.add_column("Statistic", style="cyan", width=20)
latency_table.add_column("Value (s)", style="green", justify="right", width=15)
latency_table.add_row("Average", f"{metrics['latency_avg_s']:.3f}")
latency_table.add_row("Min", f"{metrics['latency_min_s']:.3f}")
latency_table.add_row("Max", f"{metrics['latency_max_s']:.3f}")
latency_table.add_row("P50 (Median)", f"{metrics['latency_p50_s']:.3f}")
latency_table.add_row("P95", f"{metrics['latency_p95_s']:.3f}")
console.print()
console.print(metrics_table)
console.print()
console.print(latency_table)
console.print()
success_text = Text()
success_text.append("✓ ", style="bold green")
success_text.append("Results saved to:\n", style="white")
success_text.append(f" JSONL: {output_jsonl}\n", style="bold cyan")
success_text.append(f" Metrics: {output_json}", style="bold cyan")
console.print(Panel(success_text, border_style="green"))
def main():
parser = argparse.ArgumentParser(
description="Evaluate diagnostic accuracy against an endpoint",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python main.py --endpoint http://localhost:8000/diagnose --dataset-dir ./data --name my_submission
python main.py -e http://api.example.com/diagnose -d ./protocols -n team_alpha -p 10
""",
)
parser.add_argument(
"-n",
"--name",
required=True,
help="Submission/project name (used for output file naming)",
)
parser.add_argument(
"-e",
"--endpoint",
required=True,
help="URL of the diagnostic endpoint",
)
parser.add_argument(
"-d",
"--dataset-dir",
required=True,
type=Path,
help="Directory containing JSON protocol files",
)
parser.add_argument(
"-p",
"--parallelism",
type=int,
default=2,
help="Number of simultaneous requests (default: 2)",
)
parser.add_argument(
"-o",
"--output-dir",
type=Path,
default=Path("data/evals"),
help="Output directory for results (default: data/evals)",
)
args = parser.parse_args()
console = Console()
if not args.dataset_dir.exists():
console.print(
f"[red]Error: Dataset directory '{args.dataset_dir}' does not exist[/red]"
)
return 1
if not args.dataset_dir.is_dir():
console.print(f"[red]Error: '{args.dataset_dir}' is not a directory[/red]")
return 1
args.output_dir.mkdir(parents=True, exist_ok=True)
results = asyncio.run(
run_evaluation(
endpoint=args.endpoint,
dataset_dir=args.dataset_dir,
parallelism=args.parallelism,
)
)
if results:
output_jsonl = args.output_dir / f"{args.name}.jsonl"
output_json = args.output_dir / f"{args.name}_metrics.json"
write_jsonl(results, output_jsonl)
metrics = compute_metrics(results)
write_metrics_json(args.name, metrics, output_json)
display_summary(results, metrics, output_jsonl, output_json, console)
return 0
if __name__ == "__main__":
exit(main())