Repository navigation
Read EDF and WAV files from physionet:// and other URIs - #674
Conversation
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks, fixed in b02b38e.
Tests: a new test, for both All offline and network tests, ruff and the docs build pass. |
tompollard
left a comment
There was a problem hiding this comment.
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.
## Summary Bump the version from 5.1.0 to 5.2.0, and collect the 19 entries in `changelog.d/` into `docs/changes.rst` with `towncrier build --date "Nov 2026"`, with a short summary at the top of the new section. This is a minor release. The one change that existing code may notice is that `rdann` and `Annotation.apply_range` now stop before `sampto`, as `rdrecord` and `rdsamp` do, so an annotation exactly at `sampto` is no longer included (#667). The summary calls it out, with how to get the old result. The release also includes: - **Fixes for PhysioNet:** `get_dbs`, which PhysioNet's API change had broken (#665), record names with folders in `pn_dir` (#666), and `physionet://` and other URIs in `read_edf`, `rdedfann` and `read_wav` (#674). - **Robustness:** - headers whose base time or date is in another form (#668); - multi-segment writes that leave no partial record after an error (#673); - a warning before `wrsamp` replaces another record's signal files (#675). - **New:** a beat-by-beat comparison of annotation files, as WFDB `bxb` does, as a development feature (#679), and axis labels in `plot_wfdb` (#670). - **Smaller fixes:** - integer overflow and precision in `find_peaks` and `normalize_bound`; - constant signals in `normalize_bound`; - multi-character custom annotation symbols; - format 61 signals read with `smooth_frames=False`; - skew written twice when rewriting records; - docstring examples, and more. This will be the first release published by `.github/workflows/publish.yml`, from the GitHub release, with provenance. ## Checks - `pytest -m "not network"`: 679 passed (4 skipped, the `bxb` comparison that needs WFDB installed), also with `--python 3.11 --resolution lowest-direct`. `pytest -m network`: 20 passed. - Ruff and the Sphinx HTML build with warnings as errors pass. - The wheel and source distribution build and pass `twine check`. They have `Version: 5.2.0` and include `wfdb/processing/beat_comparison.py`. - PyPI has no 5.2.0 release yet. <sub>@tompollard 🟥 red 🦥 sloth · aspen</sub>
Part of #534: the first step towards phasing out
pn_dir, which is that every function that takespn_diralso takes aphysionet://URI.Of the 15 public functions that take
pn_dir, all but three already accepted aphysionet://URI asrecord_name, because they read throughrdrecord,rdheaderorrdann. The other three,read_edf,rdedfannandread_wav, opened the path withopen(), soread_edf("physionet://chbmit/1.0.0/chb01/chb01_01.edf")failed withFileNotFoundError. They also couldn't reads3://ormemory://paths._coreio._open_input_file, opens a local file as before, or reads a URI into memory (throughfsspec, sophysionet://uses the physionet package, with its cache and sign-in).read_edfandread_wavread the samples withnp.frombufferand the file size withseek, instead ofnp.fromfileandos.path.getsize, which need a local file.read_wavnow also closes its file.record_nameof the returned record, and the files written bywfdb_to_edfandwfdb_to_wav) use the last part of a URI on every platform, instead of splitting onos.sep.URIs are read without saving a copy, unlike
pn_dir, which downloads the file into the current directory (andread_edfleaves it there). Thatpn_dirbehaviour is unchanged here.Testing
n16.edf,SC4001E0-PSG.wavandtest_edfann.edfwithphysionet://(from the cache) andmemory://URIs, and compare them with the local files. They also check that nothing is written to the current directory. These fail onmain. Another test checks thatwfdb_to_matandsigavgreadphysionet://URIs, as they already did.main(signals, names, initial values and checksums) forn16.edf,wave_4.edf,SC4001E0-PSG.wavand therdedfannoutput oftest_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