Skip to content

Commit 48c1528

Browse files
authored
Fix remove_self_loops mutation for adjacency graphs (#659)
1 parent b9cdc0f commit 48c1528

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

GNNGraphs/src/transform.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565

6666
function remove_self_loops(g::GNNGraph{<:ADJMAT_T})
6767
@assert isempty(g.edata)
68-
A = g.graph
68+
A = copy(g.graph)
6969
A[diagind(A)] .= 0
7070
if A isa AbstractSparseMatrix
7171
dropzeros!(A)

GNNGraphs/test/transform.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,20 @@ end
376376
@test size(get_edge_weight(g2)) == (g2.num_edges,)
377377
@test size(g2.edata.e1) == (3, g2.num_edges)
378378
@test size(g2.edata.e2) == (g2.num_edges,)
379+
else
380+
A = [1 1 0
381+
0 1 1
382+
1 0 0]
383+
A_no_loops = [0 1 0
384+
0 0 1
385+
1 0 0]
386+
g = GNNGraph(A; graph_type = GRAPH_T)
387+
g2 = remove_self_loops(g)
388+
389+
@test Matrix(adjacency_matrix(g)) == A
390+
@test Matrix(adjacency_matrix(g2)) == A_no_loops
391+
@test g2.num_edges == count(!iszero, A_no_loops)
392+
@test g.graph !== g2.graph
379393
end
380394
end
381395
end

0 commit comments

Comments
 (0)