Skip to content

Commit 1d831de

Browse files
authored
Merge pull request #4 from Krekep/master
Graphviz for python wrapper
2 parents 81573de + 93cbc67 commit 1d831de

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

python/pycubool/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .utils import *
2323
from .matrix import *
2424
from .io import *
25+
from .gviz import *
2526

2627
# Setup global module state
2728
init_wrapper()

python/pycubool/gviz.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from io import StringIO
2+
3+
__all__ = [
4+
"matrix_data_to_gviz",
5+
"matrices_data_to_gviz"
6+
]
7+
8+
9+
def matrix_data_to_gviz(shape, rows, cols, **kwargs) -> str:
10+
check_len_rows_cols(rows, cols)
11+
check_shape(shape)
12+
13+
_result = StringIO()
14+
args = unboxing_kwargs(kwargs)
15+
_result.write("digraph {\n")
16+
_result.write(args["graph_name"])
17+
_result.write(args["vertex_color"])
18+
n = shape[0]
19+
20+
used_vertex = [False] * n
21+
temp = build_edges(rows, cols, args["base_vertex"], args["label"], args["edge_color"], used_vertex)
22+
_result.write(temp[0])
23+
used_vertex = temp[1]
24+
for i in range(n):
25+
if not used_vertex[i]:
26+
_result.write(f'{i + args["base_vertex"]};\n')
27+
_result.write("}\n")
28+
result = _result.getvalue()
29+
_result.close()
30+
return result
31+
32+
33+
def matrices_data_to_gviz(shape, matrices: dict, **kwargs):
34+
check_shape(shape)
35+
for name in matrices.keys():
36+
check_len_rows_cols(matrices[name][0], matrices[name][1])
37+
38+
_result = StringIO()
39+
args = unboxing_kwargs(kwargs)
40+
_result.write("digraph {\n")
41+
_result.write(args["graph_name"])
42+
_result.write(args["vertex_color"])
43+
n = shape[0]
44+
45+
used_vertex = [False] * n
46+
for name in matrices.keys():
47+
rows = matrices[name][0]
48+
cols = matrices[name][0]
49+
label = name
50+
edge_color = args["edge_colors"].get(name)
51+
temp = build_edges(rows, cols, args["base_vertex"], label, edge_color, used_vertex)
52+
_result.write(temp[0])
53+
used_vertex = temp[1]
54+
for i in range(n):
55+
if not used_vertex[i]:
56+
_result.write(f'{i + args["base_vertex"]};\n')
57+
_result.write("}\n")
58+
result = _result.getvalue()
59+
_result.close()
60+
return result
61+
62+
63+
def build_edges(rows, cols, base_vertex, label, edge_color, used_vertex):
64+
_result = StringIO()
65+
for i in range(len(rows)):
66+
_result.write(f'{rows[i] + base_vertex} -> {cols[i] + base_vertex} [label={label},color={edge_color}];\n')
67+
used_vertex[rows[i]] = True
68+
used_vertex[cols[i]] = True
69+
result = _result.getvalue()
70+
_result.close()
71+
return result, used_vertex
72+
73+
74+
def unboxing_kwargs(kwargs: dict) -> dict:
75+
graph_name = kwargs.get("graph_name")
76+
vertex_color = kwargs.get("vertex_color")
77+
edge_color = kwargs.get("edge_color")
78+
_base_vertex = kwargs.get("base_vertex")
79+
_label = kwargs.get("label")
80+
label = "" if _label is None else _label
81+
base_vertex = 0 if _base_vertex is None else _base_vertex
82+
edge_colors = kwargs.get("edge_colors")
83+
84+
if not (graph_name is None):
85+
graph_name = f'graph [label={graph_name}];\n'
86+
else:
87+
graph_name = ""
88+
if not (vertex_color is None):
89+
vertex_color = f'node [color={vertex_color}];\n'
90+
else:
91+
vertex_color = ""
92+
93+
result = dict()
94+
result["graph_name"] = graph_name
95+
result["vertex_color"] = vertex_color
96+
result["edge_color"] = edge_color
97+
result["label"] = label
98+
result["base_vertex"] = base_vertex
99+
result["edge_colors"] = edge_colors
100+
101+
return result
102+
103+
104+
def check_shape(shape: list):
105+
if shape[0] != shape[1]:
106+
raise Exception("Matrix must be square")
107+
108+
109+
def check_len_rows_cols(rows, cols):
110+
if len(rows) != len(cols):
111+
raise Exception("Rows and cols arrays must have equal size")

0 commit comments

Comments
 (0)