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

This module defines the DAG class (Directed Acyclic Graph).
 
Classes:
    DAG: Inherits from Graph and provides methods for DAG manipulation and generation.

 
Modules
       
graphical_models
itertools
numpy
networkx
pandas

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

 
class DAG(structure_learning.data_structures.graph.Graph)
    DAG(incidence=None, nodes=None)
 
Represents a Directed Acyclic Graph (DAG).
 
Attributes:
    incidence (numpy.ndarray): Adjacency matrix of the graph.
    nodes (list): List of node labels.
 
 
Method resolution order:
DAG
structure_learning.data_structures.graph.Graph
builtins.object

Methods defined here:
__init__(self, incidence=None, nodes=None)
Initialize a DAG instance.
 
Parameters:
    incidence (numpy.ndarray): Adjacency matrix of the graph.
    nodes (list): List of node labels.
 
Raises:
    Exception: If the adjacency matrix contains cycles.
fit(self, data: pandas.core.frame.DataFrame)
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.
to_cpdag(self, blocklist: numpy.ndarray = None, verbose=False)
Replace with arcs those edges whose orientations can be determined by Meek rules:
=====
See Koller & Friedman, Algorithm 3.5
 
Adapted from pgmpy (https://github.com/uhlerlab/graphical_models/blob/main/graphical_models/classes/dags/pdag.py)
Parameters:
    verbose (bool): If True, print detailed information about the process.
Returns:
    CPDAG: Completed Partially Directed Acyclic Graph.
to_cpdag_old(self)
Convert the DAG to a CPDAG.
 
Returns:
    CPDAG: Completed Partially Directed Acyclic Graph.
topological_sort(self)
Perform topological sorting of the nodes in the DAG.
 
Returns:
    List: Nodes sorted in topological order.

Class methods defined here:
compute_ancestor_matrix(adj_matrix)
Compute the ancestor matrix from an adjacency matrix.
 
Parameters:
    adj_matrix (numpy.ndarray, optional): Adjacency matrix of the graph.
 
Returns:
    numpy.ndarray: Ancestor matrix.
count_dags(n: int) -> int
Count all possible DAGs for a given number of nodes.
 
Parameters:
    n (int): Number of nodes.
 
Returns:
    int: Number of possible DAGs.
generate_all_dags(n_nodes, node_labels=None)
Generate all unique DAGs with a specified number of nodes.
 
Parameters:
    n_nodes (int): Number of nodes in the graph.
    node_labels (list, optional): List of node labels.
 
Returns:
    list: List of all unique DAGs.
generate_random(nodes, prob=0.5, seed=None)
Generate a random DAG represented by a lower triangular adjacency matrix.
 
Parameters:
    nodes (list): List of node labels.
    prob (float): Edge probability.
    seed (int, optional): Seed for random number generation.
 
Returns:
    DAG: Randomly generated DAG.

Methods inherited from structure_learning.data_structures.graph.Graph:
__copy__(self)
__eq__(self, g: Type[ForwardRef('G')])
Checks equality of two Graph objects.
 
Parameters:
    g (Graph): Another Graph object.
 
Returns:
    bool: True if the graphs are equal, False otherwise.
__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

Data and other attributes inherited from structure_learning.data_structures.graph.Graph:
__hash__ = None

 
Functions
       
comb(n, k, /)
Number of ways to choose k items from n items without repetition and without order.
 
Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates
to zero when k > n.
 
Also called the binomial coefficient because it is equivalent
to the coefficient of k-th term in polynomial expansion of the
expression (1 + x)**n.
 
Raises TypeError if either of the arguments are not integers.
Raises ValueError if either of the arguments are negative.

 
Data
        D = ~DAG