[Numpy-discussion] Numpy helper function for __getitem__?

Stephan Hoyer shoyer at gmail.com
Wed Aug 26 13:59:32 EDT 2015


Indeed, the helper function I wrote for xray was not designed to handle
None/np.newaxis or non-1d Boolean indexers, because those are not valid
indexers for xray objects. I think it could be straightforwardly extended
to handle None simply by not counting them towards the total number of
dimensions.

On Tue, Aug 25, 2015 at 8:41 AM, Fabien <fabien.maussion at gmail.com> wrote:

> I think that Stephan's function for xray is very useful. A possible
> improvement (probably at a certain performance cost) would be to be able
> to provide a shape instead of a number of dimensions. The output would
> then be slices with valid start and ends.
>
> Current behavior:
> In[9]: expanded_indexer(slice(None), 2)
> Out[9]: (slice(None, None, None), slice(None, None, None))
>
> With shape:
> In[9]: expanded_indexer(slice(None), (3, 4))
> Out[9]: (slice(0, 4, 1), slice(0, 5, 1))
>
> But if nobody needed something like this before me, I think that I might
> have a design problem in my code (still quite new to python).
>

Glad you found it helpful!

Python's slice object has the indices method which implements this logic,
e.g.,

In [15]: s = slice(None, 10)

In [16]: s.indices(100)
Out[16]: (0, 10, 1)

Cheers,
Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150826/a8659640/attachment.html>


More information about the NumPy-Discussion mailing list