Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RaggedArray.__getitem__ fails on 2d lists #44

Open
justinrporter opened this issue May 13, 2017 · 0 comments
Open

RaggedArray.__getitem__ fails on 2d lists #44

justinrporter opened this issue May 13, 2017 · 0 comments
Assignees
Labels

Comments

@justinrporter
Copy link
Collaborator

justinrporter commented May 13, 2017

In numpy, it is possible to do the following:

>>> a = np.array(np.arange(9)).reshape(3, 3)
>>> a[list(zip( (1, 2), (2, 0), (0, 0) ))]

array([5, 6, 0])

Specifically, ndarray.__getitem__ handles the case where the requested indices are 2-element list, similar to the output of np.where, but a list rather than a tuple.

Similar code on a RaggedArray:

a = ra.RaggedArray([np.arange(3), np.arange(4), np.arange(2)])
a[list(zip( (1, 2), (2, 0), (0, 0) ))]

fails with

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-108-d8376781920d> in <module>()
      1 a = ra.RaggedArray([np.arange(3), np.arange(4), np.arange(2)])
----> 2 a[list(zip( (1, 2), (2, 0), (0, 0) ))]

/home/jrporter/modules/enspara/util/array.py in __getitem__(self, iis)
    455         elif (type(iis) is slice) or (type(iis) is list) \
    456                 or (type(iis) is np.ndarray):
--> 457             return RaggedArray(self._array[iis])
    458         # tuples get index conversion from 2d to 1d
    459         elif type(iis) is tuple:

IndexError: too many indices for array

The very-similar following succeeds:

>>> a = ra.RaggedArray([np.arange(3), np.arange(4), np.arange(2)])
>>> a[tuple(zip( (1, 2), (2, 0), (0, 0) ))]

array([2, 0, 0])

IIRC this tuple/list issue is one of the things that was gave me a lot of trouble when I wrote the __getitem__ we wound up not using. (I don't remember if I eventually figured it out or not.)

Anyway, it looks like the __getitem__ we currently have implemented attempts to use the type of the indexer ("iis") as an proxy for the meaning of the contained data. I would suggest using the length of the data instead. As a probably-flawed example of what I'm suggesting, if the list only has two elements of matching dimensions, then it's an np.where-result-like indexer. If it instead is a list of many elements of one dimension, then it can be interpreted as a list of indices in the appropriate dimension.

@justinrporter justinrporter changed the title RaggedArray.__setitem__ fails on 2d lists RaggedArray.__getitem__ fails on 2d lists Jun 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants