Skip to content

Commit 0680e02

Browse files
committed
add a new demo and function help messages
1 parent 0df4cfd commit 0680e02

File tree

5 files changed

+211
-37
lines changed

5 files changed

+211
-37
lines changed

demo.ipynb

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": null,
14+
"execution_count": 3,
1515
"metadata": {
1616
"collapsed": false
1717
},
@@ -201,30 +201,58 @@
201201
"outputs": [],
202202
"source": [
203203
"# Demo 9 - Write a WFDB record without using a Record object via the gateway wrsamp function.\n",
204-
"# This is the easiest way to write physical signals to a WFDB file. \n",
204+
"# This is the basic way to write physical signals to a WFDB file. \n",
205205
"\n",
206206
"# Read part of a record from Physiobank\n",
207207
"sig, fields = wfdb.srdsamp('a103l', sampfrom = 50000, channels = [0,1], pbdir = 'challenge/2015/training')\n",
208208
"\n",
209209
"# Call the gateway wrsamp function, manually inserting fields as function input parameters\n",
210-
"wfdb.wrsamp('ecgrecord', fs = 250, units = ['mV', 'mV'], signames = ['I', 'II'], p_signals = sig, fmt = ['16', '16'])"
210+
"wfdb.wrsamp('ecgrecord', fs = 250, units = ['mV', 'mV'], signames = ['I', 'II'], p_signals = sig, fmt = ['16', '16'])\n",
211+
"\n",
212+
"# The new file can be read\n",
213+
"recordecg = wfdb.rdsamp('ecgrecord')"
211214
]
212215
},
213216
{
214217
"cell_type": "code",
215-
"execution_count": null,
218+
"execution_count": 7,
216219
"metadata": {
217220
"collapsed": false
218221
},
219222
"outputs": [],
220223
"source": [
221-
"# Demo 10 - Read a WFDB annotation file and create a copy under a new extension.\n",
224+
"# Demo 10 - Read a WFDB annotation file and create a copy via the wrann() instance method\n",
225+
"# of the Annotation object\n",
226+
"\n",
227+
"# Read an annotation from Physiobank\n",
222228
"annotation = wfdb.rdann('sampledata/100', 'atr')\n",
223-
"annotation.annotator = 'copy'\n",
229+
"annotation.annotator = 'cpy'\n",
230+
"\n",
231+
"# Call the instance method of the object\n",
224232
"annotation.wrann()\n",
225233
"\n",
226234
"# The new file can be read\n",
227-
"annotationcopy = wfdb.rdann('100', 'copy')"
235+
"ann100copy = wfdb.rdann('100', 'cpy')"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": 8,
241+
"metadata": {
242+
"collapsed": true
243+
},
244+
"outputs": [],
245+
"source": [
246+
"# Demo 11 - Write a WFDB annotation file without using an Annotator object via the gateway wrann function.\n",
247+
"\n",
248+
"# Read an annotation as an Annotation object\n",
249+
"annotation = wfdb.rdann('b001', 'atr', pbdir='cebsdb')\n",
250+
"\n",
251+
"# Call the gateway wrann function, manually inserting fields as function input parameters\n",
252+
"wfdb.wrann('b001', 'cpy', annotation.annsamp, annotation.anntype)\n",
253+
"\n",
254+
"# The new file can be read\n",
255+
"annbcopy = wfdb.rdann('b001', 'cpy')"
228256
]
229257
},
230258
{
@@ -235,7 +263,7 @@
235263
},
236264
"outputs": [],
237265
"source": [
238-
"# Demo 11 - View what the 'anntype' symbols mean in the standard WFDB library\n",
266+
"# Demo 12 - View what the 'anntype' symbols mean in the standard WFDB library\n",
239267
"wfdb.showanncodes()"
240268
]
241269
},
@@ -258,7 +286,7 @@
258286
},
259287
"outputs": [],
260288
"source": [
261-
"# Demo 12 - List the Physiobank Databases\n",
289+
"# Demo 13 - List the Physiobank Databases\n",
262290
"\n",
263291
"dbs = wfdb.getdblist()\n",
264292
"display(dbs)"
@@ -272,7 +300,7 @@
272300
},
273301
"outputs": [],
274302
"source": [
275-
"# Demo 13 - Download all the WFDB records and annotations from a small Physiobank Database\n",
303+
"# Demo 14 - Download all the WFDB records and annotations from a small Physiobank Database\n",
276304
"\n",
277305
"# Make a temporary download directory in your current working directory\n",
278306
"cwd = os.getcwd()\n",
@@ -296,7 +324,7 @@
296324
},
297325
"outputs": [],
298326
"source": [
299-
"# Demo 14 - Download specified files from a Physiobank database\n",
327+
"# Demo 15 - Download specified files from a Physiobank database\n",
300328
"\n",
301329
"# The files to download\n",
302330
"filelist = ['STAFF-Studies-bibliography-2016.pdf', 'data/001a.hea', 'data/001a.dat', 'data/001b.hea', 'data/001b.dat']\n",

wfdb/annotations.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ class Annotation():
1212
"""
1313
The class representing WFDB annotations.
1414
15-
Annotation objects can be created as with any other class, or by reading a WFDB annotation
15+
Annotation objects can be created using the constructor, or by reading a WFDB annotation
1616
file with 'rdann'.
1717
1818
The attributes of the Annotation object give information about the annotation as specified
19-
by https://www.physionet.org/physiotools/wag/ <INSERT>:
19+
by https://www.physionet.org/physiotools/wag/annot-5.htm:
2020
- annsamp: The annotation location in samples relative to the beginning of the record.
2121
- anntype: The annotation type according the the standard WFDB codes.
2222
- subtype: The marked class/category of the annotation.
2323
- chan: The signal channel associated with the annotations.
2424
- num: The labelled annotation number.
2525
- aux: The auxiliary information string for the annotation.
26-
- fs: The sampling frequency of the record.
26+
- fs: The sampling frequency of the record if contained in the annotation file.
2727
2828
Call 'showanncodes()' to see the list of standard annotation codes. Any text used to label
29-
annotations that are not one of the codes go in the 'aux' field rather than the 'anntype'
30-
field.
29+
annotations that are not one of these codes should go in the 'aux' field rather than the
30+
'anntype' field.
3131
"""
3232
def __init__(self, recordname, annotator, annsamp, anntype, subtype = None, chan = None, num = None, aux = None, fs = None):
3333
self.recordname = recordname
@@ -350,28 +350,59 @@ def field2bytes(field, value):
350350

351351

352352
# Function for writing annotations
353-
def wrann(recordname, annotator, annsamp, anntype, num = None, subtype = None, chan = None, aux = None, fs = None):
354-
# Create Annotation object
355-
annotation = Annotation(recordname, annotator, annsamp, anntype, num, subtype, chan, aux, fs)
356-
# Perform field checks and write the annotation file
357-
annotation.wrann()
353+
def wrann(recordname, annotator, annsamp, anntype, subtype = None, chan = None, num = None, aux = None, fs = None):
354+
"""Write a WFDB annotation file.
355+
356+
Usage:
357+
wrann(recordname, annotator, annsamp, anntype, num = None, subtype = None, chan = None, aux = None, fs = None)
358+
359+
Input arguments:
360+
- recordname (required): The string name of the WFDB record to be written (without any file extensions).
361+
- annsamp (required): The annotation location in samples relative to the beginning of the record.
362+
- anntype (required): The annotation type according the the standard WFDB codes.
363+
- subtype (default=None): The marked class/category of the annotation.
364+
- chan (default=None): The signal channel associated with the annotations.
365+
- num (default=None): The labelled annotation number.
366+
- aux (default=None): The auxiliary information string for the annotation.
367+
- fs (default=None): The numerical sampling frequency of the record to be written to the file.
368+
369+
Note: This gateway function was written to enable a simple way to write WFDB annotation files without
370+
needing to explicity create an Annotation object beforehand.
371+
372+
You may also create an Annotation object, manually set its attributes, and call its wrann() instance method.
373+
374+
Example Usage:
375+
import wfdb
376+
# Read an annotation as an Annotation object
377+
annotation = wfdb.rdann('b001', 'atr', pbdir='cebsdb')
378+
# Call the gateway wrann function, manually inserting fields as function input parameters
379+
wfdb.wrann('b001', 'cpy', annotation.annsamp, annotation.anntype)
380+
"""
381+
382+
# Create Annotation object
383+
annotation = Annotation(recordname, annotator, annsamp, anntype, num, subtype, chan, aux, fs)
384+
# Perform field checks and write the annotation file
385+
annotation.wrann()
358386

359387
# Display the annotation symbols and the codes they represent
360388
def showanncodes():
361389
"""
362-
Display the annotation symbols and the codes they represent
390+
Display the annotation symbols and the codes they represent according to the
391+
standard WFDB library 10.5.24
363392
364-
Usage: showanncodes()
393+
Usage:
394+
showanncodes()
365395
"""
366396
display(symcodes)
367397

368398
## ------------- Reading Annotations ------------- ##
369399

370400
def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
371-
""" Read a WFDB annotation file recordname.annotator and return the fields as lists or arrays
401+
""" Read a WFDB annotation file recordname.annotator and return an
402+
Annotation object.
372403
373404
Usage:
374-
annotation = rdann(recordname, annot, sampfrom=0, sampto=None)
405+
annotation = rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None)
375406
376407
Input arguments:
377408
- recordname (required): The record name of the WFDB annotation file. ie. for
@@ -380,9 +411,12 @@ def rdann(recordname, annotator, sampfrom=0, sampto=None, pbdir=None):
380411
file '100.atr', annotator='atr'
381412
- sampfrom (default=0): The minimum sample number for annotations to be returned.
382413
- sampto (default=None): The maximum sample number for annotations to be returned.
414+
- pbdir (default=None): Option used to stream data from Physiobank. The Physiobank database
415+
directory from which to find the required annotation file.
416+
eg. For record '100' in 'http://physionet.org/physiobank/database/mitdb', pbdir = 'mitdb'.
383417
384418
Output argument:
385-
- annotation: The annotation object. Call help(wfdb.Annotation) for the attribute
419+
- annotation: The Annotation object. Call help(wfdb.Annotation) for the attribute
386420
descriptions.
387421
388422
Note: For every annotation sample, the annotation file explictly stores the 'annsamp'

