Convert a heterogeneous graph to a homogeneous graph and return. By default, the function stores the node and edge types of the input graph as the ``dgl.NTYPE`` and ``dgl.ETYPE`` features in the returned graph. Each feature is an integer representing the type id, determined by the :
(
G, ndata=None, edata=None, store_type=True, return_count=False
)
| 890 | |
| 891 | |
| 892 | def to_homogeneous( |
| 893 | G, ndata=None, edata=None, store_type=True, return_count=False |
| 894 | ): |
| 895 | """Convert a heterogeneous graph to a homogeneous graph and return. |
| 896 | |
| 897 | By default, the function stores the node and edge types of the input graph as |
| 898 | the ``dgl.NTYPE`` and ``dgl.ETYPE`` features in the returned graph. |
| 899 | Each feature is an integer representing the type id, determined by the |
| 900 | :meth:`DGLGraph.get_ntype_id` and :meth:`DGLGraph.get_etype_id` methods. |
| 901 | One can omit it by specifying ``store_type=False``. |
| 902 | |
| 903 | The result graph assigns nodes and edges of the same type with IDs in continuous range |
| 904 | (i.e., nodes of the first type have IDs 0 ~ ``G.num_nodes(G.ntypes[0])``; nodes |
| 905 | of the second type come after; so on and so forth). Therefore, a more memory-efficient |
| 906 | format for type information is an integer list; the i^th corresponds to |
| 907 | the number of nodes/edges of the i^th type. One can choose this format by |
| 908 | specifying ``return_count=True``. |
| 909 | |
| 910 | Parameters |
| 911 | ---------- |
| 912 | G : DGLGraph |
| 913 | The heterogeneous graph. |
| 914 | ndata : list[str], optional |
| 915 | The node features to combine across all node types. For each feature ``feat`` in |
| 916 | :attr:`ndata`, it concatenates ``G.nodes[T].data[feat]`` across all node types ``T``. |
| 917 | As a result, the feature ``feat`` of all node types should have the same shape and |
| 918 | data type. By default, the returned graph will not have any node features. |
| 919 | edata : list[str], optional |
| 920 | The edge features to combine across all edge types. For each feature ``feat`` in |
| 921 | :attr:`edata`, it concatenates ``G.edges[T].data[feat]`` across all edge types ``T``. |
| 922 | As a result, the feature ``feat`` of all edge types should have the same shape and |
| 923 | data type. By default, the returned graph will not have any edge features. |
| 924 | store_type : bool, optional |
| 925 | If True, store type information as the ``dgl.NTYPE`` and ``dgl.ETYPE`` features |
| 926 | in the returned graph. |
| 927 | return_count : bool, optional |
| 928 | If True, return type information as an integer list; the i^th element corresponds to |
| 929 | the number of nodes/edges of the i^th type. |
| 930 | |
| 931 | Returns |
| 932 | ------- |
| 933 | DGLGraph |
| 934 | A homogeneous graph. |
| 935 | ntype_count : list[int], optional |
| 936 | Number of nodes of each type. Return when ``return_count`` is True. |
| 937 | etype_count : list[int], optional |
| 938 | Number of edges of each type. Return when ``return_count`` is True. |
| 939 | |
| 940 | Notes |
| 941 | ----- |
| 942 | |
| 943 | * Calculating type information may introduce noticeable cost. Setting both ``store_type`` |
| 944 | and ``return_count`` to False can avoid such cost if type information is not needed. |
| 945 | Otherwise, DGL recommends to use ``store_type=False`` and ``return_count=True`` due |
| 946 | to its memory efficiency. |
| 947 | * The ``ntype_count`` and ``etype_count`` lists can help speed up some operations. |
| 948 | See :class:`~dgl.nn.pytorch.conv.RelGraphConv` for such an example. |
| 949 | * Calling :func:`~dgl.to_homogeneous` then calling :func:`~dgl.to_heterogeneous` again |
no test coverage detected