Skip to content

Commit

Permalink
Fix incorrect edges in adjacency_matrix_to_graph (#1202)
Browse files Browse the repository at this point in the history
* Fix matrix bug

Signed-off-by: rahulbshrestha <[email protected]>

* Fixed graphviz import

Signed-off-by: rahulbshrestha <[email protected]>

---------

Signed-off-by: rahulbshrestha <[email protected]>
  • Loading branch information
rahulbshrestha authored Aug 4, 2024
1 parent daf5f94 commit 4c3a083
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions dowhy/utils/graph_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,29 @@ def adjacency_matrix_to_graph(adjacency_matrix, labels=None):
:param labels: List of labels.
:returns: Graph in DOT format.
"""
import graphviz

if adjacency_matrix.ndim != 2:
raise ValueError("Adjacency matrix must have a dimension of 2.")

if isinstance(adjacency_matrix, np.matrix):
adjacency_matrix = np.asarray(adjacency_matrix)

# Only consider edges have absolute edge weight > 0.01
idx = np.abs(adjacency_matrix) > 0.01
dirs = np.where(idx)
import graphviz

d = graphviz.Digraph(engine="dot")
names = labels if labels else [f"x{i}" for i in range(len(adjacency_matrix))]
for name in names:
d.node(name)

if labels is None:
labels = [f"x{i}" for i in range(len(adjacency_matrix))]

for label in labels:
d.node(label)

for to, from_, coef in zip(dirs[0], dirs[1], adjacency_matrix[idx]):
d.edge(names[from_], names[to], label=str(coef))
d.edge(labels[from_], labels[to], label=str(coef))

return d


Expand Down

0 comments on commit 4c3a083

Please sign in to comment.