Repository navigation
Add MIRA medical time-series foundation model (NeurIPS 2025) with pretrained checkpoint - #1420
Conversation
Port MIRA (Li et al., NeurIPS 2025, microsoft/MIRA, MIT) as plain PyTorch: CT-RoPE attention, top-2-of-8 mixture of experts with a shared expert, and the neural-ODE extrapolation block. Parameter names follow the authors' code, so the published MIRA-Large checkpoint (MIRA-Mode/MIRA, MIT, pinned revision) loads with all 453,331,072 weights matched; outputs match the authors' MIRAForPrediction to within 1e-6. MIRA(dataset, pretrained=True) forecasts the PhysioNet2012Forecasting task channel-independently (same fields and metrics as APN, reusing masked_mse and RMSNorm). Adds offline tests, docs, and an example notebook comparing zero-shot MIRA with APN and LOCF on the APN split. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt
|
/review |
Test CoverageTotal: 79.19% (27618 / 34875 lines)
|
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. It adds MIRA as a plain-PyTorch port, with a loader for the published MIRA-Large checkpoint that requires every weight to match. It also adds tests, API docs, and an executed example notebook that compares MIRA with APN and last observation carried forward. It is well documented and carefully tested.
This is close. One thing needs to be settled before merging.
To get this merged:
-
pyhealth/models/mira.pyimports_ForecastLabel(a private class) andmasked_msefrompyhealth/models/apn.py, andRMSNormfrompyhealth/models/ehrmamba.py. The description namesmasked_mseandRMSNormas reused, but it doesn't mention_ForecastLabel. It also doesn't say whyRMSNormcomes from the EHRMamba module. Please do one of two things:- Confirm in the description that this sharing is intended, naming
_ForecastLabel,masked_mseandRMSNorm, and say why. For example, you might want identical loss and label scoring to APN so the two compare directly. - Keep MIRA self-contained by copying these pieces into
mira.py.
Either is fine. The point is that a later change to APN or EHRMamba shouldn't silently change MIRA.
- Confirm in the description that this sharing is intended, naming
Optional suggestions:
- Coverage meets the bar:
mira.pyis at 96.65% and the repository total is 78.69%, against 80% per file and 60% overall. - The change doesn't touch base classes or other shared code. The only changes outside the new model are the export lines in
models/__init__.pyand the docs index entry. - Consider noting in the notebook's install cell how to get
torchdiffeq. Without it, the ODE block uses a fixed-step RK4 solver and gives slightly different numbers.
Reviewed commit b93fa7b
A later change to APN or EHRMamba can no longer change MIRA's outputs or loss (AI review on sunlabuiuc#1420). The notebook's install cell notes the optional torchdiffeq dependency of the ODE block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. It adds MIRA-Large to PyHealth as a plain-PyTorch port. The checkpoint loads with every weight matched, and a Trainer-compatible forecasting path runs on PhysioNet2012Forecasting. The docs page, the 18 offline tests and the executed notebook with a clear comparison are all helpful. The change is self-contained. It builds only on BaseModel, SampleDataset and RegressionLabelProcessor, and the only shared-file edit is the new exports in models/init.py. The copies of masked_mse, the forecast label processor and RMSNorm are stated and explained in the description. It is close.
To get this merged:
- Add a test for the default download path of load_mira_checkpoint (path=None), which the local-directory test doesn't exercise. Mock huggingface_hub.hf_hub_download, point it at a tiny saved checkpoint, and assert that it is called with HF_REPO_ID and the pinned HF_REVISION. This protects the pinned-revision guarantee that pretrained=True relies on.
Optional suggestions:
- MIRA(..., pretrained=True, config=...) silently replaces the passed config with the checkpoint's. Consider raising an error or warning instead.
- The ODE results and the 1e-6 parity claim depend on whether torchdiffeq is installed, and it is not a declared dependency. Say so in the docstring or docs.
- You derive some code from Time-MoE (Apache-2.0), so consider including or pointing to that license text, as you did for the MIT notice.
- The parity check against the authors' code is only described in the notebook. A small script or a skipped-by-default test would make it reproducible.
Coverage is above the bar. pyhealth/models/mira.py is at 96.2% against the 80% target per file, and the whole repository is at 79.18% against 60%.
Reviewed commit 77ed89e
Test load_mira_checkpoint's default Hugging Face path with a mocked hf_hub_download, checking the repo id and pinned revision. Passing config together with pretrained now raises instead of being silently replaced. Document that torchdiffeq is optional and point to the Apache-2.0 text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt
|
/review Addressed in 48aea5a:
|
|
|
||
| Submodule and parameter names match the authors' ``MIRAForPrediction``, | ||
| so its state dict loads unchanged (see :func:`load_mira_checkpoint`). | ||
| Differences from the authors' code, none of which changes the outputs of |
There was a problem hiding this comment.
[medium] Docstring says port differences do not change outputs, but the RK4 fallback does
The MIRABackbone docstring says the listed differences from the authors' code are "none of which changes the outputs of an unpadded series". The bullet on the ODE block says the built-in fixed-step RK4 is used when torchdiffeq is missing, while the checkpoint was trained with dopri5. That changes outputs, and the MIRA class docstring and notebook say the forecasts "differ slightly". The no-KV-cache bullet also changes the autoregressive computation, which the notebook's notes on the authors' cache discuss. Reword the sentence so it names the solver difference as changing outputs, and say that the default parity claim assumes torchdiffeq is installed.
| torch.testing.assert_close(out, torch.zeros(1, 1)) | ||
|
|
||
|
|
||
| class TestMIRABackbone(unittest.TestCase): |
There was a problem hiding this comment.
[medium] No reference-output test for the backbone forward pass
TestMIRABackbone checks parameter names, padding invariance, time-shift invariance, that forecast appends predictions, that the ODE moves state forward only, and that RK4 matches a fine Euler solve. It never checks the forward pass against fixed expected outputs. A wrong rotation sign, a wrong expert routing or a wrong attention mask would still pass, because the tests only check self-consistency. The description's 1e-6 match to the authors' code is verified only off-repo, and the notebook's port check is not reproducible here. Add a golden-output test: a small seeded config with saved expected outputs generated from the authors' code, covering attention, MoE and the ODE block. Or state in the description that parity was checked only manually.
| torch.testing.assert_close(rk4, euler, atol=1e-3, rtol=1e-3) | ||
|
|
||
|
|
||
| class TestMIRAModel(unittest.TestCase): |
There was a problem hiding this comment.
[low] No test runs MIRA on PhysioNet2012Forecasting samples
TestMIRAModel uses synthetic create_sample_dataset samples. APN's tests (tests/core/test_apn.py) build samples through PhysioNet2012Forecasting via set_task. MIRA's compatibility with the task's real sample layout (NaN patterns, trailing missing steps with y_time 0) is covered only by the notebook. A short end-to-end test on the fake PhysioNet root used by test_apn.py would cover it.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this contribution. It adds MIRA, a medical time-series foundation model, to PyHealth. It includes a plain-PyTorch backbone, a strict-matching loader for the pinned MIRA-Large checkpoint, and a channel-independent wrapper that runs on PhysioNet2012Forecasting. It also comes with docs and an executed notebook that compares MIRA with APN and last observation carried forward.
It looks ready to merge. The model is self-contained. Its only library links are the BaseModel and RegressionLabelProcessor base classes, and it keeps its own copies of masked_mse, the forecast label processor and RMSNorm, as your description says. Coverage is good. The new pyhealth/models/mira.py is at about 97%, and the whole repository is at 79.19%, above the 60% bar.
Optional suggestions:
- The MIRABackbone docstring says none of the listed differences from the authors' code changes the outputs of an unpadded series. The RK4 fallback used without torchdiffeq does change them slightly, as your own use_ode docs and notebook say. Please reword that sentence.
- The 1e-6 parity with the authors' code is verified outside the repo. If it's feasible, add a small test with fixed reference outputs, or at least note in the docs how it was checked.
- A small test that runs MIRA on real PhysioNet2012Forecasting task output would catch future drift. Right now only the notebook covers it.
Reviewed commit 48aea5a
This adds MIRA (Li et al., NeurIPS 2025, https://arxiv.org/abs/2506.07584, code at microsoft/MIRA, MIT) to
pyhealth.models.MIRABackboneis a plain-PyTorch port of the authors'MIRAForPrediction: continuous-time rotary attention, a top-2-of-8 mixture of experts with a shared expert, and the neural-ODE extrapolation block. The parameter names are the original ones.MIRA(dataset, pretrained=True)loads the published MIRA-Large checkpoint (MIRA-Mode/MIRA, MIT, pinned revision) and fails unless all 453,331,072 weights match. Its outputs match the authors' code to within 1e-6.MIRAruns channel-independently onPhysioNet2012Forecasting, with the same fields, loss and metrics asAPN, so the two can be compared on the same split. Like APN, it scores the forecasts through its own regressionlabel_processors(the Add LabelProcessor: label semantics live on the label processor #1389 API). It supports zero-shot forecasting and fine-tuning withTrainer.masked_mse(the same loss as APN, so the two are scored identically), the forecast label processor andRMSNorm, so a later change to APN or EHRMamba cannot silently change MIRA. No shared code changes.tests/core/test_mira.py, 18 offline tests with no downloads. Docs:docs/api/models/pyhealth.models.MIRA.rst.examples/mira_physionet2012_forecasting.ipynb(executed, outputs included) runs zero-shot MIRA next to APN and last observation carried forward, puts the paper's numbers alongside, explains how the setups differ, and lists questions for the authors.Results (PhysioNet 2012 test split, 1,192 records, z-score units, same split as the APN notebook)
Zero-shot MIRA lowers the MSE by about 20% over carrying the last value forward but stays behind APN trained on this split, consistent with the paper, where full-shot models also beat zero-shot MIRA on CinC 2012. The paper reports CinC 2012 as RMSE 6.221 / MAE 6.115 on a scale labelled "(10¹)" and does not describe its windows or normalization, so its numbers are not directly comparable. The Hugging Face card describes the checkpoint as a demo model trained on public data; we will confirm with the authors that it is the right one to cite.
Integration contact nominated by the authors: Hao Li.
🤖 Generated with Claude Code
https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt