-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
29 lines (25 loc) · 722 Bytes
/
utils.py
File metadata and controls
29 lines (25 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np
import igraph as ig
def is_dag(W: np.ndarray) -> bool:
"""
Returns ``True`` if ``W`` is a DAG, ``False`` otherwise.
"""
G = ig.Graph.Weighted_Adjacency(W.tolist())
return G.is_dag()
def dag_to_adj_mat(dag):
# Transform causal learn format into bn format
n = len(dag.graph)
adj_mat = np.zeros((n, n))
for i in range(n):
for j in range(n):
if dag.graph[i][j] == -1:
adj_mat[i][j] = 1
return adj_mat
def adj_mat_to_edge_list(adj_mat):
n = adj_mat.shape[0]
edge_list = []
for i in range(n):
for j in range(n):
if adj_mat[i][j] == 1:
edge_list.append((i, j))
return edge_list