Skip to content

Commit 9c20204

Browse files
authored
Change in type promotion. Fixes to _signal.py (#507)
As discussed in #493, numpy v2.0 introduced changes to type promotion rules: https://numpy.org/devdocs/numpy_2_0_migration_guide.html#changes-to-numpy-data-type-promotion Running pytest with `numpy==2.0.2` and `NPY_PROMOTION_STATE=weak_and_warn` raises the following warning for wfdb/io/_signal.py: ``` tests/test_record.py::TestRecord::test_1a /Users/tompollard/projects/wfdb-python/wfdb/io/_signal.py:2374: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int32 to int16. d_signal[d_signal < 0] = d_signal[d_signal < 0] + 65536 ``` The changes in this pull request address these issues by explicitly casting the type. I also make a couple of minor modifications for efficiency (switching to inplace addition). I plan to follow up with several additional fixes to other modules.
2 parents 5427181 + 80f822e commit 9c20204

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

wfdb/io/_signal.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,11 +2319,10 @@ def wr_dat_file(
23192319

23202320
if fmt == "80":
23212321
# convert to 8 bit offset binary form
2322-
d_signal = d_signal + 128
2323-
# Concatenate into 1D
2324-
d_signal = d_signal.reshape(-1)
2325-
# Convert to un_signed 8 bit dtype to write
2326-
b_write = d_signal.astype("uint8")
2322+
d_signal += 128
2323+
2324+
# Convert to unsigned 8 bit dtype to write (and flatten if necessary)
2325+
b_write = d_signal.astype("uint8").reshape(-1)
23272326

23282327
elif fmt == "212":
23292328
# Each sample is represented by a 12 bit two's complement
@@ -2336,7 +2335,7 @@ def wr_dat_file(
23362335
# repeated for each successive pair of samples.
23372336

23382337
# convert to 12 bit two's complement
2339-
d_signal[d_signal < 0] = d_signal[d_signal < 0] + 4096
2338+
d_signal[d_signal < 0] += 4096
23402339

23412340
# Concatenate into 1D
23422341
d_signal = d_signal.reshape(-1)
@@ -2371,7 +2370,8 @@ def wr_dat_file(
23712370

23722371
elif fmt == "16":
23732372
# convert to 16 bit two's complement
2374-
d_signal[d_signal < 0] = d_signal[d_signal < 0] + 65536
2373+
d_signal = d_signal.astype(np.uint16)
2374+
23752375
# Split samples into separate bytes using binary masks
23762376
b1 = d_signal & [255] * tsamps_per_frame
23772377
b2 = (d_signal & [65280] * tsamps_per_frame) >> 8
@@ -2383,8 +2383,8 @@ def wr_dat_file(
23832383
# Convert to un_signed 8 bit dtype to write
23842384
b_write = b_write.astype("uint8")
23852385
elif fmt == "24":
2386-
# convert to 24 bit two's complement
2387-
d_signal[d_signal < 0] = d_signal[d_signal < 0] + 16777216
2386+
# convert to 32 bit two's complement (as int24 not an option)
2387+
d_signal = d_signal.astype(np.uint32)
23882388
# Split samples into separate bytes using binary masks
23892389
b1 = d_signal & [255] * tsamps_per_frame
23902390
b2 = (d_signal & [65280] * tsamps_per_frame) >> 8
@@ -2400,7 +2400,8 @@ def wr_dat_file(
24002400

24012401
elif fmt == "32":
24022402
# convert to 32 bit two's complement
2403-
d_signal[d_signal < 0] = d_signal[d_signal < 0] + 4294967296
2403+
d_signal = d_signal.astype(np.uint32)
2404+
24042405
# Split samples into separate bytes using binary masks
24052406
b1 = d_signal & [255] * tsamps_per_frame
24062407
b2 = (d_signal & [65280] * tsamps_per_frame) >> 8

0 commit comments

Comments
 (0)