【问题标题】:Python Delaunay TriangulationPython Delaunay三角剖分
【发布时间】:2019-04-18 12:57:47
【问题描述】:

我有一个数组,其中包含代表点的不同向量。我想要得到的是每个点的所有邻居。这些点位于“numpy.ndarray”中,类型为“numpy.float64”。我想使用 Delaunay 三角剖分,但我的问题是我在数组中使用“numpy.float64”而不是整数。我已经发现是这样的:

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])

我不明白的是,使用上面的列表(点和 dir_vec)它可以工作,但是当我在我的实际代码中使用它时它不起作用。在我的实际代码中,我从 3D 图片中提取点并将这些点放在一个列表中。该列表与列表“点”相同,只是具有更多条目。因此,在将前 8 个条目(如上所示的代码)放入我的主代码之前,我尝试获取它的邻居。 当我在我的代码上尝试这个时,我得到了这个错误:

"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"

我不明白为什么上面的代码有效,但在我的实际代码中却没有。 我的实际代码:

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

cell2 的前 8 个条目与 Points 中的相同。如果我打印 cell2 的类型,然后打印前 8 个条目,就会出现这种情况:

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

我得到这个输出:

<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]]

我想要一个新列表中的所有相邻点,以便我可以进一步分析它们。有谁知道我做错了什么?

【问题讨论】:

  • 问题不完整。你在用scipy.spatial.Delaunay吗?您的代码的问题是您将 nx3 矩阵的元素存储到变量 neighbor 中,这是一个浮点数。您不能将浮点数用作 list/np.ndarray 的索引,这是您在调用 points[neighbor] 时尝试的方法。我认为你必须修改你的算法。写到这里,意义不大。
  • 很抱歉我第一次提问。所以感谢您的反馈,希望在编辑我的问题后现在更容易理解了。
  • @normanius 是的,对不起,我的错误我的代码有错误,这就是它的实际样子。
  • 鉴于cells2的构造方式,确实很难看出问题的核心在哪里,但应该清楚cells2不仅仅是一个像@987654331那样的二维numpy数组@ 在上面的例子中。或者,如果是,请向我们展示(部分)实际输入 (cells2) 数组。
  • 还建议显示完整的回溯,而不仅仅是错误消息。如果回溯很长,可以截取一段,只显示第一行和最后 10 行左右,但我想这里不是这样,可以显示完整的回溯。这样我们就可以确认它来自neighbor = tri.vertex_neighbor_vertices[1]... 行,或者其他地方。

标签: python delaunay numpy-ndarray


【解决方案1】:

正如您在 cmets 中发布的,cells2 是一个列表,而不是一个 numpy 数组。索引数组不适用于列表,您需要一个 numpy 数组。

也许这行得通:

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

或者只是尝试:

cells2 = np.asarray(cells2)

请注意,您发布的异常不能由列表引发。所以我假设你的帖子代表了几个不同问题的混合体。

【讨论】:

  • 感谢它的工作。我是编程新手,所以我现在不认为索引数组不适用于列表。你帮了我很多。
猜你喜欢
  • 2015-01-07
  • 2021-02-14
  • 2021-05-14
  • 2013-05-12
  • 1970-01-01
  • 2016-02-08
  • 2014-01-21
  • 2020-05-06
  • 2019-09-28
相关资源
最近更新 更多