Skip to content

Commit 30e0e43

Browse files
author
Chen Xie
committed
download messages
1 parent 54ddfff commit 30e0e43

File tree

5 files changed

+136
-85
lines changed

5 files changed

+136
-85
lines changed

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ See the <strong>wfdbdemo.ipynb</strong> file for example scripts on how to call
2626
<strong>rdsamp</strong> - Read a WFDB file and return the signal as a numpy array and the metadata as a dictionary.
2727

2828
```
29-
import numpy as np
30-
from wfdb import readsignal
31-
sig, fields = readsignal.rdsamp(recordname, sampfrom, sampto, channels, physical, stacksegments)
29+
import wfdb
30+
sig, fields = wfdb.rdsamp(recordname, sampfrom, sampto, channels, physical, stacksegments)
3231
```
3332

3433
Input Arguments:
@@ -58,9 +57,8 @@ Output Arguments:
5857
<strong>rdann</strong> - Read a WFDB annotation file <code>recordname.annot</code> and return the fields as lists or arrays.
5958

6059
```
61-
import numpy as np
62-
from wfdb import readannot
63-
annsamp, anntype, subtype, chan, num, aux, annfs) = readannot.rdann(recordname, annot, sampfrom, sampto, anndisp)
60+
import wfdb
61+
annsamp, anntype, subtype, chan, num, aux, annfs) = wfdb.rdann(recordname, annot, sampfrom, sampto, anndisp)
6462
```
6563

6664
Input Arguments:
@@ -92,11 +90,10 @@ Output arguments:
9290
<strong>plotsigs</strong> - Subplot and label each channel of an nxm signal on a graph. Also subplot annotation locations on selected channels if present.
9391

9492
```
95-
import numpy as np
96-
from wfdb import readsignal
93+
import wfdb
9794
from wfdb import plotwfdb
98-
sig, fields = readsignal.rdsamp(recordname)
99-
annsamp=readannot.rdann('recordname', 'annot')[0]
95+
sig, fields = wfdb.rdsamp(recordname)
96+
annsamp=wfdb.rdann('recordname', 'annot')[0]
10097
plotwfdb.plotsigs(sig, fields, annsamp, annch, title, plottime):
10198
10299
```

devtests.ipynb

