Skip to content

Commit c687060

Browse files
authored
Change in type promotion. Fixes to annotation.py (#506)
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 warnings for wfdb/io/annotation.py: ``` /Users/tompollard/projects/wfdb-python/wfdb/io/annotation.py:2222: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int64 to uint8. while filebytes[bpi, 1] >> 2 == 59: /Users/tompollard/projects/wfdb-python/wfdb/io/annotation.py:2239: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int64 to uint8. label_store = filebytes[bpi, 1] >> 2 tests/test_plot.py::TestPlotInternal::test_get_plot_dims /Users/tompollard/projects/wfdb-python/wfdb/io/annotation.py:2240: UserWarning: result dtype changed due to the removal of value-based promotion from NumPy. Changed from int64 to uint8. sample_diff += int(filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3)) ``` The changes in this pull request address these issues by explicitly casting the type. I plan to follow up with several additional fixes to other modules.
2 parents d3439da + 6e99aa1 commit c687060

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

wfdb/io/annotation.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ def proc_ann_bytes(filebytes, sampto):
21622162
update = {"subtype": True, "chan": True, "num": True, "aux_note": True}
21632163
# Get the next label store value - it may indicate additional
21642164
# fields for this annotation, or the values of the next annotation.
2165-
current_label_store = filebytes[bpi, 1] >> 2
2165+
current_label_store = int(filebytes[bpi, 1]) >> 2
21662166

21672167
while current_label_store > 59:
21682168
subtype, chan, num, aux_note, update, bpi = proc_extra_field(
@@ -2176,7 +2176,7 @@ def proc_ann_bytes(filebytes, sampto):
21762176
update,
21772177
)
21782178

2179-
current_label_store = filebytes[bpi, 1] >> 2
2179+
current_label_store = int(filebytes[bpi, 1]) >> 2
21802180

21812181
# Set defaults or carry over previous values if necessary
21822182
subtype, chan, num, aux_note = update_extra_fields(
@@ -2219,7 +2219,7 @@ def proc_core_fields(filebytes, bpi):
22192219

22202220
# The current byte pair will contain either the actual d_sample + annotation store value,
22212221
# or 0 + SKIP.
2222-
while filebytes[bpi, 1] >> 2 == 59:
2222+
while int(filebytes[bpi, 1]) >> 2 == 59:
22232223
# 4 bytes storing dt
22242224
skip_diff = (
22252225
(int(filebytes[bpi + 1, 0]) << 16)
@@ -2236,8 +2236,10 @@ def proc_core_fields(filebytes, bpi):
22362236
bpi = bpi + 3
22372237

22382238
# Not a skip - it is the actual sample number + annotation type store value
2239-
label_store = filebytes[bpi, 1] >> 2
2240-
sample_diff += int(filebytes[bpi, 0] + 256 * (filebytes[bpi, 1] & 3))
2239+
label_store = int(filebytes[bpi, 1]) >> 2
2240+
sample_diff += np.int64(filebytes[bpi, 0]) + 256 * (
2241+
np.int64(filebytes[bpi, 1]) & 3
2242+
)
22412243
bpi = bpi + 1
22422244

22432245
return sample_diff, label_store, bpi
@@ -2322,7 +2324,7 @@ def proc_extra_field(
23222324
aux_notebytes = filebytes[
23232325
bpi + 1 : bpi + 1 + int(np.ceil(aux_notelen / 2.0)), :
23242326
].flatten()
2325-
if aux_notelen & 1:
2327+
if int(aux_notelen) & 1:
23262328
aux_notebytes = aux_notebytes[:-1]
23272329
# The aux_note string
23282330
aux_note.append("".join([chr(char) for char in aux_notebytes]))

0 commit comments

Comments
 (0)