Skip to content

Commit 93cbc67

Browse files
committed
[gviz] Translation of several matrices into gviz
- Add method: matrices_data_to_gviz - Add methods with collective code
1 parent 0934f7c commit 93cbc67

File tree

1 file changed

+99
-19
lines changed

1 file changed

+99
-19
lines changed

python/pycubool/gviz.py

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,111 @@
1-
def matrix_data_to_gviz(shape, rows, cols, **kwargs):
2-
if len(rows) != len(cols):
3-
raise Exception("Rows and cols arrays must have equal size")
4-
result = "digraph {\n"
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:
575
graph_name = kwargs.get("graph_name")
676
vertex_color = kwargs.get("vertex_color")
777
edge_color = kwargs.get("edge_color")
878
_base_vertex = kwargs.get("base_vertex")
979
_label = kwargs.get("label")
1080
label = "" if _label is None else _label
1181
base_vertex = 0 if _base_vertex is None else _base_vertex
12-
13-
n = max(shape[0], shape[1])
82+
edge_colors = kwargs.get("edge_colors")
1483

1584
if not (graph_name is None):
16-
result += f'graph [label={graph_name}];\n'
17-
if not (vertex_color is None):
18-
result += f'node [color={vertex_color}];\n'
85+
graph_name = f'graph [label={graph_name}];\n'
86+
else:
87+
graph_name = ""
1988
if not (vertex_color is None):
20-
result += f'edge [color={edge_color}];\n'
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
21100

22-
used_vertex = [False] * n
23-
for i in range(len(rows)):
24-
result += f"{rows[i] + base_vertex} -> {cols[i] + base_vertex} [label={label}];\n"
25-
used_vertex[rows[i]] = True
26-
used_vertex[cols[i]] = True
27-
for i in range(n):
28-
if not used_vertex[i]:
29-
result += f"{i + base_vertex};\n"
30-
result += "}\n"
31101
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)