Skip to content

Add record-specific download and listing names - #794

Merged
tompollard merged 7 commits into
mainfrom
api/download-records
Nov 30, 2026
Merged

tompollard merged 7 commits into
mainfrom
api/download-records

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Add wfdb.download_records and wfdb.list_records as readable names for WFDB-specific operations. download_records selects the headers, signal files, segments, and optional annotations needed for records in a PhysioNet database, then uses the existing PhysioNet-backed transfer path. list_records reads the database's RECORDS file. The existing dl_database and get_record_list functions keep working without warnings.

I chose download_records rather than the proposed download_database name in #672 because this function requires a RECORDS file and selects WFDB files. For a whole PhysioNet dataset, including CSVs, PDFs, and other files, physionet.download is the clearer API. I left generic download_files and list_databases names out of this PR for the same reason. This keeps the package dependency direction as wfdb → physionet.

Both new functions take a database slug with an optional subfolder and a separate keyword-only version argument. Omitting version uses the latest database version. list_records expands nested RECORDS folders and returns concrete record paths; sibling folders are read concurrently in a bounded pool. download_records accepts one record name, a sequence of names, or a folder name ending in /. Its default records="all" expands nested folders, and an empty sequence downloads nothing. It returns the selected saved file paths. Annotations are opt in: annotators=None skips them; annotators="all" includes every type. It is quiet by default, with verbose=True available for progress messages. The legacy dl_database keeps its existing defaults and return value.

Checks

  • Offline suite: 1,176 passed, 4 skipped, 22 deselected; a further focused test of sibling-folder reads passes
  • Ruff check and format: passed
  • Sphinx HTML build with warnings as errors: passed

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

Thanks. Choosing download_records over download_database and pointing whole-project downloads to physionet.download both make sense to me. Since these are meant to be the long-term names, there are a couple of behaviours I'd fix before they're public. I tested them against the demo_database fixture.

The two functions read database differently.

wfdb.download_records("demo/sub", out)     # sub is a subfolder; latest version inserted
wfdb.list_records("demo/sub")              # treated as a versioned path: requests demo/sub/...
wfdb.list_records("demo/1.0/sub")          # works
wfdb.download_records("demo/1.0/sub", out) # builds demo/1.0/1.0/sub

So the same string can't be passed to both, and download_records can't download a specific version. Could both take database as a slug with an optional subfolder, plus a keyword-only version=None (None meaning latest)? Then list_records("mitdb", version="1.0.0") and download_records("mitdb", out, version="1.0.0") would work the same way.

records="100" is read one character at a time. The hint list[str] | str suggests a single name works, but dl_database loops over anything other than "all", so it looks for records 1, 0 and 0 (in the fixture it asked for 1.hea). Wrapping a single string in a list, or accepting only a list (with None meaning all) and raising TypeError otherwise, would avoid this.

Smaller points:

  • download_records keeps dl_database's two progress lines per record, which is a lot of output for a large database. A new name is a good point to make it quiet by default, or to add verbose.
  • For nested databases, RECORDS lists folders such as 30/, which dl_database expands but list_records returns as they are. Either expand them, or say in the docstring that folder entries can appear.
  • Tests that pass the same database string to both functions, and one with records="100", would cover the first two points.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, these are good catches. I pushed dc17f27:

  • list_records and download_records now use the same database format: a slug with an optional subfolder. Both take version= separately and use the latest version when it is omitted. I tested the same "demo/sub" string with both functions, with and without an explicit version.
  • download_records(records="100") now treats "100" as one record; a list selects several records, and None selects everything in RECORDS. I also handle a single annotator string in the same way.
  • download_records is quiet by default and accepts verbose=True. Legacy dl_database keeps its existing progress output by default.
  • list_records deliberately returns nested folder entries such as "30/" as they appear in RECORDS; the docstring and guide now say this. download_records expands those entries when selecting files.

The full offline suite passes (1,171 tests), as do Ruff and the warnings-as-errors docs build.

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

