Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/get-dbs-api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**Fix get_dbs, which PhysioNet no longer supported**
``get_dbs`` failed with a 404 error, because PhysioNet has removed the
``/rest/database-list/`` page that it read. It now lists databases through
PhysioNet's API, using the ``physionet`` package, and returns the slug and
title of the latest version of each database, as before.
26 changes: 17 additions & 9 deletions src/wfdb/io/download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import json
import multiprocessing.dummy
import os
import posixpath
Expand Down Expand Up @@ -233,7 +232,12 @@ def _stream_annotation(file_name, pn_dir):


def get_dbs():
"""Get a list of all the PhysioNet databases available.
"""Get a list of the databases published on PhysioNet.

The list comes from PhysioNet's API, through the ``physionet`` package. It
includes every database on PhysioNet, including those that need
credentials or a signed data use agreement to access, but not software,
models or challenges.

Parameters
----------
Expand All @@ -242,7 +246,7 @@ def get_dbs():
Returns
-------
dbs : list
All of the databases currently available for analysis.
The slug and title of each database, sorted by slug.

Examples
--------
Expand All @@ -256,13 +260,17 @@ def get_dbs():
]

"""
with _url.openurl("https://physionet.org/rest/database-list/", "rb") as f:
content = f.read()
dbs = json.loads(content)
dbs = [[d["slug"], d["title"]] for d in dbs]
dbs.sort()
import physionet

return dbs
with physionet.PhysioNetClient() as client:
projects = client.projects.list_published()
# The API lists every version of each project, with the title of each
titles = {
project.slug: project.title
for project in projects
if project.resource_type == "Database" and project.is_latest_version
}
return [[slug, title] for slug, title in sorted(titles.items())]


# ---- Helper functions for downloading PhysioNet files ------- #
Expand Down
61 changes: 61 additions & 0 deletions tests/test_physionet_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,64 @@ def test_dl_files_overwrite_removes_nothing_itself(demo_database, tmp_path):
[(args, kwargs)] = demo_database
assert kwargs["overwrite"] is True
assert kwargs["files"] == {"100.hea": "100.hea", "999.hea": "999.hea"}


def test_get_dbs_lists_the_latest_version_of_each_database(monkeypatch):
"""get_dbs lists databases from PhysioNet's API, once each, sorted by slug."""
import physionet
from physionet.api.endpoints import ProjectsAPI

def project(slug, version, title, resource_type="Database", latest=True):
return physionet.api.models.PublishedProject.from_dict(
{
"slug": slug,
"version": version,
"title": title,
"resource_type": resource_type,
"is_latest_version": latest,
}
)

projects = [
project("mitdb", "1.0.0", "MIT-BIH Arrhythmia Database"),
project("ludb", "1.0.0", "Old title", latest=False),
project("ludb", "1.0.1", "Lobachevsky University Database"),
project("wfdb-python", "4.0.0", "WFDB Python Package", "Software"),
project("challenge-2015", "1.0.0", "Reducing False Alarms", "Challenge"),
]
monkeypatch.setattr(ProjectsAPI, "list_published", lambda self: projects)
closed = []
monkeypatch.setattr(
physionet.PhysioNetClient, "close", lambda self: closed.append(self)
)

assert wfdb.get_dbs() == [
["ludb", "Lobachevsky University Database"],
["mitdb", "MIT-BIH Arrhythmia Database"],
]
assert len(closed) == 1


def test_get_dbs_closes_its_session_after_an_error(monkeypatch):
import physionet
from physionet.api.endpoints import ProjectsAPI

def list_published(self):
raise ConnectionError("PhysioNet is unavailable")

monkeypatch.setattr(ProjectsAPI, "list_published", list_published)
closed = []
monkeypatch.setattr(
physionet.PhysioNetClient, "close", lambda self: closed.append(self)
)

with pytest.raises(ConnectionError):
wfdb.get_dbs()
assert len(closed) == 1


@pytest.mark.network
def test_get_dbs_from_physionet():
dbs = wfdb.get_dbs()
assert ["mitdb", "MIT-BIH Arrhythmia Database"] in dbs
assert len({slug for slug, title in dbs}) == len(dbs)
Loading