Skip to content

Document signal precedence and fix flattened writes - #773

Merged
tompollard merged 2 commits into
mainfrom
document-legacy-signal-precedence
Nov 30, 2026
Merged

tompollard merged 2 commits into
mainfrom
document-legacy-signal-precedence

Conversation

@tompollard

@tompollard tompollard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Document how legacy Record methods choose signal arrays when physical and digital forms coexist:

  • to_dataframe() uses the first available of p_signal, d_signal, e_p_signal, and e_d_signal.
  • wrsamp(expanded=False) writes d_signal when present, otherwise converts p_signal. With expanded=True, it uses e_d_signal before e_p_signal.
  • Editing physical values does not refresh an already loaded digital array. The docs show how to refresh it with set_d_features(do_adc=True) before writing.

Fix #774 as part of this PR. When a flattened multi-segment record needs format 32 because channels sharing a file have incompatible formats, an existing digital array now remains the source of written values. It is recoded to the new format while retaining its calibration. Previously, the writer recalculated calibration from the physical array but wrote the old digital array, changing the physical values on readback without warning. A physical-only flattened record still calculates suitable new calibration.

The tests cover uniform and expanded write/readback, DataFrame precedence, differing expanded channel lengths, and the flattened-record regression in both layouts.

Closes #768 and #774.

Verification

  • Offline suite: 1,032 passed, 4 skipped, 22 network tests deselected (MPLBACKEND=Agg).
  • Ruff check and format pass.
  • Sphinx HTML build passes with warnings treated as errors.

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

The documentation matches the code for ordinary records, and the tests capture the behaviour well. While checking it, I found a related bug on main for flattened multi-segment records, now #774.

Checked:

  • to_dataframe tries p_signal, d_signal, e_p_signal and e_d_signal in that order, as documented.
  • wrsamp converts p_signal only when d_signal is None, and likewise e_p_signal when e_d_signal is None, as documented.
  • Tests: tests/test_record_signal_precedence.py (3 passed) and CI pass.

Related bug, for flattened records (#774). _prepare_flattened_write can recalculate a flattened record's calibration (switching to format 32 when channels sharing a file have different formats). wrsamp then writes the existing d_signal, computed with the old calibration, under the new gains. With 200 samples of 041s, all channels in one file and channel 0 changed to format 16, reading back gives errors of 0.384, 0.892, 0.386 and 44.175 in the first four channels, with no warning. Without d_signal, the same record reads back exactly. That needs a code fix, so it's separate from this PR. Until then, the new section could note that flattened records can be an exception to "writes d_signal when present", or this could wait until the fix lands.

Minor:

  • Expanded arrays of different lengths: "Expanded arrays must have equal lengths to form a DataFrame" is correct. It could also say that records with different samples per frame, like mixedsignals, can't make a DataFrame from their expanded arrays.
  • One more test: to_dataframe choosing d_signal over e_p_signal would cover the remaining part of the documented order.
  • Readability: test_write_uses_existing_digital_until_refreshed branches on expanded in several places. Separate uniform and expanded tests might be easier to follow.

@tompollard tompollard changed the title Document legacy signal array precedence Document signal precedence and fix flattened writes Nov 30, 2026
@tompollard

Copy link
Copy Markdown
Member Author

Thanks for finding #774. I fixed it in 95cf608 rather than leaving a caveat in the docs. For flattened records, the selected digital array now remains authoritative when incompatible signal formats have to be changed to format 32. The writer recodes its missing-value markers but keeps its calibration, so readback no longer interprets old digital values using newly calculated gains. Physical-only flattened records still calculate new calibration as before.

I added regression tests for both uniform and expanded flattened writes, plus a physical-only test. I also clarified the DataFrame limitation for channels with different samples per frame, added the missing d_signal-over-e_p_signal test, and split the earlier combined test into separate uniform and expanded cases. The full offline suite passes (1,032 passed, 4 skipped), as do Ruff and the strict docs build. I updated the PR description to cover both #768 and #774.

@tompollard
tompollard merged commit 5422883 into main Nov 30, 2026
19 checks passed
@tompollard
tompollard deleted the document-legacy-signal-precedence branch November 30, 2026 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant