Skip to content

Commit 9264a68

Browse files
support edge_weights in degree
1 parent 5d53d05 commit 9264a68

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ on:
66
push:
77
branches:
88
- master
9-
tags: '*'
109
jobs:
11-
test:
10+
test:https://piazza.com/unibocconi.it/fall2020/csdb/resources
1211
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
1312
runs-on: ${{ matrix.os }}
1413
strategy:
@@ -45,5 +44,4 @@ jobs:
4544
- uses: julia-actions/julia-processcoverage@v1
4645
- uses: codecov/codecov-action@v1
4746
with:
48-
# token: ${{ secrets.CODECOV_TOKEN }}
4947
file: lcov.info

src/GNNGraphs/query.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ edge_index(g::GNNGraph{<:COO_T}) = g.graph[1:2]
1313

1414
edge_index(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes=g.num_nodes)[1][1:2]
1515

16-
edge_weight(g::GNNGraph{<:COO_T}) = g.graph[3]
16+
get_edge_weight(g::GNNGraph{<:COO_T}) = g.graph[3]
1717

18-
edge_weight(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes=g.num_nodes)[1][3]
18+
get_edge_weight(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes=g.num_nodes)[1][3]
1919

2020
Graphs.edges(g::GNNGraph) = zip(edge_index(g)...)
2121

