Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b77a79

Browse files
committed
allow custom db index url. Fixes MIT-LCP#125
1 parent d0fe3c3 commit 2b77a79

File tree

8 files changed

+46
-17
lines changed

8 files changed

+46
-17
lines changed

docs/io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ Downloading
3232
-----------
3333

3434
.. automodule:: wfdb.io
35-
:members: get_dbs, get_record_list, dl_database, dl_files
35+
:members: get_dbs, get_record_list, dl_database, dl_files, set_db_index_url

docs/wfdb.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Downloading
3232
-----------
3333

3434
.. automodule:: wfdb
35-
:members: get_dbs, get_record_list, dl_database, dl_files
35+
:members: get_dbs, get_record_list, dl_database, dl_files, set_db_index_url
3636

3737

3838
Plotting

sample-data/100-no-len.hea

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# unnecessary comment
2+
100-no-len 2 360
3+
100.dat 212 200 11 1024 995 -22131 0 MLII
4+
5+
100.dat 212 200 11 1024 1011 20052 0 V5
6+
# 69 M 1085 1629 x1
7+
# Aldomet, Inderal

wfdb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
wrsamp, dl_database)
33
from .io.annotation import (Annotation, rdann, wrann, show_ann_labels,
44
show_ann_classes)
5-
from .io.download import get_dbs, get_record_list, dl_files
5+
from .io.download import get_dbs, get_record_list, dl_files, set_db_index_url
66
from .plot.plot import plot_items, plot_wfdb, plot_all_records
77

88
from .version import __version__

wfdb/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
from ._signal import est_res, wr_dat_file
44
from .annotation import (Annotation, rdann, wrann, show_ann_labels,
55
show_ann_classes)
6-
from .download import get_dbs, get_record_list, dl_files
6+
from .download import get_dbs, get_record_list, dl_files, set_db_index_url
77
from .tff import rdtff

wfdb/io/_signal.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,6 @@ def _infer_sig_len(file_name, fmt, n_sig, dir_name, pb_dir=None):
18131813
if pb_dir is None:
18141814
file_size = os.path.getsize(os.path.join(dir_name, file_name))
18151815
else:
1816-
url = posixpath.join(db_index_url, pb_dir, file_name)
18171816
file_size = download._remote_file_size(file_name=file_name,
18181817
pb_dir=pb_dir)
18191818

wfdb/io/download.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,30 @@
66
import requests
77

88

9-
DB_INDEX_URL = 'http://physionet.org/physiobank/database/'
9+
# The physiobank index url
10+
PB_INDEX_URL = 'http://physionet.org/physiobank/database/'
11+
12+
class Config(object):
13+
pass
14+
15+
# The configuration database index url. Uses physiobank index by default.
16+
config = Config()
17+
config.db_index_url = PB_INDEX_URL
18+
19+
20+
def set_db_index_url(db_index_url=PB_INDEX_URL):
21+
"""
22+
Set the database index url to a custom value, to stream remote
23+
files from another location.
24+
25+
Parameters
26+
----------
27+
db_index_url : str, optional
28+
The desired new database index url. Leave as default to reset
29+
to the physiobank index url.
30+
31+
"""
32+
config.db_index_url = db_index_url
1033

1134

1235
def _remote_file_size(url=None, file_name=None, pb_dir=None):
@@ -34,7 +57,7 @@ def _remote_file_size(url=None, file_name=None, pb_dir=None):
3457

3558
# Option to construct the url
3659
if file_name and pb_dir:
37-
url = posixpath.join(DB_INDEX_URL, pb_dir, file_name)
60+
url = posixpath.join(config.db_index_url, pb_dir, file_name)
3861

