Datasets
See Datasets for information about available datasets and instructions for retrieving NSRR data.
sleepecg.download_nsrr(db_slug, subfolder='', pattern='*', shallow=False, data_dir='.')
Recursively download files from NSRR.
Specify a subfolder and/or a filename pattern to filter results. Implemented according to the NSRR API specs.
Parameters:
-
db_slug
(str
) –Short identifier of a database, e.g.
'mesa'
. -
subfolder
(str
) –The folder at which to start the search, by default
''
(i.e. the root folder). -
pattern
(str
) –Glob-like pattern to select files (only applied to the basename, not the dirname), by default
'*'
. -
shallow
(bool
) –If
True
, only download files in the given subfolder (i.e. no recursion), by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored, by default
'.'
.
Source code in sleepecg/io/nsrr.py
sleepecg.download_physionet(db_slug, requested_records, extensions, db_version='1.0.0', data_dir='.')
Download requested files from PhysioNet.
All files with extensions
for record IDs in requested_records
are downloaded from
the PhysioNet database db_slug
.
Parameters:
-
db_slug
(str
) –Short identifier of a database, e.g.
'mitdb'
. -
requested_records
(list[str]
) –Records with those IDs are downloaded.
-
extensions
(Iterable[str]
) –Files with those extensions are downloaded.
-
db_version
(str
) –Version of the database, by default
'1.0.0'
. -
data_dir
(str | Path
) –Directory where all datasets are stored, by default
'.'
.
Source code in sleepecg/io/physionet.py
sleepecg.export_ecg_record(record, filename)
Export ECG record to CSV.
Parameters:
Source code in sleepecg/io/ecg_readers.py
sleepecg.read_gudb(offline=False, data_dir=None)
Lazily read records from GUDB.
Required files are downloaded if not present in '<data_dir>/gudb'
.
Parameters:
-
offline
(bool
) –If
True
, only local files will be used (i.e. no files will be downloaded), by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
ECGRecord
–Each element in the generator is of type
ECGRecord
and contains the ECG signal (.ecg
), sampling frequency (.fs
), annotated beat indices (.annotations
),.lead
, and.id
.
Source code in sleepecg/io/ecg_readers.py
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 |
|
sleepecg.read_ltdb(records_pattern='*', offline=False, data_dir=None)
Lazily read records from LTDB.
Parameters:
-
records_pattern
(str
) –Glob-like pattern to select record IDs, by default
'*'
. -
offline
(bool
) –If
True
, only local files will be used (i.e. no files will be downloaded), by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
ECGRecord
–Each element in the generator is of type
ECGRecord
and contains the ECG signal (.ecg
), sampling frequency (.fs
), annotated beat indices (.annotations
),.lead
, and.id
.
Source code in sleepecg/io/ecg_readers.py
sleepecg.read_mesa(records_pattern='*', heartbeats_source='annotation', offline=False, keep_edfs=False, data_dir=None)
Lazily read records from MESA.
Each MESA record consists of an .edf
file containing raw polysomnography data and an
.xml
file containing annotated events. Since the entire MESA dataset requires about
385 GB of disk space, .edf
files can be deleted after heartbeat times have been
extracted. Heartbeat times are cached in an .npy
file in
<data_dir>/mesa/preprocessed/heartbeats
.
Parameters:
-
records_pattern
(str
) –Glob-like pattern to select record IDs, by default
'*'
. -
heartbeats_source
(('annotation', 'cached', 'ecg')
) –If
'annotation'
(default), get heartbeat times frompolysomnography/annotations-rpoints/<record_id>-rpoints.csv
(not available for all records). If'ecg'
, usesleepecg.detect_heartbeats()
on the ECG contained inpolysomnography/edfs/<record_id>.edf
and cache the result inpreprocessed/heartbeats/<record_id>.npy
. If'cached'
, get the cached heartbeats. -
offline
(bool
) –If
True
, search for local files only instead of using the NSRR API, by defaultFalse
. -
keep_edfs
(bool
) –If
False
, remove.edf
after heartbeat detection, by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
SleepRecord
–Each element in the generator is of type
SleepRecord
.
Source code in sleepecg/io/sleep_readers.py
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 |
|
sleepecg.read_mitdb(records_pattern='*', offline=False, data_dir=None)
Lazily read records from MITDB.
Parameters:
-
records_pattern
(str
) –Glob-like pattern to select record IDs, by default
'*'
. -
offline
(bool
) –If
True
, only local files will be used (i.e. no files will be downloaded), by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
ECGRecord
–Each element in the generator is of type
ECGRecord
and contains the ECG signal (.ecg
), sampling frequency (.fs
), annotated beat indices (.annotations
),.lead
, and.id
.
Source code in sleepecg/io/ecg_readers.py
sleepecg.read_shhs(records_pattern='*', heartbeats_source='annotation', offline=False, keep_edfs=False, data_dir=None)
Lazily read records from SHHS.
Each SHHS record consists of an .edf
file containing raw polysomnography data and an
.xml
file containing annotated events. Since the entire SHHS dataset requires about
356 GB of disk space, .edf
files can be deleted after heartbeat times have been
extracted. Heartbeat times are cached in an .npy
file in
<data_dir>/shhs/preprocessed/heartbeats
.
Parameters:
-
records_pattern
(str
) –Glob-like pattern to select record IDs, by default
'*'
. -
heartbeats_source
(('annotation', 'cached', 'ecg')
) –If
'annotation'
(default), get heartbeat times frompolysomnography/annotations-rpoints/shhsX/<record_id>-rpoints.csv
(not available for all records). If'ecg'
, usesleepecg.detect_heartbeats()
on the ECG contained inpolysomnography/edfs/shhsX/<record_id>.edf
and cache the result inpreprocessed/heartbeats/shhsX/<record_id>.npy
. If'cached'
, get the cached heartbeats. -
offline
(bool
) –If
True
, search for local files only instead of using the NSRR API, by defaultFalse
. -
keep_edfs
(bool
) –If
False
, remove.edf
after heartbeat detection, by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
SleepRecord
–Each element in the generator is of type
SleepRecord
.
Source code in sleepecg/io/sleep_readers.py
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
sleepecg.read_slpdb(records_pattern='*', offline=False, data_dir=None)
Lazily read records from SLPDB.
Required files are downloaded from PhysioNet to <data_dir>/slpdb
.
Parameters:
-
records_pattern
(str
) –Glob-like pattern to select record IDs, by default
'*'
. -
offline
(bool
) –If
True
, search for local files only instead of downloading from PhysioNet, by defaultFalse
. -
data_dir
(str | Path
) –Directory where all datasets are stored. If
None
(default), the value will be taken from the configuration.
Yields:
-
SleepRecord
–Each element in the generator is of type
SleepRecord
.
Source code in sleepecg/io/sleep_readers.py
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 |
|
sleepecg.set_nsrr_token(token)
Set and verify the NSRR download token.
Implemented according to the NSRR API specs.
Parameters:
-
token
(str
) –NSRR download token.
Source code in sleepecg/io/nsrr.py
sleepecg.ECGRecord
dataclass
Store a single ECG record.
Attributes:
-
ecg
(ndarray
) –The ECG signal.
-
fs
(float
) –The sampling frequency in Hz.
-
annotation
(ndarray
) –Indices of annotated heartbeats.
-
lead
((str, optional)
) –Which ECG lead the signal was recorded from, by default
None
. -
id
((str, optional)
) –The record ID, by default
None
.
Source code in sleepecg/io/ecg_readers.py
export(filename)
Export ECG record to CSV.
Parameters:
plot(**kwargs)
Plot ECG time series with optional markers.
Parameters:
-
**kwargs
(ndarray
) –Positions of annotations (i.e. heartbeats) in samples. If more than one marker sequence is given, the keywords will be used as labels in the plot legend.
Returns:
-
fig
(Figure
) –The figure.
-
ax
(Axes
) –The axes in the figure.
Source code in sleepecg/io/ecg_readers.py
sleepecg.Gender
sleepecg.SleepRecord
dataclass
Store a single sleep record.
Attributes:
-
sleep_stages
((ndarray, optional)
) –Sleep stages according to AASM guidelines, stored as integers as defined by
SleepStage
, by defaultNone
. -
sleep_stage_duration
((int, optional)
) –Duration of each sleep stage in seconds, by default
None
. -
id
((str, optional)
) –The record ID, by default
None
. -
recording_start_time
((time, optional)
) –Time at which the recording was started, by default
None
. -
heartbeat_times
((ndarray, optional)
) –Times of heartbeats relative to recording start in seconds, by default
None
. -
subject_data
((SubjectData, optional)
) –Dataclass containing subject data (such as gender or age), by default
None
.
Source code in sleepecg/io/sleep_readers.py
sleepecg.SleepStage
Bases: IntEnum
Mapping of AASM sleep stages to integers.
To facilitate hypnogram plotting, values start with zero and increase with wakefulness.
Source code in sleepecg/io/sleep_readers.py
sleepecg.SubjectData
dataclass
Store data about a single subject.
Attributes:
-
gender
((int, optional)
) –The subject's gender, stored as an integer as defined by
Gender
, by defaultNone
. -
age
((int, optional)
) –The subject's age in years, by default
None
. -
weight
((float, optional)
) –The subject's weight in kg, by default
None
.