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..438a11a7 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,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 ------- # diff --git a/tests/test_physionet_uri.py b/tests/test_physionet_uri.py index 6c7ea850..487015f4 100644 --- a/tests/test_physionet_uri.py +++ b/tests/test_physionet_uri.py @@ -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)