Mercurial > repos > bernhardlutz > graphconverter
changeset 0:fe01a054a6b2 draft
Uploaded
author | bernhardlutz |
---|---|
date | Fri, 28 Feb 2014 08:57:44 -0500 |
parents | |
children | 29cc4ec84a56 |
files | 3_molecules.gspan convert_graph.py convert_graph.xml deps.txt gspantest2 jsonex jsonwiki neu.gml neu.xgmml neu.xml neu2.gml test-data/bsp_graph.gml test-data/bsp_graph.gml~ test-data/graph2.gexf test.gexf test.gml test.gspan test.json test.xgmml test.xml test.yaml tool_dependencies.xml xgmml_networkx.py xgmml_networkx.pyc |
diffstat | 23 files changed, 1693 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 08:57:44 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 08:57:44 2014 -0500 @@ -0,0 +1,129 @@ +#!/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(" ") + if line[0] == "v": + G.add_node(idoffset, label=line_split[2].strip()) + idoffset+=1 + elif line[0] == "e": + G.add_edge(old_id_start+int(line_split[1]), old_id_start+int(line_split[2]), label=line_split[3].strip()) + 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]]) + outfile.write("e "+str(node_dict[e[0]])+" "+str(node_dict[e[1]])+" "+graph[e[0]][e[1]]['label']+"\n") + + 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 08:57:44 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="pajek">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="pajek">XGMML</option> + <option value="yaml">YAML</option> + </param> + </inputs> + <outputs> + <data format="txt " name="output" /> + </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/deps.txt Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,1 @@ +libyaml->pyyaml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gspantest2 Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,18 @@ +t # id 1 +v 0 O +v 3 O +e 0 3 1 +t # id 2 +v 0 O +v 1 O +v 2 O +v 3 O +e 0 1 1 +e 1 2 1 +e 1 3 1 + +t # id 3 +v 0 O +v 1 O +e 0 1 1 +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jsonex Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,19 @@ +{ + "id": "root", + "children": [{ + id: "n1", + labels: [ { text: "n1" } ], + width: 100, + height: 100, + },{ + id: "n2", + labels: [ { text: "n2" } ], + width: 100, + height: 50 + }], + "edges": [{ + id: "e1", + source: "n1", + target: "n2" + }] +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jsonwiki Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,16 @@ +{ + "Herausgeber": "Xema", + "Nummer": "1234-5678-9012-3456", + "Deckung": 2e+6, + "Währung": "EURO", + "Inhaber": { + "Name": "Mustermann", + "Vorname": "Max", + "männlich": true, + "Hobbys": [ "Reiten", "Golfen", "Lesen" ], + "Alter": 42, + "Kinder": [], + "Partner": null + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neu.gml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,18 @@ +graph [ + directed 1 + edge_default [ + ] + node [ + id 0 + label "Word" + ] + node [ + id 1 + label "Hello" + ] + edge [ + source 1 + target 0 + id "0" + ] +]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neu.xgmml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,28 @@ +<?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="xgmml" type="string"/> +<att name="shared name" value="xgmml" type="string"/> + +<node id="1" label="A"> +<att name="weiteresAttribut" value="42" type="string" /> +<att name="id" value="1" type="string" /> +</node> +<node id="2" label="B"> +<att name="weiteresAttribut" value="43" type="string" /> +<att name="id" value="2" type="string" /> +</node> +<node id="3" label="C"> +<att name="weiteresAttribut" value="44" type="string" /> +<att name="id" value="3" type="string" /> +</node> +<edge source="1" target="2"> +<att name="label" value="Kante AB" type="string" /> +</edge> +<edge source="2" target="3"> +<att name="label" value="Kante BC" type="string" /> +</edge> +<edge source="3" target="1"> +<att name="label" value="Kante CA" type="string" /> +</edge> +</graph>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neu.xml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,6 @@ +<?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="graph" type="string"/> +<att name="shared name" value="graph" type="string"/> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/neu2.gml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,39 @@ +graph [ + directed 1 + node [ + id 1 + label "A" + weiteresAttribut "42" + ] + node [ + id 3 + label "C" + weiteresAttribut "44" + ] + node [ + id 2 + label "B" + weiteresAttribut "43" + ] + edge [ + source 1 + target 2 + weiteresAttribut "44" + id "3" + label "Kante AB" + ] + edge [ + source 3 + target 1 + weiteresAttribut "44" + id "3" + label "Kante CA" + ] + edge [ + source 2 + target 3 + weiteresAttribut "44" + id "3" + label "Kante BC" + ] +]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/bsp_graph.gml Fri Feb 28 08:57:44 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 08:57:44 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 08:57:44 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/test.gexf Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,389 @@ +<?xml version="1.0" encoding="utf-8"?><gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance"> + <graph defaultedgetype="undirected" mode="static"> + <attributes class="edge" mode="static"> + <attribute id="0" title="label" type="string" /> + </attributes> + <nodes> + <node id="0" label="O" /> + <node id="1" label="O" /> + <node id="2" label="O" /> + <node id="3" label="O" /> + <node id="4" label="C" /> + <node id="5" label="C" /> + <node id="6" label="C" /> + <node id="7" label="C" /> + <node id="8" label="C" /> + <node id="9" label="C" /> + <node id="10" label="C" /> + <node id="11" label="C" /> + <node id="12" label="C" /> + <node id="13" label="H" /> + <node id="14" label="H" /> + <node id="15" label="H" /> + <node id="16" label="H" /> + <node id="17" label="H" /> + <node id="18" label="H" /> + <node id="19" label="H" /> + <node id="20" label="H" /> + <node id="21" label="O" /> + <node id="22" label="O" /> + <node id="23" label="O" /> + <node id="24" label="O" /> + <node id="25" label="C" /> + <node id="26" label="C" /> + <node id="27" label="C" /> + <node id="28" label="C" /> + <node id="29" label="C" /> + <node id="30" label="C" /> + <node id="31" label="C" /> + <node id="32" label="C" /> + <node id="33" label="C" /> + <node id="34" label="H" /> + <node id="35" label="H" /> + <node id="36" label="H" /> + <node id="37" label="H" /> + <node id="38" label="H" /> + <node id="39" label="H" /> + <node id="40" label="H" /> + <node id="41" label="H" /> + <node id="42" label="O" /> + <node id="43" label="O" /> + <node id="44" label="O" /> + <node id="45" label="O" /> + <node id="46" label="C" /> + <node id="47" label="C" /> + <node id="48" label="C" /> + <node id="49" label="C" /> + <node id="50" label="C" /> + <node id="51" label="C" /> + <node id="52" label="C" /> + <node id="53" label="C" /> + <node id="54" label="C" /> + <node id="55" label="H" /> + <node id="56" label="H" /> + <node id="57" label="H" /> + <node id="58" label="H" /> + <node id="59" label="H" /> + <node id="60" label="H" /> + <node id="61" label="H" /> + <node id="62" label="H" /> + </nodes> + <edges> + <edge id="0" source="0" target="11"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="1" source="0" target="4"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="2" source="1" target="10"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="3" source="1" target="20"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="4" source="2" target="10"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="5" source="3" target="11"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="6" source="4" target="5"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="7" source="4" target="6"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="8" source="5" target="10"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="9" source="5" target="7"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="10" source="6" target="8"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="11" source="6" target="13"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="12" source="7" target="9"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="13" source="7" target="14"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="14" source="8" target="9"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="15" source="8" target="15"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="16" source="9" target="16"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="17" source="11" target="12"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="18" source="12" target="19"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="19" source="12" target="17"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="20" source="12" target="18"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="21" source="21" target="32"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="22" source="21" target="25"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="23" source="22" target="41"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="24" source="22" target="31"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="25" source="23" target="31"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="26" source="24" target="32"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="27" source="25" target="26"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="28" source="25" target="27"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="29" source="26" target="28"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="30" source="26" target="31"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="31" source="27" target="34"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="32" source="27" target="29"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="33" source="28" target="35"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="34" source="28" target="30"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="35" source="29" target="36"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="36" source="29" target="30"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="37" source="30" target="37"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="38" source="32" target="33"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="39" source="33" target="40"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="40" source="33" target="38"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="41" source="33" target="39"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="42" source="42" target="53"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="43" source="42" target="46"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="44" source="43" target="52"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="45" source="43" target="62"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="46" source="44" target="52"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="47" source="45" target="53"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="48" source="46" target="48"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="49" source="46" target="47"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="50" source="47" target="49"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="51" source="47" target="52"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="52" source="48" target="50"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="53" source="48" target="55"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="54" source="49" target="56"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="55" source="49" target="51"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="56" source="50" target="57"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="57" source="50" target="51"> + <attvalues> + <attvalue for="0" value="2" /> + </attvalues> + </edge> + <edge id="58" source="51" target="58"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="59" source="53" target="54"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="60" source="54" target="59"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="61" source="54" target="60"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + <edge id="62" source="54" target="61"> + <attvalues> + <attvalue for="0" value="1" /> + </attvalues> + </edge> + </edges> + </graph> +</gexf>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.gml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,569 @@ +graph [ + node [ + id 0 + label "O" + ] + node [ + id 1 + label "O" + ] + node [ + id 2 + label "O" + ] + node [ + id 3 + label "O" + ] + node [ + id 4 + label "C" + ] + node [ + id 5 + label "C" + ] + node [ + id 6 + label "C" + ] + node [ + id 7 + label "C" + ] + node [ + id 8 + label "C" + ] + node [ + id 9 + label "C" + ] + node [ + id 10 + label "C" + ] + node [ + id 11 + label "C" + ] + node [ + id 12 + label "C" + ] + node [ + id 13 + label "H" + ] + node [ + id 14 + label "H" + ] + node [ + id 15 + label "H" + ] + node [ + id 16 + label "H" + ] + node [ + id 17 + label "H" + ] + node [ + id 18 + label "H" + ] + node [ + id 19 + label "H" + ] + node [ + id 20 + label "H" + ] + node [ + id 21 + label "O" + ] + node [ + id 22 + label "O" + ] + node [ + id 23 + label "O" + ] + node [ + id 24 + label "O" + ] + node [ + id 25 + label "C" + ] + node [ + id 26 + label "C" + ] + node [ + id 27 + label "C" + ] + node [ + id 28 + label "C" + ] + node [ + id 29 + label "C" + ] + node [ + id 30 + label "C" + ] + node [ + id 31 + label "C" + ] + node [ + id 32 + label "C" + ] + node [ + id 33 + label "C" + ] + node [ + id 34 + label "H" + ] + node [ + id 35 + label "H" + ] + node [ + id 36 + label "H" + ] + node [ + id 37 + label "H" + ] + node [ + id 38 + label "H" + ] + node [ + id 39 + label "H" + ] + node [ + id 40 + label "H" + ] + node [ + id 41 + label "H" + ] + node [ + id 42 + label "O" + ] + node [ + id 43 + label "O" + ] + node [ + id 44 + label "O" + ] + node [ + id 45 + label "O" + ] + node [ + id 46 + label "C" + ] + node [ + id 47 + label "C" + ] + node [ + id 48 + label "C" + ] + node [ + id 49 + label "C" + ] + node [ + id 50 + label "C" + ] + node [ + id 51 + label "C" + ] + node [ + id 52 + label "C" + ] + node [ + id 53 + label "C" + ] + node [ + id 54 + label "C" + ] + node [ + id 55 + label "H" + ] + node [ + id 56 + label "H" + ] + node [ + id 57 + label "H" + ] + node [ + id 58 + label "H" + ] + node [ + id 59 + label "H" + ] + node [ + id 60 + label "H" + ] + node [ + id 61 + label "H" + ] + node [ + id 62 + label "H" + ] + edge [ + source 0 + target 11 + label "1" + ] + edge [ + source 0 + target 4 + label "1" + ] + edge [ + source 1 + target 10 + label "1" + ] + edge [ + source 1 + target 20 + label "1" + ] + edge [ + source 2 + target 10 + label "2" + ] + edge [ + source 3 + target 11 + label "2" + ] + edge [ + source 4 + target 5 + label "1" + ] + edge [ + source 4 + target 6 + label "2" + ] + edge [ + source 5 + target 10 + label "1" + ] + edge [ + source 5 + target 7 + label "2" + ] + edge [ + source 6 + target 8 + label "1" + ] + edge [ + source 6 + target 13 + label "1" + ] + edge [ + source 7 + target 9 + label "1" + ] + edge [ + source 7 + target 14 + label "1" + ] + edge [ + source 8 + target 9 + label "2" + ] + edge [ + source 8 + target 15 + label "1" + ] + edge [ + source 9 + target 16 + label "1" + ] + edge [ + source 11 + target 12 + label "1" + ] + edge [ + source 12 + target 19 + label "1" + ] + edge [ + source 12 + target 17 + label "1" + ] + edge [ + source 12 + target 18 + label "1" + ] + edge [ + source 21 + target 32 + label "1" + ] + edge [ + source 21 + target 25 + label "1" + ] + edge [ + source 22 + target 41 + label "1" + ] + edge [ + source 22 + target 31 + label "1" + ] + edge [ + source 23 + target 31 + label "2" + ] + edge [ + source 24 + target 32 + label "2" + ] + edge [ + source 25 + target 26 + label "1" + ] + edge [ + source 25 + target 27 + label "2" + ] + edge [ + source 26 + target 28 + label "2" + ] + edge [ + source 26 + target 31 + label "1" + ] + edge [ + source 27 + target 34 + label "1" + ] + edge [ + source 27 + target 29 + label "1" + ] + edge [ + source 28 + target 35 + label "1" + ] + edge [ + source 28 + target 30 + label "1" + ] + edge [ + source 29 + target 36 + label "1" + ] + edge [ + source 29 + target 30 + label "2" + ] + edge [ + source 30 + target 37 + label "1" + ] + edge [ + source 32 + target 33 + label "1" + ] + edge [ + source 33 + target 40 + label "1" + ] + edge [ + source 33 + target 38 + label "1" + ] + edge [ + source 33 + target 39 + label "1" + ] + edge [ + source 42 + target 53 + label "1" + ] + edge [ + source 42 + target 46 + label "1" + ] + edge [ + source 43 + target 52 + label "1" + ] + edge [ + source 43 + target 62 + label "1" + ] + edge [ + source 44 + target 52 + label "2" + ] + edge [ + source 45 + target 53 + label "2" + ] + edge [ + source 46 + target 48 + label "2" + ] + edge [ + source 46 + target 47 + label "1" + ] + edge [ + source 47 + target 49 + label "2" + ] + edge [ + source 47 + target 52 + label "1" + ] + edge [ + source 48 + target 50 + label "1" + ] + edge [ + source 48 + target 55 + label "1" + ] + edge [ + source 49 + target 56 + label "1" + ] + edge [ + source 49 + target 51 + label "1" + ] + edge [ + source 50 + target 57 + label "1" + ] + edge [ + source 50 + target 51 + label "2" + ] + edge [ + source 51 + target 58 + label "1" + ] + edge [ + source 53 + target 54 + label "1" + ] + edge [ + source 54 + target 59 + label "1" + ] + edge [ + source 54 + target 60 + label "1" + ] + edge [ + source 54 + target 61 + label "1" + ] +]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.gspan Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,7 @@ +t # id 1 +v 0 A +v 1 B +v 2 C +e 0 1 Kante AB +e 1 2 Kante BC +e 2 0 Kante CA
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.xgmml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,16 @@ +<?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="MyGraph" type="string"/> +<att name="shared name" value="MyGraph" type="string"/> + +<node id="0" label="Word"> +<att name="id" value="0" type="string" /> +</node> +<node id="1" label="Hello"> +<att name="id" value="1" type="string" /> +</node> +<edge source="1" target="0"> +<att name="id" value="0" type="string" /> +</edge> +</graph>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.xml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,6 @@ +<?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="MeinGraph" type="string"/> +<att name="shared name" value="MeinGraph" type="string"/> +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.yaml Fri Feb 28 08:57:44 2014 -0500 @@ -0,0 +1,28 @@ +!!python/object:networkx.classes.digraph.DiGraph +adj: &id001 + !!python/unicode '1': + !!python/unicode '2': &id003 {!!python/unicode 'id': !!python/unicode '3', !!python/unicode 'label': !!python/unicode 'Kante + AB', !!python/unicode 'weiteresAttribut': !!python/unicode '44'} + !!python/unicode '2': + !!python/unicode '3': &id004 {!!python/unicode 'id': !!python/unicode '3', !!python/unicode 'label': !!python/unicode 'Kante + BC', !!python/unicode 'weiteresAttribut': !!python/unicode '44'} + !!python/unicode '3': + !!python/unicode '1': &id002 {!!python/unicode 'id': !!python/unicode '3', !!python/unicode 'label': !!python/unicode 'Kante + CA', !!python/unicode 'weiteresAttribut': !!python/unicode '44'} +edge: *id001 +graph: {} +node: + !!python/unicode '1': {!!python/unicode 'id': !!python/unicode '1', label: !!python/unicode 'A', + !!python/unicode 'weiteresAttribut': !!python/unicode '42'} + !!python/unicode '2': {!!python/unicode 'id': !!python/unicode '2', label: !!python/unicode 'B', + !!python/unicode 'weiteresAttribut': !!python/unicode '43'} + !!python/unicode '3': {!!python/unicode 'id': !!python/unicode '3', label: !!python/unicode 'C', + !!python/unicode 'weiteresAttribut': !!python/unicode '44'} +pred: + !!python/unicode '1': + !!python/unicode '3': *id002 + !!python/unicode '2': + !!python/unicode '1': *id003 + !!python/unicode '3': + !!python/unicode '2': *id004 +succ: *id001
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Fri Feb 28 08:57:44 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 08:57:44 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>' +