Repository navigation
Add write_record for single-segment records - #785
Conversation
tompollard
left a comment
There was a problem hiding this comment.
Round trips from records read from files work well. Two things should be fixed before merge, plus a duplication worth removing.
Checked locally:
-
The caller's record is unchanged. I compared every attribute, including the arrays, before and after writing these records:
100, read as physical and as digital;mixedsignals, expanded;a103l;041s, flattened from multiple segments;- a record with an edited physical value.
Nothing changed, and
check_recordreports no issues for any output. -
Skew is handled correctly. Read with skew applied, the output header has no skew; read with
ignore_skew=True, it keepsskew=3. The values match either way. -
Multiple signal forms. A record with both
p_signalandd_signalraises "Multiple signal forms are loaded", andkind="digital"then writes it. -
Destinations. Archives and
memory://work.x.hea,my rec,dir/andphysionet://are rejected. -
The offline suite and ruff pass.
Should fix: a record built in code can't be written.
record = wfdb.Record(record_name="x", fs=100, n_sig=1, sig_len=3, sig_name=["I"],
units=["mV"], p_signal=np.array([[0.0], [0.1], [0.2]]))
wfdb.write_record("out/x", record) # KeyError: None, in _digi_nanAdding fmt, adc_gain and baseline only gets as far as "Missing field required: block_size".
Record.wrsampfails the same way, so this isn't new.- The
wrsamp()function handles this case withset_d_features(do_adc=1)andset_defaults(), and #672 listswrite_recordas its replacement.
When only physical values are loaded and the format or gain fields are missing, filling them in the same way would make write_record cover that workflow. If that's out of scope for now, a clear error naming the missing fields would be much better than the KeyError.
Should fix: the byte offset is carried over. Writing a103l produces a103l.dat 16+24 and prints "Writing file … with 24 empty leading bytes". #776 cleared byte_offset in SignalRead.write for this reason, so the two writers currently give different files for the same data.
Duplication: the renaming code here repeats _destination_file_names in signal_values.py almost line for line. The byte-offset difference shows how two copies drift apart.
- Suggestion: move one helper into
record.pythat copies the record, renames its signal files and clears the byte offset. write_recordcould use that helper, andSignalRead.writecould simply callwrite_record(path, self.to_legacy_record()).
Tests: cases for a record built in code and for a source with a byte offset (a103l) would cover both fixes.
|
Thanks for checking these cases. I pushed 1d6c76a to address them.
Both I added regression tests for the built records, missing digital calibration, and |
tompollard
left a comment
There was a problem hiding this comment.
Thanks, this looks ready to merge.
-
Records built in code. They now write and read back:
- physical with no calibration:
80 1270.0(-127)/mV, the same aswrsamp(); - physical with
fmt,adc_gainandbaseline; - digital with calibration.
check_recordreports no issues, and the caller's record is unchanged. Digital values without calibration, andadc_gainwithoutfmt, give clear errors. - physical with no calibration:
-
Calibration for records read from files is unchanged. I compared output headers from the previous and current heads for:
100, physical and digital;a103l;mixedsignals, expanded;03700181;s0010_re;041s, flattened.
Formats, gains and baselines are identical, and values round-trip exactly, so the new
set_d_featurescall keeps existing calibration. -
Byte offsets are cleared, and
SignalRead.writenow uses the same helper. -
The offline suite and ruff pass.
Two older failures in the calibration step, which the legacy wrsamp() function shares, are now tracked in #786: physical signals that contain NaN, and expanded physical signals without calibration.
Summary
Add
write_record(path, record, *, kind=None, layout=None)as the path-first counterpart toread_record. This first version accepts a single-segmentRecordand writes its current in-memory view through the existingRecord.wrsampimplementation. A selected or flattened read is written as one record; the function does not claim to reconstruct the source files or their segment layout.MultiRecordis explicitly rejected for now, whileMultiRecord.wrsampremains available.The function accepts a local record stem, supported cloud URI, or local
.wfdbarchive path. It renames signal file groups under the destination stem, clears source byte offsets, and writes from a metadata copy so the caller's record is unchanged. If more than one signal array is loaded,kindand/orlayoutmust select one; no representation is chosen silently. Physical values may be quantized by the existing writer, while digital samples are written directly. For records built in code, it calculates missing physical calibration and header defaults. Digital records requirefmt,adc_gain, andbaseline.The legacy
wrsampAPIs are unchanged. This is a focused first step toward #500 and the naming plan in #672.Verification
memory://paths; ambiguous signal arrays; unsupported inputs; and preservation of the caller's metadata and values.