[Numpy-discussion] Numpy helper function for __getitem__?

Sebastian Berg sebastian at sipsolutions.net
Mon Aug 24 04:23:22 EDT 2015


On So, 2015-08-23 at 11:08 -0700, Stephan Hoyer wrote:
> I don't think NumPy has a function like this (at least, not exposed to
> Python), but I wrote one for xray, "expanded_indexer", that you are
> welcome to borrow:
> https://github.com/xray/xray/blob/v0.6.0/xray/core/indexing.py#L10
> 
> 

Yeah, we have no such functionality. We do have a function which does
all of this in C but it is somewhat more complex not exposed in any
case.
That function seems nice, though on its own not complete? It does not
seem to handle `np.newaxis`/`None` or boolean indexing arrays well.
One other thing which is not really important, we are deprecating the
use of multiple ellipsis.

Fabien, just to make sure you are aware. If you are overriding
`__getitem__`, you should also implement `__setitem__`. NumPy does some
magic if you do not. That will seem to make `__setitem__` work fine, but
breaks down if you have advanced indexing involved (or if you return
copies, though it spits warnings in that case).

- Sebastian


> 
> 
> ​Stephan
> 
> 
> 
> On Sunday, Aug 23, 2015 at 7:54 PM, Fabien
> <fabien.maussion at gmail.com>, wrote:
>         Folks, 
>         
>         My search engine was not able to help me on this one, possibly
>         because I 
>         don't know exactly *what* I am looking for. 
>         
>         I need to override __getitem__ for a class that wrapps a numpy
>         array. I 
>         know the dimensions of my array (which can be variable from
>         instance to 
>         instance), and I know what I want to do: for one preselected
>         dimension, 
>         I need to select another slice than requested by the user, do
>         something 
>         with the data, and return the variable. 
>         
>         I am looking for a function that helps me to "clean" the input
>         of 
>         __getitem__. There are so many possible cases, when the user
>         uses [:] or 
>         [..., 1:2] or [0, ..., :] and so forth. But all these cases
>         have an 
>         equivalent index array of len(ndimensions) with only valid
>         slice() 
>         objects in it. This array would be much easier for me to work
>         with. 
>         
>         in pseudo code: 
>         
>         def __getitem__(self, item): 
>         # clean input 
>         item = np.clean_item(item, ndimensions=4) 
>         # Ok now item is guaranteed to be of len 4 
>         item[2] = slice() 
>         # Continue 
>         etc. 
>         
>         Is there such a function in numpy? 
>         
>         I hope I have been clear enough... Thanks a lot! 
>         
>         Fabien 
>         
>         _______________________________________________ 
>         NumPy-Discussion mailing list 
>         NumPy-Discussion at scipy.org 
>         http://mail.scipy.org/mailman/listinfo/numpy-discussion 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150824/d4287a2b/attachment.sig>


More information about the NumPy-Discussion mailing list