Repository navigation
[BTY-23] Migrate SHHSDataset to PyHealth 2.0 BaseDataset - #1785
kheper1970 wants to merge 5 commits into
Conversation
- Replace BaseSignalDataset with BaseDataset following TUEV reference port - Add YAML config (configs/shhs.yaml) for shhs1/shhs2 tables - Add prepare_metadata() to index EDF files into metadata CSVs - Support cache fallback via metadata_cache_dir - Preserve dev mode (limit to 5 patients) - Preserve patient_id parsing from EDF filenames - Preserve label_file paths for Profusion XML annotations
|
Thanks for taking on BTY-23. Why this PR has
Please also check that the SHHS sleep-staging task in |
Test CoverageTotal: 89.25% (68025 / 76215 lines)
|
- Add tests/core/test_shhs.py with 7 test cases covering prepare_metadata, dev mode, missing dirs, config YAML, and patient ID parsing - Remove SHHSDataset from test_removed_1x_classes.py (it now works) - Fix ruff UP045: Optional[str] -> str | None
|
Thanks for the detailed review! All three issues are now fixed:
I've also verified the SHHS sleep-staging task in pyhealth/tasks/shhs_multimodal_sleep_staging.py - it reads EDF files and Profusion XML annotations directly via the signal_file and label_file columns in the metadata CSV, which is exactly what the ported dataset provides. The task should work on the migrated dataset. Please let me know if anything else is needed. |
|
Duplicate: BTY-23 (porting Separately, from a manual look at this PR: We're still running an AI review on this PR so you get feedback on your code, but we're unlikely to merge a second implementation. If your version fixes or adds something the existing one lacks, a focused follow-up PR that extends it is very welcome; please link this PR from it. Before opening a PR, please check master and open PRs for the same component. |
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: human review
Thank you for porting SHHSDataset to the 2.0 BaseDataset, with a YAML config, metadata indexing and tests. It is close, but I found a few blocking problems.
To get this merged:
- Dev mode writes a 5-patient CSV to the shared location (shhs.py, prepare_metadata). Later runs with dev=False skip regeneration because the CSV already exists, so they silently reuse the truncated file. Please don't persist dev-truncated metadata. For example, apply the dev limit when loading, or write dev output to a separate file.
devis never passed toBaseDataset.__init__. Please forward it so dev mode behaves like the other 2.0 datasets.label_fileis stored relative to root, whilesignal_fileis absolute. If the cache fallback is used, the relative path resolves against the cache directory instead of the real data root. Please store absolute paths, or document how they are resolved.- The repo already has
NSRRSleepDataset(cohort="shhs")(pyhealth/datasets/nsrr_sleep.py), which indexes the same SHHS recordings. The description doesn't mention it. Please explain how SHHSDataset differs, or how the two relate. - There is no 2.0 sleep-staging task or
default_task, and the legacysleep_staging_shhs_fnexpects 1.x record fields. Please add a task, or state clearly in the description that this is out of scope. - The tests build instances with
__new__, so the constructor, cache fallback and dev path are untested.shhs.pycoverage is 65.08%, below the 80% bar (the whole repo is at 89.34%, which is fine). Please add a constructor-level test, for example with a small synthetic dataset, and test the cache fallback.
Optional suggestions:
- Move the
if __name__ == "__main__"block to the end of tests/core/test_shhs.py. It currently sits mid-file, before more test classes. - Remove the unused
original_contentvariable in the tests. - Update the old API usage in examples/datasets_overview.ipynb and in the
__main__block of sleep_staging.py. - The description lists only a syntax check and an import check. Please confirm that the new tests pass.
Reviewed commit 10b0c55
|
/review Generated by Claude Code |
|
|
||
| rows.append({ | ||
| "patient_id": pid, | ||
| "signal_file": str(edf_path.relative_to(root)), |
There was a problem hiding this comment.
[high] signal_file stored relative to root, so downstream reads fail
signal_file is written as edf_path.relative_to(root) (e.g. edfs/shhs1/shhs1-200001.edf), and label_file (lines 145-153) is also relative. BaseDataset passes CSV values through unchanged, and tasks open event.signal_file / event.label_file directly (pyhealth/tasks/sleep_staging_nsrr.py:240-241, pyhealth/tasks/signal_forecasting.py:247). These paths only resolve when the working directory is root's parent, so samples cannot be read in normal use. When the cache fallback in init (line 100) switches root to the cache directory, the relative paths are resolved against the wrong folder. Fix: store absolute paths, for example str(edf_path.resolve()) for signal_file and str((root / label_file).resolve()) for label_file, as chbmit.py:213 does. Add a test that asserts the stored path exists on disk.
| for visit in ("shhs1", "shhs2"): | ||
| shared_csv = root / f"shhs-{visit}-pyhealth.csv" | ||
| cache_csv = cache_dir / f"shhs-{visit}-pyhealth.csv" | ||
| if shared_csv.exists() or cache_csv.exists(): |
There was a problem hiding this comment.
[high] Dev-mode subset is persisted and reused by full runs
prepare_metadata() skips a visit whenever its CSV exists, whatever dev was. A dev=True run writes only 5 patients per visit (line 140) to the shared CSV, and a later dev=False run reuses that CSV and silently trains on 5 patients. The reverse order also leaks the full CSV into dev runs. Fix: keep dev-limited metadata out of the shared file name (for example a -dev suffix in the file name), or skip persisting it, and add a test that a dev run followed by a full run returns all patients.
There was a problem hiding this comment.
AI Review
suggestion: need change
decision: human review
Thank you for migrating SHHSDataset to the 2.0 BaseDataset, with a YAML config, metadata CSV indexing and tests. It's close, but a few blocking problems remain.
To get this merged:
- Relative file paths:
signal_fileandlabel_fileare written relative toroot(for exampleedfs/shhs1/shhs1-200001.edf). Existing tasks such assleep_staging_nsrr.pyandsignal_forecasting.pyopenevent.signal_filedirectly, with no root join. Loading would only work when the working directory happens to be the parent ofroot. The cache fallback makes this worse, becauserootis then replaced by the cache directory. Please write absolute paths, or otherwise make sure they resolve. Add a test that checks the paths resolve to real files. - Dev-mode CSV reuse:
prepare_metadata()skips any CSV that already exists, whateverdevis. Adev=Truerun writes a 5-row CSV, and later full runs silently reuse it. Please write dev-mode metadata under a separate name or location, or make the cache depend ondev. - Coverage:
shhs.pyis at 65%, below the 80% bar. The tests bypass__init__with__new__, so the constructor and the cache-fallback logic are never run. Please add a test that builds aSHHSDatasetand checks that it loads, and one that covers the cache fallback. - Breaking changes: the old
refresh_cacheargument and theparse_patient_idandpatientsmembers are gone. Please say so clearly in the description. The oldprocess_EEG_dataalso usedannotations-events-profusion/shhs1for visit 1 and.../labelfor visit 2. Please confirm that this layout is intended. - Link between components: the description doesn't mention that the dataset uses the shared
metadata_csvhelper. Please confirm that this is intended and say why, or keep the dataset self-contained.
Optional suggestions:
- The
if __name__ == "__main__"block intest_shhs.pysits in the middle of the file, before the additional test class, so move it to the end. Also remove the unusedoriginal_contentvariable. - Add a docs/api page for the dataset if one doesn't exist.
Reviewed commit fe0d268
Summary
Migrates SHHSDataset from the legacy BaseSignalDataset (1.x) to BaseDataset (2.0), following the TUEV reference port as specified in the bounty.
Changes
pyhealth/datasets/shhs.py: Rewrote SHHSDataset to extend BaseDataset instead of BaseSignalDataset. Added prepare_metadata() that indexes EDF files into metadata CSVs with cache fallback via metadata_cache_dir. Preserved dev mode, patient_id parsing, and label_file paths.
pyhealth/datasets/configs/shhs.yaml: New YAML config defining shhs1 and shhs2 tables with patient_id, signal_file, and label_file attributes.
Testing
Bounty
BTY-23: SHHS Dataset PyHealth 2.0 Migration