physioex.train.replication#
Library support for replicating published cardiorespiratory results.
The three integrated cardiorespiratory sleep-staging networks (Wav2Sleep, SleepPPG-Net and the ECG-only network of Jones et al.) report their headline numbers as Cohen’s kappa computed in two different ways:
pooled — one confusion matrix over every scored epoch of every recording (Wav2Sleep’s
kappa_T, Jones’overall_kappa);median record — one kappa per recording, then the median across recordings (SleepPPG-Net, Jones’ headline number).
This module provides the plumbing to recompute those numbers on whole nights and check them against the values printed in the papers:
iter_nights()pulls whole nights (preprocessed signals + aligned labels + metadata) out of aBasePhysioDatasetbuilt in whole-night mode (sequence_length <= 0);align()trims a prediction and its label array to a common length the way the authors’ inference code does (see the docstring);evaluate()wrapsphysioex.train.metrics.pooled_kappa()and friends;load_paper_metrics()/check()/format_table()read the paper’s claimed values (a small JSON file), compare them with the measured ones and render a fixed-width table.
No GPU or data access happens at import time.
Module Attributes
Visits that the published test splits draw from, per dataset registry name. |
Functions
|
Cut prediction and labels to their common length. |
|
Compare measured results against the paper's claimed values. |
|
Compute the kappa metrics of one evaluation run. |
|
Render comparison rows as a fixed-width text table. |
|
True when the night has no N1, N3 or REM epoch in its raw AASM labels. |
|
Yield one entry per whole night of the given subjects. |
|
Read the paper's claimed results from a JSON file. |
|
Constructor keyword sets that together cover a dataset's split nights. |
Classes
|
One number to replicate from a paper. |
- class physioex.train.replication.PaperTarget(dataset, condition, value, metric='pooled_kappa', tolerance=0.05, assert_=True, requires=<factory>, note='')[source]#
Bases:
objectOne number to replicate from a paper.
- Parameters:
- assert_#
True= the target must PASS (FAILotherwise);False= the number is only reported (REPORT).- Type:
- physioex.train.replication.SPLIT_VISITS: dict[str, tuple[int, ...]] = {'mros': (1, 2), 'shhs': (1, 2), 'wsc': (1, 2, 3, 4)}#
Visits that the published test splits draw from, per dataset registry name. The readers load one visit per instance (
visit=argument), so a replication builds one instance per visit and pools the nights.
- physioex.train.replication.align(pred, labels)[source]#
Cut prediction and labels to their common length.
Whole-night models with a fixed input grid (Wav2Sleep, SleepPPG-Net: 1200 epochs) produce predictions for padded epochs when the night is shorter than the grid, and the authors’ inference drops the tail labels of nights longer than the grid. Both effects leave pred and labels at different lengths; cutting both to
min(len)recovers the epochs that are scored and predicted alike.- Parameters:
- Returns:
(pred_aligned, labels_aligned, n_dropped)where the first two have shape(min(L_p, L_l),)andn_droppedis the number of labelled epochs that were dropped (max(0, L_l - min(L_p, L_l))) so callers can report it.- Return type:
- physioex.train.replication.check(results, targets)[source]#
Compare measured results against the paper’s claimed values.
- Parameters:
results (dict[tuple[str, str], dict]) – mapping
(dataset, condition)-> the dict returned byevaluate().targets (Sequence[PaperTarget]) – the paper targets from
load_paper_metrics().
- Returns:
One dict per target with keys
dataset,condition,metric,paper,ours,delta,tolerance,status(plusnote).statusis"SKIP"when any dataset in the target’srequireshas no results,"PASS"/"FAIL"whenassert_is true and|delta| <= tolerance(or not), and"REPORT"whenassert_is false.ours/deltaareNoneforSKIProws.- Return type:
- physioex.train.replication.evaluate(y_true_records, y_pred_records, n_classes)[source]#
Compute the kappa metrics of one evaluation run.
Wraps
physioex.train.metrics.pooled_kappa(),physioex.train.metrics.median_record_kappa()andphysioex.train.metrics.per_record_kappa()(epochs with an unscored true label are ignored there).- Parameters:
- Returns:
dict with
"pooled_kappa","median_record_kappa","per_record_kappa"(list),"n_records"and"n_epochs"(total scored epochs across recordings).- Return type:
- physioex.train.replication.format_table(rows)[source]#
Render comparison rows as a fixed-width text table.
- physioex.train.replication.has_scoring_issues(dataset, subject_id)[source]#
True when the night has no N1, N3 or REM epoch in its raw AASM labels.
wav2sleep’s ingestion marks such nights as
.issues(recordings scored sleep/wake only, with N2 as the sole sleep class) and its per-dataset test evaluation leaves them out (exclude_issues=True). The check reads the labels before anylabel_transform, sinceaasm4merges N1 into Light.- Parameters:
dataset (BasePhysioDataset)
subject_id (str)
- Return type:
- physioex.train.replication.iter_nights(dataset, subject_ids)[source]#
Yield one entry per whole night of the given subjects.
The dataset must be built in whole-night mode (
sequence_length <= 0); in that mode item indexiis exactly subjecti(seeBasePhysioDataset._get_recording_item).- Parameters:
dataset (BasePhysioDataset) – a
BasePhysioDatasetinstance (whole-night mode).subject_ids (Sequence[str]) – subject ids to iterate over, in any order.
- Yields:
Tuples
(subject_id, signals, labels, metadata)wheresignalsmaps the requested channel name (channel_info[key] ["request"], e.g."ECG"; the item key such as"ECG_0"when no request is recorded) to a(L, T_e)float32 array of the (preprocessed) whole-night signal. Channels the subject lacks (available=False, zero-filled by the dataset) are left out;labelsis the(L,)int64 label array in the dataset’s label transform (e.g.aasm4: W=0, Light=1, Deep=2, REM=3;-1unscored), aligned with the signals;metadatais the return value ofdataset.subject_metadata(subject_id)when the dataset provides that method, else{}.
- Return type:
Iterator[tuple[str, dict[str, np.ndarray], np.ndarray, dict]]
- physioex.train.replication.load_paper_metrics(path)[source]#
Read the paper’s claimed results from a JSON file.
The file has the top-level keys
"model","source","metric","tolerance"and"targets"(a list of objects with keysdataset,condition,valueand optionalmetric,tolerance,assert(bool, default true),requires,note). A target’s optionalmetric/tolerance/assertoverride the top-level ones.- Parameters:
- Returns:
(header, targets)whereheaderis the top-level dict without"targets"andtargetsthe list ofPaperTarget.- Return type:
tuple[dict, list[PaperTarget]]
- physioex.train.replication.replication_kwargs(dataset_name)[source]#
Constructor keyword sets that together cover a dataset’s split nights.
One dict per visit for multi-visit datasets (see
SPLIT_VISITS), else a single dict. CCSHS also getsexclude_subjects=[]: its default exclusion list (recordings whose XML disagrees with the NSRR summary CSV) is a physioex choice the published evaluations did not make.