Dataset¶
ecgbench.dataset — the single PyTorch Dataset every supported dataset loads
through, plus the collate function and the two errors it raises.
The read-time adapters (window=, leads=, units=, transform=) shape the
returned tensor only, in that order. They never touch the source files, the
exported fold CSVs, or validation — which reads whole records through its own
window-less copy of _load_signal in ecgbench/validation/engine.py.
dataset
¶
Unified PyTorch Dataset for loading any ECG dataset supported by ECGBench.
Uses the dataset's YAML config to determine how to load signals and metadata. Adding a new dataset requires only a config file — no changes to this class.
ECGDataset
¶
ECGDataset(dataset: str | Any, split: str | None = 'train', version: str = 'clean', data_path: Path | str | None = None, sampling_rate: int | None = None, fold_numbers: list[int] | None = None, transform: Callable | None = None, metadata_source: str = 'hf', labels: bool = False, leads: list[str] | None = None, units: str = 'mV', window: tuple[int, int | None] | None = None)
Bases: Dataset
PyTorch Dataset for loading any ECG dataset supported by ECGBench.
This class uses the dataset's YAML config to determine how to load signals and metadata. Adding a new dataset requires only a config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str | Any
|
Dataset slug (e.g., "ptbxl") or a DatasetConfig object |
required |
split
|
str | None
|
"train", "val", "test", or None. None selects records purely by
|
'train'
|
version
|
str
|
"clean" (default) or "original" |
'clean'
|
data_path
|
Path | str | None
|
Path to the dataset's signal files on disk. If None, attempts auto-download from config.download_url. |
None
|
sampling_rate
|
int | None
|
Which sampling rate to load (default: config.default_sampling_rate) |
None
|
fold_numbers
|
list[int] | None
|
Specific fold(s) to load. None = all folds for the split.
With a named |
None
|
window
|
tuple[int, int | None] | None
|
|
None
|
transform
|
Callable | None
|
Optional callable applied to the signal tensor, after
|
None
|
metadata_source
|
str
|
"hf" (download fold CSVs from HuggingFace) or "local". |
'hf'
|
leads
|
list[str] | None
|
select and reorder leads by name, e.g. |
None
|
units
|
str
|
"mV" (default) or "uV" — applied after lead selection and before
|
'mV'
|
labels
|
bool
|
attach per-record labels and metadata as |
False
|
Example
Source code in ecgbench/dataset.py
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 | |
__getitem__
¶
Get a single ECG record with signal and metadata.
The dict holds:
"signal": torch.Tensor, float32, shape (leads, samples).leadsislen(self.lead_names)after anyleads=selection, andsamplesis thewindow=length when one is set."record_id": record identifier"split": the dataset's split, or — when constructed withsplit=None— this record's owndefault_split"fold": int (if available)"labels": dict of label fields (only withlabels=True)- All other metadata columns
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The record, as described above. |
Raises:
| Type | Description |
|---|---|
WindowOutOfRangeError
|
|
Source code in ecgbench/dataset.py
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 | |
WindowOutOfRangeError
¶
Bases: ValueError
The requested sample window does not fit inside the record.
SplitsNotPublishedError
¶
Bases: RuntimeError
The dataset's splits are deliberately not on the Hub.
Raised instead of a bare 404 for credentialed or restricted sources, whose identifiers ECGBench will not republish. The message carries the command that regenerates the identical split locally.
UnitConversionError
¶
Bases: ValueError
The dataset's samples are not in a physical unit, so units= cannot apply.
Raised for sources whose publisher standardised the waveforms — see
DatasetConfig.signal_units. Scaling them by 1000 would produce a number
that looks like microvolts and means nothing.
ecg_collate_fn
¶
Custom collate function for ECG dataset batches.
Stacks tensors, keeps dicts and strings as lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
list[dict[str, Any]]
|
List of samples from the dataset |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Batched dictionary |