diff --git a/fsl/utils/naninfrange.py b/fsl/utils/naninfrange.py index bfb497f386499ece2aaf50708937eddc9b7810f6..bcdf5b12cffa1cc80bc1b3d09d8cc7f141640bef 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()