#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Timestamp: "2026-01-29 22:40:00 (ywatanabe)"
# File: /home/ywatanabe/proj/scitex-dataset/src/scitex_dataset/neuroscience/physionet.py
"""
PhysioNet dataset fetcher.
PhysioNet hosts physiological signal databases including EEG, ECG, EMG,
and other biomedical signals.
API: https://physionet.org/api/v1/
Example:
>>> from scitex_dataset.neuroscience import physionet
>>> datasets = physionet.fetch_all_datasets(max_datasets=10)
>>> formatted = [physionet.format_dataset(ds) for ds in datasets]
"""
from __future__ import annotations
from typing import Optional
import httpx as _httpx
from scitex_dev.decorators import supports_return_as
PHYSIONET_API = "https://physionet.org"
__all__ = [
"PHYSIONET_API",
"fetch_datasets",
"fetch_all_datasets",
"format_dataset",
]
#: The published-projects listing. ``/rest/database-list/`` (the old
#: endpoint) answers 404 since PhysioNet's API move; this one returns every
#: published project version, unpaginated.
PUBLISHED_ENDPOINT = "/api/v1/project/published/"
[docs]
@supports_return_as
def fetch_datasets(page: int = 1) -> list:
"""Fetch the published PhysioNet projects (LATEST version of each).
The endpoint is not paginated; ``page`` > 1 returns an empty list so the
pagination loop of :func:`fetch_all_datasets` ends after one call.
"""
if page > 1:
return []
response = _httpx.get(f"{PHYSIONET_API}{PUBLISHED_ENDPOINT}", timeout=60.0)
response.raise_for_status()
return [p for p in response.json() if p.get("is_latest_version", True)]
[docs]
@supports_return_as
def fetch_all_datasets(
max_datasets: Optional[int] = None,
logger=None,
) -> list[dict]:
"""Fetch all databases from PhysioNet with pagination."""
all_datasets = []
page = 1
while True:
try:
result = fetch_datasets(page=page)
except _httpx.HTTPStatusError as exc:
if logger:
logger.error(f"HTTP Error: {exc}")
break
except _httpx.RequestError as exc:
if logger:
logger.error(f"Request Error: {exc}")
break
# PhysioNet returns a list directly or paginated dict
if isinstance(result, list):
databases = result
has_next = False
else:
databases = result.get("results", result.get("databases", []))
has_next = bool(result.get("next"))
if not databases:
break
all_datasets.extend(databases)
if logger:
logger.info(f"Fetched {len(all_datasets)} databases...")
if max_datasets and len(all_datasets) >= max_datasets:
all_datasets = all_datasets[:max_datasets]
break
if not has_next:
break
page += 1
return all_datasets
[docs]
def _plain_text(html: str) -> str:
"""The abstract without markup, for full-text search."""
import html as _html
import re
return _html.unescape(re.sub(r"<[^>]+>", " ", html or "")).strip()
# EOF