r"""Add self-loops for each node in the graph and return a new graph. Parameters ---------- g : DGLGraph The graph. edge_feat_names : list[str], optional The names of the self-loop features to apply `fill_data`. If None, it will apply `fill_data` to all self-
(g, edge_feat_names=None, fill_data=1.0, etype=None)
| 1904 | |
| 1905 | |
| 1906 | def add_self_loop(g, edge_feat_names=None, fill_data=1.0, etype=None): |
| 1907 | r"""Add self-loops for each node in the graph and return a new graph. |
| 1908 | |
| 1909 | Parameters |
| 1910 | ---------- |
| 1911 | g : DGLGraph |
| 1912 | The graph. |
| 1913 | edge_feat_names : list[str], optional |
| 1914 | The names of the self-loop features to apply `fill_data`. If None, it will apply `fill_data` |
| 1915 | to all self-loop features. Default: None. |
| 1916 | fill_data : int, float or str, optional |
| 1917 | The value to fill the self-loop features. Default: 1. |
| 1918 | |
| 1919 | * If ``fill_data`` is ``int`` or ``float``, self-loop features will be directly given by |
| 1920 | ``fill_data``. |
| 1921 | * if ``fill_data`` is ``str``, self-loop features will be generated by aggregating the |
| 1922 | features of the incoming edges of the corresponding nodes. The supported aggregation are: |
| 1923 | ``'mean'``, ``'sum'``, ``'max'``, ``'min'``. |
| 1924 | etype : str or (str, str, str), optional |
| 1925 | The type names of the edges. The allowed type name formats are: |
| 1926 | |
| 1927 | * ``(str, str, str)`` for source node type, edge type and destination node type. |
| 1928 | * or one ``str`` edge type name if the name can uniquely identify a |
| 1929 | triplet format in the graph. |
| 1930 | |
| 1931 | Can be omitted if the graph has only one type of edges. |
| 1932 | |
| 1933 | Return |
| 1934 | ------ |
| 1935 | DGLGraph |
| 1936 | The graph with self-loops. |
| 1937 | |
| 1938 | Notes |
| 1939 | ----- |
| 1940 | * The function only supports homogeneous graphs or heterogeneous graphs but |
| 1941 | the relation graph specified by the :attr:`etype` argument is homogeneous. |
| 1942 | * The function adds self-loops regardless of whether they already exist or not. |
| 1943 | If one wishes to have exactly one self-loop for every node, |
| 1944 | call :func:`remove_self_loop` before invoking :func:`add_self_loop`. |
| 1945 | * This function discards the batch information. Please use |
| 1946 | :func:`dgl.DGLGraph.set_batch_num_nodes` |
| 1947 | and :func:`dgl.DGLGraph.set_batch_num_edges` on the transformed graph |
| 1948 | to maintain the information. |
| 1949 | |
| 1950 | Examples |
| 1951 | -------- |
| 1952 | >>> import dgl |
| 1953 | >>> import torch |
| 1954 | |
| 1955 | **Homogeneous Graphs** |
| 1956 | |
| 1957 | >>> g = dgl.graph((torch.tensor([0, 0, 2]), torch.tensor([2, 1, 0]))) |
| 1958 | >>> g.ndata['hv'] = torch.arange(3).float().reshape(-1, 1) |
| 1959 | >>> g.edata['he'] = torch.arange(3).float().reshape(-1, 1) |
| 1960 | >>> g = dgl.add_self_loop(g, fill_data='sum') |
| 1961 | >>> g |
| 1962 | Graph(num_nodes=3, num_edges=6, |
| 1963 | ndata_schemes={'hv': Scheme(shape=(1,), dtype=torch.float32)} |
no test coverage detected