Skip to content

Read EDF and WAV files from physionet:// and other URIs - #674

Merged
tompollard merged 2 commits into
mainfrom
physionet-uris-everywhere
Nov 24, 2026
Merged

tompollard merged 2 commits into
mainfrom
physionet-uris-everywhere

Conversation

@tompollard

Copy link
Copy Markdown
Member

Part of #534: the first step towards phasing out pn_dir, which is that every function that takes pn_dir also takes a physionet:// URI.

Of the 15 public functions that take pn_dir, all but three already accepted a physionet:// URI as record_name, because they read through rdrecord, rdheader or rdann. The other three, read_edf, rdedfann and read_wav, opened the path with open(), so read_edf("physionet://chbmit/1.0.0/chb01/chb01_01.edf") failed with FileNotFoundError. They also couldn't read s3:// or memory:// paths.

  • A new helper, _coreio._open_input_file, opens a local file as before, or reads a URI into memory (through fsspec, so physionet:// uses the physionet package, with its cache and sign-in).
  • read_edf and read_wav read the samples with np.frombuffer and the file size with seek, instead of np.fromfile and os.path.getsize, which need a local file. read_wav now also closes its file.
  • Output names (record_name of the returned record, and the files written by wfdb_to_edf and wfdb_to_wav) use the last part of a URI on every platform, instead of splitting on os.sep.

URIs are read without saving a copy, unlike pn_dir, which downloads the file into the current directory (and read_edf leaves it there). That pn_dir behaviour is unchanged here.

Testing

  • New offline tests read n16.edf, SC4001E0-PSG.wav and test_edfann.edf with physionet:// (from the cache) and memory:// URIs, and compare them with the local files. They also check that nothing is written to the current directory. These fail on main. Another test checks that wfdb_to_mat and sigavg read physionet:// URIs, as they already did.
  • Local files give identical results to main (signals, names, initial values and checksums) for n16.edf, wave_4.edf, SC4001E0-PSG.wav and the rdedfann output of test_edfann.edf.
  • read_edf("physionet://chbmit/1.0.0/chb01/chb01_01.edf") reads all 23 signals from PhysioNet in about 14 seconds.
  • pytest: all offline and network tests pass, also with --resolution lowest-direct. Ruff and the docs build pass.

@tompollard 🟥 red 🦥 sloth · aspen

read_edf, rdedfann and read_wav opened record_name with open(), so they
could not read a physionet:// (or s3://, memory://) URI, although every
other function that takes pn_dir can. Open the file with a new helper,
_open_input_file, which reads a URI into memory, and read the samples with
np.frombuffer and the file size with seek, instead of np.fromfile and
os.path.getsize, which need a local file. Name output files after the last
part of a URI on every platform. Local files and pn_dir are read as before.

Part of #534.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 185e919. I found one issue in src/wfdb/io/_coreio.py at lines 84-88:

A header-only URI read fetches the entire EDF file. _open_input_file calls f.read() without a size before read_edf can inspect the header and return for header_only=True. I reproduced an unrestricted read of a 31.8 MB input with read_edf("memory://n16.edf", header_only=True). This makes header inspection expensive for large remote records. Full EDF and WAV reads also hold a complete in-memory copy before parsing. Please keep the URI file open for incremental reads and buffer only where needed.

The 3 focused URI tests and 38 converter tests pass. CI has no failures so far, with most matrix jobs still running. I would address the eager read before merging.

_open_input_file read the whole file at a URI into memory before
read_edf could look at the header, so read_edf(uri, header_only=True)
fetched the whole file. Return the open fsspec file instead, which reads
as needed (with range requests for physionet://) and can be sought and
sized without fetching the file. Close the file when read_edf returns
only the header, which it did not do for local files either.
@tompollard

Copy link
Copy Markdown
Member Author

Thanks, fixed in b02b38e.

  • Reading as needed: _open_input_file now returns the open fsspec file instead of reading it into a BytesIO. For physionet:// that's the package's range-request file, so read_edf reads the header with small requests, and returns with header_only=True before anything else is fetched. Seeking to the end (which read_wav uses for the file size) uses the known size, without fetching the file.
  • Memory: a full read now holds the signal bytes once, as np.fromfile did for local files, since np.frombuffer doesn't copy. The previous version also held the BytesIO copy.
  • Closing the file: read_edf now closes it when returning only the header. It didn't do that for local files either.

Tests: a new test, for both physionet:// and memory://, counts the bytes read from the underlying filesystem during read_edf(uri, header_only=True). It's now exactly the 1,536-byte header of n16.edf; on the previous commit it was the whole file (30,801,536 bytes). Against PhysioNet, read_edf("physionet://chbmit/1.0.0/chb01/chb01_02.edf", header_only=True) takes 0.5 s, and the full read still works.

All offline and network tests, ruff and the docs build pass.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed commit b02b38e. The eager-read finding from my earlier review is resolved: URI files are now read as needed, and the new header-only tests confirm that read_edf consumes only the EDF header. All 5 focused URI tests and 38 converter tests pass. I found no remaining blocker. Docs and style checks pass; the test matrix is still running.

@tompollard
tompollard merged commit d7087ea into main Nov 24, 2026
18 checks passed
@tompollard
tompollard deleted the physionet-uris-everywhere branch November 24, 2026 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant