dataset: publish clean-room reproduce_mimic_eval.py for CaleraLabs/medcodebench
Browse files- reproduce_mimic_eval.py +62 -0
reproduce_mimic_eval.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Reproducible Layer 2 MIMIC-IV Demo 2.2 Evaluation Guide (Zero ODbL Redistribution)
|
| 4 |
+
|
| 5 |
+
Because MIMIC-IV Clinical Database Demo 2.2 is licensed under Open Data Commons
|
| 6 |
+
Open Database License (ODbL) via PhysioNet, CaleraLabs/medcodebench does NOT
|
| 7 |
+
redistribute MIMIC-IV rows. Researchers can independently download official
|
| 8 |
+
MIMIC-IV Demo 2.2 from PhysioNet and score any open-weights model identically:
|
| 9 |
+
|
| 10 |
+
1. Download official PhysioNet MIMIC-IV Clinical Database Demo v2.2:
|
| 11 |
+
https://physionet.org/content/mimic-iv-demo/2.2/
|
| 12 |
+
Files required: hosp/diagnoses_icd.csv.gz, hosp/d_icd_diagnoses.csv.gz,
|
| 13 |
+
hosp/admissions.csv.gz, hosp/patients.csv.gz
|
| 14 |
+
2. Filter to ICD-10-CM admissions (icd_version == 10), yielding 123 inpatient
|
| 15 |
+
admissions and 734 gold ICD-10-CM codes.
|
| 16 |
+
3. Prompt any model with temperature=0.0 using strict JSON schema:
|
| 17 |
+
{"principal_dx": "<ICD-10-CM>", "icd10_cm_codes": ["..."]}
|
| 18 |
+
4. Score exact ICD-10-CM micro-F1, category (3-char) micro-F1, Principal Dx
|
| 19 |
+
exact accuracy, and Non-CMS Fabrication Rate (predicted codes absent from
|
| 20 |
+
the official US CMS FY2026 ICD-10-CM 98,780-code schedule).
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import json
|
| 26 |
+
import re
|
| 27 |
+
from typing import Iterable, Set, Tuple
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def normalize_icd10_cm(code: str) -> str:
|
| 31 |
+
s = str(code).strip().upper()
|
| 32 |
+
if s.startswith("ICD:"):
|
| 33 |
+
s = s[4:].strip()
|
| 34 |
+
s = re.sub(r"\s+", "", s)
|
| 35 |
+
if len(s) > 3 and "." not in s and re.match(r"^[A-Z][0-9A-Z]{2,}$", s):
|
| 36 |
+
s = f"{s[:3]}.{s[3:]}"
|
| 37 |
+
return s
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def score_admission(
|
| 41 |
+
predicted_codes: Iterable[str],
|
| 42 |
+
gold_codes: Iterable[str],
|
| 43 |
+
cms_valid_set: Set[str],
|
| 44 |
+
) -> Tuple[int, int, int, int, int]:
|
| 45 |
+
"""Return (tp, fp, fn, total_pred, fabricated_non_cms)."""
|
| 46 |
+
pred = {normalize_icd10_cm(c) for c in predicted_codes if str(c).strip()}
|
| 47 |
+
gold = {normalize_icd10_cm(c) for c in gold_codes if str(c).strip()}
|
| 48 |
+
tp = len(pred & gold)
|
| 49 |
+
fp = len(pred - gold)
|
| 50 |
+
fn = len(gold - pred)
|
| 51 |
+
fabricated = sum(1 for c in pred if c not in cms_valid_set)
|
| 52 |
+
return tp, fp, fn, len(pred), fabricated
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
print(json.dumps({
|
| 57 |
+
"benchmark": "MIMIC-IV Clinical Database Demo 2.2 (PhysioNet ODbL)",
|
| 58 |
+
"url": "https://physionet.org/content/mimic-iv-demo/2.2/",
|
| 59 |
+
"icd10_admissions": 123,
|
| 60 |
+
"gold_icd10_codes": 734,
|
| 61 |
+
"metrics": ["exact_micro_f1", "category_micro_f1", "principal_dx_acc", "non_cms_fabrication_rate"],
|
| 62 |
+
}, indent=2))
|