MIT-LCP / wfdb-python

Native Python WFDB package
MIT License
730 stars 298 forks source link

Streamline file reading to use io streams #498

Open Ivorforce opened 2 weeks ago

Ivorforce commented 2 weeks ago

I've recently needed to read files from buffers rather than files (streamed from file selection pickers). I've opened up a Pull Request with a quick fix allowing a buffer override: #491

However, a simple buffer override is suboptimal. Ideally, there are as few diverging paths in the code as possible. Additionally, it makes sense to separate concerns of parsing and I/O, because being agnostic to the buffer source enables flexibility and decreases complexity on both sides. Hence, merging this PR is much preferred to #491.

This Pull Request implements my recommendation for a restructure of rdrecord, rdheader and rdann. Both rdheader and rdann were easy to modify, but rdrecord's ability to read multi-segment records does not easily translate to reads using streams. Instead, I decided to only streamline _signal._rd_segment to use buffers. Someone needing to read data from buffers must therefore implement surrounding functionality, such as pre-reading the header or acting on multi-segment records, themselves. Still, streamlining the functions addressed in this PR already facilitate accomplishing this goal at all, without needing to re-write all of the parsing functionality of wfdb.

Before Merging

This pull request needs:

Concerns and observations