dgl.DGLGraph

class dgl.DGLGraph[source]

Class for storing graph structure and node/edge feature data.

There are a few ways to create a DGLGraph:

  • To create a homogeneous graph from Tensor data, use dgl.graph().

  • To create a heterogeneous graph from Tensor data, use dgl.heterograph().

  • To create a graph from other data sources, use dgl.* create ops. See Graph Create Ops.

Read the user guide chapter Chapter 1: Graph for an in-depth explanation about its usage.

Querying metagraph structure

Methods for getting information about the node and edge types. They are typically useful when the graph is heterogeneous.

DGLGraph.ntypes

Return all the node type names in the graph.

DGLGraph.etypes

Return all the edge type names in the graph.

DGLGraph.srctypes

Return all the source node type names in this graph.

DGLGraph.dsttypes

Return all the destination node type names in this graph.

DGLGraph.canonical_etypes

Return all the canonical edge types in the graph.

DGLGraph.metagraph()

Return the metagraph of the heterograph.

DGLGraph.to_canonical_etype(etype)

Convert an edge type to the corresponding canonical edge type in the graph.

Querying graph structure

Methods for getting information about the graph structure such as capacity, connectivity, neighborhood, etc.

DGLGraph.num_nodes([ntype])

Return the number of nodes in the graph.

DGLGraph.number_of_nodes([ntype])

Alias of num_nodes()

DGLGraph.num_edges([etype])

Return the number of edges in the graph.

DGLGraph.number_of_edges([etype])

Alias of num_edges()

DGLGraph.num_src_nodes([ntype])

Return the number of source nodes in the graph.

DGLGraph.number_of_src_nodes([ntype])

Alias of num_src_nodes()

DGLGraph.num_dst_nodes([ntype])

Return the number of destination nodes in the graph.

DGLGraph.number_of_dst_nodes([ntype])

Alias of num_dst_nodes()

DGLGraph.is_unibipartite

Return whether the graph is a uni-bipartite graph.

DGLGraph.is_multigraph

Return whether the graph is a multigraph with parallel edges.

DGLGraph.is_homogeneous

Return whether the graph is a homogeneous graph.

DGLGraph.has_nodes(vid[, ntype])

Return whether the graph contains the given nodes.

DGLGraph.has_edges_between(u, v[, etype])

Return whether the graph contains the given edges.

DGLGraph.predecessors(v[, etype])

Return the predecessor(s) of a particular node with the specified edge type.

DGLGraph.successors(v[, etype])

Return the successor(s) of a particular node with the specified edge type.

DGLGraph.edge_ids(u, v[, return_uv, etype])

Return the edge ID(s) given the two endpoints of the edge(s).

DGLGraph.find_edges(eid[, etype])

Return the source and destination node ID(s) given the edge ID(s).

DGLGraph.in_edges(v[, form, etype])

Return the incoming edges of the given nodes.

DGLGraph.out_edges(u[, form, etype])

Return the outgoing edges of the given nodes.

DGLGraph.in_degrees([v, etype])

Return the in-degree(s) of the given nodes.

DGLGraph.out_degrees([u, etype])

Return the out-degree(s) of the given nodes.

Querying and manipulating sparse format

Methods for getting or manipulating the internal storage formats of a DGLGraph.

DGLGraph.formats([formats])

Get a cloned graph with the specified allowed sparse format(s) or query for the usage status of sparse formats.

DGLGraph.create_formats_()

Create all sparse matrices allowed for the graph.

Querying and manipulating node/edge ID type

Methods for getting or manipulating the data type for storing structure-related data such as node and edge IDs.

DGLGraph.idtype

The data type for storing the structure-related graph information such as node and edge IDs.

DGLGraph.long()

Cast the graph to one with idtype int64

DGLGraph.int()

Cast the graph to one with idtype int32

Using Node/edge features

Methods for getting or setting the data type for storing structure-related data such as node and edge IDs.

DGLGraph.nodes

Return a node view

DGLGraph.ndata

Return a node data view for setting/getting node features

DGLGraph.edges

Return an edge view

DGLGraph.edata

Return an edge data view for setting/getting edge features.

DGLGraph.node_attr_schemes([ntype])

Return the node feature schemes for the specified type.

DGLGraph.edge_attr_schemes([etype])

Return the edge feature schemes for the specified type.

DGLGraph.srcnodes

Return a node view for source nodes

DGLGraph.dstnodes

Return a node view for destination nodes

DGLGraph.srcdata

Return a node data view for setting/getting source node features.

DGLGraph.dstdata

Return a node data view for setting/getting destination node features.

Transforming graph

Methods for generating a new graph by transforming the current ones. Most of them are alias of the Subgraph Extraction Ops and Graph Transform Ops under the dgl namespace.

DGLGraph.subgraph(nodes, *[, relabel_nodes, …])

Alias of dgl.node_subgraph().

DGLGraph.edge_subgraph(edges, *[, …])

Alias of dgl.edge_subgraph().

DGLGraph.node_type_subgraph(ntypes[, …])

Alias of dgl.node_type_subgraph().

DGLGraph.edge_type_subgraph(etypes[, …])

