[Numpy-discussion] Numpy helper function for __getitem__?

Fabien fabien.maussion at gmail.com
Sun Aug 23 13:54:29 EDT 2015


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




More information about the NumPy-Discussion mailing list