Repository navigation
[Dataset + Task + Model] WatchSleepNet sleep stage prediction (Wang et al., 2025) with additional data sources (Sleep-Accel, ISRUC) - #1645
Conversation
…er author was used to build pyhealth class
…atching watchsleepnet processing) instead of removing
…nyee31_aluoma_watchsleepnet
Test CoverageTotal: 87.39% (53005 / 60652 lines)
|
…nyee31_aluoma_watchsleepnet
|
/review |
solarsys
left a comment
There was a problem hiding this comment.
Thank you, Aleksi and Nathan, and thanks for raising the overlap. Your PR was opened first (as a draft), and it covers the most ground: SHHS, MESA, ISRUC and Sleep-Accel, plus pretraining, fine-tuning and quantization. We'd like those additions in PyHealth. Here's the plan and what needs to change.
The plan
Three open PRs implement WatchSleepNet on DREAMT: this one, #1648 and #1650. PyHealth keeps one implementation per component. We're taking #1650 as the base for the WatchSleepNet model and the DREAMT task, because it was ready for review first, it's small enough for the AI review, and it only adds new files. This PR then becomes the home for the parts nobody else has: the IBI tasks for the other cohorts, the Sleep-Accel dataset and the training workflow.
Blocking issues
- Too large to review. The diff is about 264,000 characters. The AI review refuses anything over 200,000, so it can't run on this PR. Please split it (suggested split below).
- Duplicate model. After #1650 merges, please drop
pyhealth/models/watchsleepnet.pyand build on the mergedWatchSleepNet.- It takes a per-night tensor of shape
(T, 750)and a(T,)label tensor;feature_keyandlabel_keylet you use other field names. - Make your IBI tasks produce inputs it accepts, or explain in the description why they can't.
- If your model has something #1650's lacks (hooks for pretraining and fine-tuning, quantization support), propose it as a focused change to the merged model, with tests.
- It takes a per-night tensor of shape
- Duplicate DREAMT task.
SleepStagingIBIDREAMToverlaps #1650'sSleepStagingDREAMT. Please drop it. If your IBI extraction differs in a way that matters, propose it as an option on the existing task, and say what it changes. - Edits to existing files that aren't needed:
pyhealth/datasets/nsrr_sleep.pyandpyhealth/tasks/sleep_staging_nsrr.py: the changes are only comments linking to the original repo. Please revert them; the links can go in your new files or the example README.pyhealth/datasets/dreamt.py:- Move
DREAMT_STAGE_MAPPING,map_dreamt_stagesandload_dreamt_frameinto your task module. They're task-side helpers, andsleep_staging_ibi.pyalready imports them lazily. - Drop the new
default_taskproperty. Changing an existing dataset's default task is a separate decision. - Drop the
Optional[...]toX | Nonerewrite.
- Move
- The
get_patient_filerewrite fixes a real bug: an undefined variable when the folder name is unexpected. It also changes the return value fromPathtostr. Please send it as its own small PR with a test. It's useful on its own, but it isn't part of WatchSleepNet. tests/core/test_dreamt.py,test_isruc.pyandtest_nsrr_sleep.py: please leave the existing tests and their helpers untouched, and put the new tests in new files, as you already do intest_sleep_staging_ibi.py.
- Shared private helpers. The IBI tasks import private helpers from other task modules:
_read_edf_channels,_read_stage_codes,CHANNEL_ALIASESandLABEL_SCHEMESfromsleep_staging_nsrr, and helpers fromsleep_staging_isruc. The AI review blocks sharing between components unless the PR description names it and gives a reason. Please add that sentence, or copy the pieces you need.
Also, please fix
peak_backend="auto"usesbiosppyorneurokit2when they're installed, neither of which PyHealth depends on, and falls back to scipy otherwise. The same code can then give different IBIs on different machines. Please default to"scipy"and let users opt in to the others.
Suggested split, after #1650 merges
- The IBI tasks for SHHS, MESA and ISRUC, producing inputs the merged
WatchSleepNetaccepts, with their tests in new files and one short example. - The Sleep-Accel dataset (
sleep_accel.py, its config, docs and test) andSleepStagingIBISleepAccel. - The pretraining, fine-tuning, evaluation and quantization examples, plus any model additions from item 2 above.
Each part then fits under the review limit and touches only new files plus the __init__.py and docs registrations, so it can go through the automatic AI review. Once you've split it, you can close this PR or repurpose it as the first part. Thank you, and sorry for the extra work. The additional cohorts are the most valuable part of this PR.
|
Thank you for the review @solarsys. PR #1650 creates DREAMT sleep staging as its own isolated task while this PR creates a generalized sleep staging task that can also be applied to NSRR data (SHHS, MESA), ISRUC, and Sleep-Accel. The shared core task structure would be more extensible than a dedicated task per dataset. Can we re-evaluate which sleep staging task we should pick for a final merge, or have both present instead of depending on the other task implementation? |
|
#1650 (WatchSleepNet and the DREAMT |
|
Thanks for the update @solarsys. I have made a few changes to resolve the points raised in no 4. Regarding no 1 on the commit size. We are completing this PR for a final class project in CSE 6250, which requires us to raise a PR with our contributions. We wanted to implement a full pipeline with dataset + task + model + examples, but since the model was added after our PR was implemented this has complicated things that I need to verify with my grader. I am not sure if multiple PRs can be graded as a part of this class. Regarding the merge, My partner and I will evaluate what has to change as I will be unavailable. We will stage it as a PR on my fork and merge it into this branch. |
|
Hi @solarsys, I compared the model that is on master with my PR and the published code for the WatchSleepNet paper. Both models deviate from the original paper, but the model on the master branch made several changes to the paper's implementation that aren't on this PR. Since you already approve of the deviated model, I would like to ask how to proceed with reusing the model or updating it. Should the structure of the model be updated to align closer with the paper (which may be a breaking change)?
|
|
The DREAMT tasks also process different subsets of the dataset. The master's merged SleepStagingDREAMT pulls from the 64hz DREAMT data while this branches SleepStagingIBIDREAMT pulls from the 100Hz DREAMT data. The 100Hz subset processing on this PR aligns with how the authors of the WatchSleepNet paper processed the DREAMT data. Since the files don't conflict, maybe we can keep both of the processing tasks and rename them to be clear that one processes 100Hz data while the other processes 64Hz data. |
Contributors
CSE 6250 Team A2
Contribution type
Full Pipeline: Dataset + Task + Model (CSE6250 BD4H Reproducibility Challenge, Fall 2026)
Original paper
Wang, W.K., Chen, B., Yang, J., Jeong, H., Hershkovich, L., Islam, S.M.M., Liu, M., Roghanizad, A.R., Shandhi, M.M.H., Spector, A.R. & Dunn, J.. (2025). WatchSleepNet: A Novel Model and Pretraining Approach for Advancing Sleep Staging with Smartwatches. Proceedings of the sixth Conference on Health, Inference, and Learning, in Proceedings of Machine Learning Research 287:145-165 Available from https://proceedings.mlr.press/v287/wang25a.html.
Description
Replicate findings from the WatchSleepNet paper, with processing to downsample and transform ECG and PPG data to inter-beat interval (IBI), train, and evaluate model using IBI to predict 3-class (Wake, NREM, REM) sleep stage.
Extend on paper finds with IBI processing of new datasets (ISRUC, Sleep-Accel) and additional evaluation of model. Perform additional ablation through model quantization (bf16, fp16) both during training and evaluating performance of an already trained quantized model.
Code Changes
Dataset: DREAMT and Sleep-Accel
Task: SleepStagingIBI*
Model: WatchSleepNet
Examples:
Exclusions
Code for an additional dataset (MIT-BIH Polysomnographic Database) was written, however, dataset processing would require a new repository dependency on
wfdbversion 4.1.0 and above. We have excluded the additional dataset due to the repository-wide impacts of the new dpeendency.File guide
Dataset
pyhealth/datasets/dreamt.py- stage mapping, CSV loading, IBI default taskpyhealth/datasets/sleep_accel.py- SleepAccelDataset and file loaderspyhealth/datasets/configs/sleep_accel.yaml- metadata table schemapyhealth/datasets/__init__.py- export SleepAccelDatasetpyhealth/datasets/nsrr_sleep.py- source-reference commentsTask
pyhealth/tasks/sleep_staging_ibi.py- five IBI staging tasks and helperspyhealth/tasks/sleep_staging_nsrr.py- source-reference commentspyhealth/tasks/__init__.py- export the five tasksModel
pyhealth/models/watchsleepnet.py- WatchSleepNet and its building blockspyhealth/models/__init__.py- export WatchSleepNetExample
examples/watchsleepnet/watchsleepnet_common.py- shared CLI, builders, training and checkpoint helpersexamples/watchsleepnet/watchsleepnet_pretrain_shhs_mesa.py- pretraining scriptexamples/watchsleepnet/watchsleepnet_finetune.py- finetuning scriptexamples/watchsleepnet/watchsleepnet_quantize.py- checkpoint quantization scriptexamples/watchsleepnet/watchsleepnet_evaluate.py- evaluation scriptexamples/watchsleepnet/watchsleepnet_synthetic_data.py- synthetic data generator for testing examplesDocumentation
docs/api/datasets/pyhealth.datasets.SleepAccelDataset.rst,docs/api/datasets.rstdocs/api/tasks/pyhealth.tasks.SleepStagingIBI*.rst,docs/api/tasks.rstdocs/api/models/pyhealth.models.WatchSleepNet.rst,docs/api/models.rstTests
tests/core/test_dreamt.py- dataset and IBI task teststests/core/test_isruc.py- IBI task teststests/core/test_nsrr_sleep.py- SHHS/MESA IBI task teststests/core/test_sleep_accel.py- Sleep-Accel dataset and task teststests/core/test_sleep_staging_ibi.py- IBI helper teststests/core/test_watchsleepnet.py- model testsTesting
42 new unit tests across six files, all on small synthetic data in temporary directories.
Use of original code
The authors' repository github.com/willKeWang/WatchSleepNet_public was used as a reference for the data preprocessing and model structure. We have extended upon it by aligning with PyHealth structure, replicating the data preprocessing on additional datasets, and adding additional configurations for extension and ablation.