Skip to content

Commit 2244bdc

Browse files
committed
Guard node growth on batched graphs
1 parent 54025c6 commit 2244bdc

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎GNNGraphs/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "GNNGraphs"
22
uuid = "aed8fd31-079b-4b5a-b342-a13352159b8c"
3-
version = "1.6.1-DEV"
3+
version = "1.6.2-DEV"
44
authors = ["Carlo Lucibello and contributors"]
55

66
[workspace]

‎GNNGraphs/src/transform.jl‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,15 @@ function remove_nodes(g::GNNGraph, p::AbstractFloat)
280280
return remove_nodes(g, nodes_to_remove)
281281
end
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
290299
Returns a new graph sharing part of the underlying data with `g`.
291300
292301
If 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)
358370
end
359371

@@ -553,15 +565,17 @@ end
553565
554566
Add `n` new nodes to graph `g`. In the
555567
new 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
"""
558571
function 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)
566580
end
567581

‎GNNGraphs/test/transform.jl‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,40 @@ end
467467
end
468468
end
469469

470+
@testitem "node growth and batched graph indicators" setup=[GraphsTestModule] begin
471+
using .GraphsTestModule
472+
473+
g1 = GNNGraph(([1, 2], [2, 3]); num_nodes = 3)
474+
g2 = GNNGraph(([1, 2], [2, 1]); num_nodes = 4)
475+
g = batch([g1, g2])
476+
477+
@test g.num_nodes == length(g.graph_indicator) == 7
478+
@test_throws ArgumentError add_nodes(g, 2)
479+
@test_throws ArgumentError add_edges(g, [8], [1])
480+
@test add_nodes(g, 0).graph_indicator == g.graph_indicator
481+
@test add_edges(g, Int[], Int[]) === g
482+
483+
# Adding edges between existing nodes does not grow the batched graph.
484+
g_with_edge = add_edges(g, [1], [2])
485+
@test g_with_edge.num_nodes == length(g_with_edge.graph_indicator) == 7
486+
487+
# A single graph with explicit membership can safely extend that membership.
488+
single = GNNGraph(([1], [2]); num_nodes = 2, graph_indicator = [1, 1])
489+
single_with_nodes = add_nodes(single, 2)
490+
single_with_edge = add_edges(single, [3], [1])
491+
@test single_with_nodes.graph_indicator == [1, 1, 1, 1]
492+
@test single_with_edge.graph_indicator == [1, 1, 1]
493+
494+
# Unbatched graphs represent membership implicitly, so their stored field stays `nothing`.
495+
unbatched = GNNGraph(([1], [2]); num_nodes = 2)
496+
unbatched_with_nodes = add_nodes(unbatched, 2)
497+
unbatched_with_edge = add_edges(unbatched, [3], [1])
498+
@test isnothing(unbatched_with_nodes.graph_indicator)
499+
@test length(graph_indicator(unbatched_with_nodes)) == unbatched_with_nodes.num_nodes
500+
@test isnothing(unbatched_with_edge.graph_indicator)
501+
@test length(graph_indicator(unbatched_with_edge)) == unbatched_with_edge.num_nodes
502+
end
503+
470504
@testitem "remove_self_loops" setup=[GraphsTestModule] begin
471505
using .GraphsTestModule
472506
for GRAPH_T in GRAPH_TYPES

0 commit comments

Comments
 (0)