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
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 1244 1245 1246 1247 1248 1249 | |
__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
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 | |
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 |