wfdb-python icon indicating copy to clipboard operation
wfdb-python copied to clipboard

wrsamp: IndexError: Channel 0 contains values outside the allowed range [-32768, 32767] for fmt 16

Open nickfong opened this issue 1 year ago • 6 comments

We're encountering an issue when trying to write data to WFDB files using the wrsamp function. Specifically, we’re seeing an error that prevents us from writing the files correctly. When attempting to call wrsamp to save our data, we receive the following error message:

IndexError: Channel 0 contains values outside the allowed range [-32768, 32767] for fmt 16.

To investigate the issue further, we modified the WFDB source code to provide additional debugging information. The csv file we’re attempting to convert to wfdb has values ranging between:

Minimum: -90.113269
Maximum: 622.269327

But this is being scaled to -32767 and 32772 after calling wrsamp, so the max value allowed in fmt 16 is being exceeded.

We are using package versions:

Please see a screenshot below depicting the error we get using:
wfdb==4.1.2
pandas==2.2.3
numpy==2.1.2
image001

We have a minimal reproducible sample that we sent to Ben Moody and Tom Pollard.

nickfong avatar Dec 17 '24 22:12 nickfong

@nickfong are you able to share example data to reproduce the error? (and if possible the code from the example above, as text rather than image).

tompollard avatar Dec 18 '24 22:12 tompollard

Hey @tompollard, Tyler Law shared the reproducible code sample and data with you on 2024-12-04; it's the same thread that you responded to yesterday suggesting we open a GH issue 😃

Let me know if you want me to send to you again via email. We'd like to avoid posting the data here publicly.

nickfong avatar Dec 18 '24 22:12 nickfong

@nickfong the only attachment to the email is the screenshot that you've shared above, but I do now notice a message saying:

UCSF IT’s email security system has detected and removed a potentially harmful attachment from this email.

Please could you share here instead?  

tompollard avatar Dec 18 '24 22:12 tompollard

@nickfong , thanks for privately sharing the CSV of data that allows for reproducing this problem. I haven't looked into it in detail but when I ran your code + data in a new environment I was able to reproduce the error. When I went to my wfdb-python environment I could not reproduce the error with the same code + data. We don't currently support numpy versions > 2 . This seemed like the obvious difference in setups, so I confirmed that with numpy==1.26.1 in both environments that the error does not occur. I haven't dug much further but this may be related to: https://github.com/MIT-LCP/wfdb-python/issues/493 . We are actively working on support for numpy > 2 : https://github.com/MIT-LCP/wfdb-python/pull/511 .

briangow avatar Dec 19 '24 19:12 briangow

Please could you update to the latest version of WFDB (v4.2.0) to see if this issue is addressed?

The changelog is at: https://github.com/MIT-LCP/wfdb-python/blob/main/docs/changes.rst

tompollard avatar Jan 21 '25 22:01 tompollard

Hi @tompollard, unfortunately it seems that the issue is still persisting on 4.2.0. See error below and screenshot showing that we're using WFDB 4.2.0

Same error as before:

File ~/openox/venv/lib/python3.12/site-packages/wfdb/io/_signal.py:264, in SignalMixin.check_sig_cohesion(self, write_fields, expanded)
    262     chmax = max(self.d_signal[:, ch])
    263     if (chmin < dmin) or (chmax > dmax):
--> 264         raise IndexError(
    265             "Channel "
    266             + str(ch)
    267             + " contain values outside allowed range ["
    268             + str(dmin)
    269             + ", "
    270             + str(dmax)
    271             + "] for fmt "
    272             + str(fmt)
    273         )
    275 # Ensure that the checksums and initial value fields match the digital signal (if the fields are present)
    276 if self.n_sig > 0:

IndexError: Channel 0 contain values outside allowed range [-32768, 32767] for fmt 16
Image

nickfong avatar Jan 25 '25 01:01 nickfong