使用网络x做某事的策略 - 第一次使用者,

时间:2013-09-20 09:07:10

标签: python networkx depth-first-search nearest-neighbor minimum-spanning-tree

我有一百万xyzc点。

我将遍历python中的每个点。

  • 将其作为节点添加到图表中。
  • 使用python
  • 中的python绑定计算其最近邻居
  • 将每个最近邻居添加到图表中作为节点
  • 从点到每个邻居添加边缘。
  • 为边缘指定权重,这是两个点标量c值的乘法。

然后询问我的图表的最小生成树。

然后我如何告诉项目x从点(2,1,3)开始并启动深度搜索,遍历每个点,如果它们连续点的乘积c值是 - 用以下项替换后面的点c值 - ç

由于

1 个答案:

答案 0 :(得分:1)

import numpy as np
from sklearn.neighbors import NearestNeighbors
import networkx as nx

#Get coordinates
f_name = 'horse'
coord = np.genfromtxt(str(f_name)+'.txt',autostrip=True)
coord = np.hstack((coord, np.zeros((coord.shape[0], 3), dtype=coord.dtype))) 


def Normals(XYZ):

    #The below code uses the PCA Eigen method to fit plane.

    #Get the covariance matrix
    average=sum(XYZ)/XYZ.shape[0]
    b = np.transpose(XYZ - average)
    cov=np.cov(b)

    #Get eigen val and vec
    e_val,e_vect = np.linalg.eigh(cov)

    #Diagonlize eigen vals
    e_val_d = np.diag(e_val)

    #Find min eigen val
    h = np.rank(min(e_val))

    #Find normal
    norm =  e_vect[:,h]

    #Calc curvature
    curvature = e_val[0]/(e_val[0]+e_val[1]+e_val[2])

    return norm[0],norm[1],norm[2]


print 'CALCULATE NORMALS'
k = 8
neigh = NearestNeighbors(k) 
neigh.fit(coord)

for i in range(0,len(coord)):

     #Determine the neighbours of point 
    d = neigh.kneighbors(coord[i]) 

    #Add coordinates of neighbours , dont include center point to array. Determine coordinate by the index of the neighbours.
    print i*100/len(coord)
##    k = 8 #number neighbours
    y = np.zeros((k-1,3))
    for c in range(1,k):
              #Get coordinate
              y[c-1,0] = coord[d[1][0][c],0]
              y[c-1,1] = coord[d[1][0][c],1]
              y[c-1,2] = coord[d[1][0][c],2]
    #Get information content 
    b = Normals(y) 

    #Assign information content to each point i.e xyzb
    coord[i,3] = b[0]
    coord[i,4] = b[1]
    coord[i,5] = b[2]



print 'CALCULATE NORMAL ORIENTATION'

#coord = np.random.randn(40, 6)

k=4
###Fit nearest neighbours to coordinates
neigh = NearestNeighbors(k) 
neigh.fit(coord[:,:3])

#Get the point with highest z value , this will be used as the starting point for my depth search
z_max_point = np.where(coord[:,2]==np.max(coord[:,2]))
z_max_point=int(z_max_point[0])
print z_max_point

if coord[z_max_point,5] < 0 : #ie normal doesnt point out
  coord[z_max_point,3:6]=-coord[z_max_point,3:6]
  print 'flip'
#Create a graph
G = nx.Graph() 

#Add all points and there neighbours to graph, make the weight equal to the distance between points
for i in range(0,len(coord)):


    d = neigh.kneighbors(coord[i,:3]) 

    for c in range(1,k):
        p1 = d[1][0][0]
        p2 = d[1][0][c]
        n1 = coord[d[1][0][0],3:6]
        n2 = coord[d[1][0][c],3:6]
        dot = np.dot(n1,n2)

        G.add_edge(p1,p2,weight =1-np.abs(dot))



T=nx.minimum_spanning_tree(G)

x=[]
for i in nx.dfs_edges(T,z_max_point):
    x+=i



inds = np.where(np.diff(x))[0]
out = np.split(x,inds[np.diff(inds)==1][1::2]+1)

print len(out)
for j in range(0,len(out)):
    print j*100/len(out)
    for i in range(0,len(out[j])-1):


        n1 = coord[out[j][i],3:6]

        n2 = coord[out[j][i+1],3:6]



        if np.dot(n2,n1)<0:

            coord[out[j][i+1],3:6]=-coord[out[j][i+1],3:6]




np.savetxt('horsenormalsorientated.txt',coord)