MCPcopy
hub / github.com/dmlc/dgl / remove_self_loop

Function remove_self_loop

python/dgl/transforms/functional.py:2048–2120  ·  view source on GitHub ↗

r"""Remove self-loops for each node in the graph and return a new graph. Parameters ---------- g : DGLGraph The graph. etype : str or (str, str, str), optional The type names of the edges. The allowed type name formats are: * ``(str, str, str)`` for source n

(g, etype=None)

Source from the content-addressed store, hash-verified

2046
2047
2048def remove_self_loop(g, etype=None):
2049 r"""Remove self-loops for each node in the graph and return a new graph.
2050
2051 Parameters
2052 ----------
2053 g : DGLGraph
2054 The graph.
2055 etype : str or (str, str, str), optional
2056 The type names of the edges. The allowed type name formats are:
2057
2058 * ``(str, str, str)`` for source node type, edge type and destination node type.
2059 * or one ``str`` edge type name if the name can uniquely identify a
2060 triplet format in the graph.
2061
2062 Can be omitted if the graph has only one type of edges.
2063
2064 Notes
2065 -----
2066 If a node has multiple self-loops, remove them all. Do nothing for nodes without
2067 self-loops.
2068
2069 This function preserves the batch information.
2070
2071 Examples
2072 ---------
2073
2074 >>> import dgl
2075 >>> import torch
2076
2077 **Homogeneous Graphs**
2078
2079 >>> g = dgl.graph((torch.tensor([0, 0, 0, 1]), torch.tensor([1, 0, 0, 2])))
2080 >>> g.edata['he'] = torch.arange(4).float().reshape(-1, 1)
2081 >>> g = dgl.remove_self_loop(g)
2082 >>> g
2083 Graph(num_nodes=3, num_edges=2,
2084 edata_schemes={'he': Scheme(shape=(2,), dtype=torch.float32)})
2085 >>> g.edata['he']
2086 tensor([[0.],[3.]])
2087
2088 **Heterogeneous Graphs**
2089
2090 >>> g = dgl.heterograph({
2091 ... ('user', 'follows', 'user'): (torch.tensor([0, 1, 1, 1, 2]),
2092 ... torch.tensor([0, 0, 1, 1, 1])),
2093 ... ('user', 'plays', 'game'): (torch.tensor([0, 1]),
2094 ... torch.tensor([0, 1]))
2095 ... })
2096 >>> g = dgl.remove_self_loop(g, etype='follows')
2097 >>> g.num_nodes('user')
2098 3
2099 >>> g.num_nodes('game')
2100 2
2101 >>> g.num_edges('follows')
2102 2
2103 >>> g.num_edges('plays')
2104 2
2105

Callers 3

neighbor_matchingFunction · 0.85
knn_graphFunction · 0.85
segmented_knn_graphFunction · 0.85

Calls 6

DGLErrorClass · 0.85
remove_edgesFunction · 0.85
formatMethod · 0.80
to_canonical_etypeMethod · 0.45
edgesMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected