Mercurial > repos > bernhardlutz > graphconverter
changeset 2:89b606a49225 draft
Uploaded
author | bernhardlutz |
---|---|
date | Fri, 28 Feb 2014 09:46:59 -0500 |
parents | 29cc4ec84a56 |
children | b7cdfd11614a |
files | 3_molecules.gspan convert_graph.py convert_graph.xml test-data/bsp_graph.gml test-data/bsp_graph.gml~ test-data/graph2.gexf tool_dependencies.xml xgmml_networkx.py xgmml_networkx.pyc |
diffstat | 9 files changed, 542 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3_molecules.gspan Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,129 @@ +t # id 1 +v 0 O +v 1 O +v 2 O +v 3 O +v 4 C +v 5 C +v 6 C +v 7 C +v 8 C +v 9 C +v 10 C +v 11 C +v 12 C +v 13 H +v 14 H +v 15 H +v 16 H +v 17 H +v 18 H +v 19 H +v 20 H +e 0 4 1 +e 0 11 1 +e 1 10 1 +e 1 20 1 +e 2 10 2 +e 3 11 2 +e 4 5 1 +e 4 6 2 +e 5 7 2 +e 5 10 1 +e 6 8 1 +e 6 13 1 +e 7 9 1 +e 7 14 1 +e 8 9 2 +e 8 15 1 +e 9 16 1 +e 11 12 1 +e 12 17 1 +e 12 18 1 +e 12 19 1 +t # id 2 +v 0 O +v 1 O +v 2 O +v 3 O +v 4 C +v 5 C +v 6 C +v 7 C +v 8 C +v 9 C +v 10 C +v 11 C +v 12 C +v 13 H +v 14 H +v 15 H +v 16 H +v 17 H +v 18 H +v 19 H +v 20 H +e 0 4 1 +e 0 11 1 +e 1 10 1 +e 1 20 1 +e 2 10 2 +e 3 11 2 +e 4 5 1 +e 4 6 2 +e 5 7 2 +e 5 10 1 +e 6 8 1 +e 6 13 1 +e 7 9 1 +e 7 14 1 +e 8 9 2 +e 8 15 1 +e 9 16 1 +e 11 12 1 +e 12 17 1 +e 12 18 1 +e 12 19 1 +t # id 3 +v 0 O +v 1 O +v 2 O +v 3 O +v 4 C +v 5 C +v 6 C +v 7 C +v 8 C +v 9 C +v 10 C +v 11 C +v 12 C +v 13 H +v 14 H +v 15 H +v 16 H +v 17 H +v 18 H +v 19 H +v 20 H +e 0 4 1 +e 0 11 1 +e 1 10 1 +e 1 20 1 +e 2 10 2 +e 3 11 2 +e 4 5 1 +e 4 6 2 +e 5 7 2 +e 5 10 1 +e 6 8 1 +e 6 13 1 +e 7 9 1 +e 7 14 1 +e 8 9 2 +e 8 15 1 +e 9 16 1 +e 11 12 1 +e 12 17 1 +e 12 18 1 +e 12 19 1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/convert_graph.py Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# Aufruf convert_graph.py --infile datei --informat typ --outfile ausgabedatei --outformat ausgabetyp + +import sys, os +import networkx as nx +import argparse +import json + +from xgmml_networkx import XGMMLParserHelper, XGMMLWriter +from networkx.readwrite import json_graph + +#supported graph_types +graph_types = ["gml", "yaml", "gspan", "xgmml", "gexf", "graphml", "json", "pajek"] + +func_dic_read= {'gml': nx.read_gml, 'yaml':nx.read_yaml, 'gexf': nx.read_gexf, + 'graphml': nx.read_graphml, 'pajek': nx.read_pajek} + +func_dic_write= {'gml': nx.write_gml, 'yaml':nx.write_yaml, 'gexf': nx.write_gexf, + 'graphml': nx.write_graphml, 'pajek': nx.write_pajek} + +#completely supported types by networkx +completely_supported_types = ["gml", "gexf", "yaml", "graphml", "pajek"] + +def read_gspan(infile): + G = nx.Graph() + idoffset=0 + old_id_start=0 + for line in infile: + line_split=line.split(" ") + length_split=len(line_split) + if line[0] == "v": + G.add_node(idoffset, label=line_split[2].strip()) + idoffset+=1 + elif line[0] == "e": + if length_split < 3: + raise InvalidGraph(line) + elif length_split > 3: + G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label=line_split[3].strip()) + else: + G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label="") + elif line[0] == "t": + # its a new subgraph + #idoffset*=1 + old_id_start=idoffset + #print(nx.is_connected(G)) + return G + + +def write_gspan(graph, outfile): + # get all subgraphs only works with undirected + subgraphs=nx.connected_components(graph.to_undirected()) + id_count=1 + node_count=0 + #get labels + label_dic=nx.get_node_attributes(graph,'label') + for s in subgraphs: + node_count_tree=0 + node_dict={} + outfile.write("t # id "+str(id_count)+"\n") + # for every node in subgraph + for v in sorted(s): + # node id restart from 0 for every sub graph + node_dict[v]=node_count_tree + outfile.write("v "+str(node_count_tree)+" "+label_dic[v]+" \n") + node_count_tree+=1 + node_count+=1 + + # all edges adjacent to a node of s + edges=nx.edges(graph, s) + for e in sorted(edges): + #print(graph[e[0]][e[1]]) + try: + outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]])+" "+graph[e[0]][e[1]]['label']+"\n") + except KeyError: + outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]])) + + id_count+=1 +def read_json(file): + json_string=file.read() + print(json_string) + json_dict=json.loads(json_string) + print(json_dict) + return nx.Graph() + +def write_json(graph, outfile): + json_dict=json_graph.node_link_data(graph) + json_string=json.dumbs(json_dict) + outfile.write(json_string) + print("did it") + +def main( args ): + + if args.informat not in graph_types: + print "EXCEPTION COMPUTER EXPLODING" + # everything networkx can do by itself ;) + elif args.informat in completely_supported_types: + function = func_dic_read[args.informat] + graph = function(args.infile) + elif args.informat == "gspan": + graph = read_gspan(args.infile) + elif args.informat == "json": + graph = read_json(args.infile) + elif args.informat == "xgmml": + xgmml=XGMMLParserHelper() + xgmml.parseFile(args.infile) + graph=xgmml.graph() + + + + if args.outformat in completely_supported_types: + function = func_dic_write[args.outformat] + function(graph, args.outfile) + elif args.outformat == "gspan": + write_gspan(graph, args.outfile) + elif args.outformat == "json": + write_json(graph, args.outfile) + elif args.outformat == "xgmml": + #xgmml=XGMMLParserHelper(graph) + #xgmml.parseFile(open(sys.argv[1])) + a=XGMMLWriter(args.outfile, graph, "MyGraph") + +if __name__ == "__main__": + + parser = argparse.ArgumentParser() + parser.add_argument('--infile', type=argparse.FileType('r'), + help="Specify the input file representing a graph") + parser.add_argument('--outfile', type=argparse.FileType('w'), + help="Specify one output file") + parser.add_argument('--informat', type=str, + help="Specify the format of the input graph", choices = graph_types) + parser.add_argument('--outformat', type=str, + help="Specify the format of the output graph", choices = graph_types) + if len(sys.argv) < 8: + print "Too few arguments..." + parser.print_help() + exit(1) + args = parser.parse_args() + main( args )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/convert_graph.xml Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,44 @@ +<tool id="graph_converter" name="Graph Converter" version="0.1.0"> + <description>Convert between different Graphformats</description> + <version_command>echo "0.1.0"</version_command> + <requirements> + <requirement type="set_environment">GRAPHCONVERTER_SCRIPT_PATH</requirement> + </requirements> + <command> + python \$GRAPHCONVERTER_SCRIPT_PATH/convert_graph.py --infile $inputfile --informat $informat --outfile $outfile --outformat $outformat + </command> + + <inputs> + <param format="txt" name="inputfile" type="data" label="Graph to convert" /> + <param name="informat" multiple="false" type="select" label="Input Format"> + <option value="gml">GML</option> + <option value="gexf">GEXF</option> + <option value="gspan">GSPAN</option> + <option value="graphml">GRAPHML</option> + <option value="json">JSON</option> + <option value="pajek">PAJEK</option> + <option value="xgmml">XGMML</option> + <option value="yaml">YAML</option> + </param> + <param name="outformat" multiple="false" type="select" label="Output Format"> + <option value="gml">GML</option> + <option value="gexf">GEXF</option> + <option value="gspan">GSPAN</option> + <option value="graphml">GRAPHML</option> + <option value="json">JSON</option> + <option value="pajek">PAJEK</option> + <option value="xgmml">XGMML</option> + <option value="yaml">YAML</option> + </param> + </inputs> + <outputs> + <data format="txt " name="outfile" /> + </outputs> + <tests> + </tests> + <help> +**What it does** + +This tool converts a file representing a graph into another format. + </help> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/bsp_graph.gml Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,37 @@ +graph [ + comment "Das ist ein Beispielgraph." + directed 1 + id 42 + label "Graph" + node [ + id 1 + label "A" + weiteresAttribut 42 + ] + node [ + id 2 + label "B" + weiteresAttribut 43 + ] + node [ + id 3 + label "C" + weiteresAttribut 44 + ] + edge [ + source 1 + target 2 + label "Kante AB" + ] + edge [ + source 2 + target 3 + label "Kante BC" + ] + edge [ + source 3 + target 1 + label "Kante CA" + ] +] +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/bsp_graph.gml~ Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,49 @@ + Creator "yFiles" + Version 2.2 + graph + [ hierarchic 1 + directed 1 + node + [ id 0 + graphics + [ x 200.0 + y 0.0 + ] + LabelGraphics + [ text "January" ] + ] + node + [ id 1 + graphics + [ x 425.0 + y 75.0 + ] + LabelGraphics + [ text "December" ] + ] + edge + [ source 1 + target 0 + graphics + [ Line + [ point + [ x 425.0 + y 75.0 + ] + point + [ x 425.0 + y 0.0 + ] + point + [ x 200.0 + y 0.0 + ] + ] + ] + LabelGraphics + [ text "Happy New Year!" + model "six_pos" + position "head" + ] + ] + ]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/graph2.gexf Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2"> + <meta lastmodifieddate="2009-03-20"> + <creator>Gexf.net</creator> + <description>A hello world! file</description> + </meta> + <graph mode="static" defaultedgetype="directed"> + <nodes> + <node id="0" label="Hello" /> + <node id="1" label="Word" /> + </nodes> + <edges> + <edge id="0" source="0" target="1" /> + </edges> + </graph> +</gexf>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,5 @@ +<tool_dependency> + <set_environment version="1.0"> + <environment_variable name="GRAPHCONVERTER_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable> + </set_environment> +</tool_dependency>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xgmml_networkx.py Fri Feb 28 09:46:59 2014 -0500 @@ -0,0 +1,124 @@ +__author__ = "Yasunobu OKAMURA" +__copyright__ = "Copyright (c) 2012 Y.Okamura" +__license__ = "GPL v3+" + +import xml.parsers.expat +import networkx as nx + +class XGMMLParserHelper(object): + """ + """ + + def __init__(self, graph=nx.DiGraph()): + """ + + Arguments: + - `graph`: Network X graph object + """ + self._graph = graph + self._parser = xml.parsers.expat.ParserCreate() + self._parser.StartElementHandler = self._start_element + self._parser.EndElementHandler = self._end_element + self._tagstack = list() + + self._current_attr = dict() + self._current_obj = dict() + + def _start_element(self, tag, attr): + """ + + Arguments: + - `self`: + - `tag`: + - `attr`: + """ + + self._tagstack.append(tag) + + if tag == 'node' or tag == 'edge': + self._current_obj = dict(attr) + + if tag == 'att' and (self._tagstack[-2] == 'node' or self._tagstack[-2] == 'edge'): + if attr['type'] == 'string': + self._current_attr[attr['name']] = attr['value'] + elif attr['type'] == 'real': + self._current_attr[attr['name']] = float(attr['value']) + elif attr['type'] == 'integer': + self._current_attr[attr['name']] = int(attr['value']) + elif attr['type'] == 'boolean': + self._current_attr[attr['name']] = bool(attr['value']) + else: + raise NotImplementedError(attr['type']) + + def _end_element(self, tag): + """ + + Arguments: + - `self`: + - `tag`: + """ + + if tag == 'node': + self._graph.add_node(self._current_obj['id'], label=self._current_obj['label'], **self._current_attr) + #print 'add node', self._current_obj + elif tag == 'edge': + self._graph.add_edge(self._current_obj['source'], self._current_obj['target'], **self._current_attr) + + self._tagstack.pop() + + def parseFile(self, file): + """ + + Arguments: + - `self`: + - `file`: + """ + + self._parser.ParseFile(file) + + def graph(self): + """ + + Arguments: + - `self`: + """ + + return self._graph + +def XGMMLWriter(file, graph, graph_name): + """ + + Arguments: + - `graph`: + """ + + print >>file, """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<graph directed="1" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.cs.rpi.edu/XGMML"> +<att name="selected" value="1" type="boolean" /> +<att name="name" value="{0}" type="string"/> +<att name="shared name" value="{0}" type="string"/> +""".format(graph_name) + + + for onenode in graph.nodes(data=True): + id = onenode[0] + attr = dict(onenode[1]) + + if 'label' in attr: + label = attr['label'] + del attr['label'] + else: + label = id + + print >>file, '<node id="{id}" label="{label}">'.format(id=id, label=label) + for k, v in attr.iteritems(): + print >>file, '<att name="{}" value="{}" type="string" />'.format(k, v) + print >>file, '</node>' + + for oneedge in graph.edges(data=True): + print >>file, '<edge source="{}" target="{}">'.format(oneedge[0], oneedge[1]) + for k, v in oneedge[2].iteritems(): + print >>file, '<att name="{}" value="{}" type="string" />'.format(k, v) + print >>file, '</edge>' + print >>file, '</graph>' +