Skip to content

[BTY-37] MIRA: PTB-XL and Sleep-EDF forecasting tasks - #1647

Merged
github-actions[bot] merged 1 commit into
sunlabuiuc:masterfrom
irvifa:bty-37-mira-biosignals
Nov 28, 2026
Merged

github-actions[bot] merged 1 commit into
sunlabuiuc:masterfrom
irvifa:bty-37-mira-biosignals

Conversation

@irvifa

@irvifa irvifa commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR covers the remaining PTB-XL and Sleep-EDF scope of BTY-37 (MIRA, Li et al., NeurIPS 2025, paper, code). The MIRA model and the PhysioNet 2012 example were merged in #1420. Table 3 of the paper reports zero-shot forecasting on held-out parts of PTB-XL and Sleep-EDF. This PR adds those two forecasting tasks and an example that scores models on them.

  • PTBXLForecasting: each PTB-XL ECG lead (100 Hz or 500 Hz) is a univariate series, z-scored over its 10-second record. The model sees context_length samples and forecasts the next horizon. By default there is one window per record, starting at the first sample, as model_eval.py in the MIRA repo does. Samples carry strat_fold for split_by_strat_fold.
  • SleepEDFForecasting: the same for the Sleep-EDF EEG channels (Fpz-Cz and Pz-Oz by default). The recordings start and end with hours of wake, so windows are taken evenly from the sleep period: from the first to the last scored sleep stage (1-4 or R) in the hypnogram. Each channel is z-scored over that period.
    • The hypnogram is attached to the recording with raw.set_annotations, as SleepStagingSleepEDF does, so its onsets line up with the PSG. On the 39 nights I have locally this gives the same sleep periods as the raw onsets.
  • Normalization: as in model_eval.py, the z-score statistics are computed over the whole record or sleep period, so they include the forecast targets. The docstrings say so. The "record mean" baseline (always 0) therefore has a small amount of future information.
  • Shared options: num_windows and drop_rate. drop_rate sets random context values to NaN, the way the paper turns regular series irregular. The targets are always complete.
  • Output fields: the samples have the fields of PhysioNet2012Forecasting (x [context, channels], x_time, y_time, y [horizon, channels], with times in seconds), so MIRA and APN run on them unchanged.

Checkpoint availability

The MIRA-Large checkpoint is no longer downloadable. MIRA-Mode/MIRA returns 401 on Hugging Face (see #1635 and #1636). So this PR can't report MIRA zero-shot numbers, and the paper's MIRA results are not reproduced.

The example runs MIRA when --mira gives a local snapshot. I checked that path with a tiny randomly initialized snapshot, which tests the code only; its numbers mean nothing.

Links to other components

  • PTBXLDataset / load_ptbxl_record, and SleepEDFDataset with MNE: used to read the signals, as the existing PTB-XL and Sleep-EDF tasks do. They are not modified.
  • PhysioNet2012Forecasting: its sample layout is reused so that MIRA and APN work without changes. Nothing is imported from it.
  • examples/mira_signal_forecasting.py: uses MIRA, APN, split_by_strat_fold and split_by_patient. Its scoring follows examples/mira_physionet2012_forecasting.ipynb.

Datasets

Results

The (context, horizon) pairs are those of model_eval.py. The splits are PTB-XL's recommended folds (1-8 train, 9 validation, 10 test; 2,198 test records, all 12 leads, at 100 Hz) and, for Sleep-EDF, a split by subject (80/10/10, seed 0).

I had the first 20 cassette subjects locally (39 nights, 100 windows per night): 3,100 train, 400 validation and 400 test windows. That test set is only 4 nights, so the Sleep-EDF numbers are noisy.

APN uses Adam (lr 0.01, up to 30 epochs with early stopping on validation MSE), with its patches initialized over the context span (history = context length in seconds). Results are averaged over seeds 0-2. The metric is per-series RMSE / MAE in z-score units: each (window, channel) series is scored, then the scores are averaged, as in model_eval.py.

PTB-XL (test fold 10)

Context → horizon Last value Record mean APN (3 seeds)
48 → 24 0.904 / 0.676 0.912 / 0.706 0.755 / 0.532
72 → 36 1.024 / 0.733 0.929 / 0.666 0.820 / 0.539
96 → 48 1.063 / 0.745 0.915 / 0.621 0.833 / 0.525
128 → 64 1.134 / 0.779 0.929 / 0.588 0.883 / 0.531

Sleep-EDF (cassette subjects 0-19, 4 test nights)

Context → horizon Last value Record mean APN (3 seeds)
48 → 24 0.810 / 0.695 0.808 / 0.709 0.705 / 0.605
72 → 36 0.948 / 0.807 0.831 / 0.712 0.835 / 0.716
96 → 48 1.021 / 0.869 0.841 / 0.712 0.821 / 0.693
128 → 64 1.069 / 0.910 0.843 / 0.704 0.814 / 0.678

APN beats both baselines in every PTB-XL setting. On Sleep-EDF it does in three of the four; at 72 → 36 it is level with the record mean.

The paper's Table 3 gives MIRA-Large, averaged over horizons, an RMSE / MAE of 0.121 / 0.091 on PTB-XL and 0.189 / 0.156 "(10²)" on Sleep-EDF. These can't be compared with the numbers above:

  • Unknown setup: the paper doesn't state the held-out records, the leads or channels, the context length or the scale of its metrics.
  • Leakage: both datasets are part of MIRA's pretraining corpus, which is why the paper calls them in-distribution.
  • No checkpoint: without the weights, we can't measure MIRA in this setup either.

Testing

pytest tests/core/test_signal_forecasting.py                      # 11 passed
pytest --doctest-modules pyhealth/tasks/signal_forecasting.py     # 2 passed
python examples/mira_signal_forecasting.py --dataset ptbxl --root <ptb-xl/1.0.3> --seeds 0 1 2
python examples/mira_signal_forecasting.py --dataset sleepedf --root <sleep-edfx/1.0.0> --seeds 0 1 2

pyhealth/tasks/signal_forecasting.py coverage: 100%. The tests use synthetic signals: they patch load_ptbxl_record and MNE's EDF readers, and train an APN step on the samples.

Add the forecasting tasks of the MIRA evaluation (Li et al., NeurIPS
2025) on two open datasets PyHealth already loads:

- PTBXLForecasting: each ECG lead is a univariate series, z-scored over
  its 10 s record; forecast `horizon` samples after a `context_length`
  window (one window per record by default, as model_eval.py does).
- SleepEDFForecasting: the same for the Sleep-EDF EEG channels, with
  windows taken from the hypnogram's sleep period.

Both produce the fields of PhysioNet2012Forecasting, so MIRA and APN run
on them unchanged. examples/mira_signal_forecasting.py scores last value,
record mean and APN for model_eval.py's (context, horizon) pairs, and
MIRA when given a local checkpoint snapshot (the hosted one is no longer
public). Adds docs pages and tests on synthetic signals.

Signed-off-by: Irvi Aini <irvi.fa@gmail.com>
@irvifa
irvifa marked this pull request as ready for review November 28, 2026 03:02
@github-actions github-actions Bot mentioned this pull request Nov 28, 2026
@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 87.17% (50826 / 58306 lines)

Modified file Coverage Lines
pyhealth/tasks/__init__.py 98.29% 115 / 117
pyhealth/tasks/signal_forecasting.py 100.00% 99 / 99

Workflow run · 9f73dca

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 28, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

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

Reviewed commit 9f73dca

starts = np.linspace(first, last - window, self.num_windows)
starts = sorted({int(s) for s in starts})
rng = np.random.default_rng([self.seed, zlib.crc32(record_key.encode())])
x_time = np.arange(self.context_length, dtype=np.float32) / sampling_rate

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] Times are in seconds; MIRA's time unit is not stated

