Skip to content

RF+TST: allow dtype specifiers as fileslice input #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions nibabel/fileslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions nibabel/tests/test_fileslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<i4'):
arr = np.arange(24, dtype=dt).reshape((2, 3, 4))
fobj = BytesIO(arr.tostring())
new_slice = fileslice(fobj, sliceobj, arr.shape, dt)
assert_array_equal(arr[sliceobj], new_slice)


def test_fileslice_errors():
# Test fileslice causes error on fancy indexing
arr = np.arange(24).reshape((2, 3, 4))
Expand Down