Skip to content

Commit d7073db

Browse files
committed
add fsspec to rdheader
1 parent 0d45b74 commit d7073db

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dependencies = [
1616
"soundfile >= 0.10.0",
1717
"matplotlib >= 3.2.2",
1818
"requests >= 2.8.1",
19+
"fsspec >= 2023.10.0",
20+
"aiohttp >= 3.11.11",
1921
]
2022
dynamic = ["version"]
2123

wfdb/io/download.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import posixpath
55

6+
import fsspec
67
import numpy as np
78

89
from wfdb.io import _url
@@ -12,6 +13,9 @@
1213
PN_INDEX_URL = "https://physionet.org/files/"
1314
PN_CONTENT_URL = "https://physionet.org/content/"
1415

16+
# Cloud protocols
17+
CLOUD_PROTOCOLS = ["az:", "azureml:", "s3:", "gs:"]
18+
1519

1620
class Config(object):
1721
"""
@@ -101,11 +105,15 @@ def _stream_header(file_name: str, pn_dir: str) -> str:
101105
The text contained in the header file
102106
103107
"""
104-
# Full url of header location
105-
url = posixpath.join(config.db_index_url, pn_dir, file_name)
108+
# Full cloud url
109+
if any(pn_dir.startswith(proto) for proto in CLOUD_PROTOCOLS):
110+
url = posixpath.join(pn_dir, file_name)
111+
# Full physionet database url
112+
else:
113+
url = posixpath.join(config.db_index_url, pn_dir, file_name)
106114

107115
# Get the content of the remote file
108-
with _url.openurl(url, "rb") as f:
116+
with fsspec.open(url, "rb") as f:
109117
content = f.read()
110118

111119
return content.decode("iso-8859-1")

wfdb/io/record.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import re
66

7+
import fsspec
78
import numpy as np
89
import pandas as pd
910

@@ -1826,8 +1827,11 @@ def rdheader(record_name, pn_dir=None, rd_segments=False):
18261827
dir_name, base_record_name = os.path.split(record_name)
18271828
dir_name = os.path.abspath(dir_name)
18281829

1829-
# Construct the download path using the database version
1830-
if (pn_dir is not None) and ("." not in pn_dir):
1830+
# If this is a cloud path we leave it as is
1831+
if (pn_dir is not None) and any(pn_dir.startswith(proto) for proto in download.CLOUD_PROTOCOLS):
1832+
pass
1833+
# If it isn't a cloud path, construct the download path using the database version
1834+
elif (pn_dir is not None) and ("." not in pn_dir):
18311835
dir_list = pn_dir.split("/")
18321836
pn_dir = posixpath.join(
18331837
dir_list[0], download.get_version(dir_list[0]), *dir_list[1:]
@@ -1836,7 +1840,7 @@ def rdheader(record_name, pn_dir=None, rd_segments=False):
18361840
# Read the local or remote header file.
18371841
file_name = f"{base_record_name}.hea"
18381842
if pn_dir is None:
1839-
with open(
1843+
with fsspec.open(
18401844
os.path.join(dir_name, file_name),
18411845
"r",
18421846
encoding="ascii",

0 commit comments

Comments
 (0)