@@ -38,6 +38,64 @@ function add_self_loops(g::GNNGraph{<:ADJMAT_T})
38
38
g. ndata, g. edata, g. gdata)
39
39
end
40
40
41
+ """
42
+ add_self_loops(g::GNNHeteroGraph, edge_t::EType)
43
+
44
+ Return a graph with the same features as `g`
45
+ but also adding self-loops of the specified type, edge_t
46
+
47
+ Nodes with already existing self-loops of type edge_t will obtain a second self-loop of type edge_t.
48
+
49
+ If the graphs has edge weights for edges of type edge_t, the new edges will have weight 1.
50
+
51
+ If no edges of type edge_t exist, or all existing edges have no weight, then all new self loops will have no weight.
52
+ """
53
+ function add_self_loops (g:: GNNHeteroGraph{Tuple{T, T, V}} , edge_t:: EType ) where {T <: AbstractVector{<:Integer} , V}
54
+ function get_edge_weight_nullable (g:: GNNHeteroGraph{<:COO_T} , edge_t:: EType )
55
+ get (g. graph, edge_t, (nothing , nothing , nothing ))[3 ]
56
+ end
57
+
58
+ src_t, _, tgt_t = edge_t
59
+ (src_t === tgt_t) ||
60
+ @error " cannot add a self-loop with different source and target types"
61
+
62
+ n = get (g. num_nodes, src_t, 0 )
63
+
64
+ if haskey (g. graph, edge_t)
65
+ x = g. graph[edge_t]
66
+ s, t = x[1 : 2 ]
67
+ nodes = convert (typeof (s), [1 : n;])
68
+ s = [s; nodes]
69
+ t = [t; nodes]
70
+ else
71
+ nodes = convert (T, [1 : n;])
72
+ s = nodes
73
+ t = nodes
74
+ end
75
+
76
+ graph = g. graph |> copy
77
+ ew = get (g. graph, edge_t, (nothing , nothing , nothing ))[3 ]
78
+
79
+ if ew != = nothing
80
+ ew = [ew; fill! (similar (ew, n), 1 )]
81
+ end
82
+
83
+ graph[edge_t] = (s, t, ew)
84
+ edata = g. edata |> copy
85
+ ndata = g. ndata |> copy
86
+ ntypes = g. ntypes |> copy
87
+ etypes = g. etypes |> copy
88
+ num_nodes = g. num_nodes |> copy
89
+ num_edges = g. num_edges |> copy
90
+ num_edges[edge_t] = length (get (graph, edge_t, ([],[]))[1 ])
91
+
92
+ return GNNHeteroGraph (graph,
93
+ num_nodes, num_edges, g. num_graphs,
94
+ g. graph_indicator,
95
+ ndata, edata, g. gdata,
96
+ ntypes, etypes)
97
+ end
98
+
41
99
"""
42
100
remove_self_loops(g::GNNGraph)
43
101
0 commit comments