deepecgkit.datasets¶
Dataset classes, data modules, and preprocessing utilities for ECG analysis.
Base Classes¶
BaseECGDataset
¶
Bases: Dataset, ABC
Base class for all ECG datasets in deepecg-kit.
Subclasses implement _download and _load_data. The accessor layer
(__getitem__, __len__, num_classes, class_names,
get_record_info, get_class_distribution) is provided here for any
dataset that fills the standard storage attributes during _load_data:
self.signals: sequence of per-sample arrays (list or ndarray)self.labels: sequence of integer class labelsself.record_names: one source-record name per sampleCLASS_LABELS: class-level list of class namesbinary_classification: optional bool collapsing labels to Non-AF/AF
Datasets with a different storage model (multi-label PTB-XL, the composed unified-af) override the relevant accessors.
Source code in deepecgkit/datasets/base.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
group_ids
property
¶
Identifiers used to keep correlated samples inside a single split.
Defaults to record_names, which is correct whenever one record corresponds to one subject. Datasets where a single subject contributes several records must override this to return subject-level identifiers, otherwise samples from the same subject can be divided across train and test.
Returns:
| Type | Description |
|---|---|
list[str]
|
One identifier per sample, or an empty list if grouping is unavailable. |
get_default_data_dir
classmethod
¶
Get the default data directory for this dataset.
Returns:
| Type | Description |
|---|---|
Path
|
|
Path
|
|
Path
|
points elsewhere. |
Source code in deepecgkit/datasets/base.py
is_downloaded
classmethod
¶
Report whether a usable copy of the dataset is present.
The default treats any non-empty directory as the dataset. Datasets that download archives override this to look for the files their loader needs, so an interrupted download is detected and repeated rather than mistaken for the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dir
|
str | Path | None
|
Directory to inspect; the default data directory when None |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True when the dataset can be loaded from data_dir as it stands. |
Source code in deepecgkit/datasets/base.py
download_to
classmethod
¶
Download the dataset without constructing (and loading) an instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dir
|
str | Path | None
|
Target directory; the default data directory when None |
None
|
verbose
|
bool
|
Print progress messages |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
The directory the dataset was downloaded into. |
Source code in deepecgkit/datasets/base.py
download
¶
get_record_info
¶
Describe one sample: source record, label, class name, and shape.
Source code in deepecgkit/datasets/base.py
stratification_labels
¶
Return every sample's label as getitem gives it, without reading a signal.
ECGDataModule stratifies its split on these. Collecting them through getitem reads every signal on the way, which for a memory-mapped dataset of several gigabytes means paging all of it in.
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Integer labels, one per sample, or None when they cannot be had |
ndarray | None
|
without reading the samples: a target_transform is set, or the |
ndarray | None
|
subclass builds its samples in a getitem of its own. |
Source code in deepecgkit/datasets/base.py
get_class_distribution
¶
Count samples per class name.
Source code in deepecgkit/datasets/base.py
get_preprocessing
¶
Describe how a raw recording becomes one of this dataset's samples.
Checkpoints record this (see ECGTrainer.data_config) so that a new recording can be put through the same steps before it reaches the model: resample to sampling_rate, cut into segments of segment_duration_seconds, then normalise each segment.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Mapping with sampling_rate, segment_duration_seconds (None when |
dict[str, Any]
|
recordings are not segmented), normalization, and leads. |
Source code in deepecgkit/datasets/base.py
get_metadata
¶
Get metadata about the dataset.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing metadata such as: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
Source code in deepecgkit/datasets/base.py
Data Module¶
ECGDataModule
¶
Data module for ECG datasets.
Handles dataset creation, train/val/test splitting, and DataLoader construction.
Source code in deepecgkit/datasets/modules.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
setup
¶
Set up the datasets.
The dataset is loaded and split on the first call. Later calls, which the trainer makes for every stage, return at once unless the dataset or a split setting has changed since.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
str | None
|
Current stage ('fit', 'validate', 'test', or 'predict') |
None
|
Source code in deepecgkit/datasets/modules.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
train_dataloader
¶
val_dataloader
¶
test_dataloader
¶
describe
¶
Describe the data a model is trained on, in the form checkpoints record.
ECGTrainer stores this as its data_config, which is what lets a checkpoint be used on new recordings: it names the preprocessing the inputs need and the classes the outputs stand for.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Mapping with the registered dataset name, its preprocessing |
dict[str, Any]
|
(sampling rate, segment length, normalisation), the class names, |
dict[str, Any]
|
the sample shape, the labelling mode, and the split protocol. |
Source code in deepecgkit/datasets/modules.py
get_metadata
¶
Get metadata about the dataset.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing dataset metadata |
Source code in deepecgkit/datasets/modules.py
Data Splitting¶
DataSplitter
¶
Handles dataset splitting into train, validation, and test sets.
Source code in deepecgkit/datasets/splitting.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
split
¶
Split the dataset into train, validation, and test sets.
Returns:
| Type | Description |
|---|---|
tuple[Dataset, Dataset, Dataset]
|
Tuple of (train_dataset, val_dataset, test_dataset) |
Source code in deepecgkit/datasets/splitting.py
Preprocessing¶
ECGStandardizer
¶
Resamples, length-standardises, and normalises raw ECG signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_sampling_rate
|
int
|
Sampling rate signals are resampled to (Hz) |
300
|
target_length
|
int | None
|
Fixed output length in samples; mutually exclusive with target_duration_seconds, None leaves lengths unchanged |
None
|
target_duration_seconds
|
float | None
|
Fixed output duration; converted to samples at the target rate |
None
|
normalization
|
str
|
"zscore", "minmax", or "none" |
'zscore'
|
clip_method
|
str
|
Where clipping/padding takes place: "center", "start", or "end" |
'center'
|
Source code in deepecgkit/datasets/preprocessing.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
resample
¶
Resample a (leads, samples) or 1-D signal to the target rate.
Source code in deepecgkit/datasets/preprocessing.py
normalize
¶
Normalise per lead using the configured method.
Source code in deepecgkit/datasets/preprocessing.py
clip_or_pad
¶
Bring the signal to target_length by clipping or zero-padding.
Source code in deepecgkit/datasets/preprocessing.py
ECGSegmenter
¶
Cuts a continuous recording into fixed-length, optionally overlapping segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segment_duration_seconds
|
float
|
Length of each segment in seconds |
required |
sampling_rate
|
int
|
Sampling rate of the incoming signal (Hz) |
required |
overlap
|
float
|
Fraction of a segment shared with its successor, in [0, 1) |
0.0
|
Source code in deepecgkit/datasets/preprocessing.py
segment
¶
Cut the signal into segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ecg_signal
|
ndarray
|
Signal of shape (leads, samples) or (samples,) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (segments, start indices); both empty when the signal is |
ndarray
|
shorter than one segment. |
Source code in deepecgkit/datasets/preprocessing.py
RhythmAnnotationExtractor
¶
Expands wfdb rhythm annotations into per-sample label timelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampling_rate
|
int
|
Rate of the (possibly resampled) signal the labels must align with (Hz) |
required |
binary_classification
|
bool
|
Collapse all rhythms to AF (1) vs Non-AF (0) |
False
|
Source code in deepecgkit/datasets/preprocessing.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
extract_labels
¶
extract_labels(
annotation: Any,
signal_length: int,
original_sampling_rate: int | None = None,
) -> np.ndarray
Expand a wfdb annotation into one label per signal sample.
Only rhythm-change annotations, whose aux_note opens a rhythm such as
(AFIB or (N, move the label: each one sets the rhythm from its
own position until the next rhythm change. Some annotation files
(LTAFDB's among them) also hold an annotation for every beat, with an
empty aux_note. Those say nothing about the rhythm and are skipped;
treating them as changes would end every episode at its first beat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
|
Any
|
wfdb annotation with sample and aux_note attributes |
required |
signal_length
|
int
|
Length of the (resampled) signal in samples |
required |
original_sampling_rate
|
int | None
|
Rate the annotation indices refer to, used to rescale them onto the resampled signal |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Integer label per sample, 0 where no rhythm is annotated. |
Source code in deepecgkit/datasets/preprocessing.py
segment_with_labels
¶
segment_with_labels(
labels: ndarray,
segment_start_indices: ndarray,
segment_length: int,
) -> np.ndarray
Assign each segment the majority label of its samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
ndarray
|
Per-sample label timeline from extract_labels |
required |
segment_start_indices
|
ndarray
|
Segment start positions from ECGSegmenter |
required |
segment_length
|
int
|
Segment length in samples |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
One label per segment. |
Source code in deepecgkit/datasets/preprocessing.py
convert_to_tensor
¶
Convert an array to a tensor of the given dtype, passing tensors through.
Source code in deepecgkit/datasets/preprocessing.py
Dataset Implementations¶
AFClassificationDataset
¶
Bases: BaseECGDataset
PhysioNet/Computing in Cardiology Challenge 2017 AF Classification Dataset.
This dataset contains over 10,000 single-lead ECG recordings of 30-60 seconds duration for atrial fibrillation (AF) classification. Each recording is labeled as one of four categories: Normal (N), Atrial Fibrillation (A), Other rhythm (O), or Noisy (~).
The recordings are from AliveCor device and represent patient-initiated recordings.
Reference
Clifford GD, Liu C, Moody B, Li-wei HL, Silva I, Li Q, Johnson AE, Mark RG. AF classification from a short single lead ECG recording: The PhysioNet/computing in cardiology challenge 2017. In 2017 Computing in Cardiology (CinC) 2017 Sep 24 (pp. 1-4). IEEE.
URL
https://physionet.org/content/challenge-2017/1.0.0/
Source code in deepecgkit/datasets/af_classification.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
is_downloaded
classmethod
¶
Report whether the labels and the training recordings are present.
A directory holding only an interrupted download (a partial file, or a truncated archive) does not count, so download=True repeats the download instead of failing on the leftovers.
Source code in deepecgkit/datasets/af_classification.py
LTAFDBDataset
¶
Bases: BaseECGDataset
Long-Term AF Database (LTAFDB) Dataset.
Contains 84 long-term (typically 24-hour) two-lead ECG recordings of subjects with paroxysmal or sustained atrial fibrillation. Rhythm annotations indicate Normal, AF, Atrial Flutter, and Junctional rhythm segments.
Reference
Petrutiu S, Sahakian AV, Swiryn S. Abrupt changes in fibrillatory wave characteristics at the termination of paroxysmal atrial fibrillation in humans. Europace. 2007;9(7):466-470.
URL
https://physionet.org/content/ltafdb/1.0.0/
Source code in deepecgkit/datasets/ltafdb.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
is_downloaded
classmethod
¶
Report whether record files, or a finished segment cache, are present.
A directory still holding the archive or a partial download does not count: the archive is deleted once extraction finishes.
Source code in deepecgkit/datasets/ltafdb.py
MITBIHAFDBDataset
¶
Bases: BaseECGDataset
MIT-BIH Atrial Fibrillation Database (AFDB) Dataset.
Contains 25 long-term (10-hour) two-lead ECG recordings from subjects with atrial fibrillation (mostly paroxysmal). Rhythm annotations indicate Normal, AF, Atrial Flutter, and Junctional rhythm segments.
Reference
Moody GB, Mark RG. A new method for detecting atrial fibrillation using R-R intervals. Computers in Cardiology. 1983;10:227-230.
URL
https://physionet.org/content/afdb/1.0.0/
Source code in deepecgkit/datasets/mitbih_afdb.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
is_downloaded
classmethod
¶
Report whether extracted record files are present.
A directory still holding the archive or a partial download does not count: the archive is deleted once extraction finishes.
Source code in deepecgkit/datasets/mitbih_afdb.py
PTBXLDataset
¶
Bases: BaseECGDataset
PTB-XL ECG Dataset.
PTB-XL is a large publicly available electrocardiography dataset containing 21,837 clinical 12-lead ECGs from 18,885 patients of 10 second length. Each ECG is annotated with up to 71 different diagnostic statements conforming to the SCP-ECG standard.
The dataset supports multiple diagnostic classification tasks: - Superclass: 5 diagnostic superclasses (NORM, MI, STTC, CD, HYP) - Subclass: 23 diagnostic subclasses - Diagnostic: All individual diagnostic SCP codes (~44 statements) - Form: 19 form statements - Rhythm: 12 rhythm statements - All: All SCP statement codes (~71 statements)
Reference
Wagner P, Strodthoff N, Bousseljot RD, Kreiseler D, Lunze FI, Samek W, Schaeffter T. PTB-XL, a large publicly available electrocardiography dataset. Scientific Data. 2020 May 25;7(1):154.
URL
https://physionet.org/content/ptb-xl/1.0.3/
Source code in deepecgkit/datasets/ptbxl.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
group_ids
property
¶
Patient identifiers, used so a patient never spans multiple splits.
PTB-XL contains multiple ECGs for some patients, so grouping by ecg_id would place different recordings of the same patient in train and test. The official strat_fold assignment is likewise made per patient.
Returns:
| Type | Description |
|---|---|
list[str]
|
One patient identifier per loaded record. |
is_downloaded
classmethod
¶
Report whether the extracted metadata table is present.
A directory still holding the archive or a partial download does not count: the archive is deleted once extraction finishes.
Source code in deepecgkit/datasets/ptbxl.py
get_preprocessing
¶
Describe the preprocessing, adding the fixed recording duration.
Source code in deepecgkit/datasets/ptbxl.py
get_record_info
¶
Get record information for a specific sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the sample |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing record information |
Source code in deepecgkit/datasets/ptbxl.py
get_class_distribution
¶
Get the distribution of classes in the dataset.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary mapping class names to their counts |
Source code in deepecgkit/datasets/ptbxl.py
get_folds_split
¶
get_folds_split(
train_folds: list[int] | None = None,
val_folds: list[int] | None = None,
test_folds: list[int] | None = None,
) -> dict[str, PTBXLDataset]
Create train/val/test splits based on stratified folds.
The PTB-XL dataset comes with 10 pre-defined stratified folds. Recommended split: folds 1-8 for training, 9 for validation, 10 for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_folds
|
list[int] | None
|
Folds for training (default: 1-8) |
None
|
val_folds
|
list[int] | None
|
Folds for validation (default: 9) |
None
|
test_folds
|
list[int] | None
|
Folds for testing (default: 10) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, PTBXLDataset]
|
Dictionary with 'train', 'val', 'test' PTBXLDataset instances |
Source code in deepecgkit/datasets/ptbxl.py
UnifiedAFDataset
¶
Bases: BaseECGDataset
Unified AF Dataset combining multiple PhysioNet AF databases.
Combines samples from the PhysioNet 2017 Challenge, MIT-BIH AFDB, and LTAFDB into a single dataset for AF classification. Supports both binary (AF vs Non-AF) and 4-class (Normal, AF, AFL, J) classification modes.
PhysioNet 2017 labels are remapped to the unified scheme
- Normal (N) → Normal, AF (A) → AF
- Other (O) and Noisy (~) are dropped in 4-class mode
- Other (O) → Non-AF and Noisy (~) is dropped in binary mode
A source that cannot be loaded is an error: a dataset built from two of its three sources is a different dataset under the same name. Pass skip_unavailable=True to build it from whichever sources do load, or name the wanted sources in datasets.
Source code in deepecgkit/datasets/unified_af.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
is_downloaded
classmethod
¶
Report whether every source dataset is present in its own directory.
Source code in deepecgkit/datasets/unified_af.py
stratification_labels
¶
Return every source's labels in order, without reading a signal.
Source code in deepecgkit/datasets/unified_af.py
get_preprocessing
¶
Describe the preprocessing shared by every source dataset.
File Reading and Inference Preprocessing¶
read_ecg_recording
¶
Read an ECG recording from a supported file format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a .npy, .csv, or WFDB (.dat/.hea) recording |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The decoded signal array. WFDB records are transposed to put leads |
ndarray
|
first; .npy and .csv arrays are returned as stored. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file suffix is not one of SUPPORTED_ECG_SUFFIXES. |
Source code in deepecgkit/datasets/io.py
read_ecg_recording_with_rate
¶
Read an ECG recording together with its sampling rate, where the file records one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to a .npy, .csv, or WFDB (.dat/.hea) recording |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of the decoded signal array and its sampling rate in Hz. WFDB |
float | None
|
records carry their rate in the header; .npy and .csv files carry none, |
tuple[ndarray, float | None]
|
so the rate is None for them. WFDB records are transposed to put leads |
tuple[ndarray, float | None]
|
first; .npy and .csv arrays are returned as stored. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file suffix is not one of SUPPORTED_ECG_SUFFIXES. |
Source code in deepecgkit/datasets/io.py
as_model_input
¶
Reshape an ECG array to the (batch, leads, time) layout models expect.
A one-dimensional array is treated as a single single-lead recording. A two-dimensional array is ambiguous, so the lead count decides the orientation: whichever axis matches input_channels becomes the lead axis. When the lead count is unknown or one, rows are treated as independent single-lead recordings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Signal array of one, two, or three dimensions |
required |
input_channels
|
int | None
|
Number of leads the target model consumes, used to disambiguate two-dimensional input |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (batch, leads, time). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a two-dimensional array matches neither orientation, or the array has more than three dimensions. |
Source code in deepecgkit/datasets/io.py
prepare_recording
¶
prepare_recording(
recording: ndarray,
preprocessing: Mapping[str, Any] | None,
source_sampling_rate: float | None = None,
) -> PreparedRecording
Put one raw recording through a model's training-time preprocessing.
The steps mirror what the dataset loaders do: resample to the training rate, cut into training-length segments, and normalise each segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recording
|
ndarray
|
Signal of shape (leads, samples) |
required |
preprocessing
|
Mapping[str, Any] | None
|
Mapping as returned by BaseECGDataset.get_preprocessing and recorded in checkpoints, with sampling_rate, segment_duration_seconds and normalization. None leaves the recording untouched and returns it as a single segment. |
required |
source_sampling_rate
|
float | None
|
Rate the recording was sampled at, in Hz. None assumes it is already at the training rate. |
None
|
Returns:
| Type | Description |
|---|---|
PreparedRecording
|
The prepared segments, with notes describing what was done. |
Source code in deepecgkit/datasets/io.py
PreparedRecording
dataclass
¶
One recording after the preprocessing a model was trained with.
Attributes:
| Name | Type | Description |
|---|---|---|
segments |
ndarray
|
Array of shape (num_segments, leads, samples), float32 |
start_seconds |
ndarray
|
Start of each segment within the recording, in seconds |
sampling_rate |
float | None
|
Rate of the segments in Hz, or None when unknown |
notes |
list[str]
|
What was applied or assumed, for reporting to the user |