-1

I have an array with different vectors which represent points. What I'm trying to get is all neighbors from each point. Those points are in 'numpy.ndarray' and have the type 'numpy.float64'. I want to use Delaunay Triangulation but my problem is that I'm using 'numpy.float64' in my arrays and not integer. What I already found out is this way:

from scipy.spatial import Delaunay
import numpy as np

points = np.array([[-0.30352158,  0.73558974,  0.60562561],
      [ 0.46504451, -0.4754239,   0.74679697],
      [-0.52149363,  0.11833734, -0.84500927],
      [ 0.11225645,  0.80278751, -0.58560285],
      [-0.72246172,  0.57197704,  0.38844732],
      [ 0.89957812, -0.07875899, -0.42960008],
      [-0.4316689,  -0.20747224,  0.87784807],
      [-0.19440343,  0.55628405, -0.80793277]])

tri = Delaunay(points)

neighbor_cell = []

for i in range(len(points)):
    neighbor = tri.vertex_neighbor_vertices[1][
           tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i + 1]]  #from stack overflow
    neighbor_cell.append(points[neighbor])

What I don't understand is that with the above lists (points and dir_vec) it works, but when I use it in my actual code it doesn't work. In my actual Code I'm extracting the Points from a 3D Picture and put the Points in a list. The list is the same as the list "points" just with more entries. So I tried to get the neighbors of the first 8 entries (the code shown above) before putting it in my main Code. When I try this on my Code I get this Error:

"Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/.../PycharmProjects/.../Code.py", line 80, in <module>
neighborPoints(convertCells())
  File "/Users/.../PycharmProjects/.../Code.py", line 71, in neighborPoints
neighbor_cells.append(cells2[neighbor])
TypeError: only integer scalar arrays can be converted to a scalar index"

I don't understand why the Code above works but on my actual code it doesn't. My actual Code:

import _pickle as cPickle
from Cell import *
from scipy.spatial import Delaunay

def load(pick):

    with open(pick, 'rb') as input:
        cells = cPickle.load(input, encoding='latin1')
        coms = cPickle.load(input, encoding='latin1')
        point_tree = cPickle.load(input, encoding='bytes')
        print("cPickled loaded")
        return cells, coms, point_tree 

cells, coms, point_tree = load("...")

def convertCells():
# Converts Cells from Cell.Cell to np.array

    cells2 = []
    for i in range(len(cells)):
        cells2.append(cells[i].getMainDir())

    return cells2

def neighborPoints(cells2):

    tri = Delaunay(cells2)

    neighbor_cells = [] # local vector
    help_func = []
    neighbor_cell_dir = []  # directional vector

    for i in range(len(cells2)):
        neighbor = tri.vertex_neighbor_vertices[1][
               tri.vertex_neighbor_vertices[0][i]:tri.vertex_neighbor_vertices[0][i+1]]
        neighbor_cells.append(cells2[neighbor])
        help_func.append(neighbor)

    for i in help_func:
        neighbor_cell_dir.append(coms[i])

    return neighbor_cells, neighbor_cell_dir

The first 8 entries of cells2 are the same as in Points. This comes out if I print the type of cells2 and then the first 8 entries :

print(type(cell2))
for i in range(8):
    print(cells2[i])

I get this output:

<class 'numpy.ndarray'> 
[[-0.30352158 0.73558974 0.60562561] 
[ 0.46504451 -0.4754239 0.74679697] 
[-0.52149363 0.11833734 -0.84500927] 
[ 0.11225645 0.80278751 -0.58560285] 
[-0.72246172 0.57197704 0.38844732] 
[ 0.89957812 -0.07875899 -0.42960008] 
[-0.4316689 -0.20747224 0.87784807] 
[-0.19440343 0.55628405 -0.80793277]]

I want all the neighbor points in a new list so that I can analyze them further. Does anyone have an idea what I'm doing wrong?

