Skip to content

Commit 18d949a

Browse files
authored
Merge pull request #236 from MIT-LCP/ann2rr
Adds original ann2rr function
2 parents 57c15d1 + a4ac7b2 commit 18d949a

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

wfdb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .io.record import (Record, MultiRecord, rdheader, rdrecord, rdsamp,
22
wrsamp, dl_database, edf2mit, sampfreq, signame)
33
from .io.annotation import (Annotation, rdann, wrann, show_ann_labels,
4-
show_ann_classes)
4+
show_ann_classes, ann2rr)
55
from .io.download import get_dbs, get_record_list, dl_files, set_db_index_url
66
from .plot.plot import plot_items, plot_wfdb, plot_all_records
77

wfdb/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
dl_database, edf2mit, sampfreq, signame, SIGNAL_CLASSES)
33
from ._signal import est_res, wr_dat_file
44
from .annotation import (Annotation, rdann, wrann, show_ann_labels,
5-
show_ann_classes)
5+
show_ann_classes, ann2rr)
66
from .download import get_dbs, get_record_list, dl_files, set_db_index_url
77
from .tff import rdtff

wfdb/io/annotation.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,76 @@ def rm_last(*args):
22032203
return
22042204

22052205

2206+
def ann2rr(record_name, extension, pn_dir=None, start_time=None,
2207+
stop_time=None, format=None):
2208+
from wfdb.processing import hr
2209+
"""
2210+
Obtain RR interval series from ECG annotation files.
2211+
2212+
Parameters
2213+
----------
2214+
record_name : str
2215+
The record name of the WFDB annotation file. ie. for file '100.atr',
2216+
record_name='100'.
2217+
extension : str
2218+
The annotatator extension of the annotation file. ie. for file
2219+
'100.atr', extension='atr'.
2220+
pn_dir : str
2221+
Option used to stream data from Physionet. The Physionet database
2222+
directory from which to find the required annotation file. eg. For
2223+
record '100' in 'http://physionet.org/content/mitdb': pn_dir='mitdb'.
2224+
start_time : float
2225+
The time to start the intervals in seconds.
2226+
stop_time : float
2227+
The time to stop the intervals in seconds.
2228+
format : str
2229+
Print intervals in the specified format. By default, intervals are
2230+
printed in units of sample intervals. Other formats include
2231+
's' (seconds), 'm' (minutes), 'h' (hours). Set to 'None' for samples.
2232+
2233+
Returns
2234+
-------
2235+
N/A
2236+
2237+
Examples
2238+
--------
2239+
>>> wfdb.ann2rr('sample-data/100', 'atr')
2240+
>>> 18
2241+
>>> 59
2242+
>>> ...
2243+
>>> 250
2244+
>>> 257
2245+
2246+
"""
2247+
if (pn_dir is not None) and ('.' not in pn_dir):
2248+
dir_list = pn_dir.split(os.sep)
2249+
pn_dir = posixpath.join(dir_list[0], record.get_version(dir_list[0]),
2250+
*dir_list[1:])
2251+
2252+
ann = rdann(record_name, extension, pn_dir=pn_dir)
2253+
2254+
rr_interval = hr.calc_rr(ann.sample, fs=ann.fs)
2255+
rr_interval = np.insert(rr_interval, 0, ann.sample[0])
2256+
2257+
time_interval = rr_interval / ann.fs
2258+
if start_time is not None:
2259+
time_interval = time_interval[(time_interval > start_time).astype(bool)]
2260+
if stop_time is not None:
2261+
time_interval = time_interval[(time_interval < stop_time).astype(bool)]
2262+
2263+
# Already given in seconds (format == 's')
2264+
if format == 's':
2265+
out_interval = time_interval
2266+
elif format == 'm':
2267+
out_interval = time_interval / 60
2268+
elif format == 'h':
2269+
out_interval = time_interval / (60*60)
2270+
else:
2271+
out_interval = np.around(time_interval * ann.fs).astype(np.int)
2272+
2273+
print(*out_interval, sep='\n')
2274+
2275+
22062276
## ------------- Annotation Field Specifications ------------- ##
22072277

22082278

0 commit comments

Comments
 (0)