6.2.1. pywhy_graphs.export.graph_to_numpy#
- pywhy_graphs.export.graph_to_numpy(causal_graph)[source]#
Convert causal graph to a numpy adjacency array.
- Parameters:
- causal_graphinstance of causal graph
The causal graph that is represented in pywhy.
- Returns:
- numpy_grapharray-like of shape (n_nodes, n_nodes)
The numpy array that represents the graph. The values representing edges are mapped according to a pre-defined set of values. See Notes.
Notes
The adjacency matrix is defined where the ijth entry of
numpy_graph
has a non-zero entry if there is an edge from i to j. The ijth entry is symmetric with the jith entry if the edge is ‘undirected’, or ‘bidirected’. Then specific edges are mapped to the following values: - directed edge (->): 1 - circle endpoint (-o): 2 - undirected edge (–): 10 - bidirected edge (<->): 20Circle endpoints can be symmetric, but they can also contain a tail, or a directed edge at the other end. See
EDGE_TO_VALUE_MAPPING
. This corresponds to the output of the pcalg package.How are multiple edges between same pair of nodes handled?
In ADMGs, multiple edges between the same pairs of nodes are allowed. Since we map edges between pairs of nodes to numerical values, we have to treat undirected and bidirected edges separately, since one can have a directed edge and either an undirected, or bidirected edge present. Therefore for example, if there is a directed edge \(X \rightarrow Y\) and also a bidirected edge \(X \leftrightarrow Y\), then the numpy array element corresponding to (X, Y) would have the value 21, indicating uniquely a directed edge and a bidirected edge. Note, this is not an issue for any other common causal graph class because there only one edge is supported between any two nodes.
Examples
>>> arr = np.array([ [0, 21, 0], [20, 0, 0], [0, 0, 0] ]) >>> nodelist = ['x', 'y', 'z'] >>> bow_graph = numpy_to_graph(arr, nodelist, 'admg') >>> print(bow_graph.edges())