3962
response = requests.head(url, headers={'Accept-Encoding': 'identity'})
4063
# Raise HTTPError if invalid url
@@ -60,7 +83,7 @@ def _stream_header(file_name, pb_dir):
6083
6184
"""
6285
# Full url of header location
63-
url = posixpath.join(DB_INDEX_URL, pb_dir, file_name)
86+
url = posixpath.join(config.db_index_url, pb_dir, file_name)
6487
response = requests.get(url)
6588

6689
# Raise HTTPError if invalid url
@@ -117,7 +140,7 @@ def _stream_dat(file_name, pb_dir, byte_count, start_byte, dtype):
117140
"""
118141

119142
# Full url of dat file
120-
url = posixpath.join(DB_INDEX_URL, pb_dir, file_name)
143+
url = posixpath.join(config.db_index_url, pb_dir, file_name)
121144

122145
# Specify the byte range
123146
end_byte = start_byte + byte_count - 1
@@ -149,7 +172,7 @@ def _stream_annotation(file_name, pb_dir):
149172
150173
"""
151174
# Full url of annotation file
152-
url = posixpath.join(DB_INDEX_URL, pb_dir, file_name)
175+
url = posixpath.join(config.db_index_url, pb_dir, file_name)
153176

154177
# Get the content
155178
response = requests.get(url)
@@ -171,7 +194,7 @@ def get_dbs():
171194
>>> dbs = get_dbs()
172195
173196
"""
174-
url = posixpath.join(DB_INDEX_URL, 'DBS')
197+
url = posixpath.join(config.db_index_url, 'DBS')
175198
response = requests.get(url)
176199

177200
dbs = response.content.decode('ascii').splitlines()
@@ -201,7 +224,7 @@ def get_record_list(db_dir, records='all'):
201224
202225
"""
203226
# Full url physiobank database
204-
db_url = posixpath.join(DB_INDEX_URL, db_dir)
227+
db_url = posixpath.join(config.db_index_url, db_dir)
205228

206229
# Check for a RECORDS file
207230
if records == 'all':
@@ -221,7 +244,7 @@ def get_record_list(db_dir, records='all'):
221244
def get_annotators(db_dir, annotators):
222245

223246
# Full url physiobank database
224-
db_url = posixpath.join(DB_INDEX_URL, db_dir)
247+
db_url = posixpath.join(config.db_index_url, db_dir)
225248

226249
if annotators is not None:
227250
# Check for an ANNOTATORS file
@@ -283,7 +306,7 @@ def dl_pb_file(inputs):
283306
basefile, subdir, db, dl_dir, keep_subdirs, overwrite = inputs
284307

285308
# Full url of file
286-
url = posixpath.join(DB_INDEX_URL, db, subdir, basefile)
309+
url = posixpath.join(config.db_index_url, db, subdir, basefile)
287310

288311
# Supposed size of the file
289312
remote_file_size = _remote_file_size(url)
@@ -381,7 +404,7 @@ def dl_files(db, dl_dir, files, keep_subdirs=True, overwrite=False):
381404
"""
382405

383406
# Full url physiobank database
384-
db_url = posixpath.join(DB_INDEX_URL, db)
407+
db_url = posixpath.join(config.db_index_url, db)
385408
# Check if the database is valid
386409
response = requests.get(db_url)
387410
response.raise_for_status()

wfdb/io/record.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ def dl_database(db_dir, dl_dir, records='all', annotators='all',
15411541
15421542
"""
15431543
# Full url physiobank database
1544-
db_url = posixpath.join(download.DB_INDEX_URL, db_dir)
1544+
db_url = posixpath.join(download.config.db_index_url, db_dir)
15451545
# Check if the database is valid
15461546
r = requests.get(db_url)
15471547
r.raise_for_status()
@@ -1592,7 +1592,7 @@ def dl_database(db_dir, dl_dir, records='all', annotators='all',
15921592
if annotators is not None:
15931593
for a in annotators:
15941594
annfile = rec+'.'+a
1595-
url = posixpath.join(download.DB_INDEX_URL, db_dir, annfile)
1595+
url = posixpath.join(download.config.db_index_url, db_dir, annfile)
15961596
rh = requests.head(url)
15971597

15981598
if rh.status_code != 404:

0 commit comments

Comments
 (0)