Skip to content

Commit f865336

Browse files
committed
[gviz] Add gviz to python
- Add matrix_data_to_gviz method, which create gviz script
1 parent d5a6655 commit f865336

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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"
5+
graph_name = kwargs.get("graph_name")
6+
vertex_color = kwargs.get("vertex_color")
7+
edge_color = kwargs.get("edge_color")
8+
_base_vertex = kwargs.get("base_vertex")
9+
_label = kwargs.get("label")
10+
label = "" if _label is None else _label
11+
base_vertex = 0 if _base_vertex is None else _base_vertex
12+
13+
n = shape[0]
14+
m = shape[1]
15+
16+
if not (graph_name is None):
17+
result += f'graph [label={graph_name}];\n'
18+
if not (vertex_color is None):
19+
result += f'node [color={vertex_color}];\n'
20+
if not (vertex_color is None):
21+
result += f'edge [color={edge_color}];\n'
22+
23+
used_vertex = [False] * n
24+
for i in range(len(rows)):
25+
result += f"{rows[i] + base_vertex} -> {cols[i] + base_vertex} [label={label}];\n"
26+
used_vertex[rows[i]] = True
27+
used_vertex[cols[i]] = True
28+
for i in range(n):
29+
if not used_vertex[i]:
30+
result += f"{i + base_vertex};\n"
31+
result += "}\n"
32+
return result

0 commit comments

Comments
 (0)