Module Index : Topics : Keywords
 
 
structure_learning.data_structures.cpdag
index
/Users/165421/Documents/code/structure_learning/src/structure_learning/data_structures/cpdag.py

This module defines the CPDAG class, which represents a Completed Partially Directed Acyclic Graph.
 
Classes:
    CPDAG: Inherits from Graph and provides methods for DAG enumeration and comparison.

 
Modules
       
numpy
networkx
pandas

 
Classes
       
structure_learning.data_structures.graph.Graph(builtins.object)
CPDAG

 
class CPDAG(structure_learning.data_structures.graph.Graph)
    CPDAG(incidence=None, nodes=None)
 
Represents a Completed Partially Directed Acyclic Graph (CPDAG).
 
Attributes:
    undirected_edges (list): List of undirected edges in the graph.
    _undirected_edges_idx (list): List of indices of undirected edges.
    _n_dags (int): Number of DAGs represented by the CPDAG.
    dags (list): List of DAGs represented by the CPDAG.
 
 
Method resolution order:
CPDAG
structure_learning.data_structures.graph.Graph
builtins.object

Methods defined here:
__contains__(self, dag)
Check if a DAG is represented by the CPDAG.
 
Parameters:
    dag (DAG): Directed Acyclic Graph.
 
Returns:
    bool: True if the DAG is represented by the CPDAG, False otherwise.
__eq__(self, other)
Check if two CPDAGs are equal.
 
Parameters:
    other (CPDAG): Another CPDAG instance.
 
Returns:
    bool: True if the CPDAGs are equal, False otherwise.
__init__(self, incidence=None, nodes=None)
Initialize a CPDAG instance.
 
Parameters:
    incidence (numpy.ndarray): Adjacency matrix of the graph.
    nodes (list): List of node labels.
__len__(self)
__len_old__(self)
Get the number of DAGs represented by the CPDAG.
 
Returns:
    int: Number of DAGs.
enumerate_dags(self)
enumerate_dags_old(self, generate=True)
Enumerate all DAGs represented by the CPDAG.
 
Parameters:
    generate (bool): Whether to generate DAG instances.
 
Yields:
    Graph: DAG instances represented by the CPDAG.
plot(self, filename=None, text=None, data: pandas.core.frame.DataFrame = None)
Plot a networkx graph.
 
Parameters:
    title (str): Title of the plot.
    figsize (tuple): Size of the figure.
    node_size (int): Size of the nodes.
    node_color (str): Color of the nodes.
    k (int): Distance between nodes in the layout.

Class methods defined here:
from_dag(dag, blocklist=None)
Create a CPDAG from a DAG.
 
Parameters:
    dag (DAG): Directed Acyclic Graph.
 
Returns:
    CPDAG: Completed Partially Directed Acyclic Graph.

Data and other attributes defined here:
__hash__ = None

Methods inherited from structure_learning.data_structures.graph.Graph:
__copy__(self)
__mul__(self, g: Type[ForwardRef('G')])
Multiplies two Graph objects element-wise.
 
Parameters:
    g (Graph): Another Graph object.
 
Returns:
    Graph: Resulting Graph object.
__rmul__(self, g: Type[ForwardRef('G')])
Multiplies two Graph objects element-wise (reverse operation).
 
Parameters:
    g (Graph): Another Graph object.
 
Returns:
    Graph: Resulting Graph object.
__str__(self)
Converts the Graph object to a string representation.
 
Returns:
    str: String representation of the graph.
add_edge(self, edge: Union[List[str], Tuple[str]])
Adds a single edge to the graph.
 
Parameters:
    edge (list | tuple): Edge represented as a tuple (node1, node2).
add_edges(self, edges: Union[List[Tuple], Tuple[Tuple]])
Adds multiple edges to the graph.
 
Parameters:
    edges (list | tuple): List or tuple of edges, each represented as a tuple (node1, node2).
add_node(self, node: str)
Adds a single node to the graph.
 
Parameters:
    node (str): Node label.
add_nodes(self, nodes: Union[List[str], Tuple[str]])
Adds multiple nodes to the graph.
 
Parameters:
    nodes (list | tuple): List or tuple of node labels.
compare(self, other: Type[~Graph], operation: str)
Compares two Graph objects based on edge operations.
 
Parameters:
    other (Graph): Another Graph object.
    operation (str): Edge operation ('add_edge', 'delete_edge', 'reverse_edge').
 
Returns:
    str | list: Labels of nodes connected to relevant edges under the specified operation, or an error message.
copy(self)
find_parents(self, node, return_index=False)
Finds the parent nodes of a given node.
 
Parameters:
    node (str): The node for which to find parents.
    return_index (bool): If True, return indices of parent nodes along with their labels.
 
Returns:
    list | tuple: List of parent node labels, or a tuple containing parent node labels and their indices if return_index is True.
has_edge(self, node1, node2, undirected=False) -> bool
Checks if an edge exists in the graph.
 
