1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
From: Gregory Lee <grlee77@gmail.com>
Date: Tue, 29 Nov 2022 00:50:13 -0500
Subject: [PATCH] Handle new warnings introduced in NumPy 1.24 (#6637)
* ignore warning about deprecated int0 arising from SciPy<1.9.4 in one test case
This warning appears with NumPy>=1.24 where np.int0 has been deprecated
* ignore warning about deprecated np.bool8 in dtype utilities
This warning appears with NumPy>=1.24 where np.bool8 has been deprecated
* bump tolerance of float64 case in test_ciede2000_dE
uncertain why this was needed for NumPy>=1.24
* refactor relabel_sequential to avoid a new NumPy RuntimeWarning
* use explicit astype call to avoid a DeprecationWarning in Cascade
* Apply suggestions from code review
Co-authored-by: Brett M. Morris <morrisbrettm@gmail.com>
Co-authored-by: Mark Harfouche <mark.harfouche@gmail.com>
---
skimage/_shared/testing.py | 9 +++++++++
skimage/color/tests/test_delta_e.py | 3 +--
skimage/feature/_cascade.pyx | 4 +++-
skimage/feature/tests/test_cascade.py | 1 +
skimage/segmentation/_join.py | 5 ++++-
skimage/util/dtype.py | 13 +++++++++++--
skimage/util/tests/test_dtype.py | 2 +-
7 files changed, 30 insertions(+), 7 deletions(-)
diff --git a/skimage/_shared/testing.py b/skimage/_shared/testing.py
index 64b5856..e7da493 100644
--- a/skimage/_shared/testing.py
+++ b/skimage/_shared/testing.py
@@ -285,6 +285,15 @@ def setup_test():
category=DeprecationWarning
)
+ # ignore dtype deprecation warning from NumPy arising from use of SciPy
+ # as a reference in test_watershed09. Should be fixed in scipy>=1.9.4
+ # https://github.com/scipy/scipy/commit/da3ff893b9ac161938e11f9bcd5380e09cf03150
+ warnings.filterwarnings(
+ 'default',
+ message=('`np.int0` is a deprecated alias for `np.intp`'),
+ category=DeprecationWarning
+ )
+
def teardown_test():
"""Default package level teardown routine for skimage tests.
diff --git a/skimage/color/tests/test_delta_e.py b/skimage/color/tests/test_delta_e.py
index 7472126..304346f 100644
--- a/skimage/color/tests/test_delta_e.py
+++ b/skimage/color/tests/test_delta_e.py
@@ -30,8 +30,7 @@ def test_ciede2000_dE(dtype, channel_axis):
dE2 = deltaE_ciede2000(lab1, lab2, channel_axis=channel_axis)
assert dE2.dtype == _supported_float_type(dtype)
- rtol = 1e-2 if dtype == np.float32 else 1e-4
- assert_allclose(dE2, data['dE'], rtol=rtol)
+ assert_allclose(dE2, data['dE'], rtol=1e-2)
def load_ciede2000_data():
diff --git a/skimage/feature/_cascade.pyx b/skimage/feature/_cascade.pyx
index 2556584..7f3914b 100644
--- a/skimage/feature/_cascade.pyx
+++ b/skimage/feature/_cascade.pyx
@@ -909,7 +909,9 @@ cdef class Cascade:
feature_number = int(internal_nodes[0])
# list() is for Python3 fix here
lut_array = list(map(lambda x: int(x), internal_nodes[1:]))
- lut = np.asarray(lut_array, dtype='uint32')
+ # Cast via astype to avoid warning about integer wraparound.
+ # see: https://github.com/scikit-image/scikit-image/issues/6638
+ lut = np.asarray(lut_array).astype(np.uint32)
# Copy array to the main LUT array
for i in range(8):
diff --git a/skimage/feature/tests/test_cascade.py b/skimage/feature/tests/test_cascade.py
index 5d5263a..bf541d6 100644
--- a/skimage/feature/tests/test_cascade.py
+++ b/skimage/feature/tests/test_cascade.py
@@ -2,6 +2,7 @@ import skimage.data as data
from skimage.feature import Cascade
+
def test_detector_astronaut():
# Load the trained file from the module root.
diff --git a/skimage/segmentation/_join.py b/skimage/segmentation/_join.py
index d22bff4..dcf68f0 100644
--- a/skimage/segmentation/_join.py
+++ b/skimage/segmentation/_join.py
@@ -1,4 +1,5 @@
import numpy as np
+
from ..util._map_array import map_array, ArrayMap
@@ -128,6 +129,8 @@ def relabel_sequential(label_field, offset=1):
else:
out_vals = np.arange(offset, offset+len(in_vals))
input_type = label_field.dtype
+ if input_type.kind not in "iu":
+ raise TypeError("label_field must have an integer dtype")
# Some logic to determine the output type:
# - we don't want to return a smaller output type than the input type,
@@ -142,7 +145,7 @@ def relabel_sequential(label_field, offset=1):
if input_type.itemsize < required_type.itemsize:
output_type = required_type
else:
- if input_type.type(out_vals[-1]) == out_vals[-1]:
+ if out_vals[-1] < np.iinfo(input_type).max:
output_type = input_type
else:
output_type = required_type
diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py
index 23e4452..064f9a0 100644
--- a/skimage/util/dtype.py
+++ b/skimage/util/dtype.py
@@ -1,6 +1,8 @@
-import numpy as np
+import warnings
from warnings import warn
+import numpy as np
+
__all__ = ['img_as_float32', 'img_as_float64', 'img_as_float',
'img_as_int', 'img_as_uint', 'img_as_ubyte',
@@ -24,12 +26,19 @@ _integer_ranges = {t: (np.iinfo(t).min, np.iinfo(t).max)
for t in _integer_types}
dtype_range = {bool: (False, True),
np.bool_: (False, True),
- np.bool8: (False, True),
float: (-1, 1),
np.float_: (-1, 1),
np.float16: (-1, 1),
np.float32: (-1, 1),
np.float64: (-1, 1)}
+
+with warnings.catch_warnings():
+ warnings.filterwarnings('ignore', category=DeprecationWarning)
+
+ # np.bool8 is a deprecated alias of np.bool_
+ if hasattr(np, 'bool8'):
+ dtype_range[np.bool8] = (False, True)
+
dtype_range.update(_integer_ranges)
_supported_types = list(dtype_range.keys())
diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py
index e7b4529..f8804e4 100644
--- a/skimage/util/tests/test_dtype.py
+++ b/skimage/util/tests/test_dtype.py
@@ -109,7 +109,7 @@ def test_copy():
def test_bool():
img_ = np.zeros((10, 10), bool)
- img8 = np.zeros((10, 10), np.bool8)
+ img8 = np.zeros((10, 10), np.bool_)
img_[1, 1] = True
img8[1, 1] = True
for (func, dt) in [(img_as_int, np.int16),
|