[Numpy-discussion] Numpy helper function for __getitem__?

Stephan Hoyer shoyer at gmail.com
Sun Aug 23 14:08:02 EDT 2015


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










​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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150823/33f62cf6/attachment.html>


More information about the NumPy-Discussion mailing list