Skip to content

Commit ea5812f

Browse files
committed
add shiftsamps option for rdann
1 parent 3c30ae6 commit ea5812f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Reading Annotations
243243

244244
::
245245

246-
annotation = rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None)
246+
annotation = rdann(recordname, annotator, sampfrom=0, sampto=None, shiftsamps=False, pbdir=None)
247247

248248
Example Usage:
249249
::
@@ -258,6 +258,7 @@ Input arguments:
258258
file '100.atr', annotator='atr'
259259
- ``sampfrom`` (default=0): The minimum sample number for annotations to be returned.
260260
- ``sampto`` (default=None): The maximum sample number for annotations to be returned.
261+
- ``shiftsamps`` (default=False): Boolean flag that specifies whether to return the sample indices relative to 'sampfrom' (True), or sample 0 (False). Annotation files store exact sample locations.
261262
- ``pbdir`` (default=None): Option used to stream data from Physiobank. The Physiobank database directory from which to find the required annotation file. eg. For record '100' in 'http://physionet.org/physiobank/database/mitdb', pbdir = 'mitdb'.
262263

263264
Output arguments:

wfdb/readwrite/annotations.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def showanncodes():
587587
## ------------- Reading Annotations ------------- ##
588588

589589

590-
def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
590+
def rdann(recordname, annotator, sampfrom=0, sampto=None, shiftsamps=False, pbdir=None):
591591
""" Read a WFDB annotation file recordname.annotator and return an
592592
Annotation object.
593593
@@ -601,6 +601,9 @@ def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
601601
file '100.atr', annotator='atr'
602602
- sampfrom (default=0): The minimum sample number for annotations to be returned.
603603
- sampto (default=None): The maximum sample number for annotations to be returned.
604+
- shiftsamps (default=False): Boolean flag that specifies whether to return the
605+
sample indices relative to 'sampfrom' (True), or sample 0 (False). Annotation files
606+
store exact sample locations.
604607
- pbdir (default=None): Option used to stream data from Physiobank. The Physiobank database
605608
directory from which to find the required annotation file.
606609
eg. For record '100' in 'http://physionet.org/physiobank/database/mitdb', pbdir = 'mitdb'.
@@ -657,7 +660,6 @@ def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
657660
# Set the annotation type to the annotation codes
658661
anntype = [allannsyms[code] for code in anntype]
659662

660-
661663
# If the fs field was not present in the file, try to read a wfdb header
662664
# the annotation is associated with to get the fs
663665
if fs is None:
@@ -668,6 +670,11 @@ def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
668670
except:
669671
pass
670672

673+
# Return annotation samples relative to starting signal index
674+
if shiftsamps and annsamp!=[] and sampfrom:
675+
annsamp = np.array(annsamp, dtype='int64')
676+
annsamp = annsamp - sampfrom
677+
671678
# Store fields in an Annotation object
672679
annotation = Annotation(os.path.split(recordname)[1], annotator, annsamp, anntype,
673680
subtype, chan, num, aux, fs,custom_anntypes)

0 commit comments

Comments
 (0)