#!/usr/bin/env python3 """ Reproducible Layer 2 MIMIC-IV Demo 2.2 Evaluation Guide (Zero ODbL Redistribution) Because MIMIC-IV Clinical Database Demo 2.2 is licensed under Open Data Commons Open Database License (ODbL) via PhysioNet, CaleraLabs/medcodebench does NOT redistribute MIMIC-IV rows. Researchers can independently download official MIMIC-IV Demo 2.2 from PhysioNet and score any open-weights model identically: 1. Download official PhysioNet MIMIC-IV Clinical Database Demo v2.2: https://physionet.org/content/mimic-iv-demo/2.2/ Files required: hosp/diagnoses_icd.csv.gz, hosp/d_icd_diagnoses.csv.gz, hosp/admissions.csv.gz, hosp/patients.csv.gz 2. Filter to ICD-10-CM admissions (icd_version == 10), yielding 123 inpatient admissions and 734 gold ICD-10-CM codes. 3. Prompt any model with temperature=0.0 using strict JSON schema: {"principal_dx": "", "icd10_cm_codes": ["..."]} 4. Score exact ICD-10-CM micro-F1, category (3-char) micro-F1, Principal Dx exact accuracy, and Non-CMS Fabrication Rate (predicted codes absent from the official US CMS FY2026 ICD-10-CM 98,780-code schedule). """ from __future__ import annotations import json import re from typing import Iterable, Set, Tuple def normalize_icd10_cm(code: str) -> str: s = str(code).strip().upper() if s.startswith("ICD:"): s = s[4:].strip() s = re.sub(r"\s+", "", s) if len(s) > 3 and "." not in s and re.match(r"^[A-Z][0-9A-Z]{2,}$", s): s = f"{s[:3]}.{s[3:]}" return s def score_admission( predicted_codes: Iterable[str], gold_codes: Iterable[str], cms_valid_set: Set[str], ) -> Tuple[int, int, int, int, int]: """Return (tp, fp, fn, total_pred, fabricated_non_cms).""" pred = {normalize_icd10_cm(c) for c in predicted_codes if str(c).strip()} gold = {normalize_icd10_cm(c) for c in gold_codes if str(c).strip()} tp = len(pred & gold) fp = len(pred - gold) fn = len(gold - pred) fabricated = sum(1 for c in pred if c not in cms_valid_set) return tp, fp, fn, len(pred), fabricated if __name__ == "__main__": print(json.dumps({ "benchmark": "MIMIC-IV Clinical Database Demo 2.2 (PhysioNet ODbL)", "url": "https://physionet.org/content/mimic-iv-demo/2.2/", "icd10_admissions": 123, "gold_icd10_codes": 734, "metrics": ["exact_micro_f1", "category_micro_f1", "principal_dx_acc", "non_cms_fabrication_rate"], }, indent=2))