Skip to content

Graph utilities

Subgraphs, negative sampling, dropout, dense conversions and graph statistics.

Function Description
subgraph()
k_hop_subgraph() Computes the num_hops-hop subgraph around the nodes node_idx.
negative_sampling()
structured_negative_sampling() Samples one negative destination for every edge of edge_index: for each edge (src, dst_pos), a node dst_neg such that (src, dst_neg) is not an edge.
dropout_edge() Randomly drops edges of edge_index with probability p.
dropout_node() Randomly drops nodes with probability p, together with all the edges connected to them.
to_dense_batch() Converts the node features of a batch of graphs to a dense array of shape [batch_size, max_num_nodes, num_features], padded with fill_value.
to_dense_adj() Converts the edges of a batch of graphs to dense adjacency matrices of shape [batch_size, max_num_nodes, max_num_nodes].
get_laplacian() Computes the graph Laplacian of the graph given by edge_index and optional edge_weights, in sparse (edge list) form.
homophily() Computes the homophily of a graph: how often connected nodes share the same label.
add_remaining_self_loops() Adds a self-loop (i, i) to every node of the graph that does not already have one.
segregate_self_loops() Splits the edges of a graph into the self-loops and the other edges.
remove_isolated_nodes() Removes the isolated nodes of a graph and relabels the remaining ones to [0, num_remaining_nodes) in their original order.
contains_self_loops() Returns True if the graph given by edge_index contains self-loops.
contains_isolated_nodes() Returns True if the graph given by edge_index contains isolated nodes, i.e. nodes without any edge to another node (self-loops are ignored).
bipartite_subgraph() Returns the subgraph of a bipartite graph induced by a subset of the source nodes and a subset of the destination nodes, i.e. the edges whose source is in subset[0] and destination is in subset[1].
batched_negative_sampling() Samples random negative edges within each graph of a batch of graphs (see negative_sampling()): no negative edge connects two different graphs.
dropout_path() Drops edges along random walks ("MaskGAE: Masked Graph Modeling Meets Graph Autoencoders").
shuffle_node() Randomly shuffles the rows of node_features, within each graph if batch_indices is given.
mask_feature() Randomly masks the features of node_features with probability p, replacing them with fill_value.
add_random_edge() Randomly adds round(p * num_edges) new edges to edge_index.
unbatch()
unbatch_edge_index() Splits the edge_index of a batch of graphs into one edge_index per graph, with the node ids of each graph starting at 0.
assortativity()
get_ppr() Computes approximate personalized PageRank (PPR) vectors with the push algorithm of "Local Graph Partitioning using PageRank Vectors", as used in "Predict then Propagate".
normalized_cut()
train_test_split_edges()