我有以下情况:
(1) 我有一个大网格。在某些条件下,我想进一步观察该网格中的特定点/单元格。每个单元格都有一个 ID
和分别的坐标 X
、Y
。因此,在这种情况下,我们只观察一个细胞 - 在图像上标记为 C,它位于网格的边缘。通过一些公式,我可以得到一阶(图像上标记为 1)和二阶(图像上标记为 2)的所有相邻单元格。
(2) 在进一步的条件下,我识别出相邻单元格中的一些单元格,并在第二张图像上以橙色标记。我想要做的是通过优化距离来将所有橙色单元格彼此连接起来,并仅考虑 min()
距离。我的第一次尝试是仅通过计算到较低阶细胞的距离来观察细胞。因此,当查看邻居单元格 2 中的单元格时,我只查看单元格 1 中的单元格。连接的解决方案如图 2 所示,但它不是最佳的,因为理想的解决方案会比较所有单元的距离,而不仅仅是较低邻居阶单元的距离。通过这样做,我得到了图 3 所示的情况。问题是细胞当然没有连接到中心。该怎么办?
当前代码是:
CO
- 中心点列表。
data
- df 所有具有 X、Y 值的 ID
CO_list = CO['ID'].tolist()
neighbor100 = []
for p in IskanjeCO_list:
d = get_neighbors100k2(p, len(data)) #function that finds the ID's of neighbours of the first order
neighbor100.append(d)
neighbor200 = []
for p in IskanjeCO_list:
d = get_neighbors200k2(p, len(data)) #function that finds the ID's of neighbours of the second order
neighbor200.append(d)
flat100 = []
for i in neighbor100:
for j in i:
flat100.append(j)
flat200 = []
for i in neighbor200:
for j in i:
flat200.append(j)
neighbors100 = flat100
neighbors200 = flat200
data_sosedi100 = data.iloc[flat100,].reset_index(drop=True)
data_sosedi200 = data.iloc[flat200,].reset_index(drop=True)
dist200 = []
for b in flat200:
d = ((pd.DataFrame((data_sosedi100['X']* - data.iloc[b,]['X'])**2
+ (data_sosedi100['Y'] - data.iloc[b,]['Y'])**2 )**0.5)).sum(1)
dist200.append(d.min())
data_sosedi200['dist'] = dist200
data_sosedi200['id'] = None
for e in CO_list:
data_sosedi200.loc[data_sosedi200['FID_2'].isin((get_neighbors200k2(e, len(data)))),'id'] = e
您对如何进一步优化有什么建议吗?我希望我呈现了整个图像。如果需要的话,我会进一步澄清。如果您看到代码的一部分,我可以进一步优化这个循环,我将非常感激!
最佳答案
我手动定义了要使用的点: 将 numpy 导入为 np from 操作符导入 itemgetter, attrgetter
nodes = [[-2,1], [-2,0], [-1,0], [0,0], [1,1], [2,1], [2,0], [1,2], [2,2]]
center = [0,0]
def find_neighbor(node):
n=[]
for i in range(-1,2):
for j in range(-1,2):
if not (i ==0 and j ==0):
n.append([node[0]+i,node[1]+j])
return [N for N in n if N in nodes]
def distance_to_center(node):
return np.sqrt(node[0]**2+node[1]**2)
def distance_between_two_nodes(node1, node2):
return np.sqrt((node1[0]-node2[0])**2+(node1[1]-node2[1])**2)
def next_node_closest_to_center(node):
min = distance_to_center(node)
next_node = node
for n in find_neighbor(node):
if distance_to_center(n) < min:
min = distance_to_center(n)
next_node = n
return next_node, min
def get_path_to_center(node):
node_path = [node]
distance = 0.
while node!= center:
new_node = next_node_closest_to_center(node)[0]
distance += distance_between_two_nodes(node, new_node)
node_path.append(new_node)
node=new_node
return node_path,distance
def furthest_nodes_from_center(nodes):
max = 0.
for n in nodes:
if get_path_to_center(n)[1] > max:
furthest_nodes_pathwise = []
max = get_path_to_center(n)[1]
furthest_nodes_pathwise.append(n)
elif get_path_to_center(n)[1] == max:
furthest_nodes_pathwise.append(n)
return furthest_nodes_pathwise
def farthest_node_from_center(nodes):
max = 0.
farthest_node = center
for n in nodes:
if distance_to_center(n) > max:
max = distance_to_center(n)
farthest_node = n
return farthest_node
def closest_node_to_center(nodes):
min = distance_to_center(farthest_node_from_center(nodes))
for n in nodes:
if distance_to_center(n) < min:
min = distance_to_center(n)
closest_node = n
return closest_node
def closest_node_center_with_furthest_distance(node_selection):
if len(node_selection) == 1:
return node_selection[0]
else:
return closest_node_to_center(node_selection)
print(closest_node_center_with_furthest_distance(furthest_nodes_from_center(nodes)))
输出:
[2, 0]
[Finished in 0.266s]
通过在所有节点上运行,我现在可以确定路径上最远但仍最接近中心距离的节点是 [2,0]
而不是 [2,2 ]
。所以我们从那里开始。要找到另一侧的数据,只需将数据分成负 x 值和正 x 值即可。如果您在仅包含负 x 值单元格的列表上运行它,您将得到 [-2,1]
现在您已经有了 2 个起始单元格 [2,0]
和 [-2,1]
我将让您找出导航到使用我评论中的步骤中心传递所有单元格(您现在可以跳过步骤 1,因为这是发布的答案)
关于python - 使用 python 优化网格连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044859/