Thanks, dc17f27 covers everything. list_records and download_records now read "demo/sub" the same way, with or without version=. records="100" is a single record, and anything that isn't a string, a list or None raises TypeError. download_records is quiet by default, and folder entries in RECORDS are documented. I also checked that records=[] doesn't fall through to downloading the whole project, since physionet.download returns early when the selection is empty. The offline suite, Ruff and the strict docs build pass locally.

A few optional points, since these are the long-term names:

  • records="all" now looks for a record named all. People coming from dl_database are likely to try it, so treating it like None or raising a clear error would help.
  • records=("100",) raises TypeError. Accepting any sequence of strings (other than a single string) would be friendlier.
  • records=None means all records, while annotators=None means no annotations. It's documented, but None meaning opposite things for neighbouring options may trip people up. One alternative is annotators=None for all and annotators=[] for none.

Ready to merge.

@tompollard

Copy link
Copy Markdown
Member Author

I took another look at the new API's defaults in b54773b. records="all" still selects every record, but annotations are now opt in: annotators=None (the default) downloads no annotations, while annotators="all" includes them. Passing records=None is an error. An empty record sequence is a no-op.

I also changed list_records to expand nested RECORDS folders into concrete record paths, and download_records uses that list for its default selection. That way the names describe what callers get, including nested databases. The legacy functions keep their existing defaults.

The offline suite passes (1,175 tests), as do Ruff and the strict docs build.

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

Thanks, the changes in b54773b look good. None no longer means opposite things, any sequence of names works, and the empty-selection no-op is tested. Since these names should get the interface right rather than carry over dl_database's behaviour, there are a few more things I'd change before release:

  • Return what was downloaded. download_records still returns None, as dl_database does, so after records="all" the caller can't tell what was saved or where. Returning the list of saved file paths would make the result directly usable, for example to pass into read_record. (physionet.download already returns its destination.)
  • Treat folder names in records the same way as the default. records="all" now uses list_records, which expands nested folders at every level. An explicit folder such as records=["sub/"] still goes through dl_database's one-level expansion, so records=["30/"] on a database like MIMIC-III Waveform leaves the next level of folders unexpanded. Could folder names in records be expanded with the same code, or rejected with a pointer to list_records?
  • Nested listings are slow. MIMIC-III Waveform's top-level RECORDS lists 67,830 folders, each with its own RECORDS, so list_records("mimic3wdb") (and download_records("mimic3wdb") by default) now makes about 67,830 requests one after another, where list_records previously made one. Fetching folders in parallel with a small thread pool, as the downloads do, would help. At minimum, the docstring should say that listing a nested database can take a long time.

The offline suite, Ruff and the strict docs build pass locally.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks, these were worth fixing before the new API is released. I pushed 8fbd0e2:

  • download_records now returns absolute Path objects for the selected files, including files that were already present and checked. The legacy dl_database still returns None by default.
  • Explicit folder selections such as records=["sub/"] use the same recursive listing as records="all", so they work beyond one level. The tests cover both layouts and the returned paths with and without subdirectories.
  • list_records fetches sibling RECORDS files using a pool of eight threads, in bounded batches, while returning names in the original listing order. I added a test with eight sibling folders. The docs also warn that a deeply nested database still requires many requests.

The full offline suite passes (1,176 tests), as do Ruff and the strict docs build. The additional sibling-folder test passes in the focused suite (58 tests).

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

Thanks, 8fbd0e2 covers all three points. download_records returns the selected paths (with no duplicates when a folder and a record inside it are both selected), folder names in records use the same full expansion as "all", and nested listings now run eight requests at a time in their original order. I also tested a MIMIC-III Waveform-style layout, with top-level entries like 30/000/ each containing a RECORDS file: the listing completed without a deadlock and returned the records in order. The offline suite, Ruff and the strict docs build pass locally.

One optional tidy-up: _return_files adds a private option to dl_database's public signature. Moving the file selection into an internal helper used by both functions would avoid that, but it's fine as a follow-up.

Ready to merge once CI is green.

dl_database no longer needs a private _return_files option. Both
dl_database and download_records now call _download_database, which
returns the selected files.
@tompollard
tompollard merged commit fa4768e into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the api/download-records branch November 30, 2026 17:38
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