x_time and y_time are in seconds (divided by the sampling rate). The MIRA docstring in pyhealth/models/mira.py (around lines 711-713) describes ode_time_scale as converting PhysioNet 2012's hour/48 into hours, which suggests MIRA's time inputs are in hours. The description says MIRA runs on these samples unchanged, but it does not say which time unit MIRA expects for PTB-XL and Sleep-EDF, and the example passes seconds. Please state the unit MIRA was trained with, or scale the times to it, before the zero-shot MIRA path is presented as comparable. This is non-blocking because the MIRA run needs a local checkpoint that cannot be obtained here.

if label in _SLEEP_STAGES
]
if not sleep:
continue

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] Nights without a hypnogram or scored sleep are handled silently

If a night has no label_file, the whole recording is used. If it has no scored sleep stage, the night is dropped with no warning. Neither behaviour is described in the SleepEDFForecasting docstring, which only explains the sleep-period windowing. Add both cases to the docstring so users know that a night can contribute fewer or different windows than they expect.

@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 PTBXLForecasting and SleepEDFForecasting tasks in the setup of the MIRA evaluation, with docs, an example script and baseline/APN results. The checkpoint limitation is stated clearly, and the tasks produce samples in the PhysioNet2012Forecasting layout, so APN and MIRA can run on them.

It looks ready to merge. The tasks are self-contained: they subclass BaseTask through a private helper in the same module, and the only registration is the new exports in pyhealth/tasks/__init__.py. The links to other components (the PTB-XL loader, MNE, the sample layout, and the models and splits in the example) are all named in the description with their purpose. Both datasets cite their sources. Coverage is 100% for signal_forecasting.py and 98.3% for tasks/__init__.py. Total repository coverage is 87.2%, so both are above the bar.

Optional suggestions:

  • The Sleep-EDF docstring could mention two behaviours. When there is no hypnogram, the task falls back to the whole night. A night with no scored sleep stages is skipped silently.
  • Say which time unit MIRA expects for x_time. The tasks give seconds, and the MIRA docs tie its time scale to hours.

Reviewed commit 9f73dca

@irvifa

irvifa commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@github-actions
github-actions Bot merged commit 178f09c into sunlabuiuc:master Nov 28, 2026
3 checks passed
@irvifa
irvifa deleted the bty-37-mira-biosignals branch November 28, 2026 04:33
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