Skip to content

Port SHHSDataset to the PyHealth 2.0 dataset API (part of #340) #1477

Open

Description

@ptsoumaki

Summary

SHHSDataset (pyhealth/datasets/shhs.py) still subclasses the 1.x stub
BaseSignalDataset and defines no __init__. The stub only emits a
DeprecationWarning, so root, dev and filepath are never set, and the
first call to process_EEG_data() raises AttributeError. The class cannot be
used at all on master. #1201 records this in its Group A table; #1451 makes
the failure explicit and notes that porting the remaining 1.x datasets is
separate work.

I propose to port SHHS to the 2.0 API, as tracked in #340, following TUEVDataset
as the reference port. This is engineering bounty E3. It mirrors #1468
(ISRUC), the other sleep-staging port in flight.

Dataset

  • Name: Sleep Heart Health Study (SHHS), visits 1 and 2 (NSRR).
  • Source: https://sleepdata.org/datasets/shhs
  • References: Quan SF et al., "The Sleep Heart Health Study: design,
    rationale, and methods", Sleep 20(12), 1997; Redline S et al., "Methods for
    obtaining and analyzing unattended polysomnography data for a multicenter
    study", Sleep 21(7), 1998.
  • Access: restricted. An NSRR account plus an approved data access request
    (manual approval). Download via the NSRR CLI (gem install nsrr, then
    nsrr download shhs/polysomnography). Data are not redistributable.
  • Content: overnight polysomnography, 6,441 participants at visit 1 and
    3,295 re-studied at visit 2. Each recording is scored in 30 s epochs with
    stages 0-4 (W, N1, N2, N3, REM).
  • Size: hundreds of GB. dev=True should remain the documented path for a
    first run.

Current state

  1. SHHSDataset has no __init__. process_EEG_data() reads self.root,
    self.dev and self.filepath, none of which exist.
  2. process_EEG_data() returns the 1.x shape, a dict of
    patient_id -> [{"load_from_path", "signal_file", "label_file", "save_to_path"}],
    which BaseDataset.set_task() does not consume.
  3. sleep_staging_shhs_fn (pyhealth/tasks/sleep_staging.py) is a plain
    function that writes one pickle per epoch and returns epoch_path. It
    hardcodes SAMPLE_RATE = 125, returns labels as strings "0"-"4", and
    selects channels positionally (X[[2, 7]] after dropping rows by index for
    the 15- and 16-channel layouts).
  4. The class docstring example calls dataset.stat() and dataset.info().
    BaseDataset provides stats(); neither name is correct as written.
  5. Suspected path bug: shhs2 annotation files are looked up under
    annotations-events-profusion/label/shhs2-{pid}-profusion.xml, while shhs1
    uses the shhs1/ sibling. The published NSRR layout has shhs1/ and
    shhs2/. I will confirm against the released data and report the result.

Proposed change

Following the pattern of TUEVDataset / SleepEDFDataset:

  1. pyhealth/datasets/shhs.py: SHHSDataset(BaseDataset) with
    • a YAML config (configs/shhs.yaml), one table per visit;
    • a metadata CSV (one row per recording: visit, participant, signal file,
      annotation file) written through the helpers in metadata_csv.py, so a
      read-only root and a non-writable root are handled;
    • a prepare_metadata() that is idempotent and skips already-listed files;
    • a "Getting the data" section in the class docstring, already present, kept
      as is;
    • default_task returning the new task.
  2. pyhealth/tasks/sleep_staging_shhs.py: a SleepStagingSHHS(BaseTask),
    modeled on SleepStagingSleepEDF, with options for
    • epoch_seconds (default 30, the scoring resolution);
    • which channels to use, by name;
    • stage mapping to W/N1/N2/N3/REM, with unmapped or unscored labels dropped
      and counted in a log message rather than silently;
    • labels emitted as int, since multiclass cannot consume "0"-"4".
  3. Tests (tests/core/): synthetic EDF and annotation fixtures built in the
    test, no network access. They check metadata construction, epoch count and
    label alignment, the channel-missing path, unmapped-label handling, and a
    non-writable root. tests/core/test_sleep_staging_shhs.py already covers the
    XML parser and channel selection and will be updated rather than replaced.
  4. Docs and example: a task page under docs/api/tasks/, an entry in
    docs/api/tasks.rst, and the existing
    examples/eeg/sleep_staging/sleep_staging_shhs_contrawr.py updated for the
    2.0 task (it passes refresh_cache, a 1.x keyword that no longer exists).

No change to BaseDataset, the Trainer or other shared code.

Points I would like maintainers to confirm

  1. patient_id across visits. SHHS visit 2 re-studies 3,295 of the visit 1
    participants, so a numeric participant id groups two recordings under one
    Patient. Should patient_id be the numeric participant (200001, two
    events) or visit-qualified (shhs1-200001, one recording per patient)? The
    former avoids the same participant landing on both sides of a split; the
    latter matches the 1.x epoch_path naming. I lean to the numeric id.
  2. Signal units. mne returns volts. EEGEventsTUEV reads in microvolts.
    Should the SHHS task emit microvolts for consistency with TUEV?
  3. Channel selection. Name-based selection with a documented positional
    fallback, or keep the 1.x X[[2, 7]]? The 1.x indices assume a specific
    montage order that I would like to verify on the released files first.
  4. epoch_seconds. The 1.x function accepts it but silently misaligns
    labels for anything other than 30, since the annotations are 30 s. Should the
    2.0 task restrict it to divisors of 30, as Port ISRUCDataset to the PyHealth 2.0 dataset API (part of #340) #1468 proposes for ISRUC?
  5. Scope. Whether to keep sleep_staging_shhs_fn importable as
    documented-deprecated (as done for the legacy drug-recommendation functions
    in Add DrugRecommendationOMOP task #1203), or replace it outright. I lean to keeping it, with the docs
    stating it cannot run through set_task().

The epoch and task structure overlaps #1468's SleepStagingISRUC. I am happy
to build on whatever shape that PR settles on, or to have both share a common
epoch-extraction helper, if that is preferred over two similar tasks.

I will verify the on-disk layout (file names, annotation format, channel names,
the shhs2 label/ path above) on the released data before writing the
fixtures, and report any discrepancy with the 1.x loader in the PR.

I am picking up E3 from the bounty list and will open a draft PR that links this
issue.

Activity

  1. solarsys commented on Nov 16, 2026

    @solarsys
    Collaborator

    Related: #1425 adds a new NSRRSleepDataset (pyhealth/datasets/nsrr_sleep.py) that reads SHHS and MESA for cardio-respiratory sleep staging. It doesn't port the existing SHHSDataset, but the two overlap in what they load, so it's worth checking whether this port should share config or tests with it, or stay separate.

  2. added 7 commits that reference this issue on Nov 16, 2026
    4c87f35
    700e85b
    cef41dc
    67f7fb1
    d630d90
    3b18272
    89da9b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions