From 874eaf72ce0da7e58a6cb11a731afb708e50b698 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 20:04:29 +0530 Subject: [PATCH 01/17] added remove edge function --- .gitignore | 2 +- src/GNNGraphs/transform.jl | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3d1804049..529f75269 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ Manifest.toml .vscode LocalPreferences.toml .DS_Store -/test.jl +try.jl diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index ee90eec1b..e9c1b12c3 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -149,6 +149,54 @@ function remove_self_loops(g::GNNGraph{<:ADJMAT_T}) g.ndata, g.edata, g.gdata) end +""" + remove_edges(g::GNNGraph, edges_to_remove::Vector{Int}) + +Remove specified edges from a GNNGraph. + +# Arguments +- `g`: The input graph from which edges will be removed. +- `edges_to_remove`: Vector of edge indices to be removed. + +# Returns +A new GNNGraph with the specified edges removed. + +# Example +```julia +using GraphNeuralNetworks + +# Construct a GNNGraph +g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1]) + +# Remove the second edge +g_new = remove_edges(g, [2]) + +println(g_new) +``` +""" +function remove_edges(g::GNNGraph, edges_to_remove) + s, t = edge_index(g) + w = get_edge_weight(g) + edata = g.edata + + mask_to_keep = trues(length(s)) + + for edge_index in edges_to_remove + mask_to_keep[edge_index] = false + end + + s = s[mask_to_keep] + t = t[mask_to_keep] + edata = getobs(edata, mask_to_keep) + println(edata) + w = isnothing(w) ? nothing : getobs(w, mask_to_keep) + + GNNGraph((s, t, w), + g.num_nodes, length(s), g.num_graphs, + g.graph_indicator, + g.ndata, edata, g.gdata) +end + """ remove_multi_edges(g::GNNGraph; aggr=+) From a39c8918d76b7936689a79a85b20ad72c088c21c Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 20:22:45 +0530 Subject: [PATCH 02/17] tests --- test/GNNGraphs/transform.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index d56ba5d1c..f65590061 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -105,10 +105,13 @@ end if GRAPH_T == :coo s = [1, 1, 2, 3] t = [2, 3, 4, 5] - g = GNNGraph(s, t, graph_type = GRAPH_T) - snew = [1] - tnew = [4] + g = GNNGraph(s, t) + snew = [1, 3] + tnew = [4, 3] gnew = add_edges(g, snew, tnew) + @test gnew.num_edges == 6 + + gnew = remove_edge(gnew, [1]) @test gnew.num_edges == 5 @test sort(inneighbors(gnew, 4)) == [1, 2] From 848d3c642ba78705096cc9a025a91e9f3cd1fab3 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 20:23:20 +0530 Subject: [PATCH 03/17] added remove edge function --- test/GNNGraphs/transform.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index f65590061..e2feb6cd5 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -105,7 +105,7 @@ end if GRAPH_T == :coo s = [1, 1, 2, 3] t = [2, 3, 4, 5] - g = GNNGraph(s, t) + g = GNNGraph(s, t, graph_type = GRAPH_T) snew = [1, 3] tnew = [4, 3] gnew = add_edges(g, snew, tnew) From 6c082f26a26e9c58d44f3f81f35bd186f834b687 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 20:53:05 +0530 Subject: [PATCH 04/17] fix --- test/GNNGraphs/transform.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index e2feb6cd5..40374cba9 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -111,7 +111,7 @@ end gnew = add_edges(g, snew, tnew) @test gnew.num_edges == 6 - gnew = remove_edge(gnew, [1]) + gnew = remove_edges(gnew, [1]) @test gnew.num_edges == 5 @test sort(inneighbors(gnew, 4)) == [1, 2] From e5a80cae267e9de39433b77f937916ea6dcc245e Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 21:00:18 +0530 Subject: [PATCH 05/17] fix --- src/GNNGraphs/transform.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index e9c1b12c3..c8e2f85b3 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -174,7 +174,7 @@ g_new = remove_edges(g, [2]) println(g_new) ``` """ -function remove_edges(g::GNNGraph, edges_to_remove) +function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove) s, t = edge_index(g) w = get_edge_weight(g) edata = g.edata From 588ae22ccb3decbe23fb64fe545e7013a8b80c96 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Fri, 8 Mar 2024 22:41:23 +0530 Subject: [PATCH 06/17] fix --- src/GNNGraphs/GNNGraphs.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GNNGraphs/GNNGraphs.jl b/src/GNNGraphs/GNNGraphs.jl index 59cc9d9e4..908266f8b 100644 --- a/src/GNNGraphs/GNNGraphs.jl +++ b/src/GNNGraphs/GNNGraphs.jl @@ -72,6 +72,7 @@ export add_nodes, negative_sample, rand_edge_split, remove_self_loops, + remove_edges, remove_multi_edges, set_edge_weight, to_bidirected, From 04c0942e487725b9bd1923ed51c6fef70586e5c6 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Sat, 9 Mar 2024 01:52:17 +0530 Subject: [PATCH 07/17] fix --- test/GNNGraphs/transform.jl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index 40374cba9..a3c301a0e 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -101,17 +101,22 @@ end @test nodemap == 1:(g1.num_nodes) end +@testset "remove_edges" begin + s = [1, 1, 2, 3] + t = [2, 3, 4, 5] + g = GNNGraph(s, t, graph_type = GRAPH_T) + gnew = remove_edges(g, [1]) + @test gnew.num_edges == 3 +end + @testset "add_edges" begin if GRAPH_T == :coo s = [1, 1, 2, 3] t = [2, 3, 4, 5] g = GNNGraph(s, t, graph_type = GRAPH_T) - snew = [1, 3] - tnew = [4, 3] + snew = [1] + tnew = [4] gnew = add_edges(g, snew, tnew) - @test gnew.num_edges == 6 - - gnew = remove_edges(gnew, [1]) @test gnew.num_edges == 5 @test sort(inneighbors(gnew, 4)) == [1, 2] From 04d545199951e738c6079d5049f6ba82a230aef9 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Sat, 9 Mar 2024 02:16:20 +0530 Subject: [PATCH 08/17] fix --- test/GNNGraphs/transform.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index a3c301a0e..fab8c67c9 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -102,11 +102,13 @@ end end @testset "remove_edges" begin - s = [1, 1, 2, 3] - t = [2, 3, 4, 5] - g = GNNGraph(s, t, graph_type = GRAPH_T) - gnew = remove_edges(g, [1]) - @test gnew.num_edges == 3 + if GRAPH_T == :coo + s = [1, 1, 2, 3] + t = [2, 3, 4, 5] + g = GNNGraph(s, t, graph_type = GRAPH_T) + gnew = remove_edges(g, [1]) + @test gnew.num_edges == 3 + end end @testset "add_edges" begin From dcba2f1fb08a28f041381b45535de86799f3247b Mon Sep 17 00:00:00 2001 From: Rishabh <59335537+rbSparky@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:40:48 +0530 Subject: [PATCH 09/17] Update src/GNNGraphs/transform.jl Co-authored-by: Carlo Lucibello --- src/GNNGraphs/transform.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index c8e2f85b3..f36e3db16 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -188,7 +188,6 @@ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove) s = s[mask_to_keep] t = t[mask_to_keep] edata = getobs(edata, mask_to_keep) - println(edata) w = isnothing(w) ? nothing : getobs(w, mask_to_keep) GNNGraph((s, t, w), From f56751873aec73465bea07da29cd04996d618b49 Mon Sep 17 00:00:00 2001 From: Rishabh <59335537+rbSparky@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:41:00 +0530 Subject: [PATCH 10/17] Update src/GNNGraphs/transform.jl Co-authored-by: Carlo Lucibello --- src/GNNGraphs/transform.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index f36e3db16..8a82f032f 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -181,9 +181,7 @@ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove) mask_to_keep = trues(length(s)) - for edge_index in edges_to_remove - mask_to_keep[edge_index] = false - end +mask_to_keep[edges_to_remove] .= false s = s[mask_to_keep] t = t[mask_to_keep] From ba1515f25ea28d2cfceb09dceb5c499a6bda13ff Mon Sep 17 00:00:00 2001 From: Rishabh <59335537+rbSparky@users.noreply.github.com> Date: Mon, 11 Mar 2024 10:41:53 +0530 Subject: [PATCH 11/17] Update transform.jl --- src/GNNGraphs/transform.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index 8a82f032f..e3277c804 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -181,7 +181,7 @@ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove) mask_to_keep = trues(length(s)) -mask_to_keep[edges_to_remove] .= false + mask_to_keep[edges_to_remove] .= false s = s[mask_to_keep] t = t[mask_to_keep] From f4111498786ba5ec157f0288b364fd51414e7e06 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Mon, 11 Mar 2024 20:37:33 +0530 Subject: [PATCH 12/17] tests final --- Project.toml | 3 +++ test/GNNGraphs/transform.jl | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Project.toml b/Project.toml index 4785fe5dc..b5ddca2b8 100644 --- a/Project.toml +++ b/Project.toml @@ -12,15 +12,18 @@ Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458" MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd" NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" +TSne = "24678dba-d5e9-5843-a4c6-250288b04835" [weakdeps] CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index fab8c67c9..fb09455ca 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -107,7 +107,10 @@ end t = [2, 3, 4, 5] g = GNNGraph(s, t, graph_type = GRAPH_T) gnew = remove_edges(g, [1]) + new_s, new_t = edge_index(gnew) @test gnew.num_edges == 3 + @test new_s == s[2:end] + @test new_t == t[2:end] end end From 64aad8d41512a410692976307ad6d1ec48a1789e Mon Sep 17 00:00:00 2001 From: Rishabh <59335537+rbSparky@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:38:55 +0530 Subject: [PATCH 13/17] Update Project.toml --- Project.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Project.toml b/Project.toml index b5ddca2b8..4785fe5dc 100644 --- a/Project.toml +++ b/Project.toml @@ -12,18 +12,15 @@ Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458" MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd" NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce" -Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" -TSne = "24678dba-d5e9-5843-a4c6-250288b04835" [weakdeps] CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" From ea63a4cdd0ea6d6849f844eb5398f3306b5d1edb Mon Sep 17 00:00:00 2001 From: Rishabh <59335537+rbSparky@users.noreply.github.com> Date: Sun, 17 Mar 2024 01:14:15 +0530 Subject: [PATCH 14/17] Update src/GNNGraphs/transform.jl Co-authored-by: Carlo Lucibello --- src/GNNGraphs/transform.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index e3277c804..c53d52df0 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -150,7 +150,7 @@ function remove_self_loops(g::GNNGraph{<:ADJMAT_T}) end """ - remove_edges(g::GNNGraph, edges_to_remove::Vector{Int}) + remove_edges(g::GNNGraph, edges_to_remove::AbstractVector{<:Integer}) Remove specified edges from a GNNGraph. From fd5ac5660a0c1961a33970bc967eb0c7d775d9b2 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Sun, 17 Mar 2024 01:30:33 +0530 Subject: [PATCH 15/17] done --- src/GNNGraphs/transform.jl | 10 +++++----- test/GNNGraphs/transform.jl | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index c53d52df0..086978bbd 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -163,18 +163,18 @@ A new GNNGraph with the specified edges removed. # Example ```julia -using GraphNeuralNetworks +julia> using GraphNeuralNetworks # Construct a GNNGraph -g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1]) +julia> g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1]) # Remove the second edge -g_new = remove_edges(g, [2]) +julia> g_new = remove_edges(g, [2]) -println(g_new) +julia> g_new ``` """ -function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove) +function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove::AbstractVector{<:Integer}) s, t = edge_index(g) w = get_edge_weight(g) edata = g.edata diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index fb09455ca..f8c0efc97 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -106,11 +106,20 @@ end s = [1, 1, 2, 3] t = [2, 3, 4, 5] g = GNNGraph(s, t, graph_type = GRAPH_T) + + # single edge removal gnew = remove_edges(g, [1]) new_s, new_t = edge_index(gnew) @test gnew.num_edges == 3 @test new_s == s[2:end] @test new_t == t[2:end] + + # multiple edge removal + gnew = remove_edges(g, [1,2,3]) + new_s, new_t = edge_index(gnew) + @test gnew.num_edges == 1 + @test new_s == s[4:end] + @test new_t == t[4:end] end end From 04523119dd642156d81bad6047e0ee09600ffe83 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Sun, 17 Mar 2024 21:19:38 +0530 Subject: [PATCH 16/17] fixes --- .gitignore | 3 +-- src/GNNGraphs/transform.jl | 12 +++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 529f75269..a2c3a9ddc 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,4 @@ Manifest.toml /docs/build/ .vscode LocalPreferences.toml -.DS_Store -try.jl +.DS_Store \ No newline at end of file diff --git a/src/GNNGraphs/transform.jl b/src/GNNGraphs/transform.jl index 086978bbd..cfb8b4b0f 100644 --- a/src/GNNGraphs/transform.jl +++ b/src/GNNGraphs/transform.jl @@ -167,11 +167,17 @@ julia> using GraphNeuralNetworks # Construct a GNNGraph julia> g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1]) - +GNNGraph: + num_nodes: 3 + num_edges: 5 + # Remove the second edge -julia> g_new = remove_edges(g, [2]) +julia> g_new = remove_edges(g, [2]); julia> g_new +GNNGraph: + num_nodes: 3 + num_edges: 4 ``` """ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove::AbstractVector{<:Integer}) @@ -188,7 +194,7 @@ function remove_edges(g::GNNGraph{<:COO_T}, edges_to_remove::AbstractVector{<:In edata = getobs(edata, mask_to_keep) w = isnothing(w) ? nothing : getobs(w, mask_to_keep) - GNNGraph((s, t, w), + return GNNGraph((s, t, w), g.num_nodes, length(s), g.num_graphs, g.graph_indicator, g.ndata, edata, g.gdata) From 1ac09fa8ac50dc1943ca896be9fa7d7c6e3247e8 Mon Sep 17 00:00:00 2001 From: rbSparky Date: Sun, 17 Mar 2024 21:28:18 +0530 Subject: [PATCH 17/17] more tests --- test/GNNGraphs/transform.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/GNNGraphs/transform.jl b/test/GNNGraphs/transform.jl index f8c0efc97..1b30a2789 100644 --- a/test/GNNGraphs/transform.jl +++ b/test/GNNGraphs/transform.jl @@ -105,7 +105,9 @@ end if GRAPH_T == :coo s = [1, 1, 2, 3] t = [2, 3, 4, 5] - g = GNNGraph(s, t, graph_type = GRAPH_T) + w = [0.1, 0.2, 0.3, 0.4] + edata = ['a', 'b', 'c', 'd'] + g = GNNGraph(s, t, w, edata = edata, graph_type = GRAPH_T) # single edge removal gnew = remove_edges(g, [1]) @@ -115,11 +117,15 @@ end @test new_t == t[2:end] # multiple edge removal - gnew = remove_edges(g, [1,2,3]) + gnew = remove_edges(g, [1,2,4]) new_s, new_t = edge_index(gnew) + new_w = get_edge_weight(gnew) + new_edata = gnew.edata.e @test gnew.num_edges == 1 - @test new_s == s[4:end] - @test new_t == t[4:end] + @test new_s == [2] + @test new_t == [4] + @test new_w == [0.3] + @test new_edata == ['c'] end end