2017-02-18 81 views
0

-------- -------- companytrades.csv試圖實現networkx到graphicsscene

date,Company1,Company2 
1/2/2017,1001,1111 
1/3/2017,1001,1100 
1/4/2017,1111,1001 
1/5/2017,1100,1001 
1/6/2017,1011,1001 
1/7/2017,1001,1111 
1/8/2017,1111,1100 
1/9/2017,1100,1011 

,我發現了一組實施networkx類的這個很好的例子成graphicsscene GraphNetworkX.py

Node.py

Edge.py

我使用下面的代碼來實現這三個模塊

--------- -------- netxgui.py

import pandas as pd 
from numpy import * 
import sys 
from math import * 
from PyQt4 import QtCore,QtGui 
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
import networkx as nx 
import GraphNetworkX 
from x import * 

pf = pd.read_csv('/desktop/companytrades.csv',header=0,index_col=['date']) 

#setup the needed variables 
if __names__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    app.setStyle("Plastique") 

class netx(QtGui.QDialog): 
    def __init__(self): 
     super(netx,self).__init__() 
     self.uiNX = Ui_Form() 
     self.uiNX.setupUi(self) 
     G = nx.from_pandas_dataframe(pf,'Company1','Company2',create_using=nx.DiGraph()) 
Pos = nx.spring_layout(g,scale=300) 
scene = GraphNetworkx.GraphGraphicsScene(G=g,pos=Pos) 
self.uiNX.neworkx_graphicsView.setScene(scene) 

NX = netx() 
NX.show() 
sys.exit(exit(app.exec_()) 
app.deleteLater() 

------- ------- x.py

from PyQt4 import QtCore,QtGui 
try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.unicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(1492,1029) 
     self.networkx_graphicsView = QtGui.QGraphicsView(Form) 
     self.networkx_graphicsView.setGeometry(QtCore.QRect(240,70,971,911)) 
     sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) 
     sizePolicy.setHorizontalStretch(0) 
     sizePolicy.setVerticalStretch(5) 
     sizePolicy.setHeightForWidth(self.networkx_graphicsView.sizePolicy().hasHeightForWidth()) 
     self.networkx_graphicsView.setSizePolicy(sizePolicy) 
     self.networkx_graphicsView.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) 
     self.networkx_graphicsView.setObjectName(_fromUtf8("networkx_graphicsView")) 
     self.retranslateUi(Form) 
     QtCore.QMetaObject.connectSlotsByName(Form) 
    def retranslateUi(self,Form): 
     Form.setWindowTitle(_translate("Form","Form",None)) 

我在執行此操作時遇到問題。我遇到的問題是在GraphNetworkX下的drawGraph函數。看來打印功能與n正常工作,但我真的不明白用i替換它的理由。

i = 0 
    for n in G.nodes(): 
     #print str(pos[n][0]) + " " + str(pos[n][1])+ " "+str(n) 
     self.addNode(pos[i][0], pos[i][1], n) 
     i += 1 

我做了一個實驗,用n代替i,並添加節點似乎工作,但後來我添加邊的問題。

for e in G.edges(): 
     node1 = self.nodes[e[0]] 
     node2 = self.nodes[e[1]] 
     self.addEdge(node1, node2) 

它口口聲聲說E [0]出range..considering的Nodes.py功能是把self.nodes成Nodes.nodes對象的名單,我想這也許就是佔位符的每個對象,但我不太確定。然後我試圖使這一變化:

node1 = e[0] 
node2 = e[1] 

但後來我得到的錯誤在Edge.py指出

x1 = node1.centerX() 

的整數不具備的功能的centerX()

我有點不知所措。有人能幫助我理解這一點,並幫助我實現這個目標嗎?

回答

1

更簡單的問題可能有助於獲得答案。 (例如,您的x.py似乎與最後的問題似乎沒有關係)

爲了讓您開始,您可能會遭受某些命名空間重載(重命名相同的對象)。在netxgui.py你有import networkx as nx但後來幾行你宣佈一個類nx(QtGui.QDialog)。在此之後,您嘗試撥打nx.spring_layout(),您大概想要從networkx而不是您自定義的NX類。您嘗試訪問的結構可能存在於networkx Graph實例中,但不在QDialog實例中?

一般來說,避免使用from examplemodule import *會讓您在追蹤錯誤或開發代碼時更輕鬆。

--- 編輯 ---

我仔細看了看,問題是因爲在networkx.Graph/.DiGraph等,並在GraphGraphicsScene使用的不同表示。 Networkx使用下面的字典,用於存儲節點(以及邊)。 GraphGraphicsScene使用節點的列表

快速和骯髒的解決方案:這些發生是兼容的如果您的節點標記從0開始,並有一個連續的序列。

稍微更穩健。將生成自己的圖形這樣的,它可以實現如下:

def re_index_digraph(G_orig): 
    remap = {i: n for (i,n) in enumerate(G_orig.nodes())} 
    inv_remap = {v:k for k,v in remap.items()} 
    G = nx.DiGraph() 
    G.add_nodes_from(remap) 
    remap_edges = [(inv_remap[src], inv_remap[sink]) for (src, sink) in G_orig.edges()] 
    G.add_edges_from(remap_edges) 
    return G 

G_orig = nx.from_pandas_dataframe(pf,'Company1','Company2',create_using=nx.DiGraph()) 
G = re_index_digraph(G_orig) 

再進行與G你面前了。 (如果除了簡單的節點名稱和邊緣之外還有屬性,則需要相應地擴展重映射函數)。

除此之外,您可能需要投入大量精力開發GraphGraphicsScene實現。

+0

感謝您的意見。我會做更正,並得到結果..我懷疑這將完全解決問題。我添加了x.py,因爲我正在處理PyQt4,並且從經驗中知道,處理GUI的環境和組件的行爲會發生變化。 – Daniel

+0

仍然無法正常工作..請幫助。 – Daniel

+0

什麼不行?我想你沒有使用上面的確切代碼(netxgui.py),因爲它不能按原樣運行。但是,如果你是,至少你需要做一些縮進的類定義 – Bonlenfum