A step-by-step visual dissection explaining how patient-level dataset partitioning works, why string sorting caused the HEEDB validation anomaly, and how DataLoader batch composition controls self-supervised representation geometry.
bench-xecg, datasets like HEEDB, CODE, and Chapman are indexed by unique patients (not raw ECG recording files). When __getitem__(idx) is called, it retrieves recordings specifically belonging to that patient.
Because string sorting grouped all "I0006_..." patients at the very end, taking the tail 10% sent 100% of Hospital 2 to validation and starved training.
Every hospital and demographic cohort is uniformly distributed across both splits with a reproducible fixed seed.
ConcatDataset([CODE, Chapman, INCART, HEEDB]).
Here is what happens when the DataLoader constructs batches of size \(B=512\) under shuffle=False vs shuffle=True:
SIGReg evaluates: \(\mathcal{L}_{\text{SIGReg}} = \|\text{Cov}(\mathbf{Z}_B) - \mathbf{I}\|_F^2 + \dots\)
When 512 samples belong to one single hospital, all points collapse into one sector of the sphere. The covariance matrix has near-zero eigenvalues, and SIGReg flags this as collapsed variance.
Expected Geometry: With a uniform mixture across datasets and patients, the batch fills all orthogonal dimensions of the embedding space, matching standard Gaussian properties.
| Pipeline Component | Implementation Code | What It Solves |
|---|---|---|
| 1. Seeded Patient Split | _split_train_val() with np.random.default_rng(seed).permutation(n) |
Prevents alphabetical ID sorting artifacts in HEEDB, CODE, and Chapman. Ensures both train and val receive equal proportional slices of all hospital cohorts. |
| 2. Val Dataset Interleaving | val_dataset = Subset(val_dataset, val_order) in load_datasets() |
Ensures every validation batch contains a proportional mixture across all concatenated datasets, preventing batch-level covariance collapse. |
| 3. Training DataLoader | DataLoader(shuffle=True) in pretrain.py:170 |
Continuously randomizes batches across all patients and datasets every training epoch. |