Wav2Sleep (Carter & Tarassenko, 2024)#
wav2sleep stages sleep from any subset of cardiorespiratory signals (ECG, PPG, abdominal and thoracic effort) recorded over a whole night. Each signal has its own encoder; per-epoch embeddings are mixed across the available signals with attention and then across epochs with a dilated CNN. A signal that is missing is masked, so one model serves every combination of inputs it was trained on.
Paper: wav2sleep: A Unified Multi-Modal Approach to Sleep Stage Classification from Physiological Signals, ML4H 2024, arXiv:2411.04644.
Authors’ code and weights:
joncarter1/wav2sleep(GitHub),joncarter/wav2sleepandjoncarter/wav2sleep-eog(Hugging Face), MIT.physioex class:
physioex.models.wav2sleep:Wav2Sleep(a clean re-implementation; the original checkpoints load withstrict=Trueafter key conversion).
Quick start#
from physioex.data.datasets import get_dataset
from physioex.models import load_from_pretrained
from physioex.train import replication as rep
model = load_from_pretrained("wav2sleep", verbose=True) # downloads from the Hub
data = get_dataset("mesa")(
channels=["ECG", "PPG", "ABD", "THX"],
pipelines="wav2sleep", # the authors' preprocessing
sequence_length=0, # one item = one whole night
label_transform="aasm4", # W, Light (N1+N2), Deep, REM
trim_excess_wake=False,
)
test = data.get_splits(split="wav2sleep")[2]
for subject, signals, labels, metadata in rep.iter_nights(data, test[:1]):
stages = model.predict_night(signals) # (n_epochs,) int64 in 0..3
predict_night accepts any subset of the keys ECG, PPG, ABD, THX
(EOG-L, EOG-R for wav2sleep-eog); keys it does not know are ignored.
Nights longer than 10 h are truncated to 1200 epochs.
Inputs and preprocessing#
The wav2sleep preset reproduces the authors’ ingestion: ECG and EOG are
converted to millivolts, PPG and respiratory effort are scaled by their
physical range; each signal is linearly interpolated onto a grid of 1024
samples per 30-s epoch (256 for ABD/THX, 4096 for EOG), padded with zeros or
truncated to 10 h, and z-scored over the whole grid. The checkpoints use four
classes (label_transform="aasm4").
name |
signals |
classes |
|---|---|---|
|
ECG, PPG, ABD, THX |
4 (W, Light, Deep, REM) |
|
EOG-L, EOG-R |
5 (W, N1, N2, N3, REM) |
Replication#
examples/pretrained/wav2sleep/verify.py evaluates the checkpoint on the
paper’s test lists (get_splits(split="wav2sleep"), all visits of SHHS, MROS
and WSC) with the paper’s metric, pooled Cohen’s kappa, and compares with
Table 6. As in the authors’ evaluation, test nights without any N1, N3 or REM
epoch are left out, and a flat PPG is treated as absent.
The labels and the preprocessed ECG were checked against the authors’ own pipeline: the labels match on every test night of SHHS, MROS, MESA and CCSHS, and the ECG input matches (r = 1.0000, per-night kappa within 0.0004). With the public checkpoint, pooled kappa minus Table 6 is:
dataset |
all |
ECG+THX |
ECG |
PPG |
|---|---|---|---|---|
SHHS |
-0.007 |
-0.008 |
-0.007 |
- |
MESA |
-0.006 |
-0.019 |
-0.021 |
-0.011 |
WSC |
-0.003 |
-0.003 |
-0.011 |
- |
CCSHS |
+0.015 |
+0.014 |
+0.021 |
- |
MROS |
-0.005 |
-0.006 |
-0.005 |
- |
The gap comes from the checkpoint, not from the data handling. The authors confirmed (joncarter1/wav2sleep#2) that the model on the Hub is a later version: its effective receptive field is about 2 h instead of the whole 10-h night (fewer convolutional layers and smaller kernels), chosen to generalise to inputs of arbitrary length, and it scores about 1 % below the paper’s model on the original test sets. Table 6 is also the mean over three random seeds, so no single checkpoint reproduces it exactly. The targets are therefore reported, not asserted. CFS and CHAT (and the census rows that need them) are skipped when their data are not available.
Training#
physioex does not ship a training script for this model. The authors’ recipe
is summarised in examples/pretrained/wav2sleep/TRAINING.md.
API#
- class physioex.models.wav2sleep.Wav2Sleep(signal_encoders, epoch_mixer, sequence_mixer, num_classes)[source]
Bases:
ModuleModel for sleep staging.
This model is used to classify sleep stages from time-series inputs.
- The network works as follows:
Each input signal is passed through a signal encoder.
Embeddings are mixed for each sleep epoch level.
Sequence model mixes epoch features.
Classifier is applied to output features.
- Parameters:
signal_encoders (SignalEncoders)
epoch_mixer (MultiModalAttentionEmbedder)
sequence_mixer (SequenceCNN)
num_classes (int)
- forward(x)[source]
Classify sleep stages from a dictionary of input signals.
Returns per-epoch logits for each sleep stage. For
num_classes=4the class order is [W, N1+N2, N3, REM].- Parameters:
x (dict[str, Tensor]) – Dictionary of input tensors, keyed by signal name (ECG, PPG, ABD, THX, EOG-L, EOG-R). Each tensor has shape
[batch_size, T_s](T_s = samples_per_epoch * S) or[batch_size, L, T_e](T_e = samples_per_epoch). A signal may be absent (missing key) or entirely-inf; both mark it as unavailable for the attention padding mask.- Returns:
Tensor, shape [batch_size, S, num_classes]
- Return type:
- classmethod from_original(config_yaml_path, state_dict_path)[source]
Build the model from the authors’
config.yamland checkpoint.The config is parsed with
yaml.safe_load; each nested dict’s_target_names the corresponding authors’ class and is mapped to the ported class of the same name before the_target_key is dropped. The state dict is loaded withstrict=True.
- predict(x)[source]
Classify each epoch with the argmax class.
- predict_night(signals, metadata=None)[source]
Classify one whole night, already preprocessed by the
wav2sleeppreset.- Parameters:
signals (dict[str, np.ndarray]) – Mapping of signal name (ECG, PPG, ABD, THX, …) to a
(L, T_e)float array of one night, withT_e = SAMPLES_PER_EPOCH[signal]samples per 30-s epoch. Keys the model does not know are ignored; known keys missing fromsignalsare passed to the model as absent. The night is zero-padded (or truncated) to the model’s fixed 1200-epoch (10 h) grid only whenLdiffers from 1200, matching the authors’ padding/truncation of shorter/longer nights.metadata (dict | None) – Unused (kept for a common interface with the other cardiorespiratory models).
- Returns:
int64 class indices of shape
(L,)in the model’s own class order ([W, N1+N2, N3, REM] for the 4-class model). Runs undertorch.no_grad()in eval mode, batch of one, on the model’s current device.- Return type:
np.ndarray