对代码的小改动给出了AttributeError(Python,Networkx)

时间:2017-02-14 16:19:12

标签: python algorithm python-2.7 simulation networkx

我目前正在做一个项目,该项目涉及使用networkx制作一个基于csv文件的节点位置的无向图,以及具有各种属性的类补丁,以指示它们的位置,状态和每个节点的每个最近邻居的索引。然后在这组节点上执行移民算法。

我目前想要扩展我的代码,因此我可以拥有更多节点,因为这个项目本质上是天文数字。我使用的是规则欧几里德距离,由于二次尺度缩放,这对于大群体来说非常慢。因此,我使用最近邻算法来查看某个距离内的节点。然后我制作了这些的csv,即每个最近邻居节点的索引的csv。然后我尝试在这些节点之间绘制边缘,以便在结果图上执行我的算法。

然而,一旦我做了这个改变,下一个小代码块就会得到一个AttributeError:'numpy.int64'对象没有属性'pos'。

我的代码如下,欧几里德距离计算被注释掉以供比较。正在破坏的代码块是紧接在边缘绘制块之后的代码块,它是随后的算法的一种设置。我为愚蠢的变量名称道歉 - boba是最近的邻居节点索引,bubu是有边缘绘制的节点的索引。

我在这里难倒,因为我不知道改变是如何连接的,或者为什么它会引发这个错误。

nearand = np.genfromtxt('/Users/Skippy/nearand.csv', delimiter = ',',usecols=np.arange(0, 3)) # 3D cartesian co-ordinates of stars


density = 0.14 #Stellar density per cubic parsec
L = 100 # Size of box

Patches = int(0.056*density*L**3+15)


P_init = 0.0001   # Probability that a patch will be occupied at the beginning
Distance = 10 # An arbitrary parameter to determine which patches are     connected

xcoord = nearand[:,0]
ycoord = nearand[:,1]
zcoord = nearand[:,2]

bub = np.asarray(np.linspace(0, Patches, Patches, dtype = 'int'))
print bub
bobbington = np.asarray(np.genfromtxt('bobbington2.csv', delimiter = ',')) # csv of nearest neighbour node indices
bobbington0 = bobbington[:,0]
bobbington1 = bobbington[:,1]
bobbington2 = bobbington[:,2]
bobbington3 = bobbington[:,3]
bobbington4 = bobbington[:,4]
bobbington5 = bobbington[:,5]
bobbington6 = bobbington[:,6]
bobbington7 = bobbington[:,7]
bobbington8 = bobbington[:,8]
bobbington9 = bobbington[:,9]
bobbington10 = bobbington[:,10]
bobbington11 = bobbington[:,11]
bobbington12 = bobbington[:,12]


class patch:
    def __init__(self,status=0,pos=(0,0,0),boba = (0,0,0,0,0,0,0,0,0,0,0,0,0,),bubu = 0):
    self.status = status
    self.pos = pos
    self.boba = boba
    self.bubu = bubu
def __str__(self):
    return(str(self.status))

G = nx.Graph()

for i in xrange(Patches):

    Stat = 1 if np.random.uniform() < P_init else 0
    Pos  = (xcoord[i], ycoord[i], zcoord[i])
    Bob = (bobbington0[i],bobbington1[i],bobbington2[i],bobbington3[i],bobbington4[i],bobbington5[i],bobbington6[i],bobbington7[i],bobbington8[i],
bobbington9[i],bobbington10[i],bobbington11[i],bobbington12[i])
    Bubu = bub[i]
    G.add_node(patch(Stat,Pos,Bob,Bubu))

#for p1 in G.nodes():
    #for p2 in G.nodes():
        #Dist = np.sqrt((p1.pos[2] - p2.pos[2])**2 + (p1.pos[1]-p2.pos[1])**2+(p1.pos[0]-p2.pos[0])**2)
        #if Dist <= p1.dist:
        #if Dist <= Distance:
            #G.add_edge(p1,p2)

for i in G.nodes():
    edge1= (i.bubu,i.boba[0])
    edge2= (i.bubu,i.boba[1])
    edge3= (i.bubu,i.boba[2])
    edge4= (i.bubu,i.boba[3])
    edge5= (i.bubu,i.boba[4])
    edge6= (i.bubu,i.boba[5])
    edge7= (i.bubu,i.boba[6])
    edge8= (i.bubu,i.boba[7])
    edge9= (i.bubu,i.boba[8])
    edge10= (i.bubu,i.boba[9])
    edge11= (i.bubu,i.boba[10])
    edge12= (i.bubu,i.boba[11])
    edge13= (i.bubu,i.boba[12])

    G.add_edge(*edge1)
    G.add_edge(*edge2)
    G.add_edge(*edge3)
    G.add_edge(*edge4)
    G.add_edge(*edge5)
    G.add_edge(*edge6)
    G.add_edge(*edge7)
    G.add_edge(*edge8)
    G.add_edge(*edge9)
    G.add_edge(*edge10)
    G.add_edge(*edge11)
    G.add_edge(*edge12)
    G.add_edge(*edge13)

pos = {} # THE BIT CAUSING ISSUES
for i in G.nodes():
    pos[i] = i.pos


occup = [n.status for n in G]   # THIS ALSO HAS NO 'status' ATTRIBUTE ERROR

Time = [0]
Occupancy = [np.sum([n.status for n in G])/float(Patches)]

##Then algorithm goes here##

任何人都可以看到问题所在吗?

1 个答案:

答案 0 :(得分:2)

在您的代码中,变量bub在我看来是一个numpy数组。

在执行G.add_node(patch(...))时创建的每个补丁中,参数Bubu设置为来自bub的条目。所以这是一个数字。然后,对于该补丁,self.bubu是一个数字。

添加边缘时,您需要将i.bubu的边添加到i.boba[some index]。因此,i.bubu是一个数字,您可以从这些数字创建边缘到其他数字。这将导致networkx添加那些数字的节点。

因此,在进入循环for i in G.nodes(): pos[i] = i.pos之前,G中的许多节点都是数字而不是补丁。

对于这些数字,引用number.pos没有意义。这是你为补丁类定义的东西。