Skip to content

Writing a flattened record with both physical and digital arrays can write wrong values #774

Done

Description

@tompollard

Record.wrsamp can write wrong values for a flattened multi-segment record that holds both p_signal and d_signal.

When the channels of a flattened record share a signal file but have different formats, _prepare_flattened_write switches every channel to format 32 and recalculates adc_gain and baseline from p_signal. wrsamp then writes the existing d_signal, which was computed with the old calibration, under the new gains. No error or warning is raised.

import os
import tempfile

import numpy as np
import wfdb

record = wfdb.rdrecord("sample-data/multi-segment/041s/041s", sampto=200)
record.record_name = "flat"
record.file_name = ["flat.dat"] * record.n_sig
record.fmt = ["16"] + record.fmt[1:]  # channels sharing one file, different formats
record.d_signal = record.adc()

directory = tempfile.mkdtemp()
record.wrsamp(write_dir=directory)
written = wfdb.rdrecord(os.path.join(directory, "flat"))
print(np.nanmax(np.abs(written.p_signal - record.p_signal), axis=0)[:4])
# [ 0.384  0.892  0.386 44.175]

Without the d_signal line, the record is written and read back with no error. An ordinary (not flattened) record with the same mismatched formats is rejected with Each file_name (dat file) specified must have the same fmt.

Possible fixes: when _prepare_flattened_write recalculates the calibration, regenerate d_signal (or e_d_signal) from the physical values, or raise an error when digital values are also present.

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