Parameters:
    node1 (str): First node label.
    node2 (str): Second node label.
    undirected (bool): If True, checks for an undirected edge (i.e., edge exists in either direction).
 
Returns:
    bool: True if the edge exists, False otherwise.
remove_edge(self, edge: Union[List, Tuple])
Removes a single edge from the graph.
 
Parameters:
    edge (list | tuple): Edge represented as a tuple (node1, node2).
remove_edges(self, edges: Union[List[Tuple], Tuple[Tuple]])
Removes multiple edges from the graph.
 
Parameters:
    edges (list | tuple): List or tuple of edges, each represented as a tuple (node1, node2).
remove_node(self, node: str)
Removes a single node from the graph.
 
Parameters:
    node (str): Node label.
remove_nodes(self, nodes: Union[List[str], Tuple[str]])
Removes multiple nodes from the graph.
 
Parameters:
    nodes (list | tuple): List or tuple of node labels.
save(self, filename: str, compression='gzip')
Saves the Graph object to a file.
 
Parameters:
    filename (str): Path to the output file.
to_csv(self, filename)
Saves the Graph object to a CSV file.
 
Parameters:
    filename (str): Path to the output CSV file.
to_key(self, type: str = 'default')
Creates a key for the Graph object.
 
Parameters:
    type (str): Type of key to use. Defaults to 'default'.
 
Returns:
    str: Key representing the graph.
to_numpy(self, return_node_labels=False)
Converts the Graph object to a numpy array.
 
Parameters:
    return_node_labels (bool): If True, return node labels along with the adjacency matrix.
 
Returns:
    np.ndarray | tuple: Adjacency matrix, or a tuple containing the adjacency matrix and node labels.
to_nx(self)
Converts the Graph object to a networkx DiGraph.
 
Returns:
    nx.DiGraph: networkx DiGraph object.
to_pandas(self)
Converts the Graph object to a Pandas DataFrame.
 
Returns:
    pd.DataFrame: DataFrame representing the adjacency matrix.
v_structures(self)
Identifies v-structures in the graph.
 
Returns:
    set: Set of v-structures represented as tuples (node1, node2, node3).

Class methods inherited from structure_learning.data_structures.graph.Graph:
find_parent_nodes(incidence)
Finds parent nodes (nodes with no incoming edges).
 
Parameters:
    incidence (np.ndarray): Adjacency matrix.
 
Returns:
    set: Set of parent node indices.
from_csv(filename)
Creates a Graph object from a CSV file.
 
Parameters:
    filename (str): Path to the input CSV file.
 
Returns:
    GraphGraph object.
from_key(key: str, type: str = 'default', nodes: Union[List, Tuple, numpy.ndarray] = None) -> Type[~Graph]
Creates a Graph object from a key.
 
Parameters:
    key (str): String representation of the graph.
    type (str): Type of key. Defaults to 'default'.
    nodes (list | tuple | np.ndarray): Node labels.
 
Returns:
    GraphGraph object.
from_numpy(incidence: numpy.ndarray, nodes: Union[List, Tuple, numpy.ndarray]) -> Type[~Graph]
Creates a Graph object from a numpy array.
 
Parameters:
    incidence (np.ndarray): Adjacency matrix.
    nodes (list | tuple | np.ndarray): Node labels.
 
Returns:
    GraphGraph object.
from_nx(graph: networkx.classes.digraph.DiGraph) -> Type[~Graph]
Creates a Graph object from a networkx graph.
 
Parameters:
    graph (nx.DiGraph): networkx DiGraph object.
 
Returns:
    GraphGraph object.
from_pandas(graph: pandas.core.frame.DataFrame) -> Type[~Graph]
Creates a Graph object from a Pandas DataFrame.
 
Parameters:
    graph (pd.DataFrame): DataFrame representing the adjacency matrix.
 
Returns:
    GraphGraph object.
has_cycle(graph: Union[numpy.ndarray, Type[~Graph]]) -> bool
Checks if the graph has a cycle.
 
Parameters:
    graph (np.ndarray | Graph): Adjacency matrix or Graph object.
 
Returns:
    bool: True if the graph has a cycle, False otherwise.
load(filename: str, compression='gzip') -> Type[~Graph]
Loads a Graph object from a file.
 
Parameters:
    filename (str): Path to the input file.
 
Returns:
    Graph: Loaded Graph object.

Readonly properties inherited from structure_learning.data_structures.graph.Graph:
dim
Returns the number of nodes in the Graph object.
 
Returns:
    int: Number of nodes.
edges
Returns the list of edges in the graph.
 
Returns:
    set: Set of edges represented as tuples (node1, node2).
shape
Returns the shape of the adjacency matrix.
 
Returns:
    tuple: Shape of the adjacency matrix (rows, columns).

Data descriptors inherited from structure_learning.data_structures.graph.Graph:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object