Skip to content

Modern API changes to consider before the new names settle #852

Backlog

Description

@tompollard

The modern API (read_record, read_signals, write_record, read_annotations, download_records, and related names) is still new. Changes that would be breaking later are cheap now. This issue collects the changes worth considering before the new names settle, roughly from largest to smallest.

1. Choose one modern data model

The modern API currently has two result types for the same data:

  • read_record returns a mutable Record.
  • read_signals returns a frozen SignalRead, although its arrays are still mutable.

To write a Record correctly after edits, the writer has to infer what happened since the read from private state: _applied_skew, _read_skew, _incomplete_skew_tail and _read_source. #843, #844, #846, #849 and #850 were all spent keeping that state accurate.

If the modern path used immutable values plus metadata, alignment and other read facts would be part of the data instead of being inferred from it. Record could remain the legacy compatibility type. This is the question #744 and #749 are working towards, so it should be decided before more snapshot work.

2. A modern way to write new data from arrays

The most common write is "I have a NumPy array; save it as a WFDB record." Today that needs either the module-level wrsamp, or wfdb.Record(...), which has 39 constructor parameters. docs/migration.rst notes that wrsamp doesn't emit a deprecation warning because no replacement covers this case, so the legacy writers can't be removed until one exists.

For example, something like:

wfdb.write_record(path, values, fs=360, names=["MLII", "V5"], units=["mV", "mV"])

Storage format, gain and baseline would be chosen automatically unless given.

3. Remove legacy parameters from new functions

  • pn_dir already emits a FutureWarning, and physionet:// URIs work. New functions shouldn't launch with a deprecated parameter. Remove it from the modern functions outright.
  • download_records(records="all") uses a string placeholder for "all records", the pattern we chose not to carry over from dl_database. Use None for all records, and drop verbose printing in favour of the returned list.
  • Channel selection uses four parameters: channels, channel_names, force_channels and missing_channels. channels could accept indices or names, with missing_channels as the policy for absent names, and force_channels removed.

4. Use the same words for reading and writing

  • read_record(ignore_skew=True) and write_record(signal_alignment="raw") describe the same concept with different words. Using signal_alignment="aligned" | "raw" on both sides would be clearer, especially if Define skew provenance when signal values or skew are edited before writing #843 makes the choice required when writing skewed records.
  • read_signals requires layout, while read_record defaults it to "uniform". These should be consistent.

5. One return type per function

read_record(combine_segments=False) returns a MultiRecord; otherwise it returns a Record. Segment-level access already has its own types (SegmentView, read_multi_segment_headers), so read_record could always return a single type.

6. Represent invalid samples clearly

Physical reads represent invalid samples as NaN. Digital reads use a placeholder value that depends on the storage format, such as -2048 for format 212, so callers need to know format details to find them. A per-channel invalid_value in the metadata, or a mask, would make this explicit.

7. Trim the public surface of Record

  • Write internals are public methods: check_field, check_sig_cohesion, wr_dats, wr_dat_files, wr_header_file, get_write_fields, get_write_subset, set_default, preprocess_header and others.
  • Several fields have two names, such as n_sig and n_signals, or fmt and formats.

The docs could list a small supported set now, with the rest given leading underscores in the next major release.

8. Say in the docs that the new API is provisional

A sentence in docs/migration.rst would let us make the changes above without surprising users, and tells them when the API will settle. For example: "The new names may change until 6.0, when they become stable and the legacy names are removed."

Suggested order

Activity

  1. tompollard commented on Dec 2, 2026

    @tompollard
    MemberAuthor

    Two corrections to section 3:

    • Keep download_records(records="all"). Add record-specific download and listing names #794 chose it deliberately: annotators=None means no annotations, while an empty record sequence means no records. Changing records=None to mean all would make those choices less consistent.
    • Keep force_channels and missing_channels separate. force_channels controls whether a requested channel absent from selected segments of a variable-layout record remains in the result. missing_channels controls names absent from the record's header altogether. They answer different questions.

    For the proposed write_signals, we should settle invalid-sample semantics before shipping it. Each digital storage format reserves its minimum value as a missing-sample marker. A value can fit a format's integer range while colliding with that marker, so range checking alone is insufficient.

  2. tompollard commented on Dec 2, 2026

    @tompollard
    MemberAuthor

    Proposed missing-sample contract to settle before implementing write_signals:

    • Physical input: NaN means missing; reject infinity. Keep the existing format inference by default and allow an explicit format. Encoding a finite value must never produce the format's reserved missing marker.
    • Bare digital input: require an explicit missing policy, either missing="none" (all values are valid) or a boolean mask. Do not infer missingness from a magic number: a format 212 read uses -2048, while format 16 uses -32768. The modern read result should carry or expose a mask established while decoding, especially when a flattened record contains segments with different formats.
    • For digital input, default to format 16 only when every unmasked value is in its valid range. Otherwise require an explicit writable format and name the formats that can hold the valid values. Validate the requested format and mask before creating files.

    For the currently writable formats, the reserved marker and valid range are:

    Format Missing marker Valid digital values
    80, 508 -128 -127 to 127
    212 -2048 -2047 to 2047
    16, 516 -32768 -32767 to 32767
    24, 524 -8388608 -8388607 to 8388607
    32 -2147483648 -2147483647 to 2147483647

    Tests should cover a digital read with missing samples written through write_signals with its mask, the same case across flattened segments with different formats, and rejection of a valid value that collides with an explicitly requested format's marker before files are created. Also test that physical NaN becomes missing and finite physical values never encode as the marker.

    I would settle this contract, then prototype one internal model for modern readers and writers, then implement write_signals on that model. Mutable Record remains a compatibility view with one conversion at its write boundary and no second mutable metadata copy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions