gammagl.data.HeteroGraph¶
- class HeteroGraph(_mapping: Dict[str, Any] | None = None, **kwargs)[source]¶
Bases:
BaseGraphA data object describing a heterogeneous graph, holding multiple node and/or edge types in disjunct storage objects. Storage objects can hold either node-level, link-level or graph-level attributes. In general,
HeteroGraphtries to mimic the behaviour of a regular nested Python dictionary. In addition, it provides useful functionality for analyzing graph structures, and provides basic tensor functionalities.>>> from gammagl.data import HeteroGraph >>> import tensorlayerx as tlx >>> data = HeteroGraph() # Create two node types "paper" and "author" holding a feature matrix: >>> num_papers = 6 >>> num_paper_features = 16 >>> num_authors = 3 >>> num_authors_features = 8 >>> data['paper'].x = tlx.random_uniform((num_papers, num_paper_features)) >>> data['author'].x = tlx.random_uniform((num_authors, num_authors_features)) # Create an edge type "(author, writes, paper)" and building the # graph connectivity: >>> edge = tlx.convert_to_tensor([ ... [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2], ... [0, 1, 3, 5, 0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] ... ]) >>> data['author', 'writes', 'paper'].edge_index = edge >>> data['paper'].num_nodes 3 >>> data.num_nodes 9 >>> data['author', 'writes', 'paper'].num_edges 15 >>> data.num_edges 15
Note that there exists multiple ways to create a heterogeneous graph data, e.g.: * To initialize a node of type
"paper"holding a node feature matrixx_papernamedx:>>> from gammagl.data import HeteroGraph >>> data = HeteroGraph() >>> data['paper'].x = x_paper >>> data = HeteroGraph(paper={ 'x': x_paper }) >>> data = HeteroGraph({'paper': { 'x': x_paper }})
To initialize an edge from source node type
"author"to destination node type"paper"with relation type"writes"holding a graph connectivity matrixedge_index_author_papernamededge_index:
>>> data = HeteroGraph() >>> data['author', 'writes', 'paper'].edge_index = edge_index_author_paper >>> data = HeteroGraph(author__writes__paper={ 'edge_index': edge_index_author_paper }) >>> data = HeteroGraph({ ('author', 'writes', 'paper'): { 'edge_index': edge_index_author_paper } })
- node_items() List[Tuple[str, NodeStorage]][source]¶
Returns a list of node type and node storage pairs.
- edge_items() List[Tuple[Tuple[str, str, str], EdgeStorage]][source]¶
Returns a list of edge type and edge storage pairs.
- to_namedtuple() NamedTuple[source]¶
Returns a
NamedTupleof stored key/value pairs.
- __cat_dim__(key: str, value: Any, store: NodeStorage | EdgeStorage | None = None, *args, **kwargs) Any[source]¶
Returns the dimension for which the value
valueof the attributekeywill get concatenated when creating mini-batches usinggammagl.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, store: NodeStorage | EdgeStorage | None = None, *args, **kwargs) Any[source]¶
Returns the incremental count to cumulatively increase the value
valueof the attributekeywhen creating mini-batches usinggammagl.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.
- property num_node_features: Dict[str, int]¶
Returns the number of features per node type in the graph.
- property num_features: Dict[str, int]¶
Returns the number of features per node type in the graph. Alias for
num_node_features.
- property num_edge_features: Dict[Tuple[str, str, str], int]¶
Returns the number of features per edge type in the graph.
- 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.
- metadata() Tuple[List[str], List[Tuple[str, str, str]]][source]¶
Returns the heterogeneous meta-data, i.e. its node and edge types.
>>> data = HeteroGraph() >>> data['paper'].x = ... >>> data['author'].x = ... >>> data['author', 'writes', 'paper'].edge_index = ... >>> print(data.metadata()) (['paper', 'author'], [('author', 'writes', 'paper')])
- collect(key: str) Dict[str | Tuple[str, str, str], Any][source]¶
Collects the attribute
keyfrom all node and edge types.>>> data = HeteroGraph() >>> data['paper'].x = ... >>> data['author'].x = ... >>> print(data.collect('x')) { 'paper': ..., 'author': ...}
Note
This is equivalent to writing
data.x_dict.
- get_node_store(key: str) NodeStorage[source]¶
Gets the
NodeStorageobject of a particular node typekey. If the storage is not present yet, will create a newgammagl.data.storage.NodeStorageobject for the given node type.>>> data = HeteroGraph() >>> node_storage = data.get_node_store('paper')
- get_edge_store(src: str, rel: str, dst: str) EdgeStorage[source]¶
Gets the
EdgeStorageobject of a particular edge type given by the tuple(src, rel, dst). If the storage is not present yet, will create a newgammagl.data.storage.EdgeStorageobject for the given edge type.>>> data = HeteroGraph() >>> edge_storage = data.get_edge_store('author', 'writes', 'paper')
- rename(name: str, new_name: str) HeteroGraph[source]¶
Renames the node type
nametonew_namein-place.
- to_homogeneous(node_attrs: List[str] | None = None, edge_attrs: List[str] | None = None, add_node_type: bool = True, add_edge_type: bool = True) Graph[source]¶
Converts a
HeteroGraphobject to a homogeneousGraphobject. By default, all features with same feature dimensionality across different types will be merged into a single representation, unless otherwise specified via thenode_attrsandedge_attrsarguments.Furthermore, attributes named
node_typeandedge_typewill be added to the returnedGraphobject, denoting node-level and edge-level vectors holding the node and edge type as integers, respectively.- Parameters:
node_attrs (list[str], optional) – The node features to combine across all node types. These node features need to be of the same feature dimensionality. If set to
None, will automatically determine which node features to combine. (default:None)edge_attrs (list[str], optional) – The edge features to combine across all edge types. These edge features need to be of the same feature dimensionality. If set to
None, will automatically determine which edge features to combine. (default:None)add_node_type (bool, optional) – If set to
False, will not add the node-level vectornode_typeto the returnedGraphobject. (default:True)add_edge_type (bool, optional) – If set to
False, will not add the edge-level vectoredge_typeto the returnedGraphobject. (default:True)