0
|
1 #!/usr/bin/env python
|
|
2 # Aufruf convert_graph.py --infile datei --informat typ --outfile ausgabedatei --outformat ausgabetyp
|
|
3
|
|
4 import sys, os
|
|
5 import networkx as nx
|
|
6 import argparse
|
|
7 import json
|
|
8
|
|
9 from xgmml_networkx import XGMMLParserHelper, XGMMLWriter
|
|
10 from networkx.readwrite import json_graph
|
|
11
|
|
12 #supported graph_types
|
|
13 graph_types = ["gml", "yaml", "gspan", "xgmml", "gexf", "graphml", "json", "pajek"]
|
|
14
|
|
15 func_dic_read= {'gml': nx.read_gml, 'yaml':nx.read_yaml, 'gexf': nx.read_gexf,
|
|
16 'graphml': nx.read_graphml, 'pajek': nx.read_pajek}
|
|
17
|
|
18 func_dic_write= {'gml': nx.write_gml, 'yaml':nx.write_yaml, 'gexf': nx.write_gexf,
|
|
19 'graphml': nx.write_graphml, 'pajek': nx.write_pajek}
|
|
20
|
|
21 #completely supported types by networkx
|
|
22 completely_supported_types = ["gml", "gexf", "yaml", "graphml", "pajek"]
|
|
23
|
|
24 def read_gspan(infile):
|
|
25 G = nx.Graph()
|
|
26 idoffset=0
|
|
27 old_id_start=0
|
|
28 for line in infile:
|
|
29 line_split=line.split(" ")
|
|
30 if line[0] == "v":
|
|
31 G.add_node(idoffset, label=line_split[2].strip())
|
|
32 idoffset+=1
|
|
33 elif line[0] == "e":
|
|
34 G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label=line_split[3].strip())
|
|
35 elif line[0] == "t":
|
|
36 # its a new subgraph
|
|
37 #idoffset*=1
|
|
38 old_id_start=idoffset
|
|
39 #print(nx.is_connected(G))
|
|
40 return G
|
|
41
|
|
42
|
|
43 def write_gspan(graph, outfile):
|
|
44 # get all subgraphs only works with undirected
|
|
45 subgraphs=nx.connected_components(graph.to_undirected())
|
|
46 id_count=1
|
|
47 node_count=0
|
|
48 #get labels
|
|
49 label_dic=nx.get_node_attributes(graph,'label')
|
|
50 for s in subgraphs:
|
|
51 node_count_tree=0
|
|
52 node_dict={}
|
|
53 outfile.write("t # id "+str(id_count)+"\n")
|
|
54 # for every node in subgraph
|
|
55 for v in sorted(s):
|
|
56 # node id restart from 0 for every sub graph
|
|
57 node_dict[v]=node_count_tree
|
|
58 outfile.write("v "+str(node_count_tree)+" "+label_dic[v]+" \n")
|
|
59 node_count_tree+=1
|
|
60 node_count+=1
|
|
61
|
|
62 # all edges adjacent to a node of s
|
|
63 edges=nx.edges(graph, s)
|
|
64 for e in sorted(edges):
|
|
65 #print(graph[e[0]][e[1]])
|
|
66 outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]])+" "+graph[e[0]][e[1]]['label']+"\n")
|
|
67
|
|
68 id_count+=1
|
|
69 def read_json(file):
|
|
70 json_string=file.read()
|
|
71 print(json_string)
|
|
72 json_dict=json.loads(json_string)
|
|
73 print(json_dict)
|
|
74 return nx.Graph()
|
|
75
|
|
76 def write_json(graph, outfile):
|
|
77 json_dict=json_graph.node_link_data(graph)
|
|
78 json_string=json.dumbs(json_dict)
|
|
79 outfile.write(json_string)
|
|
80 print("did it")
|
|
81
|
|
82 def main( args ):
|
|
83
|
|
84 if args.informat not in graph_types:
|
|
85 print "EXCEPTION COMPUTER EXPLODING"
|
|
86 # everything networkx can do by itself ;)
|
|
87 elif args.informat in completely_supported_types:
|
|
88 function = func_dic_read[args.informat]
|
|
89 graph = function(args.infile)
|
|
90 elif args.informat == "gspan":
|
|
91 graph = read_gspan(args.infile)
|
|
92 elif args.informat == "json":
|
|
93 graph = read_json(args.infile)
|
|
94 elif args.informat == "xgmml":
|
|
95 xgmml=XGMMLParserHelper()
|
|
96 xgmml.parseFile(args.infile)
|
|
97 graph=xgmml.graph()
|
|
98
|
|
99
|
|
100
|
|
101 if args.outformat in completely_supported_types:
|
|
102 function = func_dic_write[args.outformat]
|
|
103 function(graph, args.outfile)
|
|
104 elif args.outformat == "gspan":
|
|
105 write_gspan(graph, args.outfile)
|
|
106 elif args.outformat == "json":
|
|
107 write_json(graph, args.outfile)
|
|
108 elif args.outformat == "xgmml":
|
|
109 #xgmml=XGMMLParserHelper(graph)
|
|
110 #xgmml.parseFile(open(sys.argv[1]))
|
|
111 a=XGMMLWriter(args.outfile, graph, "MyGraph")
|
|
112
|
|
113 if __name__ == "__main__":
|
|
114
|
|
115 parser = argparse.ArgumentParser()
|
|
116 parser.add_argument('--infile', type=argparse.FileType('r'),
|
|
117 help="Specify the input file representing a graph")
|
|
118 parser.add_argument('--outfile', type=argparse.FileType('w'),
|
|
119 help="Specify one output file")
|
|
120 parser.add_argument('--informat', type=str,
|
|
121 help="Specify the format of the input graph", choices = graph_types)
|
|
122 parser.add_argument('--outformat', type=str,
|
|
123 help="Specify the format of the output graph", choices = graph_types)
|
|
124 if len(sys.argv) < 8:
|
|
125 print "Too few arguments..."
|
|
126 parser.print_help()
|
|
127 exit(1)
|
|
128 args = parser.parse_args()
|
|
129 main( args )
|