Convert a homogeneous graph to a heterogeneous graph and return. The input graph should have only one type of nodes and edges. Each node and edge stores an integer feature as its type ID (specified by :attr:`ntype_field` and :attr:`etype_field`). DGL uses it to retrieve the type nam
(
G, ntypes, etypes, ntype_field=NTYPE, etype_field=ETYPE, metagraph=None
)
| 670 | |
| 671 | |
| 672 | def to_heterogeneous( |
| 673 | G, ntypes, etypes, ntype_field=NTYPE, etype_field=ETYPE, metagraph=None |
| 674 | ): |
| 675 | """Convert a homogeneous graph to a heterogeneous graph and return. |
| 676 | |
| 677 | The input graph should have only one type of nodes and edges. Each node and edge |
| 678 | stores an integer feature as its type ID |
| 679 | (specified by :attr:`ntype_field` and :attr:`etype_field`). |
| 680 | DGL uses it to retrieve the type names stored in the given |
| 681 | :attr:`ntypes` and :attr:`etypes` arguments. |
| 682 | |
| 683 | The function will automatically distinguish edge types that have the same given |
| 684 | type IDs but different src and dst type IDs. For example, it allows both edges A and B |
| 685 | to have the same type ID 0, but one has (0, 1) and the other as (2, 3) as the |
| 686 | (src, dst) type IDs. In this case, the function will "split" edge type 0 into two types: |
| 687 | (0, ty_A, 1) and (2, ty_B, 3). In another word, these two edges share the same edge |
| 688 | type name, but can be distinguished by an edge type triplet. |
| 689 | |
| 690 | The function stores the node and edge IDs in the input graph using the ``dgl.NID`` |
| 691 | and ``dgl.EID`` names in the ``ndata`` and ``edata`` of the resulting graph. |
| 692 | It also copies any node/edge features from :attr:`G` to the returned heterogeneous |
| 693 | graph, except for reserved fields for storing type IDs (``dgl.NTYPE`` and ``dgl.ETYPE``) |
| 694 | and node/edge IDs (``dgl.NID`` and ``dgl.EID``). |
| 695 | |
| 696 | Parameters |
| 697 | ---------- |
| 698 | G : DGLGraph |
| 699 | The homogeneous graph. |
| 700 | ntypes : list[str] |
| 701 | The node type names. |
| 702 | etypes : list[str] |
| 703 | The edge type names. |
| 704 | ntype_field : str, optional |
| 705 | The feature field used to store node type. (Default: ``dgl.NTYPE``) |
| 706 | etype_field : str, optional |
| 707 | The feature field used to store edge type. (Default: ``dgl.ETYPE``) |
| 708 | metagraph : networkx MultiDiGraph, optional |
| 709 | Metagraph of the returned heterograph. |
| 710 | If provided, DGL assumes that G can indeed be described with the given metagraph. |
| 711 | If None, DGL will infer the metagraph from the given inputs, which could be |
| 712 | costly for large graphs. |
| 713 | |
| 714 | Returns |
| 715 | ------- |
| 716 | DGLGraph |
| 717 | A heterogeneous graph. |
| 718 | |
| 719 | Notes |
| 720 | ----- |
| 721 | * The returned node and edge types may not necessarily be in the same order as |
| 722 | ``ntypes`` and ``etypes``. |
| 723 | * Calling :func:`~dgl.to_homogeneous` then calling :func:`~dgl.to_heterogeneous` again |
| 724 | yields the same result. |
| 725 | |
| 726 | Examples |
| 727 | -------- |
| 728 | |
| 729 | The following example uses PyTorch backend. |