From f5e1e1e1921a8da2057aba3d4552571872ebc8c0 Mon Sep 17 00:00:00 2001 From: Matthew Brett Date: Wed, 31 Aug 2016 10:34:25 -0700 Subject: [PATCH] RF+TST: allow dtype specifiers as fileslice input Fileslice function previously insisted on np.dtype object as dtype specifier. Allow any dtype specifier. --- nibabel/fileslice.py | 17 ++++++++++------- nibabel/tests/test_fileslice.py | 10 ++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/nibabel/fileslice.py b/nibabel/fileslice.py index d69ab6f3b3..a0c28b68bd 100644 --- a/nibabel/fileslice.py +++ b/nibabel/fileslice.py @@ -719,17 +719,19 @@ def fileslice(fileobj, sliceobj, shape, dtype, offset=0, order='C', Parameters ---------- fileobj : file-like object - binary file-like object. Implements ``read`` and ``seek`` + file-like object, opened for reading in binary mode. Implements + ``read`` and ``seek``. sliceobj : object - something that can be used to slice an array as in ``arr[sliceobj]`` + something that can be used to slice an array as in ``arr[sliceobj]``. shape : sequence - shape of full array inside `fileobj` - dtype : dtype object - dtype of array inside `fileobj` + shape of full array inside `fileobj`. + dtype : dtype specifier + dtype of array inside `fileobj`, or input to ``numpy.dtype`` to specify + array dtype. offset : int, optional offset of array data within `fileobj` order : {'C', 'F'}, optional - memory layout of array in `fileobj` + memory layout of array in `fileobj`. heuristic : callable, optional function taking slice object, axis length, stride length as arguments, returning one of 'full', 'contiguous', None. See @@ -743,7 +745,8 @@ def fileslice(fileobj, sliceobj, shape, dtype, offset=0, order='C', """ if is_fancy(sliceobj): raise ValueError("Cannot handle fancy indexing") - itemsize = dtype.itemsize + dtype = np.dtype(dtype) + itemsize = int(dtype.itemsize) segments, sliced_shape, post_slicers = calc_slicedefs( sliceobj, shape, itemsize, offset, order) n_bytes = reduce(operator.mul, sliced_shape, 1) * itemsize diff --git a/nibabel/tests/test_fileslice.py b/nibabel/tests/test_fileslice.py index 6fba9ea967..a515d70226 100644 --- a/nibabel/tests/test_fileslice.py +++ b/nibabel/tests/test_fileslice.py @@ -740,6 +740,16 @@ def test_fileslice(): _check_slicer(sliceobj, arr, fobj, offset, order) +def test_fileslice_dtype(): + # Test that any valid dtype specifier works for fileslice + sliceobj = (slice(None), slice(2)) + for dt in (np.dtype('int32'), np.int32, 'i4', 'int32', '>i4', '