@@ -280,6 +280,16 @@ function remove_nodes(g::GNNGraph, p::AbstractFloat)
280280 return remove_nodes (g, nodes_to_remove)
281281end
282282
283+ function _graph_indicator_after_node_growth (g:: GNNGraph , n_new_nodes:: Integer )
284+ n_new_nodes <= 0 && return g. graph_indicator
285+ g. num_graphs > 1 && throw (ArgumentError (
286+ " Cannot add nodes to a batched graph without specifying which graph they belong to. " *
287+ " Use `unbatch`, modify the component graphs, and `batch` them again." ,
288+ ))
289+ isnothing (g. graph_indicator) && return nothing
290+ return cat_features (g. graph_indicator, ones_like (g. graph_indicator, n_new_nodes))
291+ end
292+
283293"""
284294 add_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
285295 add_edges(g::GNNGraph, (s, t); [edata])
@@ -290,7 +300,8 @@ Optionally, pass the edge weight `w` and the features `edata` for the new edges
290300Returns a new graph sharing part of the underlying data with `g`.
291301
292302If the `s` or `t` contain nodes that are not already present in the graph,
293- they are added to the graph as well.
303+ they are added to the graph as well. Adding new nodes to a batched graph is
304+ not supported because their graph assignment is ambiguous.
294305
295306# Examples
296307
@@ -335,6 +346,9 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
335346 end
336347 @assert minimum (snew) >= 1
337348 @assert minimum (tnew) >= 1
349+ num_nodes = max (maximum (snew), maximum (tnew), g. num_nodes)
350+ graph_indicator = _graph_indicator_after_node_growth (g, num_nodes - g. num_nodes)
351+
338352 num_new = length (snew)
339353 edata = normalize_graphdata (edata, default_name = :e , n = num_new)
340354 edata = cat_features (g. edata, edata)
@@ -345,7 +359,6 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
345359 w = get_edge_weight (g)
346360 w = cat_features (w, wnew, g. num_edges, num_new)
347361
348- num_nodes = max (maximum (snew), maximum (tnew), g. num_nodes)
349362 if num_nodes > g. num_nodes
350363 ndata_new = normalize_graphdata ((;), default_name = :x , n = num_nodes - g. num_nodes)
351364 ndata = cat_features (g. ndata, ndata_new)
@@ -355,7 +368,7 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
355368
356369 return GNNGraph ((s, t, w),
357370 num_nodes, length (s), g. num_graphs,
358- g . graph_indicator,
371+ graph_indicator,
359372 ndata, edata, g. gdata)
360373end
361374
@@ -555,15 +568,17 @@ end
555568
556569Add `n` new nodes to graph `g`. In the
557570new graph, these nodes will have indexes from `g.num_nodes + 1`
558- to `g.num_nodes + n`.
571+ to `g.num_nodes + n`. Adding nodes to a batched graph is not supported
572+ because their graph assignment is ambiguous.
559573"""
560574function add_nodes (g:: GNNGraph{<:COO_T} , n:: Integer ; ndata = (;))
575+ graph_indicator = _graph_indicator_after_node_growth (g, n)
561576 ndata = normalize_graphdata (ndata, default_name = :x , n = n)
562577 ndata = cat_features (g. ndata, ndata)
563578
564579 GNNGraph (g. graph,
565580 g. num_nodes + n, g. num_edges, g. num_graphs,
566- g . graph_indicator,
581+ graph_indicator,
567582 ndata, g. edata, g. gdata)
568583end
569584
0 commit comments