Skip to content

Commit f3c081a

Browse files
committed
Cleans up ann2rr; adds rr2ann
1 parent 7889664 commit f3c081a

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

wfdb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
wrsamp, dl_database, edf2mit, mit2edf, wav2mit, mit2wav,
33
wfdb2mat, csv2mit, sampfreq, signame)
44
from wfdb.io.annotation import (Annotation, rdann, wrann, show_ann_labels,
5-
show_ann_classes, ann2rr)
5+
show_ann_classes, ann2rr, rr2ann)
66
from wfdb.io.download import get_dbs, get_record_list, dl_files, set_db_index_url
77
from wfdb.plot.plot import plot_items, plot_wfdb, plot_all_records
88

wfdb/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
csv2mit, sampfreq, signame, SIGNAL_CLASSES)
44
from wfdb.io._signal import est_res, wr_dat_file
55
from wfdb.io.annotation import (Annotation, rdann, wrann, show_ann_labels,
6-
show_ann_classes, ann2rr)
6+
show_ann_classes, ann2rr, rr2ann)
77
from wfdb.io.download import get_dbs, get_record_list, dl_files, set_db_index_url
88
from wfdb.io.tff import rdtff

wfdb/io/annotation.py

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ def rm_last(*args):
22122212

22132213

22142214
def ann2rr(record_name, extension, pn_dir=None, start_time=None,
2215-
stop_time=None, format=None):
2215+
stop_time=None, format=None, as_array=True):
22162216
from wfdb.processing import hr
22172217
"""
22182218
Obtain RR interval series from ECG annotation files.
@@ -2225,26 +2225,28 @@ def ann2rr(record_name, extension, pn_dir=None, start_time=None,
22252225
extension : str
22262226
The annotatator extension of the annotation file. ie. for file
22272227
'100.atr', extension='atr'.
2228-
pn_dir : str
2228+
pn_dir : str, optional
22292229
Option used to stream data from Physionet. The PhysioNet database
22302230
directory from which to find the required annotation file. eg. For
22312231
record '100' in 'http://physionet.org/content/mitdb': pn_dir='mitdb'.
2232-
start_time : float
2232+
start_time : float, optional
22332233
The time to start the intervals in seconds.
2234-
stop_time : float
2234+
stop_time : float, optional
22352235
The time to stop the intervals in seconds.
2236-
format : str
2236+
format : str, optional
22372237
Print intervals in the specified format. By default, intervals are
22382238
printed in units of sample intervals. Other formats include
22392239
's' (seconds), 'm' (minutes), 'h' (hours). Set to 'None' for samples.
2240+
as_array : bool, optional
2241+
If True, return an an 'ndarray', else print the output.
22402242
22412243
Returns
22422244
-------
22432245
N/A
22442246
22452247
Examples
22462248
--------
2247-
>>> wfdb.ann2rr('sample-data/100', 'atr')
2249+
>>> wfdb.ann2rr('sample-data/100', 'atr', as_array=False)
22482250
>>> 18
22492251
>>> 59
22502252
>>> ...
@@ -2278,7 +2280,78 @@ def ann2rr(record_name, extension, pn_dir=None, start_time=None,
22782280
else:
22792281
out_interval = np.around(time_interval * ann.fs).astype(np.int)
22802282

2281-
print(*out_interval, sep='\n')
2283+
if as_array:
2284+
return out_interval
2285+
else:
2286+
print(*out_interval, sep='\n')
2287+
2288+
2289+
def rr2ann(rr_array, record_name, extension, fs=250, as_time=False):
2290+
"""
2291+
Creates an annotation file from the standard input, which should usually
2292+
be a Numpy array of intervals in the format produced by `ann2rr`. (For
2293+
exceptions, see the `as_time` parameter below.). An optional second column
2294+
may be provided which gives the respective annotation mnemonic.
2295+
2296+
Parameters
2297+
----------
2298+
rr_array : ndarray
2299+
A Numpy array consisting of the input RR intervals. If `as_time` is
2300+
set to True, then the input should consist of times of occurences. If,
2301+
the shape of the input array is '(n_annot,2)', then treat the second
2302+
column as the annotation mnemonic ('N', 'V', etc.). If a second column
2303+
is not specified, then the default annotation will the '"' which
2304+
specifies a comment.
2305+
record_name : str
2306+
The record name of the WFDB annotation file. ie. for file '100.atr',
2307+
record_name='100'.
2308+
extension : str
2309+
The annotatator extension of the annotation file. ie. for file
2310+
'100.atr', extension='atr'.
2311+
fs : float, int, optional
2312+
Assume the specified sampling frequency. This option has no effect
2313+
unless the `as_time` parameter is set to convert to samples; in this
2314+
case, a sampling frequency of 250 Hz is assumed if this option is
2315+
omitted.
2316+
as_time : bool
2317+
Interpret the input as times of occurrence (if True), rather than as
2318+
samples (if False). There is not currently a way to input RR intervals
2319+
in time format between beats. For example, 0.2 seconds between beats
2320+
1->2, 0.3 seconds between beats 2->3, etc.
2321+
2322+
Returns
2323+
-------
2324+
N/A
2325+
2326+
Examples
2327+
--------
2328+
Using time of occurence as input:
2329+
>>> import numpy as np
2330+
>>> rr_array = np.array([[0.2, 0.6, 1.3], ['V', 'N', 'V']]).T
2331+
>>> wfdb.rr2ann(rr_array, 'test_ann', 'atr', fs=100, as_time=True)
2332+
2333+
Using samples as input:
2334+
>>> import numpy as np
2335+
>>> rr_array = np.array([4, 17, 18, 16])
2336+
>>> wfdb.rr2ann(rr_array, 'test_ann', 'atr')
2337+
2338+
"""
2339+
try:
2340+
ann_sample = rr_array[:,0]
2341+
except IndexError:
2342+
ann_sample = rr_array
2343+
2344+
if as_time:
2345+
ann_sample = (fs * ann_sample.astype(np.float64)).astype(np.int64)
2346+
else:
2347+
ann_sample = np.cumsum(ann_sample).astype(np.int64)
2348+
2349+
try:
2350+
ann_symbol = rr_array[:,1].tolist()
2351+
except IndexError:
2352+
ann_symbol = rr_array.shape[0] * ['"']
2353+
2354+
wrann(record_name, extension, ann_sample, symbol=ann_symbol)
22822355

22832356

22842357
## ------------- Annotation Field Specifications ------------- ##

0 commit comments

Comments
 (0)