Skip to content

Fancy slicing inconsistent behaviour with lists of lists and arrays of arrays #7909

Closed
@henpemaz

Description

@henpemaz

Hello !

I am working on some triangular mesh processing, and I've stumbled on some weird behaviour for some of our smaller geometry tests.

Apparently, lists of lists and arrays of arrays behave very differently for fancy queries up to a length of 32.

Here is some code to reproduce the issue:

import numpy as np
import random as rd

# The idea is to use a list of [v1, v2, v3] faces to query the vertices in groups of 3, for easy use in geometrical ops
# The result of the fancy slicing must be an array of len-3 arrays of vertices (also len-3 arrays)
# In other words, a big array containing 3-by-3 arrays of coordinates for each face
vertices = np.zeros((1000,3))  # Apparently, the size of the array being queried does not matter
face = lambda : [rd.randint(0, 999), rd.randint(0, 999), rd.randint(0, 999)]

i = 2  # If trying to slice with a 1-by-3 list, this returns a single 3-by-3 array, even given the higher dimension of the list (expected result would be a 1-by-3-by-3 array)
       # Yet this is an "understandable" behaviour, even if wrong and misleading

while True:
    faces_list = [face() for f in range(i)]
    assert vertices[np.array(faces_list)].shape == (i, 3, 3) # This works as expected and will not throw any exception

    try:
        my_vertices = vertices[faces_list]  # This behaves as some sort of multi-dimensional slicing instead, up to a certain limit of dimensions, then it starts behaving as expected
        assert my_vertices.shape == (i, 3, 3)  # Does not fail if the line above worked as intended
        break
    except:  # The block above throws "IndexError: too many indices for array"
        print("numpy will not handle a list with {} lists for fancy slicing :^/".format(i))
        i += 1

print("numpy can handle a list with {} or more lists :)".format(i))
exit(0)

I don't use numpy arrays for my faces for reasons that are not relevant here, and I hadn't run into this issue until I've tested my solution on very tiny meshes.
I can easily contour this problem by making a numpy copy of the faces list (since it's so small in this case), so I don't actually need a fix, but I do think it's a bug.

I'm running on Ubuntu 14.4, Python 3.4, and np.version.version == '1.8.2'

Good luck, and thanks for your hard work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions