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