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.
ECGAugmentations class is the core augmentation engine in SSRL-ECG. It generates two independently augmented views of the same ECG signal for contrastive learning objectives such as SimCLR and BYOL. Every transform is designed around the physiological and recording properties of 12-lead ECGs, ensuring that augmented views remain medically plausible while being sufficiently diverse to drive representation learning.
Class Overview
ECGAugmentations applies a two-stage pipeline to each input signal: a weak stage (always applied, small perturbations) followed by a strong stage (applied with probability prob_strong, larger structural transforms). The same pipeline is run twice on every input to produce two independent views, x1 and x2.
Constructor
5000 samples. Adjust to match your dataset’s temporal resolution._augment_bandpass_variation) to correctly map frequency bins to physical units. Set to 100 for PTB-XL’s low-resolution variant.0.8, roughly four in five augmentation calls include the full set of structural transforms. Reduce to 0.5 during development for faster iteration.__call__
x.
- 2D —
[channels, time]for a single sample (e.g.[12, 5000]) - 3D —
[batch, channels, time]for a mini-batch (e.g.[32, 12, 5000])
[channels, time] if a 2D tensor was passed, [batch, channels, time] if 3D. Values are clamped to [-10, 10].x1 ≠ x2 in virtually all cases.[channels, time] tensor is passed, the class silently inserts a batch dimension internally (unsqueeze(0)) so that all augmentations operate on the canonical 3D shape, then squeezes it back before returning. This means you can use the same augmenter in both dataset __getitem__ methods (single sample) and collated batch pipelines without any code changes.Usage Example
Augmentation Pipeline
The pipeline is divided into two sequential stages. The weak stage always runs; the strong stage runs with probabilityprob_strong.
- Weak Augmentations
- Strong Augmentations
| Method | Probability | Simulates |
|---|---|---|
_weak_jitter | 0.9 | Sensor noise, electrical interference — adds Gaussian noise with std=0.03 |
_weak_scaling | 0.8 | Recording gain drift across sessions — multiplies the whole signal by 1 ± 15% |
_augment_channel_noise | 0.6 | Differing signal quality across ECG leads — adds per-channel Gaussian noise at 0.5–2% of each lead’s standard deviation |
Augmentation Method Reference
_weak_jitter(x, std=0.03)
_weak_jitter(x, std=0.03)
std to the entire signal tensor. Applied with probability 0.9 on every call, making it the most consistently active augmentation.Simulates: Thermal sensor noise, 50/60 Hz electrical interference, and quantisation noise in ADC circuits._weak_scaling(x, scale_range=0.15)
_weak_scaling(x, scale_range=0.15)
[1 - scale_range, 1 + scale_range], i.e. [0.85, 1.15] at the default setting. Applied with probability 0.8.Simulates: Gain drift between recording sessions, automatic gain control variation, and electrode impedance changes._augment_channel_noise(x)
_augment_channel_noise(x)
channels leads independently, samples a noise level in [0.5%, 2%] of that lead’s empirical standard deviation and adds Gaussian noise at that level. Applied with probability 0.6 when channels > 1.Simulates: The real-world situation where individual ECG leads have different signal-to-noise ratios depending on electrode contact quality and anatomical positioning._time_warp(x, num_points=3)
_time_warp(x, num_points=3)
num_points intermediate control points with ±5% jitter, then resamples the signal through the resulting index mapping. Applied with probability 0.5.Simulates: Temporal distortions arising from heart rate variability, breathing-related signal elongation/compression, and slight recording speed errors._augment_time_shift(x, max_shift_ratio=0.1)
_augment_time_shift(x, max_shift_ratio=0.1)
[-max_shift × L, +max_shift × L]. Zeros pad the vacated end. Applied with probability 0.7.Simulates: Variability in recording trigger timing, lead-on delays, and heart-rate-driven beat alignment differences between sessions._augment_dropout(x, dropout_ratio=0.1)
_augment_dropout(x, dropout_ratio=0.1)
dropout_ratio × L samples, replacing masked values with the global signal mean. Unlike standard i.i.d. dropout, segments are spatially coherent. Applied with probability 0.5.Simulates: Signal interruptions from temporary electrode detachment, patient movement artefacts long enough to render a segment uninterpretable, and data transmission dropouts._augment_bandpass_variation(x)
_augment_bandpass_variation(x)
_augment_segment_cropping(x, crop_ratio_range=(0.1, 0.3))
_augment_segment_cropping(x, crop_ratio_range=(0.1, 0.3))
_augment_mixup(x, alpha=0.1)
_augment_mixup(x, alpha=0.1)
λ ~ Beta(alpha, alpha) and returns λ·x + (1−λ)·x_shuffled where x_shuffled is a randomly permuted copy of the batch. Requires batch > 1; silently skips otherwise. Applied with probability 0.4.Simulates: Averaged multi-lead readings, physiologically plausible blends of two patients’ signals in a shared recording environment, and label-space interpolation for multi-label classification._augment_cutmix(x, cutmix_prob=0.3)
_augment_cutmix(x, cutmix_prob=0.3)
batch > 1. Applied with probability equal to cutmix_prob (called with 0.3 in the augmentation pipeline).Simulates: Electrode cross-talk, stitched recordings from different sessions, and segment-level annotation noise._augment_motion_artifacts(x, artifact_duration_ratio=0.05)
_augment_motion_artifacts(x, artifact_duration_ratio=0.05)
_augment_channel_shift(x)
_augment_channel_shift(x)
1 ± 10%) and DC offset (drawn from N(0, 0.05)) to each lead separately. Applied with probability 0.6 when channels > 1.Simulates: Inter-lead amplitude variation from electrode placement differences, skin impedance heterogeneity, and device-specific per-channel gain calibration drift.Output Clamping
After both augmentation stages, all output values are hard-clamped to the range[-10, 10]: