def writepajek(A,filename): SP = np_to_scipy_matrix(A) edges = [] for i in range(0,A.shape[0]): for j in range(0,A.shape[1]): if A[i,j]!=0: edges.append(['A'+str(i),'B'+str(j)]) G = nx.bipartite.from_biadjacency_matrix(SP,create_using=None) nx.write_pajek(G, filename + '.net')
def writepajek(A, filename): SP = np_to_scipy_matrix(A) edges = [] for i in range(0, A.shape[0]): for j in range(0, A.shape[1]): if A[i, j] != 0: edges.append(['A' + str(i), 'B' + str(j)]) G = nx.bipartite.from_biadjacency_matrix(SP, create_using=None) nx.write_pajek(G, filename + '.net')
def drawbipartite2(A): from matplotlib.path import Path # Create the nx bipartite graph SP = np_to_scipy_matrix(A) B = nx.bipartite.from_biadjacency_matrix(SP,create_using=None) # Compute the nodes positions X,Y = nx.bipartite.sets(B) pos = dict() nx.draw(B,pos=pos) plt.show()
def drawbipartite2(A): from matplotlib.path import Path # Create the nx bipartite graph SP = np_to_scipy_matrix(A) B = nx.bipartite.from_biadjacency_matrix(SP, create_using=None) # Compute the nodes positions X, Y = nx.bipartite.sets(B) pos = dict() nx.draw(B, pos=pos) plt.show()
def drawbipartite(A): # Create the nx bipartite graph SP = np_to_scipy_matrix(A) B = nx.bipartite.from_biadjacency_matrix(SP, create_using=None) m = nx.number_of_edges(B) n = nx.number_of_nodes(B) B = nx.convert_node_labels_to_integers(B) #B = nx.relabel_nodes(B,lambda x: x+1) # Compute the nodes positions nodesleft, nodesright = nx.bipartite.sets(B) n1, n2 = len(nodesleft), len(nodesright) nLeft = len(nodesleft) nRight = len(nodesright) nodepos = dict() nodepos.update([n, (-1, max(nLeft, nRight) - i)] for i, n in enumerate(nodesleft)) nodepos.update([n, (1, max(nLeft, nRight) - i)] for i, n in enumerate(nodesright)) # Do the node position permutation lines = [] for e in B.edges(): x0 = nodepos[e[0]][0] y0 = nodepos[e[0]][1] x1 = nodepos[e[1]][0] y1 = nodepos[e[1]][1] lines.append(np.array([x0, y0, x1, y1])) lines = np.array(lines) nx.draw( B, pos=nodepos, with_labels=True #edge_color=np.random.random(nx.number_of_edges(B)), #edge_cmap=plt.get_cmap('Blues'), #node_color=np.random.random(nx.number_of_nodes(B)) #cmap=plt.get_cmap('Reds') ) plt.show() print fi.num_crosses(lines)
def drawbipartite(A): # Create the nx bipartite graph SP = np_to_scipy_matrix(A) B = nx.bipartite.from_biadjacency_matrix(SP,create_using=None) m = nx.number_of_edges(B) n = nx.number_of_nodes(B) B = nx.convert_node_labels_to_integers(B) #B = nx.relabel_nodes(B,lambda x: x+1) # Compute the nodes positions nodesleft,nodesright = nx.bipartite.sets(B) n1,n2 = len(nodesleft),len(nodesright) nLeft = len(nodesleft) nRight = len(nodesright) nodepos = dict() nodepos.update( [n,(-1,max(nLeft,nRight)-i) ] for i,n in enumerate(nodesleft)) nodepos.update( [n,(1,max(nLeft,nRight)-i) ] for i,n in enumerate(nodesright)) # Do the node position permutation lines = [] for e in B.edges(): x0 = nodepos[e[0]][0] y0 = nodepos[e[0]][1] x1 = nodepos[e[1]][0] y1 = nodepos[e[1]][1] lines.append( np.array([x0,y0,x1,y1] ) ) lines = np.array(lines) nx.draw(B,pos=nodepos, with_labels=True #edge_color=np.random.random(nx.number_of_edges(B)), #edge_cmap=plt.get_cmap('Blues'), #node_color=np.random.random(nx.number_of_nodes(B)) #cmap=plt.get_cmap('Reds') ) plt.show() print fi.num_crosses(lines)
print self.nodepos nx.draw(self.graph,self.nodepos,with_labels=True) plt.show() def info(self): print("Current crossings = %d") % self.energy() for n,xy in self.nodepos.iteritems(): print("Node %d x=%d y=%d") % (n,xy[0],xy[1]) for r in self.lines: print r if __name__ == '__main__': filename = sys.argv[1] A = np.loadtxt(filename,delimiter=',') G=nx.bipartite.from_biadjacency_matrix(np_to_scipy_matrix(A)) #G = nx.bipartite.from_biadjacency_matrix(np.loadtxt(filename, delimiter=',')) # plt.subplots(2,sharex=True) # for i in range(0,nrows): # plt.plot([A[i,0],A[i,2]],[A[i,1],A[i,3]]) # points = crosses(A) # plt.plot([p.x for p in points],[p.y for p in points],'ko') # plt.draw() # plt.show() # init_state = range(0,nrows)