@@ -20,6 +20,23 @@ get_edge_weight(g::GNNGraph{<:ADJMAT_T}) = to_coo(g.graph, num_nodes=g.num_nodes
20
20
Graphs. edges (g:: GNNGraph ) = zip (edge_index (g)... )
21
21
22
22
Graphs. edgetype (g:: GNNGraph ) = Tuple{Int, Int}
23
+ nodetype (g:: GNNGraph ) = Base. eltype (g)
24
+
25
+ """
26
+ nodetype(g::GNNGraph)
27
+
28
+ Type of nodes in `g`,
29
+ an integer type like `Int`, `Int32`, `Uint16`, ....
30
+ """
31
+ function nodetype (g:: GNNGraph{<:COO_T} , T= nothing )
32
+ s, t = edge_index (g)
33
+ return eltype (s)
34
+ end
35
+
36
+ function nodetype (g:: GNNGraph{<:ADJMAT_T} , T= nothing )
37
+ T != = nothing && return T
38
+ return eltype (g. graph)
39
+ end
23
40
24
41
function Graphs. has_edge (g:: GNNGraph{<:COO_T} , i:: Integer , j:: Integer )
25
42
s, t = edge_index (g)
@@ -77,7 +94,7 @@ function adjacency_list(g::GNNGraph; dir=:out)
77
94
return [fneighs (g, i) for i in 1 : g. num_nodes]
78
95
end
79
96
80
- function Graphs. adjacency_matrix (g:: GNNGraph{<:COO_T} , T:: DataType = Int ; dir= :out )
97
+ function Graphs. adjacency_matrix (g:: GNNGraph{<:COO_T} , T:: DataType = nodetype (g) ; dir= :out )
81
98
if g. graph[1 ] isa CuVector
82
99
# TODO revisit after https://github.com/JuliaGPU/CUDA.jl/pull/1152
83
100
A, n, m = to_dense (g. graph, T, num_nodes= g. num_nodes)
@@ -88,7 +105,7 @@ function Graphs.adjacency_matrix(g::GNNGraph{<:COO_T}, T::DataType=Int; dir=:out
88
105
return dir == :out ? A : A'
89
106
end
90
107
91
- function Graphs. adjacency_matrix (g:: GNNGraph{<:ADJMAT_T} , T:: DataType = eltype (g . graph ); dir= :out )
108
+ function Graphs. adjacency_matrix (g:: GNNGraph{<:ADJMAT_T} , T:: DataType = nodetype (g ); dir= :out )
92
109
@assert dir ∈ [:in , :out ]
93
110
A = g. graph
94
111
A = T != eltype (A) ? T .(A) : A
@@ -125,27 +142,37 @@ function Graphs.degree(g::GNNGraph{<:COO_T}, T=nothing; dir=:out, edge_weight=tr
125
142
return degs
126
143
end
127
144
128
- function Graphs. degree (g:: GNNGraph{<:ADJMAT_T} , T= Int ; dir= :out , edge_weight= true )
145
+ function Graphs. degree (g:: GNNGraph{<:ADJMAT_T} , T= nothing ; dir= :out , edge_weight= true )
129
146
@assert ! (edge_weight isa AbstractArray) " passing the edge weights is not support by adjacency matrix representations"
130
147
@assert dir ∈ (:in , :out , :both )
131
- A = adjacency_matrix (g, T)
148
+ if T === nothing
149
+ Nt = nodetype (g)
150
+ if ((edge_weight === false ) || (edge_weight === nothing )) && ! (Nt <: Integer )
151
+ T = Nt == Float32 ? Int32 :
152
+ Nt == Float16 ? Int16 : Int
153
+ else
154
+ T = Nt
155
+ end
156
+ end
157
+ A = adjacency_matrix (g)
132
158
if (edge_weight === false ) || (edge_weight === nothing )
133
- A = map (> (0 ), A)
159
+ A = map (x -> x > 0 ? T ( 1 ) : T (0 ), A)
134
160
end
161
+ A = eltype (A) != T ? T .(A) : A
135
162
return dir == :out ? vec (sum (A, dims= 2 )) :
136
163
dir == :in ? vec (sum (A, dims= 1 )) :
137
164
vec (sum (A, dims= 1 )) .+ vec (sum (A, dims= 2 ))
138
165
end
139
166
140
- function Graphs. laplacian_matrix (g:: GNNGraph , T:: DataType = Int ; dir:: Symbol = :out )
167
+ function Graphs. laplacian_matrix (g:: GNNGraph , T= nothing ; dir:: Symbol = :out )
141
168
A = adjacency_matrix (g, T; dir= dir)
142
169
D = Diagonal (vec (sum (A; dims= 2 )))
143
170
return D - A
144
171
end
145
172
146
173
147
174
"""
148
- normalized_laplacian(g, T=Float32 ; add_self_loops=false, dir=:out)
175
+ normalized_laplacian(g, T=nothing ; add_self_loops=false, dir=:out)
149
176
150
177
Normalized Laplacian matrix of graph `g`.
151
178
@@ -156,13 +183,13 @@ Normalized Laplacian matrix of graph `g`.
156
183
- `add_self_loops`: add self-loops while calculating the matrix.
157
184
- `dir`: the edge directionality considered (:out, :in, :both).
158
185
"""
159
- function normalized_laplacian (g:: GNNGraph , T:: DataType = Float32 ;
186
+ function normalized_laplacian (g:: GNNGraph , T= nodetype (g) ;
160
187
add_self_loops:: Bool = false , dir:: Symbol = :out )
161
188
à = normalized_adjacency (g, T; dir, add_self_loops)
162
189
return I - Ã
163
190
end
164
191
165
- function normalized_adjacency (g:: GNNGraph , T:: DataType = Float32 ;
192
+ function normalized_adjacency (g:: GNNGraph , T= nodetype (g) ;
166
193
add_self_loops:: Bool = false , dir:: Symbol = :out )
167
194
A = adjacency_matrix (g, T; dir= dir)
168
195
if add_self_loops
@@ -174,7 +201,7 @@ function normalized_adjacency(g::GNNGraph, T::DataType=Float32;
174
201
end
175
202
176
203
@doc raw """
177
- scaled_laplacian(g, T=Float32 ; dir=:out)
204
+ scaled_laplacian(g, T=nothing ; dir=:out)
178
205
179
206
Scaled Laplacian matrix of graph `g`,
180
207
defined as ``\h at{L} = \f rac{2}{\l ambda_{max}} L - I`` where ``L`` is the normalized Laplacian matrix.
@@ -185,7 +212,7 @@ defined as ``\hat{L} = \frac{2}{\lambda_{max}} L - I`` where ``L`` is the normal
185
212
- `T`: result element type.
186
213
- `dir`: the edge directionality considered (:out, :in, :both).
187
214
"""
188
- function scaled_laplacian (g:: GNNGraph , T:: DataType = Float32 ; dir= :out )
215
+ function scaled_laplacian (g:: GNNGraph , T= nothing ; dir= :out )
189
216
L = normalized_laplacian (g, T)
190
217
@assert issymmetric (L) " scaled_laplacian only works with symmetric matrices"
191
218
λmax = _eigmax (L)
0 commit comments