Fix functions.clip_array() (#1649)

* fix clip_array()

return error for invalid inputs.
use minmax for win32, umath.clip for other platforms

the previous code was penalizing Linux

* force output to be an array
This commit is contained in:
pijyoi 2021-03-22 09:44:26 +08:00 committed by GitHub
parent d0f5a6686f
commit d74bbe3bf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1004,19 +1004,22 @@ def clip_array(arr, vmin, vmax, out=None):
# performance since numpy 1.17
# https://github.com/numpy/numpy/issues/14281
if vmin is None and vmax is None:
# let np.clip handle the error
return np.clip(arr, vmin, vmax, out=out)
if vmin is None:
return np.core.umath.minimum(arr, vmax, out=out)
elif vmax is None:
return np.core.umath.maximum(arr, vmin, out=out)
elif sys.platform == 'win32':
# Windows umath.clip is slower than umath.maximum(umath.minimum)
if out is None:
out = np.empty_like(arr)
if vmin is not None:
arr = np.core.umath.maximum(arr, vmin, out=out)
if vmax is not None:
arr = np.core.umath.minimum(arr, vmax, out=out)
# np.core.umath.clip performs slightly better than
# the above on platforms compiled with GCC (e.g. Linux),
# but worse for CLANG (e.g. macOS) and MSVC (Windows)
return out
out = np.core.umath.minimum(arr, vmax, out=out)
return np.core.umath.maximum(out, vmin, out=out)
else:
return np.core.umath.clip(arr, vmin, vmax, out=out)
def rescaleData(data, scale, offset, dtype=None, clip=None):