API Reference: ecg_sdk.core.loaders¶
ecg_sdk.core.loaders
¶
ECG Data Loaders Module¶
Provides a unified high-level factory function load_ecg and specialized loaders
for clinical, benchmark, tabular, and synthetic ECG datasets:
- Universal multi-source dispatcher: load_ecg(...)
- PhysioNet WFDB records (local files or streaming from physionet.org)
- Automatic sampling frequency standardization (target_fs) and scaling of annotations
- Automatic voltage normalization (normalize='zscore'|'minmax'|'robust')
- MIT-BIH Arrhythmia Database (mitdb) with expert cardiologist annotations
- St. Petersburg INCART 12-lead Arrhythmia Database (incartdb)
- PTB-XL 12-lead diagnostic ECG database (ptb-xl)
- European ST-T Database (edb), Creighton University (cudb), Fantasia, etc.
- CSV / JSON tabular formats and NumPy / Pandas in-memory representations.
PHYSIONET_DATABASES = {'mitdb': {'name': 'MIT-BIH Arrhythmia Database', 'pn_dir': 'mitdb', 'nominal_fs': 360.0, 'leads': ['MLII', 'V1/V5'], 'description': 'Standard inter-patient arrhythmia benchmark with cardiologist annotations.', 'records': ['100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '111', '112', '113', '114', '115', '116', '117', '118', '119', '121', '122', '123', '124', '200', '201', '202', '203', '205', '207', '208', '209', '210', '212', '213', '214', '215', '217', '219', '220', '221', '222', '223', '228', '230', '231', '232', '233', '234']}, 'incartdb': {'name': 'St. Petersburg INCART 12-lead Arrhythmia Database', 'pn_dir': 'incartdb', 'nominal_fs': 257.0, 'leads': ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'], 'description': '75 12-lead recordings with detailed arrhythmia and ischemic annotations.', 'records': [f'I{i:02d}' for i in range(1, 76)]}, 'ptb-xl': {'name': 'PTB-XL 12-Lead Diagnostic ECG Database', 'pn_dir': 'ptb-xl/1.0.3/', 'nominal_fs': 500.0, 'leads': ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'], 'description': '21,837 clinical 12-lead records with SCP-ECG diagnostic statements.'}, 'nstdb': {'name': 'MIT-BIH Noise Stress Test Database', 'pn_dir': 'nstdb', 'nominal_fs': 360.0, 'leads': ['MLII', 'V1'], 'description': 'ECG signals with calibrated baseline wander, muscle tremor, and electrode noise.'}, 'afdb': {'name': 'MIT-BIH Atrial Fibrillation Database', 'pn_dir': 'afdb', 'nominal_fs': 250.0, 'leads': ['ECG1', 'ECG2'], 'description': '25 long-term recordings of human subjects with paroxysmal atrial fibrillation.'}, 'cudb': {'name': 'Creighton University Ventricular Tachyarrhythmia Database', 'pn_dir': 'cudb', 'nominal_fs': 250.0, 'leads': ['ECG'], 'description': '35 recordings of subjects experiencing ventricular tachycardia/fibrillation.'}, 'svdb': {'name': 'MIT-BIH Supraventricular Arrhythmia Database', 'pn_dir': 'svdb', 'nominal_fs': 128.0, 'leads': ['ECG1', 'ECG2'], 'description': '78 recordings of patients with supraventricular arrhythmias.'}, 'edb': {'name': 'European ST-T Database', 'pn_dir': 'edb', 'nominal_fs': 250.0, 'leads': ['Lead_0', 'Lead_1'], 'description': '90 ambulatory recordings for myocardial ischemia evaluation.'}, 'ludb': {'name': 'Lobachevsky University Electrocardiography Database', 'pn_dir': 'ludb', 'nominal_fs': 500.0, 'leads': ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'], 'description': '200 12-lead ECGs with delineations of P, QRS, and T wave boundaries.'}, 'fantasia': {'name': 'Fantasia Database (Healthy Aging)', 'pn_dir': 'fantasia', 'nominal_fs': 250.0, 'leads': ['RESP', 'ECG'], 'description': 'Simultaneous ECG and respiration recordings from young and elderly subjects.'}}
module-attribute
¶
DATABASE_ALIASES = {'mit-bih': 'mitdb', 'mit_bih': 'mitdb', 'mitbih': 'mitdb', 'mit': 'mitdb', 'mitdb': 'mitdb', 'incart': 'incartdb', 'incartdb': 'incartdb', 'ptb-xl': 'ptb-xl', 'ptbxl': 'ptb-xl', 'ptb': 'ptb-xl', 'nst': 'nstdb', 'nstdb': 'nstdb', 'noise_stress': 'nstdb', 'afdb': 'afdb', 'atrial_fib': 'afdb', 'cudb': 'cudb', 'svdb': 'svdb', 'edb': 'edb', 'ludb': 'ludb', 'fantasia': 'fantasia'}
module-attribute
¶
load_ecg(source, database=None, record=None, start_time=0.0, duration=None, sampling_rate=None, target_fs=None, normalize=None, lead_names=None, channels=None, load_annotations=True, **kwargs)
¶
Universal High-Level ECG Loader and Normalization Factory.
Intelligently ingests, streams, parses, and constructs a standardized ECGSignal
from diverse biomedical sources, with automatic sampling rate standardization
(target_fs) and amplitude normalization (normalize):
- PhysioNet URIs: e.g.
source="physionet:mitdb/100",source="mitdb/100". - PhysioNet Database + Record: e.g.
load_ecg("100", database="mit-bih"),load_ecg("I01", database="incartdb"). - Local Files: Automatically detected via file extension (
.csv,.hea,.dat,.parquet). - Synthetic Generator Schemes: e.g.
source="synthetic:pvc",source="synthetic:normal",source="synthetic:bradycardia". - In-Memory Arrays: Raw
numpy.ndarrayorpandas.DataFrame. - ECGSignal Objects: Passthrough with optional temporal slicing (
start_time,duration).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, ndarray, DataFrame, ECGSignal]
|
URI string, file path, database identifier, array, or DataFrame. |
required |
database
|
Optional[str]
|
Optional database alias (e.g. 'mitdb', 'mit-bih', 'incartdb', 'ptb-xl', 'fantasia'). |
None
|
record
|
Optional[Union[str, int]]
|
Optional record identifier when database is specified. |
None
|
start_time
|
float
|
Start time in seconds (default 0.0). |
0.0
|
duration
|
Optional[float]
|
Window length in seconds (None loads entire record). |
None
|
sampling_rate
|
Optional[float]
|
Sampling frequency in Hz (if required for CSV / numpy arrays). |
None
|
target_fs
|
Optional[float]
|
Target sampling frequency in Hz (e.g. 250.0). If specified, automatically resamples the signal and scales companion ground-truth annotations proportionally. |
None
|
normalize
|
Optional[str]
|
Amplitude normalization method: 'zscore' (mean=0, std=1), 'minmax' ([0, 1]), or 'robust' (median/IQR). Default is None (raw physical units). |
None
|
lead_names
|
Optional[List[str]]
|
Optional list of lead names. |
None
|
channels
|
Optional[List[int]]
|
Optional list of channel indices to load. |
None
|
load_annotations
|
bool
|
If True, automatically fetches companion clinical annotations. |
True
|
**kwargs
|
Any
|
Additional parameters forwarded to specific loaders or generators. |
{}
|
Returns:
| Type | Description |
|---|---|
ECGSignal
|
Standardized |
Examples:
>>> # 1. Stream MIT-BIH (360 Hz) and standardize to 250 Hz with Z-Score normalization:
>>> sig = load_ecg("physionet:mitdb/100", duration=10.0, target_fs=250.0, normalize="zscore")
>>>
>>> # 2. Stream St. Petersburg INCART 12-lead (257 Hz) normalized to 250 Hz:
>>> sig = load_ecg("I01", database="incartdb", duration=5.0, target_fs=250.0)
Source code in ecg_sdk/core/loaders.py
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 | |
list_physionet_databases()
¶
List all supported benchmark and clinical PhysioNet databases with metadata.
list_mitbih_records()
¶
load_wfdb_record(record_name_or_path, pn_dir=None, start_sample=0, end_sample=None, channels=None, load_annotations=True, annotator='atr', target_fs=None, normalize=None)
¶
Load a PhysioNet WFDB record from local disk or streamed directly from PhysioNet.
Source code in ecg_sdk/core/loaders.py
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 | |
load_mitbih_record(record='100', start_time=0.0, duration=10.0, channels=None, target_fs=None, normalize=None, load_annotations=True, pn_dir='mitdb')
¶
Convenience loader for the MIT-BIH Arrhythmia Database from PhysioNet.
Standard MIT-BIH sampling rate is 360 Hz, typically featuring MLII and V1/V5 leads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Union[str, int]
|
MIT-BIH record number (e.g. '100', '106', '200', 100). |
'100'
|
start_time
|
float
|
Starting offset in seconds. |
0.0
|
duration
|
Optional[float]
|
Window length in seconds (e.g. 10.0s). If None, loads entire ~30 min recording. |
10.0
|
channels
|
Optional[List[int]]
|
Channel indices to load (default None = all channels). |
None
|
target_fs
|
Optional[float]
|
Target sampling frequency in Hz (e.g. 250.0). Resamples both signal and annotations. |
None
|
normalize
|
Optional[str]
|
'zscore', 'minmax', or 'robust'. |
None
|
load_annotations
|
bool
|
If True, fetches expert cardiologist annotations (.atr). |
True
|
pn_dir
|
str
|
PhysioNet database identifier (default 'mitdb'). |
'mitdb'
|
Returns:
| Type | Description |
|---|---|
ECGSignal
|
Standardized ECGSignal instance. |
Source code in ecg_sdk/core/loaders.py
load_ptbxl_record(record_path, pn_dir=None, start_sample=0, end_sample=None, target_fs=None, normalize=None)
¶
Load a 12-lead PTB-XL diagnostic record formatted with standard 12-lead naming.
Source code in ecg_sdk/core/loaders.py
load_csv_record(file_path, sampling_rate=250.0, time_column='time_sec', lead_columns=None, target_fs=None, normalize=None)
¶
Load an ECG recording from a CSV file.
Source code in ecg_sdk/core/loaders.py
save_to_csv(signal, file_path)
¶
Save an ECGSignal object to a CSV file.