author | Sverre Rabbelier <srabbelier@gmail.com> |
Thu, 27 Nov 2008 21:57:24 +0000 | |
changeset 597 | 66088092f849 |
parent 594 | 06c2228e39cb |
permissions | -rw-r--r-- |
594
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
# Copyright (c) 2007-2008 Pedro Matiello <pmatiello@gmail.com> |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Permission is hereby granted, free of charge, to any person |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# obtaining a copy of this software and associated documentation |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# files (the "Software"), to deal in the Software without |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# restriction, including without limitation the rights to use, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# copy, modify, merge, publish, distribute, sublicense, and/or sell |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# copies of the Software, and to permit persons to whom the |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# Software is furnished to do so, subject to the following |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# conditions: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# The above copyright notice and this permission notice shall be |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# included in all copies or substantial portions of the Software. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
# OTHER DEALINGS IN THE SOFTWARE. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
Functions for reading and writing graphs. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
@sort: read_xml, write_xml, write_dot_graph, write_dot_digraph, write_dot_hypergraph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
29 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
# Imports |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
from xml.dom.minidom import Document, parseString |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
# Values |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
colors = ['aquamarine4', 'blue4', 'brown4', 'cornflowerblue', 'cyan4', |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
'darkgreen', 'darkorange3', 'darkorchid4', 'darkseagreen4', 'darkslategray', |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
'deeppink4', 'deepskyblue4', 'firebrick3', 'hotpink3', 'indianred3', |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
'indigo', 'lightblue4', 'lightseagreen', 'lightskyblue4', 'magenta4', |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
'maroon', 'palevioletred3', 'steelblue', 'violetred3'] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
# XML |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
def write_xml(graph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
Return a string specifying the given graph as a XML document. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
@rtype: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
@return: String specifying the graph as a XML document. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
# Document root |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
grxml = Document() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
grxmlr = grxml.createElement('graph') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
grxml.appendChild(grxmlr) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
# Each node... |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
for each_node in graph.nodes(): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
node = grxml.createElement('node') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
node.setAttribute('id',str(each_node)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
grxmlr.appendChild(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
for each_attr in graph.get_node_attributes(each_node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
attr = grxml.createElement('attribute') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
attr.setAttribute('attr', each_attr[0]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
70 |
attr.setAttribute('value', each_attr[1]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
node.appendChild(attr) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
# Each edge... |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
for edge_from, edge_to in graph.edges(): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
edge = grxml.createElement('edge') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
edge.setAttribute('from',str(edge_from)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
edge.setAttribute('to',str(edge_to)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
edge.setAttribute('wt',str(graph.get_edge_weight(edge_from, edge_to))) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
edge.setAttribute('label',str(graph.get_edge_label(edge_from, edge_to))) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
grxmlr.appendChild(edge) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
for attr_name, attr_value in graph.get_edge_attributes(edge_from, edge_to): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
attr = grxml.createElement('attribute') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
attr.setAttribute('attr', attr_name) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
attr.setAttribute('value', attr_value) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
edge.appendChild(attr) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
return grxml.toprettyxml() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
def write_xml_hypergraph(hypergraph): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
Return a string specifying the given hypergraph as a XML document. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
@type hypergraph: hypergraph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
@param hypergraph: Hypergraph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
96 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
97 |
@rtype: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
98 |
@return: String specifying the graph as a XML document. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
99 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
100 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
101 |
# Document root |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
102 |
grxml = Document() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
103 |
grxmlr = grxml.createElement('hypergraph') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
104 |
grxml.appendChild(grxmlr) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
105 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
106 |
# Each node... |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
107 |
nodes = hypergraph.nodes() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
108 |
hyperedges = hypergraph.get_hyperedges() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
109 |
for each_node in (nodes + hyperedges): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
110 |
if (each_node in nodes): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
111 |
node = grxml.createElement('node') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
112 |
else: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
113 |
node = grxml.createElement('hyperedge') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
114 |
node.setAttribute('id',str(each_node)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
115 |
grxmlr.appendChild(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
116 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
117 |
# and its outgoing edge |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
118 |
for each_edge in hypergraph.get_links(each_node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
119 |
edge = grxml.createElement('link') |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
120 |
edge.setAttribute('to',str(each_edge)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
121 |
node.appendChild(edge) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
122 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
123 |
return grxml.toprettyxml() |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
124 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
125 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
126 |
def read_xml(graph, string): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
127 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
128 |
Read a graph from a XML document. Nodes and edges specified in the input will be added to the current graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
129 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
130 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
131 |
@param graph: Graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
132 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
133 |
@type string: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
134 |
@param string: Input string in XML format specifying a graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
135 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
136 |
dom = parseString(string) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
137 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
138 |
# Read nodes... |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
139 |
for each_node in dom.getElementsByTagName("node"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
140 |
graph.add_node(each_node.getAttribute('id')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
141 |
for each_attr in each_node.getElementsByTagName("attribute"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
142 |
graph.add_node_attribute(each_node.getAttribute('id'), (each_attr.getAttribute('attr'), |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
143 |
each_attr.getAttribute('value'))) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
144 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
145 |
# Read edges... |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
146 |
for each_edge in dom.getElementsByTagName("edge"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
147 |
graph.add_edge(each_edge.getAttribute('from'), each_edge.getAttribute('to'), \ |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
148 |
wt=float(each_edge.getAttribute('wt')), label=each_edge.getAttribute('label')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
149 |
for each_attr in each_edge.getElementsByTagName("attribute"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
150 |
attr_tuple = (each_attr.getAttribute('attr'), each_attr.getAttribute('value')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
151 |
if (attr_tuple not in graph.get_edge_attributes(each_edge.getAttribute('from'), \ |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
152 |
each_edge.getAttribute('to'))): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
153 |
graph.add_edge_attribute(each_edge.getAttribute('from'), \ |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
154 |
each_edge.getAttribute('to'), attr_tuple) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
155 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
156 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
157 |
def read_xml_hypergraph(hypergraph, string): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
158 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
159 |
Read a graph from a XML document. Nodes and hyperedges specified in the input will be added to the current graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
160 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
161 |
@type hypergraph: hypergraph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
162 |
@param hypergraph: Hypergraph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
163 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
164 |
@type string: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
165 |
@param string: Input string in XML format specifying a graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
166 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
167 |
dom = parseString(string) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
168 |
for each_node in dom.getElementsByTagName("node"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
169 |
hypergraph.add_nodes(each_node.getAttribute('id')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
170 |
for each_node in dom.getElementsByTagName("hyperedge"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
171 |
hypergraph.add_hyperedges(each_node.getAttribute('id')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
172 |
dom = parseString(string) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
173 |
for each_node in dom.getElementsByTagName("node"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
174 |
for each_edge in each_node.getElementsByTagName("link"): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
175 |
hypergraph.link(each_node.getAttribute('id'), each_edge.getAttribute('to')) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
176 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
177 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
178 |
# DOT Language |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
179 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
180 |
def _dot_node_str(graph, node, wt): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
181 |
line = '\t"%s" [ ' % str(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
182 |
attrlist = graph.get_node_attributes(node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
183 |
for each in attrlist: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
184 |
attr = '%s="%s" ' % (each[0], each[1]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
185 |
line = line + attr |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
186 |
line = line + ']\n' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
187 |
return line |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
188 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
189 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
190 |
def _dot_edge_str(graph, u, v, wt): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
191 |
line = '\t"%s" -- "%s" [ ' % (str(u), str(v)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
192 |
attrlist = graph.get_edge_attributes(u, v) + [('label',graph.get_edge_label(u, v))] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
193 |
for each in attrlist: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
194 |
attr = '%s="%s" ' % (each[0], each[1]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
195 |
line = line + attr |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
196 |
line = line + ']\n' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
197 |
return line |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
198 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
199 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
200 |
def _dot_arrow_str(graph, u, v, wt): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
201 |
line = '\t"%s" -> "%s" [ ' % (str(u), str(v)) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
202 |
attrlist = graph.get_edge_attributes(u, v) + [('label',graph.get_edge_label(u, v))] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
203 |
for each in attrlist: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
204 |
attr = '%s="%s" ' % (each[0], each[1]) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
205 |
line = line + attr |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
206 |
line = line + ']\n' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
207 |
return line |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
208 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
209 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
210 |
def write_dot_graph(graph, wt): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
211 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
212 |
Return a string specifying the given graph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
213 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
214 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
215 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
216 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
217 |
@type wt: boolean |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
218 |
@param wt: Whether edges should be labelled with its weight. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
219 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
220 |
@rtype: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
221 |
@return: String specifying the graph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
222 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
223 |
doc = 'graph graphname \n{\n' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
224 |
for node in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
225 |
doc = doc + _dot_node_str(graph, node, wt) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
226 |
for edge in graph[node]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
227 |
if (node >= edge): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
228 |
doc = doc + _dot_edge_str(graph, node, edge, wt) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
229 |
doc = doc + '}' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
230 |
return doc |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
231 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
232 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
233 |
def write_dot_digraph(graph, wt): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
234 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
235 |
Return a string specifying the given digraph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
236 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
237 |
@type graph: graph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
238 |
@param graph: Graph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
239 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
240 |
@type wt: boolean |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
241 |
@param wt: Whether arrows should be labelled with its weight. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
242 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
243 |
@rtype: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
244 |
@return: String specifying the graph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
245 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
246 |
doc = 'digraph graphname \n{\n' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
247 |
for node in graph: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
248 |
doc = doc + _dot_node_str(graph, node, wt) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
249 |
for edge in graph[node]: |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
250 |
doc = doc + _dot_arrow_str(graph, node, edge, wt) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
251 |
doc = doc + '}' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
252 |
return doc |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
253 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
254 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
255 |
def write_dot_hypergraph(hypergraph, coloured=False): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
256 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
257 |
Return a string specifying the given hypergraph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
258 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
259 |
@type hypergraph: hypergraph |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
260 |
@param hypergraph: Hypergraph. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
261 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
262 |
@type coloured: boolean |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
263 |
@param coloured: Whether hyperedges should be coloured. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
264 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
265 |
@rtype: string |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
266 |
@return: String specifying the hypergraph in DOT Language. |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
267 |
""" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
268 |
# Start document |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
269 |
doc = "" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
270 |
doc = doc + "graph graphname" + "\n{\n" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
271 |
colortable = {} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
272 |
colorcount = 0 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
273 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
274 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
275 |
# Add hyperedges |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
276 |
color = '' |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
277 |
for each_hyperedge in hypergraph.hyperedges(): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
278 |
colortable[each_hyperedge] = colors[colorcount % len(colors)] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
279 |
colorcount = colorcount + 1 |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
280 |
if (coloured): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
281 |
color = " color=%s" % colortable[each_hyperedge] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
282 |
vars = { |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
283 |
'hyperedge' : str(each_hyperedge), |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
284 |
'color' : color |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
285 |
} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
286 |
doc = doc + '\t"%(hyperedge)s" [shape=point %(color)s]\n' % vars |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
287 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
288 |
color = "\n" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
289 |
# Add nodes and links |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
290 |
for each_node in hypergraph.nodes(): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
291 |
doc = doc + "\t\"%s\"\n" % str(each_node) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
292 |
for each_link in hypergraph.links(each_node): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
293 |
if (coloured): |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
294 |
color = " [color=%s]\n" % colortable[each_link] |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
295 |
linkvars = { |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
296 |
'node' : str(each_node), |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
297 |
'hyperedge' : str(each_link) |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
298 |
} |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
299 |
doc = doc + '\t %(node)s -- %(hyperedge)s' % linkvars + color |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
300 |
|
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
301 |
doc = doc + "}" |
06c2228e39cb
Added the python-graph module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
302 |
return doc |