wfdb/downloads.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ def getannotators(dburl, annotators):
126126
# Check for an ANNOTATORS file
127127
r = requests.get(os.path.join(dburl, 'ANNOTATORS'))
128128
if r.status_code == 404:
129-
sys.exit('The database '+dburl+' has no annotation files to download')
129+
if annotators == 'all':
130+
return
131+
else:
132+
sys.exit('The database '+dburl+' has no annotation files to download')
130133
# Make sure the input annotators are present in the database
131134
annlist = r.content.decode('ascii').splitlines()
132135
annlist = [a.split('\t')[0] for a in annlist]

wfdb/plots.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@
88
# Plot a WFDB Record's signals
99
# Optionally, overlay annotation locations
1010
def plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', returnfig = False):
11+
""" Subplot and label each channel of a WFDB Record.
12+
Optionally, subplot annotation locations over selected channels.
1113
14+
Usage: plotrec(record=None, title = None, annotation = None, annch = [0], timeunits='samples', returnfig=False)
15+
16+
Input arguments:
17+
- record (required): A wfdb Record object. The p_signals attribute will be plotted.
18+
- title (optional): A string containing the title of the graph.
19+
- annotation (optional): An Annotation object. The annsamp attribute locations will be overlaid on the signal.
20+
- annch (default=[0]): A list of channels on which to plot the annotation samples.
21+
- timeunits (default='samples'): String specifying the x axis unit.
22+
Allowed options are: 'samples', 'seconds', 'minutes', and 'hours'.
23+
- returnfig (default=False): Specifies whether the figure is to be returned as an output argument
24+
25+
Output argument:
26+
- figure: The matplotlib figure generated. Only returned if the 'returnfig' option is set to True.
27+
"""
28+
1229
# Check the validity of items used to make the plot
1330
# Return the x axis time values to plot for the record (and annotation if any)
1431
t, tann = checkplotitems(record, title, annotation, annch, timeunits)
@@ -131,7 +148,23 @@ def checkplotitems(record, title, annotation, annch, timeunits):
131148

132149
# Plot the sample locations of a WFDB annotation on a new figure
133150
def plotann(annotation, title = None, timeunits = 'samples', returnfig = False):
151+
""" Plot sample locations of an Annotation object.
152+
153+
Usage: plotann(annotation, title = None, timeunits = 'samples', returnfig = False)
134154
155+
Input arguments:
156+
- annotation (required): An Annotation object. The annsamp attribute locations will be overlaid on the signal.
157+
- title (optional): A string containing the title of the graph.
158+
- timeunits (default='samples'): String specifying the x axis unit.
159+
Allowed options are: 'samples', 'seconds', 'minutes', and 'hours'.
160+
- returnfig (default=False): Specifies whether the figure is to be returned as an output argument
161+
162+
Output argument:
163+
- figure: The matplotlib figure generated. Only returned if the 'returnfig' option is set to True.
164+
165+
Note: The plotrec function is useful for plotting annotations on top of signal waveforms.
166+
"""
167+
135168
# Check the validity of items used to make the plot
136169
# Get the x axis annotation values to plot
137170
plotvals = checkannplotitems(annotation, title, timeunits)

0 commit comments

Comments
 (0)