indexing image with a list of indices cannot, in general, return a consistent affine

Bug #514040 reported by Bago
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NIPY
Fix Committed
Undecided
Unassigned

Bug Description

In [66]: img[..., np.array((0,1))]

     95 # allow slicing of form [...,1]
     96 if Ellipsis in index:
---> 97 if sum([i == Ellipsis for i in index]) > 1:
     98 raise ValueError("only one Ellipsis (...) allowed in slice")
     99 # convert ellipsis to series of slice(None) objects. For

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I believe the i == Ellipsis should be i is Ellipsis

Revision history for this message
Matthew Brett (matthew-brett) wrote : Re: [Bug 514040] [NEW] indexing image object with numpy array fails

Hi Bago,

On Thu, Jan 28, 2010 at 10:45 PM, Bago <email address hidden> wrote:
> Public bug reported:
>
> In [66]:  img[..., np.array((0,1))]
>
>
>     95         # allow slicing of form [...,1]
>     96         if Ellipsis in index:
> ---> 97             if sum([i == Ellipsis for i in index]) > 1:
>     98                 raise ValueError("only one Ellipsis (...) allowed in slice")
>     99             # convert ellipsis to series of slice(None) objects.  For
>
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
>
> I believe the i == Ellipsis should be i is Ellipsis

Ah - yes - the problem is, that implementing fancy indexing (indexing
with an array) would be rather hard. Do you have a particular
use-case in mind? Can it be covered by getting the array:

data = np.asarray(img)
sampled = data[index_array]

and then putting in your own affine? The complications arise trying
to deal with all the possible crazy things you can do with fancy
indexing, when keeping track of the affine...

Thanks a lot,

Matthew

Revision history for this message
Bago (mrbago) wrote : Re: indexing image object with numpy array fails

Ah, i see what you're saying. I didn't consider that because my use case is pretty simple. b0 = diff_img[..., b_values == 0] (b_values == 0 can have multiple true entries). I can use the fix you suggested, but this brings up a new issue. img[[0,1,10], ...] does return an image but img[np.array([0,1,10]), ...] does not. Should the first case raise an error, should the second case raise a more informative error?

Bago

Revision history for this message
Matthew Brett (matthew-brett) wrote : Re: [Bug 514040] Re: indexing image object with numpy array fails

Yo,

On Thu, Jan 28, 2010 at 11:38 PM, Bago <email address hidden> wrote:
> Ah, i see what you're saying. I didn't consider that because my use case
> is pretty simple. b0 = diff_img[..., b_values == 0] (b_values == 0 can
> have multiple true entries). I can use the fix you suggested, but this
> brings up a new issue. img[[0,1,10], ...] does return an image but
> img[np.array([0,1,10]), ...] does not. Should the first case raise an
> error, should the second case raise a more informative error?

I think the second case should raise a more informative error, like

'Sorry, we don't support fancy (numpy array) indexing on image objects'

Does that sound reasonable?

See you,

Matthew

Revision history for this message
Bago (mrbago) wrote : Re: indexing image object with numpy array fails

I think the first case should raise the same error

Bago

Bago (mrbago)
summary: - indexing image object with numpy array fails
+ indexing image with a list of indices cannot, in general, return a valid
+ affine
summary: - indexing image with a list of indices cannot, in general, return a valid
- affine
+ indexing image with a list of indices cannot, in general, return a
+ consistent affine
Revision history for this message
Matthew Brett (matthew-brett) wrote :

I agree - thanks for keeping up with this.

Revision history for this message
Matthew Brett (matthew-brett) wrote :

Fixed in trunk I believe - could you test? Thanks a lot.

Changed in nipy:
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.