Repository navigation
[BTY-39] MedTok: multimodal medical code tokenizer embeddings as a drop-in for PyHealth models #1630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[BTY-39] MedTok: multimodal medical code tokenizer embeddings as a drop-in for PyHealth models #1630
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3548312
[BTY-39] Add MedTok code embeddings as a drop-in for PyHealth models
irvifa e0b4b3a
Reject truncated or empty MedTok JSON; make --medtok optional for
irvifa 7e06705
Address AI review: strict
irvifa 479987b
Merge remote-tracking branch 'upstream/master' into bty-39-medtok
irvifa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| pyhealth.models.MedTok | ||
| ====================== | ||
|
|
||
| Overview | ||
| -------- | ||
|
|
||
| MedTok (Su et al., "Multimodal Medical Code Tokenizer", `ICML 2025 | ||
| <https://arxiv.org/abs/2502.04397>`_; `code <https://github.com/mims-harvard/MedTok>`_, | ||
| MIT licence) tokenizes a medical code from both its text description and its | ||
| relations in a biomedical knowledge graph, quantized into one shared codebook | ||
| of 21,000 tokens. The authors release, under the MIT licence on `Hugging Face | ||
| <https://huggingface.co/mims-harvard/MedTok>`_, the 20 tokens and the | ||
| 256-dimensional quantized embedding of about 616,000 codes (ICD-9, ICD-10-CM, | ||
| ICD-10-PCS, ATC, SNOMED CT, RxNorm and others). | ||
|
|
||
| This module plugs those released files into any PyHealth model built on | ||
| :class:`~pyhealth.models.EmbeddingModel`, the way the paper swaps the | ||
| tokenizer of existing EHR models: | ||
|
|
||
| 1. :func:`~pyhealth.models.apply_medtok` replaces a built model's code | ||
| embedding table, feature by feature, with a | ||
| :class:`~pyhealth.models.MedTokEmbedding`. | ||
| 2. ``mode="embedding"`` (the default, as the official EHR code) looks codes up | ||
| in the frozen 256-d MedTok embeddings and learns a projection to the | ||
| model's size, plus one shared vector for codes MedTok does not cover. | ||
| ``mode="tokens"`` embeds a code as the mean of learned embeddings of its | ||
| MedTok tokens. | ||
| 3. :func:`~pyhealth.models.medtok_keys` converts dataset codes to MedTok's | ||
| dotted keys (MIMIC stores ICD-9 ``428.0`` as ``4280``) and maps NDC drugs | ||
| to ATC. | ||
|
|
||
| .. note:: | ||
|
|
||
| Download ``code2embeddings.json`` (4 GB) or ``code2tokens.json`` (186 MB) | ||
| from Hugging Face. :func:`~pyhealth.models.convert_medtok_embeddings` | ||
| turns the embeddings into a 630 MB ``.npz`` that loads in seconds. MedTok | ||
| does not cover every code. In the mortality task's samples on PyHealth's | ||
| synthetic MIMIC-III, 76% of the distinct diagnosis codes (74% of | ||
| diagnosis occurrences), 76% of procedure codes (72%) and 93% of drug codes | ||
| (90%) have a MedTok entry; :func:`~pyhealth.models.apply_medtok` returns | ||
| the distinct-code share for each feature. Some frequent codes, such as | ||
| ICD-9 ``401.9``, are missing and use the shared missing-code vector. | ||
| Training the tokenizer itself is not included. | ||
|
|
||
| Example | ||
| ------- | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from pyhealth.models import Transformer, apply_medtok, load_medtok | ||
|
|
||
| model = Transformer(samples, embedding_dim=128) | ||
| coverage = apply_medtok( | ||
| model, | ||
| "medtok.npz", | ||
| {"conditions": "ICD9CM", "procedures": "ICD9PROC", "drugs": "NDC"}, | ||
| ) | ||
|
|
||
| API Reference | ||
| ------------- | ||
|
|
||
| .. autofunction:: pyhealth.models.medtok_keys | ||
|
|
||
| .. autofunction:: pyhealth.models.load_medtok | ||
|
|
||
| .. autofunction:: pyhealth.models.convert_medtok_embeddings | ||
|
|
||
| .. autofunction:: pyhealth.models.apply_medtok | ||
|
|
||
| .. autoclass:: pyhealth.models.MedTokEmbedding | ||
| :members: | ||
| :show-inheritance: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| """MedTok vs learned code embeddings on MIMIC-III (Su et al., ICML 2025). | ||
|
|
||
| MedTok's claim is that swapping an EHR model's code embeddings for MedTok's | ||
| improves its predictions. This script trains the same PyHealth model twice | ||
| per seed, once with its usual learned embeddings and once with MedTok's | ||
| (:func:`pyhealth.models.apply_medtok`), and reports the test metrics as mean | ||
| (std) over seeds. | ||
|
|
||
| 1. Load MIMIC-III and set one of PyHealth's tasks: in-hospital mortality, | ||
| readmission, length of stay or drug recommendation (the paper's tasks). | ||
| 2. Split by patient (70/10/20), with a different split for every seed. | ||
| 3. For each embedding, train with early stopping on the validation AUPRC (or | ||
| AUROC for length of stay) and score the best epoch on the test split. | ||
|
|
||
| Get the embeddings from https://huggingface.co/mims-harvard/MedTok | ||
| (``code2embeddings.json``, 4 GB) and convert them once to a 630 MB ``.npz``:: | ||
|
|
||
| python -c "from pyhealth.models import convert_medtok_embeddings as c; \\ | ||
| c('code2embeddings.json', 'medtok.npz')" | ||
|
|
||
| Usage:: | ||
|
|
||
| # Smoke test on the MIMIC-III demo bundled with the repository: | ||
| python examples/medtok_mimic3.py --medtok medtok.npz --demo --epochs 2 | ||
| # The full comparison: | ||
| python examples/medtok_mimic3.py --root /data/mimic-iii/1.4 \\ | ||
| --medtok medtok.npz --task mortality --seeds 0 1 2 | ||
|
|
||
| ``--embeddings tokens`` also runs MedTok's token mode, which needs | ||
| ``code2tokens.json``. | ||
| """ | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from pyhealth.datasets import MIMIC3Dataset, get_dataloader, split_by_patient | ||
| from pyhealth.models import ( | ||
| RETAIN, | ||
| RNN, | ||
| Transformer, | ||
| apply_medtok, | ||
| load_medtok, | ||
| medtok_keys, | ||
| ) | ||
| from pyhealth.tasks import ( | ||
| DrugRecommendationMIMIC3, | ||
| LengthOfStayPredictionMIMIC3, | ||
| MortalityPredictionMIMIC3, | ||
| ReadmissionPredictionMIMIC3, | ||
| ) | ||
| from pyhealth.trainer import Trainer | ||
|
|
||
| DEMO_ROOT = Path(__file__).resolve().parent.parent / "test-resources/core/mimic3demo" | ||
| CODES = {"conditions": "ICD9CM", "procedures": "ICD9PROC", "drugs": "NDC"} | ||
| TASKS = { | ||
| "mortality": (MortalityPredictionMIMIC3, CODES, ["roc_auc", "pr_auc"]), | ||
| "readmission": (ReadmissionPredictionMIMIC3, CODES, ["roc_auc", "pr_auc"]), | ||
| "los": ( | ||
| LengthOfStayPredictionMIMIC3, | ||
| CODES, | ||
| ["roc_auc_macro_ovr", "accuracy", "f1_macro"], | ||
| ), | ||
| # Drug recommendation's inputs and labels are ATC level-3 codes. | ||
| "drugrec": ( | ||
| DrugRecommendationMIMIC3, | ||
| {**CODES, "drugs_hist": "ATC", "drugs": None}, | ||
| ["pr_auc_samples", "roc_auc_samples", "jaccard_samples"], | ||
| ), | ||
| } | ||
| MODELS = {"transformer": Transformer, "rnn": RNN, "retain": RETAIN} | ||
|
|
||
|
|
||
| def parse_args(): | ||
| p = argparse.ArgumentParser(description=__doc__.split("\n")[0]) | ||
| p.add_argument("--root", help="MIMIC-III 1.4 folder (with ADMISSIONS.csv.gz).") | ||
| p.add_argument("--demo", action="store_true", help="Use the bundled demo.") | ||
| p.add_argument("--cache-dir", default=None) | ||
| p.add_argument( | ||
| "--medtok", help="medtok.npz or code2embeddings.json, for --embeddings medtok." | ||
| ) | ||
| p.add_argument("--tokens", help="code2tokens.json, for --embeddings tokens.") | ||
| p.add_argument("--task", choices=TASKS, default="mortality") | ||
| p.add_argument("--model", choices=MODELS, default="transformer") | ||
| p.add_argument( | ||
| "--embeddings", | ||
| nargs="+", | ||
| choices=["learned", "medtok", "tokens"], | ||
| default=["learned", "medtok"], | ||
| ) | ||
| p.add_argument("--seeds", type=int, nargs="+", default=[0, 1, 2]) | ||
| p.add_argument("--epochs", type=int, default=20) | ||
| p.add_argument("--batch-size", type=int, default=256) | ||
| p.add_argument("--lr", type=float, default=1e-3) | ||
| p.add_argument("--embedding-dim", type=int, default=128) | ||
| p.add_argument("--output", default="output/medtok_mimic3") | ||
| p.add_argument( | ||
| "--device", | ||
| default="cuda" | ||
| if torch.cuda.is_available() | ||
| else "mps" | ||
| if torch.backends.mps.is_available() | ||
| else "cpu", | ||
| ) | ||
| args = p.parse_args() | ||
| if not args.demo and not args.root: | ||
| p.error("pass --root or --demo") | ||
| if "tokens" in args.embeddings and not args.tokens: | ||
| p.error("--embeddings tokens needs --tokens code2tokens.json") | ||
| if "medtok" in args.embeddings and not args.medtok: | ||
| p.error("--embeddings medtok needs --medtok medtok.npz") | ||
| return args | ||
|
|
||
|
|
||
| def load_samples(args): | ||
| dataset = MIMIC3Dataset( | ||
| root=str(DEMO_ROOT) if args.demo else args.root, | ||
| tables=["diagnoses_icd", "procedures_icd", "prescriptions"], | ||
| cache_dir=args.cache_dir, | ||
| ) | ||
| return dataset.set_task(TASKS[args.task][0]()) | ||
|
|
||
|
|
||
| def build_model(samples, args, embeddings, medtok): | ||
| kwargs = {"embedding_dim": args.embedding_dim} | ||
| if args.model == "rnn": | ||
| kwargs["hidden_dim"] = args.embedding_dim | ||
| model = MODELS[args.model](samples, **kwargs) | ||
| if embeddings != "learned": | ||
| systems = { | ||
| k: v | ||
| for k, v in TASKS[args.task][1].items() | ||
| if v and k in model.feature_keys | ||
| } | ||
| source = medtok["tokens" if embeddings == "tokens" else "medtok"] | ||
| mode = "tokens" if embeddings == "tokens" else "embedding" | ||
| coverage = apply_medtok(model, source, systems, mode=mode) | ||
| print(f"MedTok coverage of each feature's codes: {coverage}") | ||
| return model | ||
|
|
||
|
|
||
| def run(samples, args, seed, embeddings, medtok): | ||
| torch.manual_seed(seed) | ||
| np.random.seed(seed) | ||
| train, val, test = split_by_patient(samples, [0.7, 0.1, 0.2], seed=seed) | ||
| loaders = [ | ||
| get_dataloader(d, batch_size=args.batch_size, shuffle=i == 0) | ||
| for i, d in enumerate((train, val, test)) | ||
| ] | ||
| model = build_model(samples, args, embeddings, medtok) | ||
| metrics = TASKS[args.task][2] | ||
| trainer = Trainer( | ||
| model=model, | ||
| metrics=metrics, | ||
| device=args.device, | ||
| output_path=args.output, | ||
| exp_name=f"{args.task}-{args.model}-{embeddings}-seed{seed}", | ||
| ) | ||
| trainer.train( | ||
| train_dataloader=loaders[0], | ||
| val_dataloader=loaders[1], | ||
| epochs=args.epochs, | ||
| optimizer_params={"lr": args.lr}, | ||
| monitor=metrics[0], | ||
| monitor_criterion="max", | ||
| load_best_model_at_last=True, | ||
| ) | ||
| return {k: float(v) for k, v in trainer.evaluate(loaders[2]).items()} | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
| samples = load_samples(args) | ||
| print(f"{len(samples)} samples for {args.task}") | ||
| if len(samples) == 0: | ||
| # e.g. readmission on the synthetic MIMIC-III, whose birth dates make | ||
| # every patient a minor, and the task drops minors. | ||
| raise SystemExit(f"{args.task} gave no samples on this data.") | ||
| # Read each MedTok file once, keeping only the codes this task uses. | ||
| systems = {k: v for k, v in TASKS[args.task][1].items() if v} | ||
| wanted = { | ||
| key | ||
| for feature, system in systems.items() | ||
| if feature in samples.input_processors | ||
| for code in getattr(samples.input_processors[feature], "code_vocab", {}) | ||
| for key in medtok_keys(code, system) | ||
| } | ||
| medtok = {"medtok": None, "tokens": None} | ||
| if "medtok" in args.embeddings: | ||
| medtok["medtok"] = load_medtok(args.medtok, wanted) | ||
| if "tokens" in args.embeddings: | ||
| medtok["tokens"] = load_medtok(args.tokens, wanted) | ||
|
|
||
| results = {e: [] for e in args.embeddings} | ||
| for seed in args.seeds: | ||
| for embeddings in args.embeddings: | ||
| scores = run(samples, args, seed, embeddings, medtok) | ||
| results[embeddings].append(scores) | ||
| print(f"seed {seed} {embeddings}: {json.dumps(scores)}", flush=True) | ||
|
|
||
| out = Path(args.output) | ||
| out.mkdir(parents=True, exist_ok=True) | ||
| name = out / f"{args.task}-{args.model}.json" | ||
| name.write_text(json.dumps({"args": vars(args), "results": results}, indent=2)) | ||
| print(f"\n{args.task}, {args.model}, seeds {args.seeds}: test mean (std)") | ||
| metrics = TASKS[args.task][2] | ||
| print(f"{'embeddings':<10}" + "".join(f"{m:>22}" for m in metrics)) | ||
| for embeddings, runs in results.items(): | ||
| cells = [] | ||
| for m in metrics: | ||
| vals = np.array([r[m] for r in runs]) | ||
| cells.append(f"{vals.mean():.4f} ({vals.std():.4f})") | ||
| print(f"{embeddings:<10}" + "".join(f"{c:>22}" for c in cells)) | ||
| print(f"Saved to {name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[low] Example says early stopping, but none is configured
The module docstring (line 12) and the PR description say the example trains with early stopping on validation AUPRC. The
trainer.traincall passes nopatience, so every run goes the full--epochsand only the best validation epoch is restored. Either passpatience=(Trainer supports it) or describe the behaviour as best-epoch selection.