Created
November 23, 2012 02:33
-
-
Save fannix/4133774 to your computer and use it in GitHub Desktop.
networkX and graph util
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
convert an edge list to the graph VNA format. | |
VNA format can be read and visualized by Gephi | |
""" | |
import sys | |
def edge_list_to_vna(edge_list): | |
"""edge_list is a list of 3-tuples: [from, to, weight] | |
""" | |
node_set = set() | |
for from_node, to_node, weight in edge_list: | |
node_set.add(from_node) | |
node_set.add(to_node) | |
return node_set, edge_list | |
def output_vna(vna_node, vna_edge): | |
"""Output the node and edge to the appropriate vna format | |
""" | |
print "*node data" | |
print "ID" | |
for node in vna_node_set: | |
print node | |
print "*tie data\nfrom to strength" | |
for from_node, to_node, weight in edge_list: | |
print from_node, to_node, weight | |
if __name__ == "__main__": | |
edge_list = [] | |
for line in sys.stdin: | |
li = line.strip().split() | |
from_node = li[0] | |
to_node = li[1] | |
weight = li[2] | |
edge_list.append((from_node, to_node, weight)) | |
vna_node_set, vna_edge = edge_list_to_vna(edge_list) | |
output_vna(vna_node_set, vna_edge) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://networkx.lanl.gov/reference/generated/networkx.readwrite.edgelist.read_edgelist.html#networkx.readwrite.edgelist.read_edgelist | |
textline = '1 2 3' | |
fh = open('test.edgelist','w') | |
d = fh.write(textline) | |
fh.close() | |
G = nx.read_edgelist('test.edgelist', nodetype=int, data=(('weight',float),)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment