Skip to content

Commit

Permalink
Change in type promotion introduced in Numpy 2.0. Fixes to edf.py. (#527
Browse files Browse the repository at this point in the history
)

As discussed in #493, numpy
2.0 introduced changes to type promotion. The change is outlined at:
https://numpy.org/devdocs/numpy_2_0_migration_guide.html#changes-to-numpy-data-type-promotion

Running the tests in `weak_and_warn` raises the following warnings:

```
tests/io/test_convert.py::TestEdfToWfdb::test_edf_uniform
  /Users/tompollard/projects/wfdb-python/wfdb/io/convert/edf.py:409: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int16 to int64.
    temp_all_sigs[i].flatten() - baseline[i]

tests/io/test_convert.py::TestEdfToWfdb::test_edf_non_uniform
  /Users/tompollard/projects/wfdb-python/wfdb/io/convert/edf.py:420: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int16 to int64.
    (temp_sig_data[start_ind:stop_ind] - baseline[i])

tests/io/test_convert.py::TestEdfToWfdb::test_edf_non_uniform
  /Users/tompollard/projects/wfdb-python/wfdb/io/convert/edf.py:414: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int16 to int64.
    sig_data[:, i] = (temp_sig_data - baseline[i]) / adc_gain_all[i]
```

This pull request addresses the issue by setting `temp_all_sigs` and
`temp_sig_data` to `np.int64`.

For reference, `temp_all_sigs` and `temp_sig_data` are initially set as
`np.int64`:


https://github.com/MIT-LCP/wfdb-python/blob/c6d4fd9f05007ebc5f79dd07332f34410d7b8493/wfdb/io/convert/edf.py#L402-L404

`baseline` is set to `int64` here:


https://github.com/MIT-LCP/wfdb-python/blob/c6d4fd9f05007ebc5f79dd07332f34410d7b8493/wfdb/io/convert/edf.py#L353-L355
  • Loading branch information
tompollard authored Jan 17, 2025
2 parents c6d4fd9 + 262f494 commit e5c6fe5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions wfdb/io/convert/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,27 @@ def read_edf(
temp_sig_data = np.fromfile(edf_file, dtype=np.int16)
temp_sig_data = temp_sig_data.reshape((-1, sum(samps_per_block)))
temp_all_sigs = np.hsplit(temp_sig_data, np.cumsum(samps_per_block)[:-1])

for i in range(n_sig):
# Check if `samps_per_frame` has all equal values
if samps_per_frame.count(samps_per_frame[0]) == len(samps_per_frame):
sig_data[:, i] = (
temp_all_sigs[i].flatten() - baseline[i]
temp_all_sigs[i].flatten().astype(np.int64) - baseline[i]
) / adc_gain_all[i]
else:
temp_sig_data = temp_all_sigs[i].flatten()

if samps_per_frame[i] == 1:
sig_data[:, i] = (temp_sig_data - baseline[i]) / adc_gain_all[i]
sig_data[:, i] = (
temp_sig_data.astype(np.int64) - baseline[i]
) / adc_gain_all[i]
else:
for j in range(sig_len):
start_ind = j * samps_per_frame[i]
stop_ind = start_ind + samps_per_frame[i]
sig_data[j, i] = np.mean(
(temp_sig_data[start_ind:stop_ind] - baseline[i])
/ adc_gain_all[i]
temp_sig_data[start_ind:stop_ind].astype(np.int64)
- baseline[i] / adc_gain_all[i]
)

# This is the closest I can get to the original implementation
Expand Down

0 comments on commit e5c6fe5

Please sign in to comment.