I am trying to extract RR interval data from ecg annotation files from physionet by using the ann2rr
function according to documentation:
from IPython.display import display
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import os
import shutil
import posixpath
import wfdb
# Demo 1 - Read a WFDB record using the 'rdrecord' function into a wfdb.Record object.
# Plot the signals, and show the data.
# This works:
path = 'C:/Users/user/Downloads/bidmc-congestive-heart-failure-database-1.0.0/files/chf09'
ext ='ecg'
record = wfdb.rdrecord(path)
wfdb.plot_wfdb(record=record, title='Record chf09 from BIDMC heart failure database')
display(record.__dict__)
# but that generates the error:
res = wfdb.ann2rr(path, ext, as_array=True)
AttributeError Traceback (most recent call last)
Cell In[19], line 10
8 display(record.__dict__)
9 sig=np.array(record.p_signal).T
---> 10 res = wfdb.processing.ann2rr(path, ext, as_array=True)
AttributeError: module 'wfdb.processing' has no attribute 'ann2rr'
I have also tried the variant without processing, i.e. wfdb.ann2rr(...)
also without success.
I also have reinstalled the wfdb
and pyECG
libraries, also without effect.
I'm working with Jupyter Notebook and python version 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)]
Maybe I'm just missing something simple so I put my hopes on helpful replies
Here buried in the code makes it clear you need to import processing
separately:
Examples
--------
>>> import wfdb
>>> from wfdb import processing
Then you can use the annrr() function
like the following:
processing.ann2rr()
And so for your example it would be changing res = wfdb.processing.ann2rr(path, ext, as_array=True)
, or res = wfdb.ann2rr(path, ext, as_array=True)
, to these two lines below:
from wfdb import processing
res = processing.ann2rr(path, ext, as_array=True)
(By they way, please make the provided code and errors match in the future. This post is unnecessarily confusing with the problematic code appearing in two different forms, one of them buried in a code block introduced in a comment grammatically incorrectly.)
That new line will run without the error now.
I am not familiar with their documentation fully, but it does appear the exact syntax necessary doesn't match well the limited example here that reads below 'Examples' under wfdb.processing.ann2rr()
in the processing
subpackage documentation:
wfdb.ann2rr('sample-data/100', 'atr', as_array=False)
Plus, this SO post makes it clear one of the approaches you tried worked in the past.
The processing
documentation page does at least say 'subpackage'. (This issue title 'error when running processing.ann2rr' also touched on the correct syntax.)
It is not unusual to need to hack around a bit for the exact syntax when using a large package in active development. Querying with dir()
and searching around the codebase , example helpful search in codebase for this case. Or if you ran from wfdb import processing
, you can then run dir(processing)
to see ann2rr
listed as the functions and methods under that.
If you look in the codebase, you'll see there's some other ways to import this that would work. For example, from here:
from wfdb.processing.hr import compute_hr, calc_rr, calc_mean_hr, ann2rr, rr2ann
And then you'd use the function like below since you have imported it directly into the current namespace:
res = ann2rr(path, ext, as_array=True)