Skip to content

Commit a982a50

Browse files
committed
merge large restructuring branch
2 parents e6813e4 + 275e07f commit a982a50

19 files changed

+5090
-2844
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ branches:
1717
# Set the language
1818
language: python
1919
python:
20-
- "2.7"
20+
- "2.7.13"
2121
- "3.5"
2222

2323
# before_script:

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include LICENSE
33

44
# Include readme file
5-
include README.md
5+
include README.rst
66

77
# Include the data files
88
# recursive-include data *

README.rst

Lines changed: 367 additions & 105 deletions
Large diffs are not rendered by default.

demo.ipynb

Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Demo Scripts for the wfdb-python package\n",
8+
"\n",
9+
"Run this script from the base directory of the git repository to access the included demo files"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {
16+
"collapsed": false
17+
},
18+
"outputs": [],
19+
"source": [
20+
"import wfdb\n",
21+
"import numpy as np\n",
22+
"import os\n",
23+
"from IPython.display import display"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"### Reading Records and Annotations"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {
37+
"collapsed": false
38+
},
39+
"outputs": [],
40+
"source": [
41+
"# Demo 1 - Read a wfdb record using the 'rdsamp' function into a wfdb.Record object.\n",
42+
"# Plot the signals, and show the data.\n",
43+
"record = wfdb.rdsamp('sampledata/a103l') \n",
44+
"wfdb.plotrec(record, title='Record a103l from Physionet Challenge 2015') \n",
45+
"display(record.__dict__)\n",
46+
"\n",
47+
"\n",
48+
"# Can also read the same files hosted on Physiobank https://physionet.org/physiobank/database/\n",
49+
"# in the challenge/2015/training/ database subdirectory. Full url = https://physionet.org/physiobank/database/challenge/2015/training/\n",
50+
"record2 = wfdb.rdsamp('a103l', pbdir = 'challenge/2015/training/')"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {
57+
"collapsed": false
58+
},
59+
"outputs": [],
60+
"source": [
61+
"# Demo 2 - Read certain channels and sections of the WFDB record using the simplified 'srdsamp' function\n",
62+
"# which returns a numpy array and a dictionary. Show the data.\n",
63+
"signals, fields=wfdb.srdsamp('sampledata/s0010_re', channels=[14, 0, 5, 10], sampfrom=100, sampto=15000)\n",
64+
"display(signals)\n",
65+
"display(fields)\n",
66+
"\n",
67+
"# Can also read the same files hosted on Physiobank \n",
68+
"signals2, fields2=wfdb.srdsamp('s0010_re', channels=[14, 0, 5, 10], sampfrom=100, sampto=15000, pbdir = 'ptbdb/patient001/')"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {
75+
"collapsed": false
76+
},
77+
"outputs": [],
78+
"source": [
79+
"# Demo 3 - Read a WFDB header file only (without the signals)\n",
80+
"record = wfdb.rdheader('sampledata/drive02')\n",
81+
"display(record.__dict__)\n",
82+
"\n",
83+
"# Can also read the same file hosted on Physiobank\n",
84+
"record2 = wfdb.rdheader('drive02', pbdir = 'drivedb')"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": null,
90+
"metadata": {
91+
"collapsed": false
92+
},
93+
"outputs": [],
94+
"source": [
95+
"# Demo 4 - Read part of a WFDB annotation file into a wfdb.Annotation object, and plot the samples\n",
96+
"annotation = wfdb.rdann('sampledata/100', 'atr', sampfrom = 100000, sampto = 110000)\n",
97+
"annotation.fs = 360\n",
98+
"wfdb.plotann(annotation, timeunits = 'minutes')\n",
99+
"\n",
100+
"# Can also read the same file hosted on PhysioBank \n",
101+
"annotation2 = wfdb.rdann('100', 'atr', sampfrom = 100000, sampto = 110000, pbdir = 'mitdb')"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [],
111+
"source": [
112+
"# Demo 5 - Read a WFDB record and annotation. Plot all channels, and the annotation on top of channel 0.\n",
113+
"record = wfdb.rdsamp('sampledata/100', sampto = 15000)\n",
114+
"annotation = wfdb.rdann('sampledata/100', 'atr', sampto = 15000)\n",
115+
"\n",
116+
"wfdb.plotrec(record, annotation = annotation, title='Record 100 from MIT-BIH Arrhythmia Database', timeunits = 'seconds')"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"### Matched waveform examples\n",
124+
"\n",
125+
"The following sections load and plots waveforms from the MIMIC matched waveform database. These waveforms have been matched to clinical data in the MIMIC Clinical database. The input records are multi-segment (made up of multiple individual WFDB records) and relatively long.\n",
126+
"\n",
127+
"Note that these kinds of records contain segments in which certain channels are missing. <strong>matplotlib</strong> automatically zooms in on sections without Nans in individual channels but the entire durations of the signals input into <strong>plotrec</strong> are actually plotted. \n",
128+
"\n"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"metadata": {
135+
"collapsed": false
136+
},
137+
"outputs": [],
138+
"source": [
139+
"# Demo 6 - Read the multi-segment record and plot waveforms from the MIMIC matched waveform database. \n",
140+
"record=wfdb.rdsamp('sampledata/matched/s25047/s25047-2704-05-04-10-44')\n",
141+
"wfdb.plotrec(record, title='Record s25047-2704-05-04-10-44') \n",
142+
"display(record.__dict__)\n",
143+
"\n",
144+
"# Can also read the same files hosted on PhysioBank (takes long to stream the many large files)\n",
145+
"#record2 = wfdb.rdsamp('s25047-2704-05-04-10-44', pbdir = 'mimic2wdb/matched/s25047')"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"metadata": {
152+
"collapsed": false
153+
},
154+
"outputs": [],
155+
"source": [
156+
"# Demo 7 - Read the multi-segment record and plot waveforms from the MIMIC matched waveform database.\n",
157+
"# Notice that some channels have no valid values to plot\n",
158+
"record = wfdb.rdsamp('sampledata/matched/s00001/s00001-2896-10-10-00-31', sampfrom = 3000000, sampto = 4000000)\n",
159+
"wfdb.plotrec(record, title='Record s00001/s00001-2896-10-10-00-31') \n",
160+
"display(record.__dict__)\n",
161+
"\n",
162+
"# Can also read the same files hosted on PhysioBank\n",
163+
"record2 = wfdb.rdsamp('s00001-2896-10-10-00-31', sampfrom = 3000000, sampto = 4000000, pbdir = 'mimic2wdb/matched/s00001')"
164+
]
165+
},
166+
{
167+
"cell_type": "markdown",
168+
"metadata": {},
169+
"source": [
170+
"### Writing Records and Annotations"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {
177+
"collapsed": false
178+
},
179+
"outputs": [],
180+
"source": [
181+
"# Demo 8 - Read a WFDB record's digital samples and create a copy via the wrsamp() instance method \n",
182+
"# of the Record object.\n",
183+
"\n",
184+
"# Read a record as a Record object.\n",
185+
"record = wfdb.rdsamp('sampledata/100', physical = False)\n",
186+
"record.recordname = '100x'\n",
187+
"\n",
188+
"# Call the instance method of the object\n",
189+
"record.wrsamp()\n",
190+
"\n",
191+
"# The new file can be read\n",
192+
"recordx = wfdb.rdsamp('100x')"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {
199+
"collapsed": true
200+
},
201+
"outputs": [],
202+
"source": [
203+
"# Demo 9 - Write a WFDB record without using a Record object via the gateway wrsamp function.\n",
204+
"# This is the basic way to write physical signals to a WFDB file. \n",
205+
"\n",
206+
"# Read part of a record from Physiobank\n",
207+
"sig, fields = wfdb.srdsamp('a103l', sampfrom = 50000, channels = [0,1], pbdir = 'challenge/2015/training')\n",
208+
"\n",
209+
"# 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'])\n",
211+
"\n",
212+
"# The new file can be read\n",
213+
"recordecg = wfdb.rdsamp('ecgrecord')"
214+
]
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": null,
219+
"metadata": {
220+
"collapsed": false
221+
},
222+
"outputs": [],
223+
"source": [
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",
228+
"annotation = wfdb.rdann('sampledata/100', 'atr')\n",
229+
"annotation.annotator = 'cpy'\n",
230+
"\n",
231+
"# Call the instance method of the object\n",
232+
"annotation.wrann()\n",
233+
"\n",
234+
"# The new file can be read\n",
235+
"ann100copy = wfdb.rdann('100', 'cpy')"
236+
]
237+
},
238+
{
239+
"cell_type": "code",
240+
"execution_count": null,
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')"
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": null,
261+
"metadata": {
262+
"collapsed": false
263+
},
264+
"outputs": [],
265+
"source": [
266+
"# Demo 12 - View what the 'anntype' symbols mean in the standard WFDB library\n",
267+
"wfdb.showanncodes()"
268+
]
269+
},
270+
{
271+
"cell_type": "markdown",
272+
"metadata": {},
273+
"source": [
274+
"### Downloading Content from Physiobank\n",
275+
"\n",
276+
"- The downloads are made via http\n",
277+
"- See the above demos for examples on streaming WFDB files stored in Physiobank without downloading them to local disk\n",
278+
"- Physionet has rsync modules for downloading entire databases for users who have access to rsync. "
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": null,
284+
"metadata": {
285+
"collapsed": false
286+
},
287+
"outputs": [],
288+
"source": [
289+
"# Demo 13 - List the Physiobank Databases\n",
290+
"\n",
291+
"dbs = wfdb.getdblist()\n",
292+
"display(dbs)"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"metadata": {
299+
"collapsed": false
300+
},
301+
"outputs": [],
302+
"source": [
303+
"# Demo 14 - Download all the WFDB records and annotations from a small Physiobank Database\n",
304+
"\n",
305+
"# Make a temporary download directory in your current working directory\n",
306+
"cwd = os.getcwd()\n",
307+
"dldir = os.path.join(cwd, 'tmp_dl_dir')\n",
308+
"# Make sure to use a new directory\n",
309+
"while os.path.exists(dldir):\n",
310+
" dldir = dldir+'1'\n",
311+
"\n",
312+
"# Download all the WFDB content\n",
313+
"wfdb.dldatabase('ahadb', dlbasedir = dldir)\n",
314+
"\n",
315+
"# Display the downloaded content in the folder\n",
316+
"display(os.listdir(dldir))"
317+
]
318+
},
319+
{
320+
"cell_type": "code",
321+
"execution_count": null,
322+
"metadata": {
323+
"collapsed": false
324+
},
325+
"outputs": [],
326+
"source": [
327+
"# Demo 15 - Download specified files from a Physiobank database\n",
328+
"\n",
329+
"# The files to download\n",
330+
"filelist = ['STAFF-Studies-bibliography-2016.pdf', 'data/001a.hea', 'data/001a.dat', 'data/001b.hea', 'data/001b.dat']\n",
331+
"\n",
332+
"# Make a temporary download directory in your current working directory\n",
333+
"cwd = os.getcwd()\n",
334+
"dldir = os.path.join(cwd, 'tmp_dl_dir')\n",
335+
"# Make sure to use a new directory\n",
336+
"while os.path.exists(dldir):\n",
337+
" dldir = dldir+'1'\n",
338+
"\n",
339+
"# Download the listed files\n",
340+
"wfdb.dldatabasefiles('staffiii', dldir, filelist)\n",
341+
"\n",
342+
"# Display the downloaded content in the folder\n",
343+
"display(os.listdir(dldir))\n",
344+
"display(os.listdir(os.path.join(dldir, 'data')))"
345+
]
346+
},
347+
{
348+
"cell_type": "code",
349+
"execution_count": null,
350+
"metadata": {
351+
"collapsed": true
352+
},
353+
"outputs": [],
354+
"source": []
355+
}
356+
],
357+
"metadata": {
358+
"kernelspec": {
359+
"display_name": "Python 3",
360+
"language": "python",
361+
"name": "python3"
362+
},
363+
"language_info": {
364+
"codemirror_mode": {
365+
"name": "ipython",
366+
"version": 3
367+
},
368+
"file_extension": ".py",
369+
"mimetype": "text/x-python",
370+
"name": "python",
371+
"nbconvert_exporter": "python",
372+
"pygments_lexer": "ipython3",
373+
"version": "3.5.2"
374+
}
375+
},
376+
"nbformat": 4,
377+
"nbformat_minor": 0
378+
}

0 commit comments

Comments
 (0)