Skip to content

fileslice.is_fancy fails for slices with numpy scalar integers #531

@pauldmccarthy

Description

@pauldmccarthy

This code:

import numpy as np
import nibabel as nib

shape = (10, 20, 30, 5)
randVoxels = np.vstack([np.random.randint(0, s, 10) for s in shape[:3]]).T

for x, y, z in randVoxels:
    nib.fileslice.canonical_slicers((x, y, z, slice(None, None, None)), shape)

Raises this error:

ValueError                                Traceback (most recent call last)
<ipython-input-10-1584b7a87e3e> in <module>()
      6
      7 for x, y, z in randVoxels:
----> 8     nib.fileslice.canonical_slicers((x, y, z, slice(None, None, None)), shape)
      9

/Users/paulmc/Projects/nibabel/nibabel/fileslice.pyc in canonical_slicers(sliceobj, shape, check_inds)
     80         sliceobj = (sliceobj,)
     81     if is_fancy(sliceobj):
---> 82         raise ValueError("Cannot handle fancy indexing")
     83     can_slicers = []
     84     n_dim = len(shape)

ValueError: Cannot handle fancy indexing

Due to this check in fileslice.py:

        if hasattr(slicer, 'dtype'):  # ndarray always fancy
            return True

At first, I thought this was a simple bug, that could be fixed either by making the check more specific (e.g. if hasattr(slicer, 'dtype') and not isinstance(slicer, Integral), or by removing the check entirely - the code which follows should take care of any non-integral slice object.

But, tracing back through the history, and looking at the test cases, I'm struggling to understand why this check was deemed necessary in the first place, and am wondering if I've missed some edge case or subtle piece of complexity. Can anybody shed some light on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions