From a3600e42d33dd69577989ac1523d4a7f7f9d9fc9 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Wed, 9 Oct 2024 15:54:27 -0400 Subject: [PATCH 1/2] cast type to unsigned int Addresses warning when NPY_PROMOTION_STATE=weak_and_warn that result dtype changed due to the removal of value-based promotion from NumPy. Changed from int32 to int16. --- wfdb/io/_signal.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/wfdb/io/_signal.py b/wfdb/io/_signal.py index 68ca57e4..99b615e8 100644 --- a/wfdb/io/_signal.py +++ b/wfdb/io/_signal.py @@ -2371,7 +2371,8 @@ def wr_dat_file( elif fmt == "16": # convert to 16 bit two's complement - d_signal[d_signal < 0] = d_signal[d_signal < 0] + 65536 + d_signal = d_signal.astype(np.uint16) + # Split samples into separate bytes using binary masks b1 = d_signal & [255] * tsamps_per_frame b2 = (d_signal & [65280] * tsamps_per_frame) >> 8 @@ -2400,7 +2401,8 @@ def wr_dat_file( elif fmt == "32": # convert to 32 bit two's complement - d_signal[d_signal < 0] = d_signal[d_signal < 0] + 4294967296 + d_signal = d_signal.astype(np.uint32) + # Split samples into separate bytes using binary masks b1 = d_signal & [255] * tsamps_per_frame b2 = (d_signal & [65280] * tsamps_per_frame) >> 8 From 80f822e9f28ce7a350af8ef9d1c046fede1160f9 Mon Sep 17 00:00:00 2001 From: Tom Pollard Date: Wed, 9 Oct 2024 16:34:19 -0400 Subject: [PATCH 2/2] Switch to in-place addition for efficiency Modify the arrays in place. --- wfdb/io/_signal.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/wfdb/io/_signal.py b/wfdb/io/_signal.py index 99b615e8..e3df065e 100644 --- a/wfdb/io/_signal.py +++ b/wfdb/io/_signal.py @@ -2319,11 +2319,10 @@ def wr_dat_file( if fmt == "80": # convert to 8 bit offset binary form - d_signal = d_signal + 128 - # Concatenate into 1D - d_signal = d_signal.reshape(-1) - # Convert to un_signed 8 bit dtype to write - b_write = d_signal.astype("uint8") + d_signal += 128 + + # Convert to unsigned 8 bit dtype to write (and flatten if necessary) + b_write = d_signal.astype("uint8").reshape(-1) elif fmt == "212": # Each sample is represented by a 12 bit two's complement @@ -2336,7 +2335,7 @@ def wr_dat_file( # repeated for each successive pair of samples. # convert to 12 bit two's complement - d_signal[d_signal < 0] = d_signal[d_signal < 0] + 4096 + d_signal[d_signal < 0] += 4096 # Concatenate into 1D d_signal = d_signal.reshape(-1) @@ -2384,8 +2383,8 @@ def wr_dat_file( # Convert to un_signed 8 bit dtype to write b_write = b_write.astype("uint8") elif fmt == "24": - # convert to 24 bit two's complement - d_signal[d_signal < 0] = d_signal[d_signal < 0] + 16777216 + # convert to 32 bit two's complement (as int24 not an option) + d_signal = d_signal.astype(np.uint32) # Split samples into separate bytes using binary masks b1 = d_signal & [255] * tsamps_per_frame b2 = (d_signal & [65280] * tsamps_per_frame) >> 8