Lines changed: 99 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,11 @@
197197
},
198198
{
199199
"cell_type": "code",
200-
"execution_count": 1,
200+
"execution_count": null,
201201
"metadata": {
202202
"collapsed": false
203203
},
204-
"outputs": [
205-
{
206-
"name": "stdout",
207-
"output_type": "stream",
208-
"text": [
209-
"[[ nan nan nan]\n",
210-
" [ nan nan nan]\n",
211-
" [ nan nan nan]\n",
212-
" ..., \n",
213-
" [-0.45454545 nan -0.72413793]\n",
214-
" [-0.54545455 nan -0.72413793]\n",
215-
" [-0.63636364 nan -0.72413793]]\n",
216-
"['Empty Segment', {'signame': ['V', 'II', 'No Channel'], 'basetime': '31:51.982', 'initvalue': [0, 0, 'No Channel'], 'nseg': 1, 'gain': [1.0, 1.0, 'No Channel'], 'fs': 125.0, 'units': ['mV', 'mV', 'No Channel'], 'byteoffset': [0, 0, 'No Channel'], 'fmt': ['80', '80', 'No Channel'], 'nsamp': 6750, 'filename': ['3975656_0001.dat', '3975656_0001.dat', 'No Channel'], 'comments': [], 'baseline': [0, 0, 'No Channel'], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 1, 'No Channel'], 'skew': [0, 0, 'No Channel'], 'basedate': ''}, {'signame': ['V', 'II', 'No Channel'], 'basetime': '32:45.982', 'initvalue': [-11, -33, 'No Channel'], 'nseg': 1, 'gain': [11.0, 29.0, 'No Channel'], 'fs': 125.0, 'units': ['mV', 'mV', 'No Channel'], 'byteoffset': [0, 0, 'No Channel'], 'fmt': ['80', '80', 'No Channel'], 'nsamp': 125, 'filename': ['3975656_0002.dat', '3975656_0002.dat', 'No Channel'], 'comments': [], 'baseline': [0, 0, 'No Channel'], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 1, 'No Channel'], 'skew': [0, 0, 'No Channel'], 'basedate': ''}, {'signame': ['V', 'No Channel', 'MCL1'], 'basetime': '32:46.982', 'initvalue': [-4, 'No Channel', -16], 'nseg': 1, 'gain': [11.0, 'No Channel', 29.0], 'fs': 125.0, 'units': ['mV', 'No Channel', 'mV'], 'byteoffset': [0, 'No Channel', 0], 'fmt': ['80', 'No Channel', '80'], 'nsamp': 1354, 'filename': ['3975656_0003.dat', 'No Channel', '3975656_0003.dat'], 'comments': [], 'baseline': [0, 'No Channel', 0], 'nsampseg': [], 'nsig': 2, 'sampsperframe': [1, 'No Channel', 1], 'skew': [0, 'No Channel', 0], 'basedate': ''}]\n"
217-
]
218-
}
219-
],
204+
"outputs": [],
220205
"source": [
221206
"# Testing rdsamp multi-segment record with sampfrom, sampto, and channel selection\n",
222207
"import numpy as np\n",
@@ -755,43 +740,32 @@
755740
"collapsed": false
756741
},
757742
"outputs": [],
758-
"source": [
759-
"import os\n",
760-
"from wfdb import readsignal\n",
761-
"\n",
762-
"# Test 1\n",
763-
"os.chdir('/home/cx1111/Testfolder/database/')\n",
764-
"sig, fields=readsignal.rdsamp('mitdb/110')\n",
765-
"print(sig)\n"
766-
]
767-
},
768-
{
769-
"cell_type": "code",
770-
"execution_count": null,
771-
"metadata": {
772-
"collapsed": false
773-
},
774-
"outputs": [],
775-
"source": [
776-
"# Test 2\n",
777-
"sig, fields=readsignal.rdsamp('sampledata/100')\n",
778-
"\n",
779-
"print(sig)"
780-
]
743+
"source": []
781744
},
782745
{
783746
"cell_type": "code",
784-
"execution_count": null,
747+
"execution_count": 6,
785748
"metadata": {
786749
"collapsed": false
787750
},
788-
"outputs": [],
751+
"outputs": [
752+
{
753+
"ename": "AttributeError",
754+
"evalue": "'function' object has no attribute 'dlrecordfiles'",
755+
"output_type": "error",
756+
"traceback": [
757+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
758+
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
759+
"\u001b[1;32m<ipython-input-6-476c7dab8711>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mwfdb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mwfdb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrdsamp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdlrecordfiles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'macecgdb/test01_00s'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'/home/cx1111/Downloads/wfdbrecords/prcp'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
760+
"\u001b[1;31mAttributeError\u001b[0m: 'function' object has no attribute 'dlrecordfiles'"
761+
]
762+
}
763+
],
789764
"source": [
790765
"import os\n",
791-
"from wfdb import readsignal\n",
792-
"from wfdb import pbdownload\n",
766+
"import wfdb\n",
793767
"\n",
794-
"pbdownload.dlrecordfiles('prcp/12726', '/home/cx1111/Testfolder/database/prcp')"
768+
"wfdb.rdsamp.dlrecordfiles('macecgdb/test01_00s', '/home/cx1111/Downloads/wfdbrecords/prcp')"
795769
]
796770
},
797771
{
@@ -826,6 +800,86 @@
826800
" "
827801
]
828802
},
803+
{
804+
"cell_type": "code",
805+
"execution_count": 3,
806+
"metadata": {
807+
"collapsed": false
808+
},
809+
"outputs": [
810+
{
811+
"name": "stdout",
812+
"output_type": "stream",
813+
"text": [
814+
"Downloading missing file(s) into directory: /home/cx1111/Downloads/wfdbrecords/mimic2wdb/matched/s00001\n",
815+
"Download complete\n",
816+
"[[ nan nan nan nan]\n",
817+
" [ nan nan nan nan]\n",
818+
" [ nan nan nan nan]\n",
819+
" ..., \n",
820+
" [ 0.73493976 2.01818182 nan nan]\n",
821+
" [ 0.75903614 1.10909091 nan nan]\n",
822+
" [ 1.3373494 1.14545455 nan nan]]\n"
823+
]
824+
}
825+
],
826+
"source": [
827+
"import wfdb\n",
828+
"sig, fields=wfdb.rdsamp('mimic2wdb/matched/s00001/s00001-2896-10-10-00-31')\n",
829+
"\n",
830+
"print(sig)"
831+
]
832+
},
833+
{
834+
"cell_type": "code",
835+
"execution_count": null,
836+
"metadata": {
837+
"collapsed": false
838+
},
839+
"outputs": [],
840+
"source": [
841+
"sig, fields=wfdb.rdsamp('mitdb/100')"
842+
]
843+
},
844+
{
845+
"cell_type": "code",
846+
"execution_count": null,
847+
"metadata": {
848+
"collapsed": false
849+
},
850+
"outputs": [],
851+
"source": [
852+
"from configparser import ConfigParser\n",
853+
"config = loadconfig('/home/cx1111/PhysionetProjects/wfdb-python/wfdb/wfdb.config')\n",
854+
"\n"
855+
]
856+
},
857+
{
858+
"cell_type": "code",
859+
"execution_count": null,
860+
"metadata": {
861+
"collapsed": false
862+
},
863+
"outputs": [],
864+
"source": [
865+
"import os\n",
866+
"a=os.path.join('/usr/local/database/', 'mitdb')\n",
867+
"print(a)"
868+
]
869+
},
870+
{
871+
"cell_type": "code",
872+
"execution_count": null,
873+
"metadata": {
874+
"collapsed": false
875+
},
876+
"outputs": [],
877+
"source": [
878+
"dbcachedir='/usr/local/database'\n",
879+
"\n",
880+
"print(dbcachedir)"
881+
]
882+
},
829883
{
830884
"cell_type": "code",
831885
"execution_count": null,

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""The WFDB toolbox.
1+
"""The WFDB Python Toolbox.
22
See: https://www.physionet.org/physiotools/wfdb.shtml
33
"""
44

@@ -22,7 +22,7 @@
2222
# https://packaging.python.org/en/latest/single_source_version.html
2323
version='0.1.0',
2424

25-
description='The WFDB toolbox',
25+
description='The WFDB Python Toolbox',
2626
long_description=long_description,
2727

2828
# The project's main homepage.
@@ -90,4 +90,4 @@
9090
# 'sample=sample:main',
9191
# ],
9292
# },
93-
)
93+
)

