From 52e5a60b3f2287744ada33150de0c41331c6d933 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Thu, 13 Jun 2019 11:12:47 +0930 Subject: [PATCH] RF: Change naninfrange approach to flattening structured arrays, avoid copying data if possible, and avoid copydeprecation warnings. --- fsl/utils/naninfrange.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/fsl/utils/naninfrange.py b/fsl/utils/naninfrange.py index bfb497f38..bcdf5b12c 100644 --- a/fsl/utils/naninfrange.py +++ b/fsl/utils/naninfrange.py @@ -24,10 +24,28 @@ def naninfrange(data): """ # For structured arrays, we assume that - # all fields are numeric, and we simply - # take the range across all fields + # all fields have the same dtype, and we + # simply take the range across all fields if len(data.dtype) > 0: - data = np.concatenate([data[n] for n in data.dtype.names]) + + # Avoid inducing a data copy if + # at all possible. np.ndarray + # doesn't preserve the underlying + # order, so let's set that. Also, + # we're forced to make a copy if + # the array is not contiguous, + # otherwise ndarray will complain + if data.flags['C_CONTIGUOUS']: order = 'C' + elif data.flags['F_CONTIGUOUS']: order = 'F' + else: + data = np.ascontiguousarray(data) + order = 'C' + + shape = [len(data.dtype)] + list(data.shape) + data = np.ndarray(buffer=data.data, + shape=shape, + order=order, + dtype=data.dtype[0]) if not np.issubdtype(data.dtype, np.floating): return data.min(), data.max() -- GitLab