PhysioEx#
PhysioEx (Physiological Signal Explainer) is a Python library for explainable sleep staging with deep learning.
Overview#
PhysioEx provides a standard, reproducible way to train and evaluate state-of-the-art sleep-staging models, so that the effort can go into understanding what the models learn rather than into re-implementing them.
Data. Fifteen sleep datasets are read directly from the raw recordings; preprocessing runs lazily, per channel, and is cached on disk.
Models. Classic sleep-staging architectures, cardiorespiratory models and pretrained foundation encoders share one interface; published weights are loaded with
load_from_pretrained.Training. A PyTorch training and evaluation loop, also available from the command line (
train,finetune,test_model).Foundation encoders. Embedding extraction and linear probing on top of pretrained EEG and biosignal backbones.
Explainability. Gradient-based attribution (Saliency, Input×Gradient, Integrated and Expected Gradients, and their DFT/STFT variants), concept and prototype methods, and Layer-wise Relevance Propagation for CNN, recurrent and transformer models, with conservation diagnostics.
The full documentation is at https://guidogagl.github.io/physioex/.
Installation#
Install the latest release from PyPI (Python 3.11 or later):
pip install physioex
Optional extras add dependencies that only some features need:
Extra |
Enables |
|---|---|
|
the REVE encoder ( |
|
the HomePAP EDF fallback reader ( |
|
TensorBoard and Weights & Biases logging |
|
Layer-wise Relevance Propagation ( |
|
GPU monitoring during training ( |
pip install "physioex[explain]"
To install from source, which includes changes not yet released on PyPI:
git clone https://github.com/guidogagl/physioex.git
cd physioex
pip install -e .
CPU-only installs and
torchvision. Theexplainextra depends onzennit, which requirestorchvision. Iftorchcomes from the PyTorch CPU index, installtorchvisionfrom the same index before the extra; otherwise pip installs a CUDA build that fails at import withoperator torchvision::nms does not exist:pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu pip install "physioex[explain]"
Quickstart#
Set PHYSIOEX_DATA to the directory that holds your datasets.
Train a model#
import os
os.environ["PHYSIOEX_DATA"] = "/path/to/datasets"
from physioex.data.datasets import get_dataset, available_datasets
from physioex.models.tinysleepnet import TinySleepNet
from physioex.train.trainer import Trainer
print(available_datasets()) # -> ['alzheimers', 'ccshs', 'cfs', 'chat', 'dcsm', ...]
dataset = get_dataset("hmc")(
channels=["EEG", "EOG", "EMG"],
pipelines="time_domain", # preset preprocessing pipeline
sequence_length=21,
)
model = TinySleepNet(in_chan=3, n_classes=5)
model = Trainer.train(model=model, dataset=dataset, max_epochs=20)
The same dataset specification drives the command-line tools; see the CLI page in the documentation.
Load a pretrained model or extract embeddings#
from physioex.models import load_from_pretrained, extract_embeddings, linear_probe
from physioex.models import CBraModEncoder
model = load_from_pretrained("protosleepnet-gagliardi") # list_pretrained() lists all names
encoder = CBraModEncoder()
ds = encoder.get_dataset("hmc") # matched preprocessing
emb = extract_embeddings(encoder, ds)
scores = linear_probe(emb)
Stage whole nights from cardiorespiratory signals#
With sequence_length=0, one dataset item is one whole night.
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)
data = get_dataset("mesa")(
channels=["ECG", "PPG", "ABD", "THX"],
pipelines="wav2sleep", # the authors' preprocessing
sequence_length=0,
label_transform="aasm4", # W, Light, 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)
Models#
All models live in physioex.models.
Classic sleep-staging architectures#
Trained from scratch on epoch sequences.
Model |
Class |
Input |
|---|---|---|
|
raw signals |
|
|
raw signals |
|
Tsinalis et al. 2016 |
|
raw signals |
|
time-frequency images |
|
L-SeqSleepNet |
|
time-frequency images, long sequences |
|
time-frequency images |
|
CoRe-Sleep |
|
multimodal |
ProtoSleepNet |
|
prototype-based, interpretable by design |
Cardiorespiratory models#
Whole-night staging from ECG, PPG and respiratory effort. The architectures are re-implemented in PhysioEx; the published weights are downloaded from the original sources, and each model has a matching preprocessing preset.
Model |
Class |
Input |
|
Weights |
License |
|---|---|---|---|---|---|
|
any subset of ECG, PPG, ABD, THX (or EOG) |
|
authors (Hugging Face) |
MIT |
|
|
ECG, age, sex, start time |
|
authors (GitHub) |
AGPL-3.0 |
|
|
PPG |
|
third-party replica |
non-commercial |
examples/pretrained/<model>/verify.py compares each model with its paper on
the published test splits. The public wav2sleep checkpoint is a later version
than the paper’s model (about 1 % lower, as confirmed by the authors), so its
results are reported next to the paper’s numbers rather than matched.
Foundation encoders#
Pretrained backbones behind a uniform encode: (B, L, C, T) -> (B, L, D)
interface, for embedding extraction and linear probing.
Encoder |
Class |
|---|---|
CBraMod |
|
BENDR |
|
LaBraM |
|
BIOT |
|
SleepFM |
|
TF-C |
|
REVE |
|
S-JEPA |
|
NeuroLM |
|
Datasets#
Datasets are resolved by name with get_dataset(name); available_datasets()
lists them. PhysioEx reads the original recordings, which you obtain from the
source below. Descriptions of each dataset are in the
Data page of the
documentation.
Name |
Dataset |
Source |
Size |
|---|---|---|---|
|
PhysioNet |
197 recordings |
|
|
PhysioNet |
151 recordings |
|
|
ERDA |
255 recordings |
|
|
NSRR |
5,793 participants at visit 1 |
|
|
NSRR |
2,237 participants |
|
|
NSRR |
2,911 recordings |
|
|
NSRR |
1,500 participants at baseline, several visits |
|
|
NSRR |
1,914 recordings, 13 sites |
|
|
NSRR |
515 children |
|
|
NSRR |
visit-5 recordings |
|
|
NSRR |
baseline and follow-up recordings |
|
|
NSRR |
three subsets (lab-full, lab-split, home) |
|
|
CEAMS |
200 recordings |
|
|
Alzheimer’s disease dataset |
UZ Leuven |
69 participants |
|
Parkinson’s disease dataset |
UZ Leuven |
87 participants, up to 157 recordings |
Citation#
If you use PhysioEx, please cite:
@article{10.1088/1361-6579/adaf73,
author = {Gagliardi, Guido and Alfeo, Luca and Cimino, Mario G C A and Valenza, Gaetano and De Vos, Maarten},
title = {PhysioEx, a new Python library for explainable sleep staging through deep learning},
journal = {Physiological Measurement},
url = {http://iopscience.iop.org/article/10.1088/1361-6579/adaf73},
year = {2025},
}
License#
PhysioEx is released under the MIT License. Pretrained weights from third parties keep their own licenses, listed in the model tables above and printed when they are loaded.