TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Tumo505/SSL-for-ECG-classification/llms.txt
Use this file to discover all available pages before exploring further.
ssrl_ecg.utils module provides four stateless helper functions that cover the most common infrastructure concerns in an ECG self-supervised learning experiment: reproducibility seeding, device selection, multi-label evaluation metrics, and masked-reconstruction data preparation. All four are imported directly from the top-level package path.
set_seed
random module, NumPy, and PyTorch (both CPU and all CUDA devices) to a fixed seed. This is the minimal reproducibility contract required before any experiment.
Integer seed value. Common choices are
42, 0, or 2024. The same value must be used across all runs you wish to compare.| RNG | Call |
|---|---|
| Python stdlib | random.seed(seed) |
| NumPy | np.random.seed(seed) |
| PyTorch CPU | torch.manual_seed(seed) |
| PyTorch all GPUs | torch.cuda.manual_seed_all(seed) |
None.
Example
choose_device
torch.device. When a GPU is found, it also warms up the device, prints hardware diagnostics, and enables the cuDNN auto-tuner for optimal convolutional performance.
Returns
torch.device("cuda:0") if a CUDA-capable GPU is detected; torch.device("cpu") otherwise.GPU configuration performed
When a GPU is available,choose_device performs the following side effects before returning:
| Action | Effect |
|---|---|
torch.cuda.set_per_process_memory_fraction(0.95) | Reserves 95% of GPU VRAM for the current process |
torch.cuda.empty_cache() | Clears the CUDA allocator cache before training begins |
torch.backends.cudnn.benchmark = True | Enables cuDNN algorithm auto-selection (faster for fixed input sizes) |
torch.backends.cudnn.deterministic = False | Trades strict reproducibility for runtime speed |
Console output (GPU present)
Example
choose_device always returns cuda:0 (the first GPU). For multi-GPU training with DistributedDataParallel, call torch.device(f"cuda:{local_rank}") directly instead of using this helper.multilabel_metrics
Ground-truth binary label matrix of shape
[n_samples, n_classes]. Each entry must be 0 or 1.Predicted probability matrix of shape
[n_samples, n_classes]. Each entry should be a continuous value in [0, 1] (e.g. sigmoid outputs).Decision threshold applied to
y_prob to produce binary predictions for F1, sensitivity, and specificity. AUROC is threshold-independent and always uses the raw probabilities.Dictionary with four keys:
| Key | Description |
|---|---|
f1_macro | Macro-averaged F1 score across all classes (sklearn.metrics.f1_score with average="macro") |
auroc_macro | Macro-averaged area under the ROC curve; classes with only one unique label value in y_true are silently skipped. Returns NaN if no class has both labels present. |
sensitivity_micro | Micro-level sensitivity (recall): TP / (TP + FN) summed across all class-sample pairs, with a small 1e-8 epsilon to prevent division by zero |
specificity_micro | Micro-level specificity: TN / (TN + FP) summed across all class-sample pairs |
Class-skipping behaviour
auroc_macro silently skips any class c where len(np.unique(y_true[:, c])) < 2. This handles the common scenario in cardiovascular datasets where a rare condition has no positive examples in the evaluation split. The mean is taken over the remaining classes.
Example
Integration with a training loop
apply_random_mask
mask_ratio and block_size, and each block’s start position is sampled uniformly at random. Used to prepare inputs for masked auto-encoder (MAE) style reconstruction pretraining.
Input tensor of shape
[batch, channels, time]. A clone is made internally; the original tensor is not modified.Fraction of the total signal length to mask, in
[0, 1]. A value of 0.15 masks approximately 15% of each sample. Passing 0 or a negative value returns x unchanged.Length of each contiguous masked block in time-steps. At 500 Hz this corresponds to 100 ms per block. Larger
block_size values create fewer but longer gaps, which increase reconstruction difficulty and are generally better for pretraining. The number of masked blocks per sample is max(1, floor(T × mask_ratio / block_size)).Masked copy of the input tensor with the same shape
[batch, channels, time]. Masked positions are set to 0.0 across all channels simultaneously, preserving inter-lead alignment within each masked window.Understanding block_size
The
block_size parameter controls the trade-off between mask granularity and reconstruction difficulty:- Small
block_size(e.g.10): Many short gaps. The encoder can often interpolate across them using local context — easier pretraining task, weaker representations. - Large
block_size(e.g.100–200): Fewer, longer gaps. The encoder must learn longer-range temporal dependencies to reconstruct them — harder task, typically stronger representations. - Default
50(100 ms at 500 Hz): Covers roughly one cardiac half-cycle, making it difficult to reconstruct without learning global cardiac rhythm structure.