我看到一种让我发疯并且我不理解的效果: 我从6列SQL Server 2012数据库表(nodeID,string(Class),float(xCoordinate),float(ycoordinate),stringZone,char(参考))中获取数据,我想使用这些实体创建这些实体的网络图networkx Python包。对我来说重要的是我确实有几个带有预定义坐标的网络节点,我想稍后修复它们,其余部分将使用Gephi进行布局。
这是我的代码:
import pyodbc
import networkx as nx
dbConnection = pyodbc.connect('DB-Connection String')
dbCursor = dbConnection.cursor()
theGraph = nx.Graph()
theGraph.position={}
theGraph.deviceClass={}
queryStatement = "SELECT ComponentID, DeviceClass, CoordX, CoordY, AcceleratorZone, Reference FROM dbo.vComponentsWithZones WHERE Reference IN ('M','T')"
for dbRow in dbCursor.execute(queryStatement):
nodeID = dbRow[0]
theGraph.add_node(nodeID)
deviceClass = dbRow[1]
coordX = (dbRow[2] - 367421.373) / 100
coordY = (dbRow[3] - 230238.784) / 100
accelZone = dbRow[4]
if coordX != None and coordY != None:
print nodeID, coordX, coordY <-------------------------
theGraph.node[nodeID]["viz"] = {'position': {'x': coordX, 'y': coordY, 'z': 0}}
if accelZone != None:
theGraph.node[nodeID]["AccelZone"] = accelZone
if dbRow[1] != None:
theGraph.node[nodeID]["DeviceClass"] = deviceClass
if deviceClass == "Controls":
theGraph.node[nodeID]["viz"] = {'color': {'r': "255", 'g': "0", 'b': "0", 'a': "1"}}
elif deviceClass == "Magnet":
theGraph.node[nodeID]["viz"] = {'color': {'r': "255", 'g': "200", 'b': "0", 'a': "1"}}
elif deviceClass == "Vacuum":
theGraph.node[nodeID]["viz"] = {'color': {'r': "0", 'g': "0", 'b': "255", 'a': "1"}}
queryStatement = "SELECT * FROM dbo.ComponentConnections"
for dbRow in dbCursor.execute(queryStatement):
theGraph.add_edge(dbRow[0],dbRow[1], connectionTyp=dbRow[2])
dbConnection.close()
nx.write_gexf(theGraph,"cryringTopology.gexf")
一切似乎工作正常,看着印刷声明的输出,事情似乎也运作良好;我得到nodeID,coordX,coordY的值,无论数据库中的条目是什么,程序都会评估语句
if coordX != None and coordY != None:
为true并正确执行其中的代码。
然而,输出到GEXF文件只产生6个正确分配坐标的条目,它应该是40到50之类的东西。似乎创建了&#34;位置&#34;节点属性在某些情况下失败,我完全迷失了。
任何想法???
答案 0 :(得分:0)
您在此行中存储的内容
theGraph.node[nodeID]["viz"] = {'position': {'x': coordX, 'y': coordY, 'z': 0}}
被这些行覆盖
theGraph.node[nodeID]["viz"] = {'color': {'r': "255", 'g': "0", 'b': "0", 'a': "1"}}