Skip to content

[BTY-39] MedTok: support MIMIC-IV versioned ICD codes - #1633

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
irvifa:medtok-mimic4
Nov 27, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
irvifa:medtok-mimic4

Conversation

@irvifa

@irvifa irvifa commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #1630 (BTY-39 MedTok). This PR lets apply_medtok work on MIMIC-IV.

MIMIC-IV's readmission, length-of-stay and drug recommendation tasks (ReadmissionPredictionMIMIC4, LengthOfStayPredictionMIMIC4, DrugRecommendationMIMIC4) write diagnosis and procedure codes with their ICD version as a prefix, such as 9_4280 and 10_E119. MedTok's keys are dotted and unprefixed, and the same digits can mean different codes in the two versions. For example, E8889 is ICD-9 E888.9 and ICD-10 E88.89. The prefix is the only reliable way to tell them apart.

Changes

  • pyhealth/models/medtok.py
    • medtok_keys gains two code systems, "ICD_DX" and "ICD_PROC". They read the 9_ / 10_ prefix and map the rest through ICD9CM/ICD10CM or ICD9PROC/ICD10PCS, which already existed.
    • A code without a valid prefix raises ValueError.
    • The special tokens <pad> and <unk> now map to no keys in every system. Without this, the new systems raised an error on the processors' own special tokens.
    • Otherwise the existing systems behave as before. The one change is that <pad> and <unk> under "RAW" now give [] rather than themselves.
  • examples/medtok_mimic.py (renamed from examples/medtok_mimic3.py; nothing else referenced the old name)
    • New --dataset mimic3|mimic4 option.
    • On MIMIC-IV it runs readmission, length of stay and drug recommendation, using {"conditions": "ICD_DX", "procedures": "ICD_PROC", "drugs": "NDC"}.
    • --task mortality with --dataset mimic4 stops with an explanation, because MortalityPredictionMIMIC4 drops the ICD version and its codes are ambiguous.
  • docs/api/models/pyhealth.models.MedTok.rst: documents the versioned systems, the mortality limitation and the MIMIC-IV example.
  • tests/core/test_medtok.py: three new tests:
    • versioned code mapping, including the E8889 case and malformed codes;
    • special tokens;
    • an end-to-end apply_medtok on MIMIC-IV-style versioned samples.

Links to other components

  • examples/medtok_mimic.py uses MIMIC4EHRDataset and the three MIMIC-IV tasks above. These are existing PyHealth components and are not modified. They are used because they are the MIMIC-IV tasks that keep the ICD version.
  • medtok_keys reuses the ICD9CM, ICD10CM, ICD9PROC and ICD10PCS handling already in medtok.py, and the existing NDC→ATC mapping through pyhealth.medcode.CrossMap.

Results

These results come from the open MIMIC-IV Clinical Database Demo 2.2 (100 patients, PhysioNet). They use a Transformer, 5 epochs and seeds 0–2, and report the test-set mean (std):

Task (samples) Metric Learned embeddings MedTok
Drug recommendation (132) AUPRC 0.358 (0.016) 0.609 (0.010)
AUROC 0.662 0.813
Jaccard 0.224 0.322
Readmission (108) AUROC 0.479 (0.054) 0.600 (0.128)
AUPRC 0.324 0.362

MedTok coverage (distinct codes): conditions 77%, procedures 74%, drugs (NDC→ATC) 91%, drug history (ATC) 98%.

Limitations:

  • The demo is far too small to support conclusions. The readmission difference is within the noise.
  • Length of stay does not run on the demo: some of the 10 classes are missing from the test split, so the multiclass metrics cannot be computed. This is a limit of the dataset's size, not of the MedTok code.
  • I have no credentialed MIMIC-IV access, so these numbers do not reproduce the paper's MIMIC-IV results.

Testing

pytest tests/core/test_medtok.py      # 23 passed
pytest --doctest-modules pyhealth/models/medtok.py

pyhealth/models/medtok.py coverage: 99%.

MIMIC-IV's readmission, length-of-stay and drug recommendation tasks
write diagnosis and procedure codes with an ICD version prefix ("9_4280",
"10_E119"). Without the prefix the code is ambiguous: E8889 is both
ICD-9 E888.9 and ICD-10 E88.89.

- medtok_keys: add the "ICD_DX" and "ICD_PROC" code systems, which read
  the 9_/10_ prefix and map to ICD9CM/ICD10CM or ICD9PROC/ICD10PCS.
  Malformed codes raise ValueError. Special tokens (<pad>, <unk>) map to
  no MedTok keys in every system.
