Repository navigation
[BTY-37] MIRA: PTB-XL and Sleep-EDF forecasting tasks - #1647
Conversation
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>
Test CoverageTotal: 87.17% (50826 / 58306 lines)
|
|
/review |
| 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 |
There was a problem hiding this comment.
[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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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
|
/merge |
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 seescontext_lengthsamples and forecasts the nexthorizon. By default there is one window per record, starting at the first sample, asmodel_eval.pyin the MIRA repo does. Samples carrystrat_foldforsplit_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.raw.set_annotations, asSleepStagingSleepEDFdoes, 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.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.num_windowsanddrop_rate.drop_ratesets random context values to NaN, the way the paper turns regular series irregular. The targets are always complete.PhysioNet2012Forecasting(x [context, channels],x_time,y_time,y [horizon, channels], with times in seconds), soMIRAandAPNrun on them unchanged.Checkpoint availability
The MIRA-Large checkpoint is no longer downloadable.
MIRA-Mode/MIRAreturns 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
--miragives 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, andSleepEDFDatasetwith 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 thatMIRAandAPNwork without changes. Nothing is imported from it.examples/mira_signal_forecasting.py: usesMIRA,APN,split_by_strat_foldandsplit_by_patient. Its scoring followsexamples/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 inmodel_eval.py.PTB-XL (test fold 10)
Sleep-EDF (cassette subjects 0-19, 4 test nights)
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:
Testing
pyhealth/tasks/signal_forecasting.pycoverage: 100%. The tests use synthetic signals: they patchload_ptbxl_recordand MNE's EDF readers, and train an APN step on the samples.