对代码的小改动会产生AttributeError(Python、Networkx)

2024-07-04 05:46:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我目前想扩大我的代码,以便我可以有更多的节点,因为这个项目是天文性质的。我使用的是规则的欧几里德距离,由于二次标度,这对于大的人口来说是非常缓慢的。因此,我使用了一种最近邻算法来查看某个距离内的节点。然后我制作了一个csv,即每个最近邻居节点的索引的csv。然后我尝试在这些节点之间绘制边,以便在生成的图上执行我的算法。你知道吗

但是,一旦我做了这个更改,下一小段代码就会得到AttributeError:'数字.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##

有人知道问题是什么吗?你知道吗


Tags: csv代码inposselfaddfor节点
1条回答
网友
1楼 · 发布于 2024-07-04 05:46:00

在代码中,变量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是没有意义的。这是你为你的补丁类定义的东西。你知道吗

相关问题 更多 >

    热门问题