- examples: rename medtok_mimic3.py to medtok_mimic.py and add
  --dataset mimic3|mimic4. MIMIC-IV runs readmission, length of stay and
  drug recommendation. Mortality is rejected because
  MortalityPredictionMIMIC4 drops the ICD version.
- docs: describe the versioned systems and the MIMIC-IV example.
- tests: versioned code mapping, special tokens, and an end-to-end
  apply_medtok on MIMIC-IV-style samples.

Signed-off-by: Irvi Aini <irvi.fa@gmail.com>
Signed-off-by: Irvi Aini <irvi.fa@gmail.com>
@irvifa irvifa changed the title MedTok: support MIMIC-IV versioned ICD codes [BTY-39] MedTok: support MIMIC-IV versioned ICD codes Nov 27, 2026
@irvifa
irvifa marked this pull request as ready for review November 27, 2026 11:33
@github-actions github-actions Bot mentioned this pull request Nov 27, 2026
@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 27, 2026
@irvifa

irvifa commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

/review

@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 87.03% (49978 / 57425 lines)

Modified file Coverage Lines
pyhealth/models/medtok.py 99.51% 202 / 203

Workflow run · 85a9052

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 85a9052


# MIMIC-IV: samples from a task that keeps the ICD version, such as
# ReadmissionPredictionMIMIC4 (codes like "9_4280" and "10_E119")
model4 = Transformer(mimic4_samples, embedding_dim=128)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] Docs example uses an undefined name

The new MIMIC-IV snippet calls Transformer(mimic4_samples, ...), but nothing in the snippet defines mimic4_samples, so a reader copying it gets a NameError. Define it in the snippet (for example, from set_task on MIMIC4EHRDataset with ReadmissionPredictionMIMIC4) or say where it comes from.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: approve

Thank you for this contribution. It lets MedTok work on MIMIC-IV by adding the versioned ICD_DX and ICD_PROC code systems, which keep ICD-9 and ICD-10 codes with the same digits apart. It also adds a --dataset option to the example script and documents the MIMIC-IV mortality limitation.

It looks ready to merge. The change stays inside the MedTok module, and the example script uses existing MIMIC-IV components that your description names. The tests cover versioned mapping, malformed codes, special tokens and an end-to-end apply_medtok run. Coverage is 99.5% for pyhealth/models/medtok.py (bar: 80%) and 87% for the whole repository (bar: 60%). The change to <pad> and <unk> under "RAW" is stated in the description.

Optional suggestions:

  • The docs snippet uses mimic4_samples without defining it. A one-line comment or a short set_task example would make it clearer.
  • You could shorten the long module docstring in the example script.

Reviewed commit 85a9052

@solarsys
solarsys merged commit 9627afa into sunlabuiuc:master Nov 27, 2026
3 checks passed
solarsys added a commit to solarsys/PyHealth that referenced this pull request Nov 27, 2026
@irvifa
irvifa deleted the medtok-mimic4 branch November 27, 2026 12:34
NathanCYee pushed a commit to NathanCYee/PyHealth that referenced this pull request Nov 28, 2026
* MedTok: support MIMIC-IV versioned ICD codes

MIMIC-IV's readmission, length-of-stay and drug recommendation tasks
write diagnosis and procedure codes with an ICD version prefix ("9_4280",
"10_E119"). Without the prefix the code is ambiguous: E8889 is both
ICD-9 E888.9 and ICD-10 E88.89.

- medtok_keys: add the "ICD_DX" and "ICD_PROC" code systems, which read
  the 9_/10_ prefix and map to ICD9CM/ICD10CM or ICD9PROC/ICD10PCS.
  Malformed codes raise ValueError. Special tokens (<pad>, <unk>) map to
  no MedTok keys in every system.
- examples: rename medtok_mimic3.py to medtok_mimic.py and add
  --dataset mimic3|mimic4. MIMIC-IV runs readmission, length of stay and
  drug recommendation. Mortality is rejected because
  MortalityPredictionMIMIC4 drops the ICD version.
- docs: describe the versioned systems and the MIMIC-IV example.
- tests: versioned code mapping, special tokens, and an end-to-end
  apply_medtok on MIMIC-IV-style samples.

Signed-off-by: Irvi Aini <irvi.fa@gmail.com>

* MedTok docs: define model4 in the MIMIC-IV example

Signed-off-by: Irvi Aini <irvi.fa@gmail.com>

---------

Signed-off-by: Irvi Aini <irvi.fa@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants