gammagl.data.Graph

class Graph(x=None, edge_index=None, edge_attr=None, y=None, spr_format=None, to_tensor=True, **kwargs)[source]

Bases: BaseGraph

A Graph object describe a homogeneous graph. The graph object will hold node-level, link-level and graph-level attributes. In general, Graph tries to mimic the behaviour of a regular Python dictionary. In addition, it provides useful functionality for analyzing graph structures, and provides basic tensor functionalities.

>>> from gammagl.data import Graph
>>> import numpy
>>> g = Graph(x=numpy.random.randn(5, 16), edge_index=[[0, 0, 0], [1, 2, 3]], num_nodes=5,)
>>> print(g)
GNN Graph instance.
number of nodes: 5
number of edges: 2

>>> print(g.indegree.numpy(), g.outdegree.numpy())
[0. 1. 1. 1. 0.] [3. 0. 0. 0. 0.]
Parameters:
  • x (tensor, list, numpy.array) – Node feature matrix with shape [num_nodes, num_node_features]. (default: None)

  • edge_index (tensor, list, numpy.array) – Graph connectivity in COO format with shape [2, num_edges]. (default: None)

  • edge_attr (tensor) – Edge feature matrix with shape [num_edges, num_edge_features]. (default: None)

  • num_nodes (int) – The specified number of nodes. (default: None)

  • y (tensor) – Graph-level or node-level ground-truth labels with arbitrary shape. (default: None)

  • to_tensor (bool) – Set data to tensor

  • spr_format (list(str)) – Specify the other sparse storage format, like csc and csr. (default: None)

to_dict() Dict[str, Any][source]

Returns a dictionary of stored key/value pairs.

to_namedtuple() NamedTuple[source]

Returns a NamedTuple of stored key/value pairs.

__cat_dim__(key: str, value: Any, *args, **kwargs) Any[source]

Returns the dimension for which the value value of the attribute key will get concatenated when creating mini-batches using gammagl.loader.DataLoader.

Note

This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.

__inc__(key: str, value: Any, *args, **kwargs) Any[source]

Returns the incremental count to cumulatively increase the value value of the attribute key when creating mini-batches using gammagl.loader.DataLoader.

Note

This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.

is_node_attr(key: str) bool[source]

Returns True if the object at key denotes a node-level attribute.

is_edge_attr(key: str) bool[source]

Returns True if the object at key key denotes an edge-level attribute.

property in_degree

Graph property, return the node in-degree of the graph.

property out_degree

Graph property, return the node out-degree of the graph.

add_self_loop(n_loops=1)[source]
Parameters:

n_loops (int) –

Returns:

  • edge_index (Tensor) – original edges with self loop edges.

  • edge_attr (FloatTensor) – attributes of edges.

sorted_edges(sort_by='src')[source]

Return sorted edges with different strategies. This function will return sorted edges with different strategy. If sort_by="src", then edges will be sorted by src nodes and otherwise dst.

Parameters:

sort_by (str) – The type for sorted edges. (“src” or “dst”)

Returns:

A tuple of (sorted_src, sorted_dst, sorted_eid).

Return type:

tuple

tensor(inplace=True)[source]

Convert the Graph into paddle.Tensor format. In paddle.Tensor format, the graph edges and node features are in paddle.Tensor format. You can use send and recv in paddle.Tensor graph.

Parameters:

inplace (bool) – (Default True) Whether to convert the graph into tensor inplace.

numpy(inplace=True)[source]

Convert the Graph into numpy format. In numpy format, the graph edges and node features are in numpy.ndarray format. But you can’t use send and recv in numpy graph.

Parameters:

inplace (bool) – (Default True) Whether to convert the graph into numpy inplace.

to_heterogeneous(node_type=None, edge_type=None, node_type_names: List[str] | None = None, edge_type_names: List[Tuple[str, str, str]] | None = None)[source]

Converts a Graph object to a heterogeneous HeteroGraph object. For this, node and edge attributes are splitted according to the node-level and edge-level vectors node_type and edge_type, respectively. node_type_names and edge_type_names can be used to give meaningful node and edge type names, respectively. That is, the node_type 0 is given by node_type_names[0]. If the Graph object was constructed via to_homogeneous(), the object can be reconstructed without any need to pass in additional arguments.

Parameters:
  • node_type (tensor, optional) – A node-level vector denoting the type of each node. (default: None)

  • edge_type (tensor, optional) – An edge-level vector denoting the type of each edge. (default: None)

  • node_type_names (list[str], optional) – The names of node types. (default: None)

  • edge_type_names (list[tuple[str, str, str]], optional) – The names of edge types. (default: None)

classmethod from_dict(mapping: Dict[str, Any])[source]

Creates a Graph object from a Python dictionary.

property num_node_features: int

Returns the number of features per node in the graph.

property num_features: int

Returns the number of features per node in the graph. Alias for num_node_features.

property num_edge_features: int

Returns the number of features per edge in the graph.

dump(path)[source]

Dump the graph into a directory.

This function will dump the graph information into the given directory path. The graph can be read back with pgl.Graph.load

Parameters:

path (str) – The directory for the storage of the graph.

classmethod load(path, mmap_mode='r')[source]

Load Graph from path and return a Graph in numpy.

Parameters:
  • path (str) – The directory path of the stored Graph.

  • mmap_mode (str) – Default mmap_mode="r". If not None, memory-map the graph.