Alias of dgl.edge_type_subgraph().

DGLGraph.__getitem__(key)

Return the relation slice of this graph.

DGLGraph.line_graph([backtracking, shared])

Alias of dgl.line_graph().

DGLGraph.reverse([copy_ndata, copy_edata, …])

Alias of dgl.reverse().

DGLGraph.add_self_loop([edge_feat_names, …])

Alias of dgl.add_self_loop().

DGLGraph.remove_self_loop([etype])

Alias of dgl.remove_self_loop().

DGLGraph.to_simple([return_counts, …])

Alias of dgl.to_simple().

DGLGraph.to_cugraph()

Convert a DGL graph to a cugraph.Graph and return.

DGLGraph.reorder_graph([node_permute_algo, …])

Alias of dgl.reorder_graph().

Adjacency and incidence matrix

Methods for getting the adjacency and the incidence matrix of the graph.

DGLGraph.adj([etype, eweight_name])

Get the adjacency matrix of the graph.

DGLGraph.adjacency_matrix([etype])

Alias of adj()

DGLGraph.adj_tensors(fmt[, etype])

Return the adjacency matrix of edges of the given edge type as tensors of a sparse matrix representation. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. :param fmt: Either coo, csr or csc. :type fmt: str :param etype: The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges. :type etype: str or (str, str, str), optional.

DGLGraph.adj_external([transpose, ctx, …])

Return the adjacency matrix in an external format, such as Scipy or backend dependent sparse tensor.

DGLGraph.inc(typestr[, ctx, etype])

Return the incidence matrix representation of edges with the given edge type.

DGLGraph.incidence_matrix(typestr[, ctx, etype])

Return the incidence matrix representation of edges with the given edge type.

Computing with DGLGraph

Methods for performing message passing, applying functions on node/edge features, etc.

DGLGraph.apply_nodes(func[, v, ntype])

Update the features of the specified nodes by the provided function.

DGLGraph.apply_edges(func[, edges, etype])

Update the features of the specified edges by the provided function.

DGLGraph.send_and_recv(edges, message_func, …)

Send messages along the specified edges and reduce them on the destination nodes to update their features.

DGLGraph.pull(v, message_func, reduce_func)

Pull messages from the specified node(s)’ predecessors along the specified edge type, aggregate them to update the node features.

DGLGraph.push(u, message_func, reduce_func)

Send message from the specified node(s) to their successors along the specified edge type and update their node features.

DGLGraph.update_all(message_func, reduce_func)

Send messages along all the edges of the specified type and update all the nodes of the corresponding destination type.

DGLGraph.multi_update_all(etype_dict, …[, …])

Send messages along all the edges, reduce them by first type-wisely then across different types, and then update the node features of all the nodes.

DGLGraph.prop_nodes(nodes_generator, …[, …])

Propagate messages using graph traversal by sequentially triggering pull() on nodes.

DGLGraph.prop_edges(edges_generator, …[, …])

Propagate messages using graph traversal by sequentially triggering send_and_recv() on edges.

DGLGraph.filter_nodes(predicate[, nodes, ntype])

Return the IDs of the nodes with the given node type that satisfy the given predicate.

DGLGraph.filter_edges(predicate[, edges, etype])

Return the IDs of the edges with the given edge type that satisfy the given predicate.

Querying and manipulating batch information

Methods for getting/setting the batching information if the current graph is a batched graph generated from dgl.batch(). They are also widely used in the Batching and Reading Out Ops.

DGLGraph.batch_size

Return the number of graphs in the batched graph.

DGLGraph.batch_num_nodes([ntype])

Return the number of nodes for each graph in the batch with the specified node type.

DGLGraph.batch_num_edges([etype])

Return the number of edges for each graph in the batch with the specified edge type.

DGLGraph.set_batch_num_nodes(val)

Manually set the number of nodes for each graph in the batch with the specified node type.

DGLGraph.set_batch_num_edges(val)

Manually set the number of edges for each graph in the batch with the specified edge type.

Mutating topology

Methods for mutating the graph structure in-place.

DGLGraph.add_nodes(num[, data, ntype])

Add new nodes of the same node type

DGLGraph.add_edges(u, v[, data, etype])

Add multiple new edges for the specified edge type

DGLGraph.remove_nodes(nids[, ntype, store_ids])

Remove multiple nodes with the specified node type

DGLGraph.remove_edges(eids[, etype, store_ids])

Remove multiple edges with the specified edge type

Device Control

Methods for getting or changing the device on which the graph is hosted.

DGLGraph.to(device, **kwargs)

Move ndata, edata and graph structure to the targeted device (cpu/gpu).

DGLGraph.device

Get the device of the graph.

DGLGraph.cpu()

Return a new copy of this graph on CPU.

DGLGraph.pin_memory_()

Pin the graph structure and node/edge data to the page-locked memory for GPU zero-copy access.

DGLGraph.unpin_memory_()

Unpin the graph structure and node/edge data from the page-locked memory.

DGLGraph.is_pinned()

Check if the graph structure is pinned to the page-locked memory.

Misc

Other utility methods.

DGLGraph.local_scope()

Enter a local scope context for the graph.