wfdb/_rdsamp.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,14 @@ def checkrecordfiles(recordname, filedirectory):
4444
basedir, baserecname = os.path.split(recordname)
4545

4646
# At this point we do not know whether basedir is a local directory, a
47-
# physiobank directory, or both!
47+
# physiobank directory, or both.
4848

49-
if not basedir: # if there is no base directory then: 1. The files must be located
50-
# in the current working directory.
51-
# 2. There will be no target files to download. So just directly
52-
# return. If files are missing we cannot download them anyway.
49+
if not basedir: # if basedir is not defined, then there is no physiobank
50+
# database specified. If files are missing we cannot download them anyway.
5351
return recordname, []
5452

5553
# If this is reached, basedir is defined. Check if there is a directory
56-
# called 'basedir':
57-
# the 'basedir' directory exists. Check it for files.
54+
# called 'basedir'. If it exists, check it for files.
5855
if os.path.isdir(basedir):
5956
# It is possible that basedir is also a physiobank database. Therefore
6057
# if any files are missing, ,try to download files assuming basedir is
@@ -107,11 +104,11 @@ def checkrecordfiles(recordname, filedirectory):
107104
# All files were already present in the 'basedir' directory.
108105
return recordname, []
109106

110-
else: # there is no 'basedir' directory. Therefore basedir must be a
107+
else: # there is no 'basedir' directory in your relative path. Therefore basedir must be a
111108
# physiobank database directory. check the current working directory for files.
112109
# If any are missing, check the cache directory for files and download missing
113110
# files from physiobank.
114-
111+
115112
pbdir = basedir # physiobank directory
116113
downloaddir = os.path.join(dbcachedir, pbdir)
117114

