TGraph

class TGraph[source]

Bases: Graph

Wrapper class for pytorch-geometric edge_index providing fast adjacency look-up.

__init__(*args, ensure_sorted=False, **kwargs)[source]

Initialise graph

Parameters:
  • edge_index – edge index such that edge_index[0] lists the source and edge_index[1] the target node for each edge

  • edge_attr – optionally provide edge weights

  • num_nodes – specify number of nodes (default: max(edge_index)+1)

  • ensure_sorted – if False, assume that the edge_index input is already sorted

  • undir – boolean indicating if graph is directed. If not provided, the edge_index is checked to determine this value.

Methods

__init__

Initialise graph

adj

list neighbours of node

adj_weighted

list neighbours of node and corresponding edge weight :param node: source node

bfs_order

return nodes in breadth-first-search order

connected_component_ids

Find the (weakly)-connected components.

edges

return list of edges where each edge is a tuple (source, target)

edges_weighted

return list of edges where each edge is a tuple (source, target, weight)

from_networkx

from_tg

has_node_labels

is_edge

lcc

neighbourhood

find the neighbourhood of a set of source nodes

nodes_in_lcc

List all nodes in the largest connected component

partition_graph

sample_negative_edges

sample_positive_edges

subgraph

find induced subgraph for a set of nodes

to

Convert to different graph type or move to device

to_networkx

convert graph to NetworkX format

Attributes

device

device holding graph data

nodes

num_edges

num_features

weighted

boolean indicating if graph is weighted

num_nodes

number of nodes

degree

tensor of node degrees

adj_index

adjacency index such that edges starting at node i are given by edge_index[:, adj_index[i]:adj_index[i+1]]

strength

tensor of node strength

weights

edges()[source]

return list of edges where each edge is a tuple (source, target)

edges_weighted()[source]

return list of edges where each edge is a tuple (source, target, weight)

is_edge(source, target)[source]
neighbourhood(nodes: Tensor, hops: int = 1)[source]

find the neighbourhood of a set of source nodes

note that the neighbourhood includes the source nodes themselves

Parameters:
  • nodes – indices of source nodes

  • hops – number of hops for neighbourhood

Returns:

neighbourhood

subgraph(nodes: Tensor, relabel=False, keep_x=True, keep_y=True)[source]

find induced subgraph for a set of nodes

Parameters:

nodes – node indeces

Returns:

subgraph

connected_component_ids()[source]

Find the (weakly)-connected components. Component ids are sorted by size, such that id=0 corresponds to the largest connected component

nodes_in_lcc()[source]

List all nodes in the largest connected component

to_networkx()[source]

convert graph to NetworkX format

to(*args, graph_cls=None, **kwargs)[source]

Convert to different graph type or move to device

Parameters:
  • graph_cls – convert to graph class

  • device – convert to device

Can only specify one argument. If positional, type of move is determined automatically.

bfs_order(start=0)[source]

return nodes in breadth-first-search order

Parameters:

start – index of starting node (default: 0)

Returns:

tensor of node indeces

partition_graph(partition, self_loops=True)[source]
sample_negative_edges(num_samples)[source]
sample_positive_edges(num_samples)[source]