M.S
  • 13
  • 1
  • 3
  • 1
    The question is incomplete. Are you using `scipy.spatial.Delaunay`? The problem with your code is that you store an element of an nx3 matrix into variable `neighbor`, which is a float. You cannot use a float as an index for a list/np.ndarray, which is what you try when calling `points[neighbor]`. I think you have to revise your algorithm. As it is written here, it does not make much sense. – normanius Nov 15 '18 at 12:19
  • I'm sorry my first time asking a question. So thanks for the feedback hopefully after editing my question it is easier to understand now. – M.S Nov 15 '18 at 14:35
  • @normanius yes sorry my mistake I had a mistake in my code this is what it actually looks like. – M.S Nov 15 '18 at 14:36
  • Given the way `cells2` is constructed, it is really hard to see where the core of the problem is, but it should be clear that `cells2` is not simply a 2D numpy array as `points` in the top example. Or, if it is, show us (a part of) the actual input (`cells2`) array. – 9769953 Nov 15 '18 at 15:15
  • It is also recommended to show the full traceback, not just the error message. If the traceback is very long, one can snip a section and just show the first and last 10 lines or so, but I guess that's not the case here, and the full traceback can be shown. So that we can confirm it originates in the line `neighbor = tri.vertex_neighbor_vertices[1]...`, or perhaps elsewhere. – 9769953 Nov 15 '18 at 15:17
  • Have you looked at `neighbor`? What value does it take? – normanius Nov 15 '18 at 15:41
  • I'm afraid that this is not the right place for basic debugging. Try to debug your code yourself first. In case you observe a problem for which you don't know an answer: isolate your problem such that we can reproduce it, and post your issue in a generic form such that others can benefit as well. – normanius Nov 15 '18 at 15:46
  • neighbor is a 'numpy.ndarray' and is a list with indices of neighbors. For example neighbor of 'i = 0' is [1 6 4 3 5]. That means point[1], point[6], point[4], point[3] and point[5] are the neighbors of point[0]. – M.S Nov 15 '18 at 15:49
  • This cannot be the case, because if `neighbor` was equal to `np.array([1, 6, 4, 3, 5], dtype=int32)`, you would not observe the exception. – normanius Nov 15 '18 at 16:00
  • For which iteration do you observe your problem? `i=0`? – normanius Nov 15 '18 at 16:01
  • @normanius this is what I don't understand. It should work but it doesn't. If I comment the "neighbor_cells.append(cells2[neighbor])" line in my code and then "print(neighbor)" I get: "array([482, 221, 398, 417, 500, 396, 537, 527, 516], dtype=int32)" – M.S Nov 15 '18 at 16:45
  • @normanius I'm sorry if this is not the right place to ask a question. But since I'm trying to understand what the Problem is since Friday I was hoping someone could give me a hint for my Problem. Should I delete the Post? Just so I now for the future what questions are ok and which are not. – M.S Nov 15 '18 at 16:53
  • What do you see if you `print(cells2.shape)` **in** the loop? – normanius Nov 15 '18 at 17:01
  • "AttributeError: 'list' object has no attribute 'shape'" – M.S Nov 15 '18 at 17:06
  • So this is the answer to your problem: Index arrays are supported only by numpy arrays, they don't work for lists. `cells2` is a numpy array in the working example, and `cell2` is a list in the failing example. Either adjust `convertCells` to use a numpy array, or convert with `cell2 = np.asarray(cell2)`. – normanius Nov 16 '18 at 10:42
  • Your question is inconsistent, by the way. The stack trace you posted can only be issued by a numpy array. But if `cells2` doesn't have a shape attribute, it cannot be a numpy array, and therefore you cannot get this error message. – normanius Nov 16 '18 at 10:46

1 Answers1

0

As you posted in the comments, cells2 is a list, and not a numpy array. Index arrays don't work for lists, you need a numpy array.

Maybe this works:

def convertCells(cells):
    cells2 = np.array(len(cells),3)
    for i in range(len(cells)):
        cells2[i,:] = cells[i].getMainDir()
    return cells2

Or just try:

cells2 = np.asarray(cells2)

Note that the exception that you posted cannot be raised by a list. So I assume that your post represents a mélange of several different problems.

normanius
  • 8,629
  • 7
  • 53
  • 83
  • 1
    Thanks it worked. I'm new in programming so I didn't now that index arrays don't work for lists. You helped me out a lot. – M.S Nov 16 '18 at 13:18