Repository navigation
[BTY-39] MedTok: support MIMIC-IV versioned ICD codes - #1633
Conversation
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>
|
/review |
Test CoverageTotal: 87.03% (49978 / 57425 lines)
|
|
|
||
| # 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) |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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_sampleswithout 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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt
* 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>
Summary
Follow-up to #1630 (BTY-39 MedTok). This PR lets
apply_medtokwork 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 as9_4280and10_E119. MedTok's keys are dotted and unprefixed, and the same digits can mean different codes in the two versions. For example,E8889is ICD-9 E888.9 and ICD-10 E88.89. The prefix is the only reliable way to tell them apart.Changes
pyhealth/models/medtok.pymedtok_keysgains two code systems,"ICD_DX"and"ICD_PROC". They read the9_/10_prefix and map the rest through ICD9CM/ICD10CM or ICD9PROC/ICD10PCS, which already existed.ValueError.<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.<pad>and<unk>under"RAW"now give[]rather than themselves.examples/medtok_mimic.py(renamed fromexamples/medtok_mimic3.py; nothing else referenced the old name)--dataset mimic3|mimic4option.{"conditions": "ICD_DX", "procedures": "ICD_PROC", "drugs": "NDC"}.--task mortalitywith--dataset mimic4stops with an explanation, becauseMortalityPredictionMIMIC4drops 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:apply_medtokon MIMIC-IV-style versioned samples.Links to other components
examples/medtok_mimic.pyusesMIMIC4EHRDatasetand 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_keysreuses the ICD9CM, ICD10CM, ICD9PROC and ICD10PCS handling already inmedtok.py, and the existing NDC→ATC mapping throughpyhealth.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):
MedTok coverage (distinct codes): conditions 77%, procedures 74%, drugs (NDC→ATC) 91%, drug history (ATC) 98%.
Limitations:
Testing
pyhealth/models/medtok.pycoverage: 99%.