@@ -95,21 +95,33 @@ function Graphs.adjacency_matrix(g::GNNGraph{<:ADJMAT_T}, T::DataType=eltype(g.g
9595
return dir == :out ? A : A'
9696
end
9797

98-
function Graphs.degree(g::GNNGraph{<:COO_T}, T=nothing; dir=:out)
98+
function Graphs.degree(g::GNNGraph{<:COO_T}, T=nothing; dir=:out, edge_weight=true)
9999
s, t = edge_index(g)
100-
T = isnothing(T) ? eltype(s) : T
100+
101+
if edge_weight === true
102+
edge_weight = get_edge_weight(g)
103+
elseif (edge_weight === false) || (edge_weight === nothing)
104+
edge_weight = nothing
105+
elseif edge_weight isa AbstractVector
106+
edge_weight = edge_weight
107+
else
108+
error("Invalid edge_weight argument.")
109+
end
110+
edge_weight = isnothing(edge_weight) ? eltype(s)(1) : edge_weight
111+
112+
T = isnothing(T) ? eltype(edge_weight) : T
101113
degs = fill!(similar(s, T, g.num_nodes), 0)
102-
src = 1
103114
if dir [:out, :both]
104-
NNlib.scatter!(+, degs, src, s)
115+
NNlib.scatter!(+, degs, edge_weight, s)
105116
end
106117
if dir [:in, :both]
107-
NNlib.scatter!(+, degs, src, t)
118+
NNlib.scatter!(+, degs, edge_weight, t)
108119
end
109120
return degs
110121
end
111122

112-
function Graphs.degree(g::GNNGraph{<:ADJMAT_T}, T=Int; dir=:out)
123+
function Graphs.degree(g::GNNGraph{<:ADJMAT_T}, T=Int; dir=:out, edge_weight=true)
124+
@assert edge_weight === true
113125
@assert dir (:in, :out)
114126
A = adjacency_matrix(g, T)
115127
return dir == :out ? vec(sum(A, dims=2)) : vec(sum(A, dims=1))

src/GNNGraphs/transform.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ self-loops will obtain a second self-loop.
1111
function add_self_loops(g::GNNGraph{<:COO_T})
1212
s, t = edge_index(g)
1313
@assert g.edata === (;)
14-
@assert edge_weight(g) === nothing
14+
@assert get_edge_weight(g) === nothing
1515
n = g.num_nodes
1616
nodes = convert(typeof(s), [1:n;])
1717
s = [s; nodes]
@@ -39,7 +39,7 @@ function remove_self_loops(g::GNNGraph{<:COO_T})
3939
s, t = edge_index(g)
4040
# TODO remove these constraints
4141
@assert g.edata === (;)
42-
@assert edge_weight(g) === nothing
42+
@assert get_edge_weight(g) === nothing
4343

4444
mask_old_loops = s .!= t
4545
s = s[mask_old_loops]
@@ -61,7 +61,7 @@ function remove_multi_edges(g::GNNGraph{<:COO_T})
6161
# TODO remove these constraints
6262
@assert g.num_graphs == 1
6363
@assert g.edata === (;)
64-
@assert edge_weight(g) === nothing
64+
@assert get_edge_weight(g) === nothing
6565

6666
idxs, idxmax = edge_encoding(s, t, g.num_nodes)
6767
union!(idxs)
@@ -85,7 +85,7 @@ function add_edges(g::GNNGraph{<:COO_T},
8585

8686
@assert length(snew) == length(tnew)
8787
# TODO remove this constraint
88-
@assert edge_weight(g) === nothing
88+
@assert get_edge_weight(g) === nothing
8989

9090
edata = normalize_graphdata(edata, default_name=:e, n=length(snew))
9191
edata = cat_features(g.edata, edata)
@@ -126,7 +126,7 @@ function SparseArrays.blockdiag(g1::GNNGraph, g2::GNNGraph)
126126
s2, t2 = edge_index(g2)
127127
s = vcat(s1, nv1 .+ s2)
128128
t = vcat(t1, nv1 .+ t2)
129-
w = cat_features(edge_weight(g1), edge_weight(g2))
129+
w = cat_features(get_edge_weight(g1), get_edge_weight(g2))
130130
graph = (s, t, w)
131131
ind1 = isnothing(g1.graph_indicator) ? ones_like(s1, Int, nv1) : g1.graph_indicator
132132
ind2 = isnothing(g2.graph_indicator) ? ones_like(s2, Int, nv2) : g2.graph_indicator
@@ -288,7 +288,7 @@ function getgraph(g::GNNGraph, i::AbstractVector{Int}; nmap=false)
288288
graph_indicator = [graphmap[i] for i in g.graph_indicator[node_mask]]
289289

290290
s, t = edge_index(g)
291-
w = edge_weight(g)
291+
w = get_edge_weight(g)
292292
edge_mask = s .∈ Ref(nodes)
293293

294294
if g.graph isa COO_T

test/GNNGraphs/gnngraph.jl

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@
7070
adjacency_matrix(g; dir=:in) == adj_mat
7171
end
7272

73-
@testset "degree" begin
74-
@test degree(g, dir=:out) == vec(sum(adj_mat, dims=2))
75-
@test degree(g, dir=:in) == vec(sum(adj_mat, dims=1))
76-
77-
if TEST_GPU
78-
d = degree(g)
79-
d_gpu = degree(g_gpu)
80-
@test d_gpu isa CuVector
81-
@test Array(d_gpu) == d
82-
end
83-
end
84-
8573
if TEST_GPU
8674
@testset "functor" begin
8775
s_cpu, t_cpu = edge_index(g)
@@ -134,11 +122,6 @@
134122
@test adjacency_list(g, dir=:out) == adj_list_out
135123
@test adjacency_matrix(g, dir=:in) == adj_mat_in
136124
@test adjacency_list(g, dir=:in) == adj_list_in
137-
138-
@testset "degree" begin
139-
@test degree(g, dir=:out) == vec(sum(adj_mat_out, dims=2))
140-
@test degree(g, dir=:in) == vec(sum(adj_mat_out, dims=1))
141-
end
142125
end
143126

144127
@testset "Graphs constructor" begin

test/GNNGraphs/query.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,33 @@
3232
g = GNNGraph(s, t, graph_type=GRAPH_T)
3333
@test !has_self_loops(g)
3434
end
35+
36+
@testset "degree" begin
37+
s = [1, 1, 2, 3]
38+
t = [2, 2, 2, 4]
39+
eweight = [0.1, 2.1, 1.2, 1]
40+
g = GNNGraph(s, t, graph_type=GRAPH_T)
41+
42+
@test degree(g) == degree(g; dir=:out) == [2, 1, 1, 0] # default is outdegree
43+
@test degree(g; dir=:in) == [0, 3, 0, 1]
44+
@test degree(g; dir=:both) == [2, 4, 1, 1]
45+
@test eltype(degree(g, Float32)) == Float32
46+
47+
# weighted degree
48+
if GRAPH_T == :coo
49+
eweight = [0.1, 2.1, 1.2, 1]
50+
g = GNNGraph((s, t, eweight), graph_type=GRAPH_T)
51+
@test degree(g) == [2.2, 1.2, 1.0, 0.0]
52+
@test degree(g, edge_weight=false) == [2, 1, 1, 0]
53+
@test degree(g, edge_weight=nothing) == [2, 1, 1, 0]
54+
@test degree(g, edge_weight=2*eweight) == [4.4, 2.4, 2.0, 0.0]
55+
end
56+
57+
if TEST_GPU
58+
d = degree(g)
59+
d_gpu = degree(g_gpu)
60+
@test d_gpu isa CuVector
61+
@test Array(d_gpu) == d
62+
end
63+
end
3564
end

0 commit comments

Comments
 (0)