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.
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