@@ -2212,7 +2212,7 @@ def rm_last(*args):
2212
2212
2213
2213
2214
2214
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 ):
2216
2216
from wfdb .processing import hr
2217
2217
"""
2218
2218
Obtain RR interval series from ECG annotation files.
@@ -2225,26 +2225,28 @@ def ann2rr(record_name, extension, pn_dir=None, start_time=None,
2225
2225
extension : str
2226
2226
The annotatator extension of the annotation file. ie. for file
2227
2227
'100.atr', extension='atr'.
2228
- pn_dir : str
2228
+ pn_dir : str, optional
2229
2229
Option used to stream data from Physionet. The PhysioNet database
2230
2230
directory from which to find the required annotation file. eg. For
2231
2231
record '100' in 'http://physionet.org/content/mitdb': pn_dir='mitdb'.
2232
- start_time : float
2232
+ start_time : float, optional
2233
2233
The time to start the intervals in seconds.
2234
- stop_time : float
2234
+ stop_time : float, optional
2235
2235
The time to stop the intervals in seconds.
2236
- format : str
2236
+ format : str, optional
2237
2237
Print intervals in the specified format. By default, intervals are
2238
2238
printed in units of sample intervals. Other formats include
2239
2239
'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.
2240
2242
2241
2243
Returns
2242
2244
-------
2243
2245
N/A
2244
2246
2245
2247
Examples
2246
2248
--------
2247
- >>> wfdb.ann2rr('sample-data/100', 'atr')
2249
+ >>> wfdb.ann2rr('sample-data/100', 'atr', as_array=False )
2248
2250
>>> 18
2249
2251
>>> 59
2250
2252
>>> ...
@@ -2278,7 +2280,78 @@ def ann2rr(record_name, extension, pn_dir=None, start_time=None,
2278
2280
else :
2279
2281
out_interval = np .around (time_interval * ann .fs ).astype (np .int )
2280
2282
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 )
2282
2355
2283
2356
2284
2357
## ------------- Annotation Field Specifications ------------- ##
0 commit comments