Merge pull request #1632 from pijyoi/clip_regression
rescaleData : workaround np.clip regression since numpy 1.17
This commit is contained in:
commit
63c19800d2
@ -1009,7 +1009,18 @@ def rescaleData(data, scale, offset, dtype=None, clip=None):
|
|||||||
dtype = data.dtype
|
dtype = data.dtype
|
||||||
else:
|
else:
|
||||||
dtype = np.dtype(dtype)
|
dtype = np.dtype(dtype)
|
||||||
|
|
||||||
|
vmin, vmax = None, None
|
||||||
|
if dtype.kind in 'ui':
|
||||||
|
lim = np.iinfo(dtype)
|
||||||
|
if clip is None:
|
||||||
|
# don't let rescale cause integer overflow
|
||||||
|
vmin, vmax = lim.min, lim.max
|
||||||
|
else:
|
||||||
|
vmin, vmax = max(clip[0], lim.min), min(clip[1], lim.max)
|
||||||
|
elif clip is not None:
|
||||||
|
vmin, vmax = clip
|
||||||
|
|
||||||
if np.can_cast(data, np.float32):
|
if np.can_cast(data, np.float32):
|
||||||
work_dtype = np.float32
|
work_dtype = np.float32
|
||||||
else:
|
else:
|
||||||
@ -1019,16 +1030,12 @@ def rescaleData(data, scale, offset, dtype=None, clip=None):
|
|||||||
d2 *= scale
|
d2 *= scale
|
||||||
|
|
||||||
# Clip before converting dtype to avoid overflow
|
# Clip before converting dtype to avoid overflow
|
||||||
if dtype.kind in 'ui':
|
# regression in np.clip performance since numpy 1.17
|
||||||
lim = np.iinfo(dtype)
|
if vmin is not None:
|
||||||
if clip is None:
|
np.core.umath.maximum(d2, vmin, out=d2)
|
||||||
# don't let rescale cause integer overflow
|
if vmax is not None:
|
||||||
np.clip(d2, lim.min, lim.max, out=d2)
|
np.core.umath.minimum(d2, vmax, out=d2)
|
||||||
else:
|
|
||||||
np.clip(d2, max(clip[0], lim.min), min(clip[1], lim.max), out=d2)
|
|
||||||
else:
|
|
||||||
if clip is not None:
|
|
||||||
np.clip(d2, *clip, out=d2)
|
|
||||||
# don't copy if no change in dtype
|
# don't copy if no change in dtype
|
||||||
data = d2.astype(dtype, copy=False)
|
data = d2.astype(dtype, copy=False)
|
||||||
return data
|
return data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user