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?