Skip to content

Add MIRA medical time-series foundation model (NeurIPS 2025) with pretrained checkpoint - #1420

Merged
solarsys merged 3 commits into
sunlabuiuc:masterfrom
solarsys:feat/mira
Nov 9, 2026
Merged

solarsys merged 3 commits into
sunlabuiuc:masterfrom
solarsys:feat/mira

Conversation

@solarsys

@solarsys solarsys commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

This adds MIRA (Li et al., NeurIPS 2025, https://arxiv.org/abs/2506.07584, code at microsoft/MIRA, MIT) to pyhealth.models.

  • MIRABackbone is 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.
  • MIRA runs channel-independently on PhysioNet2012Forecasting, with the same fields, loss and metrics as APN, so the two can be compared on the same split. Like APN, it scores the forecasts through its own regression label_processors (the Add LabelProcessor: label semantics live on the label processor #1389 API). It supports zero-shot forecasting and fine-tuning with Trainer.
  • It reuses the existing PhysioNet 2012 dataset, task and split. It keeps its own copies of masked_mse (the same loss as APN, so the two are scored identically), the forecast label processor and RMSNorm, so a later change to APN or EHRMamba cannot silently change MIRA. No shared code changes.
  • Tests: tests/core/test_mira.py, 18 offline tests with no downloads. Docs: docs/api/models/pyhealth.models.MIRA.rst.
  • Example: 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)

Model MSE MAE
Last observation carried forward 0.4163 0.4115
MIRA-Large, zero-shot (no training) 0.3341 0.3881
MIRA-Large, zero-shot with the ODE block 0.3347 0.3892
APN, trained on this split (1 seed) 0.3050 0.3706
APN, paper (5 runs) 0.3093 0.3650

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

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
@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 9, 2026
@solarsys

solarsys commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 79.19% (27618 / 34875 lines)

Modified file Coverage Lines
pyhealth/datasets/eicu.py 100.00% 14 / 14
pyhealth/models/__init__.py 100.00% 64 / 64
pyhealth/models/knowrare.py 97.73% 301 / 308
pyhealth/models/mira.py 97.09% 334 / 344
pyhealth/tasks/__init__.py 96.08% 49 / 51
pyhealth/tasks/knowrare_mimic4.py 100.00% 89 / 89
pyhealth/tasks/yaib_eicu.py 91.98% 642 / 698
pyhealth/tasks/yaib_mimic3.py 92.96% 581 / 625

Workflow run · 48aea5a

@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.

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.py imports _ForecastLabel (a private class) and masked_mse from pyhealth/models/apn.py, and RMSNorm from pyhealth/models/ehrmamba.py. The description names masked_mse and RMSNorm as reused, but it doesn't mention _ForecastLabel. It also doesn't say why RMSNorm comes from the EHRMamba module. Please do one of two things:

    • Confirm in the description that this sharing is intended, naming _ForecastLabel, masked_mse and RMSNorm, 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.

Optional suggestions:

  • Coverage meets the bar: mira.py is 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__.py and 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
@solarsys

solarsys commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@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.

Previous review

Reviewed commit 77ed89e

@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.

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
@solarsys

solarsys commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

/review

Addressed in 48aea5a:

  • Added test_default_download_uses_pinned_revision: mocks huggingface_hub.hf_hub_download, serves a tiny saved checkpoint, and asserts both files are requested from HF_REPO_ID at the pinned HF_REVISION (via load_mira_checkpoint() and MIRA(..., pretrained=True)).
  • MIRA(..., pretrained=..., config=...) now raises ValueError instead of silently using the checkpoint's config (tested).
  • The use_ode docstring says torchdiffeq is optional and not a dependency, and that without it a fixed-step RK4 gives slightly different forecasts.
  • The module header points to the Apache-2.0 text for the Time-MoE-derived parts.
  • Not done: a separate parity script. The check needs the authors' repository and the 0.9 GB checkpoint, so it stays in the notebook.

@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 48aea5a

Comment thread pyhealth/models/mira.py

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[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.

Comment thread tests/core/test_mira.py
torch.testing.assert_close(out, torch.zeros(1, 1))


class TestMIRABackbone(unittest.TestCase):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[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.

Comment thread tests/core/test_mira.py
torch.testing.assert_close(rk4, euler, atol=1e-3, rtol=1e-3)


class TestMIRAModel(unittest.TestCase):

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] 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.

@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 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

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.

1 participant