|
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: |
5 | 75 | graph_name = kwargs.get("graph_name")
|
6 | 76 | vertex_color = kwargs.get("vertex_color")
|
7 | 77 | edge_color = kwargs.get("edge_color")
|
8 | 78 | _base_vertex = kwargs.get("base_vertex")
|
9 | 79 | _label = kwargs.get("label")
|
10 | 80 | label = "" if _label is None else _label
|
11 | 81 | 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") |
14 | 83 |
|
15 | 84 | 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 = "" |
19 | 88 | 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 |
21 | 100 |
|
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" |
31 | 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