forked from graphgeeks-lab/odsc-agentic-ai-summit-2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaml_extract_eval.py
More file actions
694 lines (559 loc) · 21.5 KB
/
Copy pathbaml_extract_eval.py
File metadata and controls
694 lines (559 loc) · 21.5 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
"""
Script to evaluate the accuracy of the FHIR extraction results.
Results from BAML: ../data/results/extracted_fhir.json
Gold standard: ../data/fhir.json
The script assumes that the results from the gold standard are in the same order as the results from BAML.
"""
import argparse
import json
from dataclasses import dataclass
from textwrap import dedent
from typing import Any, Dict, List, Optional, Tuple
# Add a mapping of state abbreviations to full state names
STATE_ABBR_TO_NAME = {
"AL": "Alabama",
"AK": "Alaska",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California",
"CO": "Colorado",
"CT": "Connecticut",
"DE": "Delaware",
"FL": "Florida",
"GA": "Georgia",
"HI": "Hawaii",
"ID": "Idaho",
"IL": "Illinois",
"IN": "Indiana",
"IA": "Iowa",
"KS": "Kansas",
"KY": "Kentucky",
"LA": "Louisiana",
"ME": "Maine",
"MD": "Maryland",
"MA": "Massachusetts",
"MI": "Michigan",
"MN": "Minnesota",
"MS": "Mississippi",
"MO": "Missouri",
"MT": "Montana",
"NE": "Nebraska",
"NV": "Nevada",
"NH": "New Hampshire",
"NJ": "New Jersey",
"NM": "New Mexico",
"NY": "New York",
"NC": "North Carolina",
"ND": "North Dakota",
"OH": "Ohio",
"OK": "Oklahoma",
"OR": "Oregon",
"PA": "Pennsylvania",
"RI": "Rhode Island",
"SC": "South Carolina",
"SD": "South Dakota",
"TN": "Tennessee",
"TX": "Texas",
"UT": "Utah",
"VT": "Vermont",
"VA": "Virginia",
"WA": "Washington",
"WV": "West Virginia",
"WI": "Wisconsin",
"WY": "Wyoming",
}
@dataclass
class NameComparison:
"""Represents a name comparison between results and gold standard"""
family_match: bool
given_match: bool
prefix_match: bool
overall_match: bool
record_id: int # Store the record ID for failed matches
@dataclass
class AddressComparison:
"""Represents an address comparison between results and gold standard"""
line_match: bool
city_match: bool
state_match: bool
postal_code_match: bool
country_match: bool
overall_match: bool
record_id: int # Store the record ID for failed matches
@dataclass
class SimpleFieldComparison:
"""Represents simple field comparisons between results and gold standard"""
gender_match: bool
birth_date_match: bool
marital_status_match: bool
overall_match: bool
record_id: int # Store the record ID for failed matches
# --- Utility Functions ---
def get_first(arr) -> Any:
if isinstance(arr, list) and arr:
return arr[0]
return None
def normalize_str(val) -> Optional[str]:
if val is None:
return None
if isinstance(val, list):
return get_first(val)
return str(val)
def compare_strict(a, b) -> bool:
return a == b
def compare_case_insensitive(a, b) -> bool:
if a is None or b is None:
return a == b
return str(a).lower() == str(b).lower()
def compare_substring(a, b) -> bool:
if a is None or b is None:
return a == b
a, b = str(a).lower(), str(b).lower()
return a in b or b in a
def normalize_fhir_name(fhir_name_array) -> Dict[str, Any]:
name = get_first(fhir_name_array) or {}
return {
"family": name.get("family"),
"given": name.get("given", []),
"prefix": get_first(name.get("prefix", [])),
}
def normalize_fhir_address(fhir_address_array) -> Dict[str, Any]:
address = get_first(fhir_address_array) or {}
return {
"line": get_first(address.get("line", [])),
"city": address.get("city"),
"state": address.get("state"),
"postalCode": address.get("postalCode"),
"country": address.get("country"),
}
def normalize_fhir_marital_status(fhir_marital_status) -> Optional[str]:
if not fhir_marital_status:
return None
text = fhir_marital_status.get("text")
if text:
return text.replace(" ", "")
coding = fhir_marital_status.get("coding", [])
if coding:
return coding[0].get("display", "").replace(" ", "")
return None
def combine_practitioner_name(practitioner_data: Dict[str, Any]) -> str:
"""Combine prefix, given, and family names from practitioner data"""
if not practitioner_data or not isinstance(practitioner_data.get("name"), dict):
return ""
name = practitioner_data["name"]
prefix = name.get("prefix", [])
given = name.get("given", [])
family = name.get("family")
# Handle prefix as array (FHIR standard)
if prefix and isinstance(prefix, list):
prefix = get_first(prefix)
# Handle given as array (FHIR standard)
if given and isinstance(given, list):
given = given # Keep as list to extend later
elif given and not isinstance(given, list):
given = [given]
else:
given = []
# Build the full name
parts = []
if prefix:
parts.append(prefix)
if given:
parts.extend(given)
if family:
parts.append(family)
return " ".join(parts).strip().lower()
# --- Generalized Evaluation ---
def parse_fhir_bundle(fhir_json_str: str) -> Dict[str, Any]:
if isinstance(fhir_json_str, dict):
return fhir_json_str
return json.loads(fhir_json_str)
def extract_patient_from_bundle(bundle: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if bundle.get("resourceType") != "Bundle":
return None
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Patient":
return resource
return None
def extract_allergy_count_from_bundle(bundle: Dict[str, Any]) -> int:
"""Extract count of AllergyIntolerance resources from FHIR bundle"""
if bundle.get("resourceType") != "Bundle":
return 0
count = 0
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "AllergyIntolerance":
count += 1
return count
def extract_immunization_count_from_bundle(bundle: Dict[str, Any]) -> int:
"""
Extract count of Immunization resources from FHIR bundle, and also count Procedure resources
mentioning vaccines/immunizations.
"""
if bundle.get("resourceType") != "Bundle":
return 0
count = 0
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Immunization":
count += 1
elif resource.get("resourceType") == "Procedure":
code = resource.get("code") or {}
text = code.get("text", "") or ""
if "vaccine" in text.lower() or "immunization" in text.lower():
count += 1
continue
for coding in code.get("coding", []):
display = coding.get("display", "") or ""
if "vaccine" in display.lower() or "immunization" in display.lower():
count += 1
break
return count
def extract_immunization_status_from_bundle(bundle: Dict[str, Any]) -> List[str]:
"""Extract status of all Immunization resources from FHIR bundle"""
if bundle.get("resourceType") != "Bundle":
return []
statuses = []
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Immunization":
status = resource.get("status")
if status:
statuses.append(status)
return statuses
def extract_immunization_dates_from_bundle(bundle: Dict[str, Any]) -> List[str]:
"""Extract occurrenceDateTime of all Immunization resources from FHIR bundle"""
if bundle.get("resourceType") != "Bundle":
return []
dates = []
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Immunization":
# Check both occurrenceDateTime and occurrenceString
date_time = resource.get("occurrenceDateTime")
date_string = resource.get("occurrenceString")
if date_time:
dates.append(date_time)
elif date_string:
dates.append(date_string)
return dates
def extract_all_practitioners_from_bundle(bundle: Dict[str, Any]) -> list:
practitioners = set()
if bundle.get("resourceType") != "Bundle":
return list(practitioners)
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Encounter":
participants = resource.get("participant", []) or []
for participant in participants:
individual = participant.get("individual", {})
display = individual.get("display", "")
if display:
practitioners.add(display.lower())
# Practitioner resources as before
for entry in bundle.get("entry", []):
resource = entry.get("resource", {})
if resource.get("resourceType") == "Practitioner":
name_array = resource.get("name", [])
if name_array:
# Handle name as array (FHIR standard) - take the first name
name = get_first(name_array)
if name:
combined = combine_practitioner_name({"name": name})
if combined:
practitioners.add(combined)
# print("DEBUG: Practitioners:", list(practitioners))
return list(practitioners)
def read_n_lines(file_path: str, n: int) -> list:
"""Read up to n lines from a file and return as a list."""
lines = []
with open(file_path, "r") as f:
for i, line in enumerate(f):
if i >= n:
break
lines.append(line)
return lines
def evaluate_fields(
eval_file_path: str, results_file_path: str, field_map: Dict[str, Dict[str, Any]]
) -> Tuple[Dict[str, int], int, Dict[str, list], int, int]:
"""
field_map: {
field_name: {
'extract_fhir': Callable[[Dict], Any],
'extract_result': Callable[[Dict], Any],
'compare': Callable[[Any, Any], bool]
}
}
"""
stats = {k: 0 for k in field_map}
total = 0
failed_records = {k: [] for k in field_map}
total_field_comparisons = 0
total_failed_comparisons = 0
with open(results_file_path, "r") as results_file:
results_data_list = json.load(results_file)
with open(eval_file_path, "r") as eval_file:
eval_data_list = json.load(eval_file)
# Truncate the eval_data_list to the same length as the results_data_list
# This logic will break if the results are out of order, or if the results from BAML begin
# at a record_id other than 1.
eval_data_list = eval_data_list[: len(results_data_list)]
for line_num, (eval_data, results_data) in enumerate(zip(eval_data_list, results_data_list), 1):
fhir_bundle = parse_fhir_bundle(eval_data)
patient = extract_patient_from_bundle(fhir_bundle)
if not patient:
continue
for field, ops in field_map.items():
total_field_comparisons += 1
if (
field.startswith("practitioner")
or field.startswith("allergy")
or field.startswith("immunization")
):
# For practitioner, allergy, and immunization fields, use bundle data
fhir_val = ops["extract_fhir"](fhir_bundle)
else:
# For patient fields, use patient data
fhir_val = ops["extract_fhir"](patient)
result_val = ops["extract_result"](results_data)
if ops["compare"](result_val, fhir_val):
stats[field] += 1
else:
failed_records[field].append(line_num)
total_failed_comparisons += 1
total += 1
return stats, total, failed_records, total_field_comparisons, total_failed_comparisons
# --- Field Extractor Functions ---
def extract_family_fhir(patient: Dict[str, Any]) -> Any:
family = normalize_fhir_name(patient.get("name", []))["family"]
return family.lower() if family else None
def extract_family_result(result: Dict[str, Any]) -> Any:
family = result.get("name", {}).get("family") if result.get("name") else None
return family.lower() if family else None
def extract_given_fhir(patient: Dict[str, Any]) -> Any:
given = normalize_fhir_name(patient.get("name", []))["given"]
if isinstance(given, list):
return [g.lower() if g else None for g in given]
return given.lower() if given else None
def extract_given_result(result: Dict[str, Any]) -> Any:
given = result.get("name", {}).get("given") if result.get("name") else None
if isinstance(given, list):
return [g.lower() if g else None for g in given]
return given.lower() if given else None
def extract_prefix_fhir(patient: Dict[str, Any]) -> Any:
prefix = normalize_fhir_name(patient.get("name", []))["prefix"]
return prefix.lower() if prefix else None
def extract_prefix_result(result: Dict[str, Any]) -> Any:
prefix = result.get("name", {}).get("prefix") if result.get("name") else None
return prefix.lower() if prefix else None
def extract_line_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_address(patient.get("address", []))["line"]
def extract_line_result(result: Dict[str, Any]) -> Any:
return result.get("address", {}).get("line") if result.get("address") else None
def extract_city_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_address(patient.get("address", []))["city"]
def extract_city_result(result: Dict[str, Any]) -> Any:
return result.get("address", {}).get("city") if result.get("address") else None
def extract_state_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_address(patient.get("address", []))["state"]
def extract_state_result(result: Dict[str, Any]) -> Any:
state = result.get("address", {}).get("state") if result.get("address") else None
if state is None:
return None
# Normalize to uppercase for lookup
state_upper = str(state).strip().upper()
return STATE_ABBR_TO_NAME.get(state_upper, state)
def extract_postalCode_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_address(patient.get("address", []))["postalCode"]
def extract_postalCode_result(result: Dict[str, Any]) -> Any:
return result.get("address", {}).get("postalCode") if result.get("address") else None
def extract_country_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_address(patient.get("address", []))["country"]
def extract_country_result(result: Dict[str, Any]) -> Any:
return result.get("address", {}).get("country") if result.get("address") else None
def extract_gender_fhir(patient: Dict[str, Any]) -> str | None:
return patient["gender"].lower() if patient["gender"] else None
def extract_gender_result(result: Dict[str, Any]) -> str | None:
return result["gender"].lower() if result["gender"] else None
def extract_birthDate_fhir(patient: Dict[str, Any]) -> Any:
return patient.get("birthDate")
def extract_birthDate_result(result: Dict[str, Any]) -> Any:
return result.get("birthDate")
def extract_maritalStatus_fhir(patient: Dict[str, Any]) -> Any:
return normalize_fhir_marital_status(patient.get("maritalStatus"))
def extract_maritalStatus_result(result: Dict[str, Any]) -> Any:
return result.get("maritalStatus")
def extract_practitioner_fhir(bundle: Dict[str, Any]) -> Any:
return extract_all_practitioners_from_bundle(bundle)
def extract_practitioner_result(result: Dict[str, Any]) -> Any:
return (
combine_practitioner_name(result.get("practitioner", {}))
if result.get("practitioner")
else None
)
def compare_practitioner(result: Any, fhir_list: Any) -> bool:
return (
result in fhir_list
if fhir_list and result
else (result is None or result == "")
and (not fhir_list or all(not item for item in fhir_list))
)
def extract_allergyRecordedCount_fhir(bundle: Dict[str, Any]) -> int:
return extract_allergy_count_from_bundle(bundle)
def extract_allergyRecordedCount_result(result: Dict[str, Any]) -> int:
allergy = result.get("allergy") or {}
substance = allergy.get("substance") or []
return len(substance) if substance else 0
def extract_immunizationCount_fhir(bundle: Dict[str, Any]) -> int:
return extract_immunization_count_from_bundle(bundle)
def extract_immunizationCount_result(result: Dict[str, Any]) -> int:
return len(result.get("immunization") or [])
def extract_immunizationDate_fhir(bundle: Dict[str, Any]) -> List[str]:
return extract_immunization_dates_from_bundle(bundle)
def extract_immunizationDate_result(result: Dict[str, Any]) -> List[str]:
return [
imm.get("occurrenceDateTime") or imm.get("occurrenceString")
for imm in (result.get("immunization") or [])
if imm.get("occurrenceDateTime") or imm.get("occurrenceString")
]
def compare_immunizationDate(result: List[str], fhir_list: List[str]) -> bool:
return (
result[0] in fhir_list
if result and fhir_list
else (not result or not result[0])
and (not fhir_list or all(not item for item in fhir_list))
)
# --- Field Map Definitions ---
FIELD_MAP = {
# Name fields
"family": {
"extract_fhir": extract_family_fhir,
"extract_result": extract_family_result,
"compare": compare_strict,
},
"given": {
"extract_fhir": extract_given_fhir,
"extract_result": extract_given_result,
"compare": compare_strict,
},
"prefix": {
"extract_fhir": extract_prefix_fhir,
"extract_result": extract_prefix_result,
"compare": compare_strict,
},
# Address fields
"line": {
"extract_fhir": extract_line_fhir,
"extract_result": extract_line_result,
"compare": compare_strict,
},
"city": {
"extract_fhir": extract_city_fhir,
"extract_result": extract_city_result,
"compare": compare_strict,
},
"state": {
"extract_fhir": extract_state_fhir,
"extract_result": extract_state_result,
"compare": compare_strict,
},
"postalCode": {
"extract_fhir": extract_postalCode_fhir,
"extract_result": extract_postalCode_result,
"compare": compare_strict,
},
"country": {
"extract_fhir": extract_country_fhir,
"extract_result": extract_country_result,
"compare": compare_strict,
},
# Simple fields
"gender": {
"extract_fhir": extract_gender_fhir,
"extract_result": extract_gender_result,
"compare": compare_case_insensitive,
},
"birthDate": {
"extract_fhir": extract_birthDate_fhir,
"extract_result": extract_birthDate_result,
"compare": compare_strict,
},
"maritalStatus": {
"extract_fhir": extract_maritalStatus_fhir,
"extract_result": extract_maritalStatus_result,
"compare": compare_strict,
},
# Practitioner fields
"practitioner": {
"extract_fhir": extract_practitioner_fhir,
"extract_result": extract_practitioner_result,
"compare": compare_practitioner,
},
# Allergy fields
"allergyRecordedCount": {
"extract_fhir": extract_allergyRecordedCount_fhir,
"extract_result": extract_allergyRecordedCount_result,
"compare": compare_strict,
},
# Immunization fields
"immunizationCount": {
"extract_fhir": extract_immunizationCount_fhir,
"extract_result": extract_immunizationCount_result,
"compare": compare_strict,
},
"immunizationDate": {
"extract_fhir": extract_immunizationDate_fhir,
"extract_result": extract_immunizationDate_result,
"compare": compare_immunizationDate,
},
}
# --- Reporting ---
def print_field_stats(
stats: Dict[str, int],
total: int,
failed_records: Dict[str, list],
total_field_comparisons: int,
total_failed_comparisons: int,
) -> None:
print("=== INFORMATION EXTRACTION EVALUATION RESULTS ===\n")
print(f"Total Records: {total}")
for field, count in stats.items():
print(f"{field}: {count}/{total} ({count/total*100:.1f}%)")
if failed_records[field]:
# Print only the first 10 failed IDs for brevity
print(f" First 10 failed IDs: {failed_records[field][:10]}")
total_passed_comparisons = total_field_comparisons - total_failed_comparisons
print(
dedent(
f"""
Overall accuracy (across all fields):
{total_passed_comparisons}/{total_field_comparisons} ({total_passed_comparisons/total_field_comparisons*100:.1f}%)
"""
)
)
print(f"Total comparisons: {total_field_comparisons}")
print(f"Failed comparisons: {total_failed_comparisons}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Evaluate FHIR extraction results.")
parser.add_argument(
"--eval_file",
"-e",
type=str,
default="../data/fhir.json",
help="Path to the evaluation (gold standard) file.",
)
parser.add_argument(
"--results_file",
"-r",
type=str,
default="../data/extracted_fhir.json",
help="Path to the results file.",
)
args = parser.parse_args()
stats, total, failed_records, total_field_comparisons, total_failed_comparisons = (
evaluate_fields(args.eval_file, args.results_file, FIELD_MAP)
)
print_field_stats(
stats, total, failed_records, total_field_comparisons, total_failed_comparisons
)