@@ -280,6 +280,15 @@ 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+ ))
288+ isnothing (g. graph_indicator) && return nothing
289+ return cat_features (g. graph_indicator, ones_like (g. graph_indicator, n_new_nodes))
290+ end
291+
283292"""
284293 add_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
285294 add_edges(g::GNNGraph, (s, t); [edata])
@@ -290,7 +299,8 @@ Optionally, pass the edge weight `w` and the features `edata` for the new edges
290299Returns a new graph sharing part of the underlying data with `g`.
291300
292301If the `s` or `t` contain nodes that are not already present in the graph,
293- they are added to the graph as well.
302+ they are added to the graph as well. Adding new nodes to a batched graph is
303+ not supported because their graph assignment is ambiguous.
294304
295305# Examples
296306
@@ -333,6 +343,9 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
333343 end
334344 @assert minimum (snew) >= 1
335345 @assert minimum (tnew) >= 1
346+ num_nodes = max (maximum (snew), maximum (tnew), g. num_nodes)
347+ graph_indicator = _graph_indicator_after_node_growth (g, num_nodes - g. num_nodes)
348+
336349 num_new = length (snew)
337350 edata = normalize_graphdata (edata, default_name = :e , n = num_new)
338351 edata = cat_features (g. edata, edata)
@@ -343,7 +356,6 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
343356 w = get_edge_weight (g)
344357 w = cat_features (w, wnew, g. num_edges, num_new)
345358
346- num_nodes = max (maximum (snew), maximum (tnew), g. num_nodes)
347359 if num_nodes > g. num_nodes
348360 ndata_new = normalize_graphdata ((;), default_name = :x , n = num_nodes - g. num_nodes)
349361 ndata = cat_features (g. ndata, ndata_new)
@@ -353,7 +365,7 @@ function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
353365
354366 return GNNGraph ((s, t, w),
355367 num_nodes, length (s), g. num_graphs,
356- g . graph_indicator,
368+ graph_indicator,
357369 ndata, edata, g. gdata)
358370end
359371
@@ -553,15 +565,17 @@ end
553565
554566Add `n` new nodes to graph `g`. In the
555567new graph, these nodes will have indexes from `g.num_nodes + 1`
556- to `g.num_nodes + n`.
568+ to `g.num_nodes + n`. Adding nodes to a batched graph is not supported
569+ because their graph assignment is ambiguous.
557570"""
558571function add_nodes (g:: GNNGraph{<:COO_T} , n:: Integer ; ndata = (;))
572+ graph_indicator = _graph_indicator_after_node_growth (g, n)
559573 ndata = normalize_graphdata (ndata, default_name = :x , n = n)
560574 ndata = cat_features (g. ndata, ndata)
561575
562576 GNNGraph (g. graph,
563577 g. num_nodes + n, g. num_edges, g. num_graphs,
564- g . graph_indicator,
578+ graph_indicator,
565579 ndata, g. edata, g. gdata)
566580end
567581
0 commit comments