Mercurial > repos > bernhardlutz > graphconverter
comparison xgmml_networkx.py @ 0:fe01a054a6b2 draft
Uploaded
author | bernhardlutz |
---|---|
date | Fri, 28 Feb 2014 08:57:44 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:fe01a054a6b2 |
---|---|
1 __author__ = "Yasunobu OKAMURA" | |
2 __copyright__ = "Copyright (c) 2012 Y.Okamura" | |
3 __license__ = "GPL v3+" | |
4 | |
5 import xml.parsers.expat | |
6 import networkx as nx | |
7 | |
8 class XGMMLParserHelper(object): | |
9 """ | |
10 """ | |
11 | |
12 def __init__(self, graph=nx.DiGraph()): | |
13 """ | |
14 | |
15 Arguments: | |
16 - `graph`: Network X graph object | |
17 """ | |
18 self._graph = graph | |
19 self._parser = xml.parsers.expat.ParserCreate() | |
20 self._parser.StartElementHandler = self._start_element | |
21 self._parser.EndElementHandler = self._end_element | |
22 self._tagstack = list() | |
23 | |
24 self._current_attr = dict() | |
25 self._current_obj = dict() | |
26 | |
27 def _start_element(self, tag, attr): | |
28 """ | |
29 | |
30 Arguments: | |
31 - `self`: | |
32 - `tag`: | |
33 - `attr`: | |
34 """ | |
35 | |
36 self._tagstack.append(tag) | |
37 | |
38 if tag == 'node' or tag == 'edge': | |
39 self._current_obj = dict(attr) | |
40 | |
41 if tag == 'att' and (self._tagstack[-2] == 'node' or self._tagstack[-2] == 'edge'): | |
42 if attr['type'] == 'string': | |
43 self._current_attr[attr['name']] = attr['value'] | |
44 elif attr['type'] == 'real': | |
45 self._current_attr[attr['name']] = float(attr['value']) | |
46 elif attr['type'] == 'integer': | |
47 self._current_attr[attr['name']] = int(attr['value']) | |
48 elif attr['type'] == 'boolean': | |
49 self._current_attr[attr['name']] = bool(attr['value']) | |
50 else: | |
51 raise NotImplementedError(attr['type']) | |
52 | |
53 def _end_element(self, tag): | |
54 """ | |
55 | |
56 Arguments: | |
57 - `self`: | |
58 - `tag`: | |
59 """ | |
60 | |
61 if tag == 'node': | |
62 self._graph.add_node(self._current_obj['id'], label=self._current_obj['label'], **self._current_attr) | |
63 #print 'add node', self._current_obj | |
64 elif tag == 'edge': | |
65 self._graph.add_edge(self._current_obj['source'], self._current_obj['target'], **self._current_attr) | |
66 | |
67 self._tagstack.pop() | |
68 | |
69 def parseFile(self, file): | |
70 """ | |
71 | |
72 Arguments: | |
73 - `self`: | |
74 - `file`: | |
75 """ | |
76 | |
77 self._parser.ParseFile(file) | |
78 | |
79 def graph(self): | |
80 """ | |
81 | |
82 Arguments: | |
83 - `self`: | |
84 """ | |
85 | |
86 return self._graph | |
87 | |
88 def XGMMLWriter(file, graph, graph_name): | |
89 """ | |
90 | |
91 Arguments: | |
92 - `graph`: | |
93 """ | |
94 | |
95 print >>file, """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
96 <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"> | |
97 <att name="selected" value="1" type="boolean" /> | |
98 <att name="name" value="{0}" type="string"/> | |
99 <att name="shared name" value="{0}" type="string"/> | |
100 """.format(graph_name) | |
101 | |
102 | |
103 for onenode in graph.nodes(data=True): | |
104 id = onenode[0] | |
105 attr = dict(onenode[1]) | |
106 | |
107 if 'label' in attr: | |
108 label = attr['label'] | |
109 del attr['label'] | |
110 else: | |
111 label = id | |
112 | |
113 print >>file, '<node id="{id}" label="{label}">'.format(id=id, label=label) | |
114 for k, v in attr.iteritems(): | |
115 print >>file, '<att name="{}" value="{}" type="string" />'.format(k, v) | |
116 print >>file, '</node>' | |
117 | |
118 for oneedge in graph.edges(data=True): | |
119 print >>file, '<edge source="{}" target="{}">'.format(oneedge[0], oneedge[1]) | |
120 for k, v in oneedge[2].iteritems(): | |
121 print >>file, '<att name="{}" value="{}" type="string" />'.format(k, v) | |
122 print >>file, '</edge>' | |
123 print >>file, '</graph>' | |
124 |