@@ -176,9 +173,10 @@ def dlrecordfiles(pbrecname, targetdir):
176173
- dledfiles: The list of files downloaded from PhysioBank.
177174
178175
"""
176+
179177
physioneturl = "http://physionet.org/physiobank/database/"
180178
pbdir, baserecname = os.path.split(pbrecname)
181-
print('Downloading missing file(s) into directory: ', targetdir)
179+
displaydlmsg=1
182180

183181
if not os.path.isdir(
184182
targetdir): # Make the target directory if it doesn't already exist
@@ -198,7 +196,9 @@ def dlrecordfiles(pbrecname, targetdir):
198196
try:
199197
remotefile = physioneturl + pbrecname + ".hea"
200198
targetfile = os.path.join(targetdir, baserecname + ".hea")
199+
print('Downloading missing file(s) into directory: ', targetdir)
201200
r = requests.get(remotefile)
201+
displaydlmsg=0
202202
with open(targetfile, "w") as text_file:
203203
text_file.write(r.text)
204204
dledfiles.append(targetfile)
@@ -224,7 +224,8 @@ def dlrecordfiles(pbrecname, targetdir):
224224
os.path.join(
225225
targetdir,
226226
f),
227-
dledfiles)
227+
dledfiles, displaydlmsg, targetdir)
228+
displaydlmsg=0
228229

229230
else: # Multi segment. Check for all segment headers and their dat files
230231
for segment in fields["filename"]:
@@ -244,28 +245,30 @@ def dlrecordfiles(pbrecname, targetdir):
244245
targetdir,
245246
segment +
246247
".hea"),
247-
dledfiles)
248+
dledfiles, displaydlmsg, targetdir)
249+
displaydlmsg=0
248250
segfields = readheader(os.path.join(targetdir, segment))
249251
for f in segfields["filename"]:
250252
if f != '~':
251-
if not os.path.isfile(
252-
os.path.join(
253-
targetdir,
254-
f)): # Missing a segment's dat file
253+
if not os.path.isfile(os.path.join(targetdir, f)): # Missing a segment's dat file
255254
dledfiles = dlorexit(
256255
physioneturl + pbdir + "/" + f,
257256
os.path.join(
258257
targetdir,
259258
f),
260-
dledfiles)
261-
262-
print('Download complete')
259+
dledfiles, displaydlmsg, targetdir)
260+
displaydlmsg=0
261+
if dledfiles:
262+
print('Downloaded all files for record')
263263
return dledfiles # downloaded files
264264

265265

266266
# Helper function for dlrecordfiles. Download the file from the specified
267267
# 'url' as the 'filename', or exit with warning.
268-
def dlorexit(url, filename, dledfiles):
268+
def dlorexit(url, filename, dledfiles, displaydlmsg=0, targetdir=[]):
269+
270+
if displaydlmsg: # We want this message to be called once for all files downloaded.
271+
print('Downloading missing file(s) into directory: ', targetdir)
269272
try:
270273
r = requests.get(url)
271274
with open(filename, "w") as text_file:
@@ -1173,10 +1176,9 @@ def rdsamp(
11731176

11741177
filestoremove = []
11751178
config = loadconfig('wfdb.config')
1176-
1177-
if config.get('pbdownload','getpbfiles') == 1: # Flag specifying whether to allow downloading from physiobank
1178-
recordname, dledfiles = checkrecordfiles(recordname, os.getcwd())
11791179

1180+
if int(config.get('pbdownload','getpbfiles')) == 1: # Flag specifying whether to allow downloading from physiobank
1181+
recordname, dledfiles = checkrecordfiles(recordname, os.getcwd())
11801182
if int(config.get('pbdownload','keepdledfiles')) == 0: # Flag specifying whether to keep downloaded physiobank files
11811183
filestoremove = dledfiles
11821184

wfdb/wfdb.config

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ getpbfiles: 1
77
# Flag that specifies whether to keep automatically downloaded physiobank files
88
keepdledfiles: 1
99

10-
# Base cache directory in which data files are searched for (after searching the current working directory)
11-
# and missing files are downloaded. The full download directory is cachedir/pbsubdir where pbsubdir is the .
12-
# Default value: /usr/local/database/.
13-
dbcachedir: '/usr/local/database/'
10+
# Base cache directory in which data files are searched for (after searching the current working directory) and missing files are downloaded. The full download directory is dbcachedir/pbsubdir where pbsubdir is the physiobank database name.
11+
dbcachedir: /home/cx1111/Downloads/wfdbrecords/

0 commit comments

Comments
 (0)