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

This module provides the Graph class and associated methods for representing and manipulating graph structures.
 
The Graph class supports operations such as adding/removing nodes and edges, finding parent nodes, converting between different graph representations (numpy, pandas, networkx), and visualizing graphs.
 
It also includes utility methods for checking graph properties like cycles and performing edge operations.

 
Modules
       
numpy
networkx
pandas
pygraphviz
matplotlib.pyplot
re

 
Classes
       
builtins.object
Graph

 
class Graph(builtins.object)
    Graph(incidence: Union[numpy.ndarray, pandas.core.frame.DataFrame] = None, nodes: Union[List, Tuple] = None, weights: Union[numpy.ndarray, pandas.core.frame.DataFrame] = None)
 
Class wrapper for graphs.
 
  Methods defined here:
__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.
__init__(self, incidence: Union[numpy.ndarray, pandas.core.frame.DataFrame] = None, nodes: Union[List, Tuple] = None, weights: Union[numpy.ndarray, pandas.core.frame.DataFrame] = None)
Initialise a graph instance.
 
Parameters:
    incidence (np.ndarray | pd.DataFrame): Adjacency matrix representing the graph structure.
    nodes (list | tuple): Node labels. If incidence is a DataFrame, this parameter is ignored.
    weights (np.ndarray | pd.DataFrame): Edge weights. Defaults to the adjacency matrix if not provided.
__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.
plot(self, filename=None, text=None, edge_colors: dict = None, edge_weights: dict = 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.
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 defined here:
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 defined here:
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 defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

Data and other attributes defined here:
__hash__ = None

 
Data
        G = ~Graph
List = typing.List
Tuple = typing.Tuple
Type = typing.Type
Union = typing.Union