From 8e6c634fa1e8a4bcd82d5f6bd9aa21e32146badf Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 17:10:01 -0500 Subject: [PATCH 1/2] List databases in get_dbs through PhysioNet's API PhysioNet has removed https://physionet.org/rest/database-list/, so get_dbs failed with a 404 error. Use the physionet package's list of published projects instead, keeping the latest version of each database. Fixes #439. --- changelog.d/get-dbs-api.rst | 5 +++++ src/wfdb/io/download.py | 25 +++++++++++++++--------- tests/test_physionet_uri.py | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 changelog.d/get-dbs-api.rst diff --git a/changelog.d/get-dbs-api.rst b/changelog.d/get-dbs-api.rst new file mode 100644 index 00000000..0a556faa --- /dev/null +++ b/changelog.d/get-dbs-api.rst @@ -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. diff --git a/src/wfdb/io/download.py b/src/wfdb/io/download.py index 1d64473b..e02d44ea 100644 --- a/src/wfdb/io/download.py +++ b/src/wfdb/io/download.py @@ -1,5 +1,4 @@ import functools -import json import multiprocessing.dummy import os import posixpath @@ -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 ---------- @@ -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 -------- @@ -256,13 +260,16 @@ 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 + projects = physionet.PhysioNetClient().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 ------- # diff --git a/tests/test_physionet_uri.py b/tests/test_physionet_uri.py index 6c7ea850..33471cb7 100644 --- a/tests/test_physionet_uri.py +++ b/tests/test_physionet_uri.py @@ -416,3 +416,41 @@ 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) + + assert wfdb.get_dbs() == [ + ["ludb", "Lobachevsky University Database"], + ["mitdb", "MIT-BIH Arrhythmia Database"], + ] + + +@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) From e25f8b8e57e175665f01bc6676758c4650623248 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Mon, 23 Nov 2026 17:31:00 -0500 Subject: [PATCH 2/2] Close the PhysioNet client in get_dbs --- src/wfdb/io/download.py | 3 ++- tests/test_physionet_uri.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wfdb/io/download.py b/src/wfdb/io/download.py index e02d44ea..438a11a7 100644 --- a/src/wfdb/io/download.py +++ b/src/wfdb/io/download.py @@ -262,7 +262,8 @@ def get_dbs(): """ import physionet - projects = physionet.PhysioNetClient().projects.list_published() + 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 diff --git a/tests/test_physionet_uri.py b/tests/test_physionet_uri.py index 33471cb7..487015f4 100644 --- a/tests/test_physionet_uri.py +++ b/tests/test_physionet_uri.py @@ -442,11 +442,34 @@ def project(slug, version, title, resource_type="Database", latest=True): 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