Graph PDAG Module¶
The PDAG (Partially Directed Acyclic Graph) class extends SDG to work specifically with partially directed acyclic graphs used in causal discovery.
Classes¶
PDAG¶
Partially Directed Acyclic Graph class for representing causal graph structures.
Features:
- Directed and undirected edges only
- Acyclicity validation
- Causal discovery algorithm support
- Conversion to/from other graph types
Usage:
from causaliq_core.graph import PDAG
# Create a PDAG
nodes = ['X', 'Y', 'Z']
edges = [
('X', '->', 'Y'), # Directed edge (oriented)
('Y', '--', 'Z'), # Undirected edge (unoriented)
]
pdag = PDAG(nodes, edges)
NotPDAGError¶
Exception raised when attempting to create an invalid PDAG.
Reference¶
Classes:
-
NotPDAGError–Indicate graph is not a PDAG when one is expected.
-
PDAG–Partially directed acyclic graph (PDAG).
Classes¶
NotPDAGError
¶
Indicate graph is not a PDAG when one is expected.
PDAG
¶
Partially directed acyclic graph (PDAG).
Parameters:
-
(nodes¶List[str]) –Nodes present in the graph.
-
(edges¶List[Tuple[str, str, str]]) –Edges which define the graph connections as list of tuples: (node1, dependency symbol, node2).
Attributes:
-
nodes(List[str]) –Graph nodes in alphabetical order.
-
edges(Dict[Tuple[str, str], EdgeType]) –Graph edges {(node1, node2): EdgeType}.
-
is_directed(bool) –Graph only has directed (causal) edges.
-
parents(Dict[str, List[str]]) –Parents of node {node: [parents]}.
Raises:
-
TypeError–If nodes and edges not both lists.
-
ValueError–If node or edge invalid.
-
NotPDAGError–If graph is not a PDAG.
Parameters:
-
(nodes¶List[str]) –Nodes present in the graph.
-
(edges¶List[Tuple[str, str, str]]) –Edges which define the graph connections.
Raises:
-
NotPDAGError–If graph is not a PDAG.
Methods:
-
edge_reversible–Return whether specified edge is in CPDAG and is reversible.
-
rename–Rename nodes in place according to name map.
-
partial_order–Return partial topological ordering for the directed part of a
-
is_DAG–Return whether graph is a Directed Acyclic Graph (DAG).
-
is_PDAG–Return whether graph is a Partially Directed Acyclic Graph (PDAG).
-
undirected_trees–Return undirected trees present in graph.
-
components–Return components present in graph.
-
number_components–Return number of components (including unconnected nodes) in graph.
-
to_adjmat–Return an adjacency matrix representation of the graph.
-
__str__–Return a human-readable description of the graph.
-
__eq__–Test if graph is identical to this one.
Functions¶
edge_reversible
¶
edge_reversible(edge: Tuple[str, str]) -> bool
Return whether specified edge is in CPDAG and is reversible.
Parameters:
-
(edge¶Tuple[str, str]) –Edge to examine, (node1, node2).
Returns:
-
bool–Whether present and reversible, or not.
Raises:
-
TypeError–If edge argument has bad type.
rename
¶
rename(name_map: Dict[str, str]) -> None
Rename nodes in place according to name map.
Parameters:
-
(name_map¶Dict[str, str]) –Name mapping {name: new name}. Must have mapping for every node.
Raises:
-
TypeError–With bad arg type.
-
ValueError–With bad arg values e.g. unknown node names.
partial_order
classmethod
¶
partial_order(
parents: Dict[str, List[str]],
nodes: Optional[Union[List[str], Set[str]]] = None,
new_arc: Optional[Tuple[str, str]] = None,
) -> Optional[List[Set[str]]]
Return partial topological ordering for the directed part of a graph.
The graph is specified by list of parents for each node.
Parameters:
-
(parents¶Dict[str, List[str]]) –Parents of each node {node: [parents]}.
-
(nodes¶Optional[Union[List[str], Set[str]]], default:None) –Optional complete list of nodes including parentless ones for use if parents argument doesn't include them already.
-
(new_arc¶Optional[Tuple[str, str]], default:None) –A new arc (n1, n2) to be added before order is evaluated. If the opposing arc is implied in parents then it is removed so that arc reversal is also supported. This argument facilitates seeing whether an arc addition or reversal would create a cycle.
Returns:
-
Optional[List[Set[str]]]–Nodes in a partial topological order as list of sets or None if
-
Optional[List[Set[str]]]–there is no ordering which means the graph is cyclic.
is_DAG
¶
Return whether graph is a Directed Acyclic Graph (DAG).
Returns:
-
bool–True if graph is a DAG, False otherwise.
is_PDAG
¶
Return whether graph is a Partially Directed Acyclic Graph (PDAG).
Returns:
-
bool–True if graph is a PDAG, False otherwise.
undirected_trees
¶
Return undirected trees present in graph.
Returns:
-
List[Set[Union[Tuple[str, str], Tuple[str, None]]]]–List of trees, each tree a set of tuples representing edges in tree
-
List[Set[Union[Tuple[str, str], Tuple[str, None]]]]–(n1, n2) or a single isolated node (n1, None).
components
¶
Return components present in graph.
Uses tree search algorithm to span the undirected graph to identify nodes in individual trees which are the spanning tree of each component.
Returns:
-
List[List[str]]–List of lists, each a list of sorted nodes in component.
number_components
¶
Return number of components (including unconnected nodes) in graph.
Returns:
-
int–Number of components.
to_adjmat
¶
Return an adjacency matrix representation of the graph.
Returns:
-
DataFrame–Adjacency matrix as a pandas DataFrame.
__str__
¶
Return a human-readable description of the graph.
Returns:
-
str–String description of graph.