-------- companytrades.csv --------
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
I found this great example of a set of classes implementing a networkx into a graphicsscene GraphNetworkX.py
I am using the following code to implement these three modules
--------- 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))
I am having issues implementing this. The issue I'm having is in GraphNetworkX under the drawGraph function. It seems the print function is working correctly with n but I really don't understand the reasoning for replacing it with 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
I did an experiment replacing i with n and adding the nodes seemed to work but then I have issues with adding the edges.
for e in G.edges():
node1 = self.nodes[e[0]]
node2 = self.nodes[e[1]]
self.addEdge(node1, node2)
It keeps saying that e[0] is out of range..considering the Nodes.py function is to turn self.nodes into a list of Nodes.nodes objects, I thought maybe that is a placeholder inside each of the objects but I'm not quite sure. I then tried making this change:
node1 = e[0]
node2 = e[1]
but then I get the error in Edge.py stating that
x1 = node1.centerX()
an integer does not have the function centerX()
I'm a bit at a loss. Could someone help me understand this and help me get this to work?