physioex.models.jones_ecg.JonesECGNet#

class physioex.models.jones_ecg.JonesECGNet(variant='primary', n_classes=5)[source]#

Bases: Module

ECG-only sleep staging network of Jones, Itti & Sheth (2024).

Parameters:
  • variant (str) – one of VARIANTS (default "primary").

  • n_classes (int) – number of sleep classes (5: W, N1, N2, N3, REM).

Forward input:

  • x: (B, L, 1, 7680) or (B, L, 7680) float32 ECG, 256 Hz, in [-1, 1].

  • additional: (B, L, 4) auxiliary features; if None it is built with build_additional(), filling the columns the variant was not trained with by standard-normal noise.

  • padding_mask: (B, L) or (B, L, 1) bool/float, 1 = real epoch; default all ones.

Forward returns (B, L, n_classes) log-probabilities over [W, N1, N2, N3, REM].

__init__(variant='primary', n_classes=5)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • variant (str)

  • n_classes (int)

Methods

__init__([variant, n_classes])

Initialize internal Module state, shared by both nn.Module and ScriptModule.

add_module(name, module)

Add a child module to the current module.

apply(fn)

Apply fn recursively to every submodule (as returned by .children()) as well as self.

bfloat16()

Casts all floating point parameters and buffers to bfloat16 datatype.

buffers([recurse])

Return an iterator over module buffers.

build_additional(n_epochs, variant[, age, ...])

Build the (n_epochs, 4) auxiliary-feature matrix for one night.

children()

Return an iterator over immediate children modules.

compile(*args, **kwargs)

Compile this Module's forward using torch.compile().

cpu()

Move all model parameters and buffers to the CPU.

cuda([device])

Move all model parameters and buffers to the GPU.

double()

Casts all floating point parameters and buffers to double datatype.

eval()

Set the module in evaluation mode.

extra_repr()

Return the extra representation of the module.

float()

Casts all floating point parameters and buffers to float datatype.

forward(x[, additional, padding_mask])

Run per-epoch inference; returns (B, L, n_classes) log-probabilities.

from_original(checkpoint_path, variant)

Load one of the authors' released checkpoints.

get_buffer(target)

Return the buffer given by target if it exists, otherwise throw an error.

get_extra_state()

Return any extra state to include in the module's state_dict.

get_parameter(target)

Return the parameter given by target if it exists, otherwise throw an error.

get_submodule(target)

Return the submodule given by target if it exists, otherwise throw an error.

half()

Casts all floating point parameters and buffers to half datatype.

ipu([device])

Move all model parameters and buffers to the IPU.

load_state_dict(state_dict[, strict, assign])

Copy parameters and buffers from state_dict into this module and its descendants.

midnight_offset(start)

Signed offset in days of start from the nearest midnight.

modules([remove_duplicate])

Return an iterator over all modules in the network.

mtia([device])

Move all model parameters and buffers to the MTIA.

named_buffers([prefix, recurse, ...])

Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

named_children()

Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

named_modules([memo, prefix, remove_duplicate])

Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.

named_parameters([prefix, recurse, ...])

Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

parameters([recurse])

Return an iterator over module parameters.

predict_night(signals[, metadata])

Classify one whole night, already preprocessed by the jones_ecg preset.

register_backward_hook(hook)

Register a backward hook on the module.

register_buffer(name, tensor[, persistent])

Add a buffer to the module.

register_forward_hook(hook, *[, prepend, ...])

Register a forward hook on the module.

register_forward_pre_hook(hook, *[, ...])

Register a forward pre-hook on the module.

register_full_backward_hook(hook[, prepend])

Register a backward hook on the module.

register_full_backward_pre_hook(hook[, prepend])

Register a backward pre-hook on the module.

register_load_state_dict_post_hook(hook)

Register a post-hook to be run after module's load_state_dict() is called.

register_load_state_dict_pre_hook(hook)

Register a pre-hook to be run before module's load_state_dict() is called.

register_module(name, module)

Alias for add_module().

register_parameter(name, param)

Add a parameter to the module.

register_state_dict_post_hook(hook)

Register a post-hook for the state_dict() method.

register_state_dict_pre_hook(hook)

Register a pre-hook for the state_dict() method.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

set_extra_state(state)

Set extra state contained in the loaded state_dict.

set_submodule(target, module[, strict])

Set the submodule given by target if it exists, otherwise throw an error.

share_memory()

See torch.Tensor.share_memory_().

state_dict(*args[, destination, prefix, ...])

Return a dictionary containing references to the whole state of the module.

to(*args, **kwargs)

Move and/or cast the parameters and buffers.

to_empty(*, device[, recurse])

Move the parameters and buffers to the specified device without copying storage.

train([mode])

Set the module in training mode.

type(dst_type)

Casts all parameters and buffers to dst_type.

xpu([device])

Move all model parameters and buffers to the XPU.

zero_grad([set_to_none])

Reset gradients of all model parameters.

Attributes

T_destination

call_super_init

dump_patches

training

static build_additional(n_epochs, variant, age=None, sex=None, midnight_offset=None, generator=None)[source]#

Build the (n_epochs, 4) auxiliary-feature matrix for one night.

Columns: [sex (0=F, 1=M), age/100, night_e (−1→1 across the night), midnight_e]. For cyclic variants the last two columns are replaced by (cos t_e, sin t_e) with t_e = 2π(e/2880 + midnight_offset mod 1). Columns the variant was not trained with (see VARIANTS) are filled with standard-normal noise drawn from generator: one draw per epoch per column, except demographics which are drawn once per night (size 2) and repeated over the epochs.

Raises:

ValueError – if age, sex or midnight_offset is missing for a variant that was trained with it.

Parameters:
Return type:

Tensor

forward(x, additional=None, padding_mask=None)[source]#

Run per-epoch inference; returns (B, L, n_classes) log-probabilities.

Parameters:
Return type:

Tensor

classmethod from_original(checkpoint_path, variant)[source]#

Load one of the authors’ released checkpoints.

The checkpoints are plain dicts with a model_state_dict entry whose keys this re-implementation reproduces exactly, so the load is strict and needs no remapping.

Parameters:
  • checkpoint_path (str | Path) – path to the .pt checkpoint (e.g. downloaded from adammj/ecg-sleep-staging).

  • variant (str) – variant name (used to build the matching architecture).

Returns:

The model in eval() mode.

Return type:

JonesECGNet

static midnight_offset(start)[source]#

Signed offset in days of start from the nearest midnight.

Positive when the start is in the (next) morning, negative in the (previous) evening: 01:30 → +1.5/24, 23:00 → -1/24.

Parameters:

start (time | datetime)

Return type:

float

predict_night(signals, metadata=None)[source]#

Classify one whole night, already preprocessed by the jones_ecg preset.

Parameters:
  • signals (dict[str, np.ndarray]) – Mapping of signal name to a (L, 7680) float array of one night (256 Hz, 30-s epochs, in [-1, 1]); the night’s "ECG" channel is required. Unlike the fixed-grid models, JonesECGNet accepts any number of epochs, so the night is not padded or truncated here.

  • metadata (dict | None) – optional dict with "age" (years), "sex" ("M"/"F") and "recording_start" (datetime.time). The auxiliary features are built with build_additional() for the model’s variant; a missing value that the variant was not trained with (see VARIANTS) is filled with the variant’s noise fallback, with one warning per missing key.

Returns:

int64 class indices of shape (L,) in AASM 5-class order [W, N1, N2, N3, REM]. Runs under torch.no_grad() in eval mode, batch of one, on the model’s current device.

Return type:

np.ndarray