2
|
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):
|
3
|
25 G = nx.DiGraph()
|
2
|
26 idoffset=0
|
|
27 old_id_start=0
|
|
28 for line in infile:
|
|
29 line_split=line.split(" ")
|
|
30 length_split=len(line_split)
|
|
31 if line[0] == "v":
|
|
32 G.add_node(idoffset, label=line_split[2].strip())
|
|
33 idoffset+=1
|
|
34 elif line[0] == "e":
|
|
35 if length_split < 3:
|
|
36 raise InvalidGraph(line)
|
|
37 elif length_split > 3:
|
|
38 G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label=line_split[3].strip())
|
|
39 else:
|
|
40 G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label="")
|
|
41 elif line[0] == "t":
|
|
42 # its a new subgraph
|
|
43 #idoffset*=1
|
|
44 old_id_start=idoffset
|
|
45 #print(nx.is_connected(G))
|
|
46 return G
|
|
47
|
|
48
|
|
49 def write_gspan(graph, outfile):
|
|
50 # get all subgraphs only works with undirected
|
|
51 subgraphs=nx.connected_components(graph.to_undirected())
|
|
52 id_count=1
|
|
53 node_count=0
|
|
54 #get labels
|
|
55 label_dic=nx.get_node_attributes(graph,'label')
|
|
56 for s in subgraphs:
|
|
57 node_count_tree=0
|
|
58 node_dict={}
|
|
59 outfile.write("t # id "+str(id_count)+"\n")
|
|
60 # for every node in subgraph
|
|
61 for v in sorted(s):
|
|
62 # node id restart from 0 for every sub graph
|
|
63 node_dict[v]=node_count_tree
|
|
64 outfile.write("v "+str(node_count_tree)+" "+label_dic[v]+" \n")
|
|
65 node_count_tree+=1
|
|
66 node_count+=1
|
|
67
|
|
68 # all edges adjacent to a node of s
|
|
69 edges=nx.edges(graph, s)
|
|
70 for e in sorted(edges):
|
|
71 #print(graph[e[0]][e[1]])
|
|
72 try:
|
|
73 outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]])+" "+graph[e[0]][e[1]]['label']+"\n")
|
|
74 except KeyError:
|
|
75 outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]]))
|
|
76
|
|
77 id_count+=1
|
|
78 def read_json(file):
|
|
79 json_string=file.read()
|
3
|
80 #print(json_string)
|
|
81 json_dict=json_graph.loads(json_string)
|
|
82 #print(json_dict)
|
|
83 #return json_graph.node_link_graph(json_dict, True, False)
|
|
84 return json_dict
|
|
85
|
2
|
86 def write_json(graph, outfile):
|
|
87 json_dict=json_graph.node_link_data(graph)
|
3
|
88 json_string=json_graph.dumps(json_dict)
|
2
|
89 outfile.write(json_string)
|
3
|
90 #print("did it")
|
2
|
91
|
|
92 def main( args ):
|
|
93
|
|
94 if args.informat not in graph_types:
|
|
95 print "EXCEPTION COMPUTER EXPLODING"
|
|
96 # everything networkx can do by itself ;)
|
|
97 elif args.informat in completely_supported_types:
|
|
98 function = func_dic_read[args.informat]
|
|
99 graph = function(args.infile)
|
|
100 elif args.informat == "gspan":
|
|
101 graph = read_gspan(args.infile)
|
|
102 elif args.informat == "json":
|
|
103 graph = read_json(args.infile)
|
|
104 elif args.informat == "xgmml":
|
|
105 xgmml=XGMMLParserHelper()
|
|
106 xgmml.parseFile(args.infile)
|
|
107 graph=xgmml.graph()
|
|
108
|
|
109
|
|
110
|
|
111 if args.outformat in completely_supported_types:
|
|
112 function = func_dic_write[args.outformat]
|
|
113 function(graph, args.outfile)
|
|
114 elif args.outformat == "gspan":
|
|
115 write_gspan(graph, args.outfile)
|
|
116 elif args.outformat == "json":
|
|
117 write_json(graph, args.outfile)
|
|
118 elif args.outformat == "xgmml":
|
|
119 #xgmml=XGMMLParserHelper(graph)
|
|
120 #xgmml.parseFile(open(sys.argv[1]))
|
|
121 a=XGMMLWriter(args.outfile, graph, "MyGraph")
|
|
122
|
|
123 if __name__ == "__main__":
|
|
124
|
|
125 parser = argparse.ArgumentParser()
|
|
126 parser.add_argument('--infile', type=argparse.FileType('r'),
|
|
127 help="Specify the input file representing a graph")
|
|
128 parser.add_argument('--outfile', type=argparse.FileType('w'),
|
|
129 help="Specify one output file")
|
|
130 parser.add_argument('--informat', type=str,
|
|
131 help="Specify the format of the input graph", choices = graph_types)
|
|
132 parser.add_argument('--outformat', type=str,
|
|
133 help="Specify the format of the output graph", choices = graph_types)
|
|
134 if len(sys.argv) < 8:
|
|
135 print "Too few arguments..."
|
|
136 parser.print_help()
|
|
137 exit(1)
|
|
138 args = parser.parse_args()
|
|
139 main( args )
|