-
Notifications
You must be signed in to change notification settings - Fork 47
/
transform.jl
1294 lines (1066 loc) · 40.4 KB
/
transform.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
add_self_loops(g::GNNGraph)
Return a graph with the same features as `g`
but also adding edges connecting the nodes to themselves.
Nodes with already existing self-loops will obtain a second self-loop.
If the graphs has edge weights, the new edges will have weight 1.
"""
function add_self_loops(g::GNNGraph{<:COO_T})
s, t = edge_index(g)
@assert isempty(g.edata)
ew = get_edge_weight(g)
n = g.num_nodes
nodes = convert(typeof(s), [1:n;])
s = [s; nodes]
t = [t; nodes]
if ew !== nothing
ew = [ew; fill!(similar(ew, n), 1)]
end
return GNNGraph((s, t, ew),
g.num_nodes, length(s), g.num_graphs,
g.graph_indicator,
g.ndata, g.edata, g.gdata)
end
function add_self_loops(g::GNNGraph{<:ADJMAT_T})
A = g.graph
@assert isempty(g.edata)
num_edges = g.num_edges + g.num_nodes
A = A + I
return GNNGraph(A,
g.num_nodes, num_edges, g.num_graphs,
g.graph_indicator,
g.ndata, g.edata, g.gdata)
end
"""
add_self_loops(g::GNNHeteroGraph, edge_t::EType)
add_self_loops(g::GNNHeteroGraph)
If the source node type is the same as the destination node type in `edge_t`,
return a graph with the same features as `g` but also add self-loops
of the specified type, `edge_t`. Otherwise, it returns `g` unchanged.
Nodes with already existing self-loops of type `edge_t` will obtain
a second set of self-loops of the same type.
If the graph has edge weights for edges of type `edge_t`, the new edges will have weight 1.
If no edges of type `edge_t` exist, or all existing edges have no weight,
then all new self loops will have no weight.
If `edge_t` is not passed as argument, for the entire graph self-loop is added to each node for every edge type in the graph where the source and destination node types are the same.
This iterates over all edge types present in the graph, applying the self-loop addition logic to each applicable edge type.
"""
function add_self_loops(g::GNNHeteroGraph{<:COO_T}, edge_t::EType)
function get_edge_weight_nullable(g::GNNHeteroGraph{<:COO_T}, edge_t::EType)
get(g.graph, edge_t, (nothing, nothing, nothing))[3]
end
src_t, _, tgt_t = edge_t
(src_t === tgt_t) ||
return g
n = get(g.num_nodes, src_t, 0)
if haskey(g.graph, edge_t)
s, t = g.graph[edge_t][1:2]
nodes = convert(typeof(s), [1:n;])
s = [s; nodes]
t = [t; nodes]
else
if !isempty(g.graph)
T = typeof(first(values(g.graph))[1])
nodes = convert(T, [1:n;])
else
nodes = [1:n;]
end
s = nodes
t = nodes
end
graph = g.graph |> copy
ew = get(g.graph, edge_t, (nothing, nothing, nothing))[3]
if ew !== nothing
ew = [ew; fill!(similar(ew, n), 1)]
end
graph[edge_t] = (s, t, ew)
edata = g.edata |> copy
ndata = g.ndata |> copy
ntypes = g.ntypes |> copy
etypes = g.etypes |> copy
num_nodes = g.num_nodes |> copy
num_edges = g.num_edges |> copy
num_edges[edge_t] = length(get(graph, edge_t, ([],[]))[1])
return GNNHeteroGraph(graph,
num_nodes, num_edges, g.num_graphs,
g.graph_indicator,
ndata, edata, g.gdata,
ntypes, etypes)
end
function add_self_loops(g::GNNHeteroGraph)
for edge_t in keys(g.graph)
g = add_self_loops(g, edge_t)
end
return g
end
"""
remove_self_loops(g::GNNGraph)
Return a graph constructed from `g` where self-loops (edges from a node to itself)
are removed.
See also [`add_self_loops`](@ref) and [`remove_multi_edges`](@ref).
"""
function remove_self_loops(g::GNNGraph{<:COO_T})
s, t = edge_index(g)
w = get_edge_weight(g)
edata = g.edata
mask_old_loops = s .!= t
s = s[mask_old_loops]
t = t[mask_old_loops]
edata = getobs(edata, mask_old_loops)
w = isnothing(w) ? nothing : getobs(w, mask_old_loops)
GNNGraph((s, t, w),
g.num_nodes, length(s), g.num_graphs,
g.graph_indicator,
g.ndata, edata, g.gdata)
end
function remove_self_loops(g::GNNGraph{<:ADJMAT_T})
@assert isempty(g.edata)
A = g.graph
A[diagind(A)] .= 0
if A isa AbstractSparseMatrix
dropzeros!(A)
end
num_edges = numnonzeros(A)
return GNNGraph(A,
g.num_nodes, num_edges, g.num_graphs,
g.graph_indicator,
g.ndata, g.edata, g.gdata)
end
"""
remove_edges(g::GNNGraph, edges_to_remove::AbstractVector{<:Integer})
remove_edges(g::GNNGraph, p=0.5)
Remove specified edges from a GNNGraph, either by specifying edge indices or by randomly removing edges with a given probability.
# Arguments
- `g`: The input graph from which edges will be removed.
- `edges_to_remove`: Vector of edge indices to be removed. This argument is only required for the first method.
- `p`: Probability of removing each edge. This argument is only required for the second method and defaults to 0.5.
# Returns
A new GNNGraph with the specified edges removed.
# Example
```julia
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
GNNGraph:
num_nodes: 3
num_edges: 4
# Remove edges with a probability of 0.5
julia> g_new = remove_edges(g, 0.5);
julia> g_new
GNNGraph:
num_nodes: 3
num_edges: 2
```
"""
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
mask_to_keep = trues(length(s))
mask_to_keep[edges_to_remove] .= false
s = s[mask_to_keep]
t = t[mask_to_keep]
edata = getobs(edata, mask_to_keep)
w = isnothing(w) ? nothing : getobs(w, mask_to_keep)
return GNNGraph((s, t, w),
g.num_nodes, length(s), g.num_graphs,
g.graph_indicator,
g.ndata, edata, g.gdata)
end
function remove_edges(g::GNNGraph{<:COO_T}, p = 0.5)
num_edges = g.num_edges
edges_to_remove = filter(_ -> rand() < p, 1:num_edges)
return remove_edges(g, edges_to_remove)
end
"""
remove_multi_edges(g::GNNGraph; aggr=+)
Remove multiple edges (also called parallel edges or repeated edges) from graph `g`.
Possible edge features are aggregated according to `aggr`, that can take value
`+`,`min`, `max` or `mean`.
See also [`remove_self_loops`](@ref), [`has_multi_edges`](@ref), and [`to_bidirected`](@ref).
"""
function remove_multi_edges(g::GNNGraph{<:COO_T}; aggr = +)
s, t = edge_index(g)
w = get_edge_weight(g)
edata = g.edata
num_edges = g.num_edges
idxs, idxmax = edge_encoding(s, t, g.num_nodes)
perm = sortperm(idxs)
idxs = idxs[perm]
s, t = s[perm], t[perm]
edata = getobs(edata, perm)
w = isnothing(w) ? nothing : getobs(w, perm)
idxs = [-1; idxs]
mask = idxs[2:end] .> idxs[1:(end - 1)]
if !all(mask)
s, t = s[mask], t[mask]
idxs = similar(s, num_edges)
idxs .= 1:num_edges
idxs .= idxs .- cumsum(.!mask)
num_edges = length(s)
w = _scatter(aggr, w, idxs, num_edges)
edata = _scatter(aggr, edata, idxs, num_edges)
end
return GNNGraph((s, t, w),
g.num_nodes, num_edges, g.num_graphs,
g.graph_indicator,
g.ndata, edata, g.gdata)
end
"""
remove_nodes(g::GNNGraph, nodes_to_remove::AbstractVector)
Remove specified nodes, and their associated edges, from a GNNGraph. This operation reindexes the remaining nodes to maintain a continuous sequence of node indices, starting from 1. Similarly, edges are reindexed to account for the removal of edges connected to the removed nodes.
# Arguments
- `g`: The input graph from which nodes (and their edges) will be removed.
- `nodes_to_remove`: Vector of node indices to be removed.
# Returns
A new GNNGraph with the specified nodes and all edges associated with these nodes removed.
# Example
```julia
using GraphNeuralNetworks
g = GNNGraph([1, 1, 2, 2, 3], [2, 3, 1, 3, 1])
# Remove nodes with indices 2 and 3, for example
g_new = remove_nodes(g, [2, 3])
# g_new now does not contain nodes 2 and 3, and any edges that were connected to these nodes.
println(g_new)
```
"""
function remove_nodes(g::GNNGraph{<:COO_T}, nodes_to_remove::AbstractVector)
nodes_to_remove = sort(union(nodes_to_remove))
s, t = edge_index(g)
w = get_edge_weight(g)
edata = g.edata
ndata = g.ndata
function find_edges_to_remove(nodes, nodes_to_remove)
return findall(node_id -> begin
idx = searchsortedlast(nodes_to_remove, node_id)
idx >= 1 && idx <= length(nodes_to_remove) && nodes_to_remove[idx] == node_id
end, nodes)
end
edges_to_remove_s = find_edges_to_remove(s, nodes_to_remove)
edges_to_remove_t = find_edges_to_remove(t, nodes_to_remove)
edges_to_remove = union(edges_to_remove_s, edges_to_remove_t)
mask_edges_to_keep = trues(length(s))
mask_edges_to_keep[edges_to_remove] .= false
s = s[mask_edges_to_keep]
t = t[mask_edges_to_keep]
w = isnothing(w) ? nothing : getobs(w, mask_edges_to_keep)
for node in sort(nodes_to_remove, rev=true)
s[s .> node] .-= 1
t[t .> node] .-= 1
end
nodes_to_keep = setdiff(1:g.num_nodes, nodes_to_remove)
ndata = getobs(ndata, nodes_to_keep)
edata = getobs(edata, mask_edges_to_keep)
num_nodes = g.num_nodes - length(nodes_to_remove)
return GNNGraph((s, t, w),
num_nodes, length(s), g.num_graphs,
g.graph_indicator,
ndata, edata, g.gdata)
end
"""
remove_nodes(g::GNNGraph, p)
Returns a new graph obtained by dropping nodes from `g` with independent probabilities `p`.
# Examples
```julia
julia> g = GNNGraph([1, 1, 2, 2, 3, 4], [1, 2, 3, 1, 3, 1])
GNNGraph:
num_nodes: 4
num_edges: 6
julia> g_new = remove_nodes(g, 0.5)
GNNGraph:
num_nodes: 2
num_edges: 2
```
"""
function remove_nodes(g::GNNGraph, p::AbstractFloat)
nodes_to_remove = filter(_ -> rand() < p, 1:g.num_nodes)
return remove_nodes(g, nodes_to_remove)
end
"""
add_edges(g::GNNGraph, s::AbstractVector, t::AbstractVector; [edata])
add_edges(g::GNNGraph, (s, t); [edata])
add_edges(g::GNNGraph, (s, t, w); [edata])
Add to graph `g` the edges with source nodes `s` and target nodes `t`.
Optionally, pass the edge weight `w` and the features `edata` for the new edges.
Returns a new graph sharing part of the underlying data with `g`.
If the `s` or `t` contain nodes that are not already present in the graph,
they are added to the graph as well.
# Examples
```jldoctest
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
julia> w = Float32[1.0, 2.0, 3.0, 4.0, 5.0];
julia> g = GNNGraph((s, t, w))
GNNGraph:
num_nodes: 4
num_edges: 5
julia> add_edges(g, ([2, 3], [4, 1], [10.0, 20.0]))
GNNGraph:
num_nodes: 4
num_edges: 7
```
```jldoctest
julia> g = GNNGraph()
GNNGraph:
num_nodes: 0
num_edges: 0
julia> add_edges(g, [1,2], [2,3])
GNNGraph:
num_nodes: 3
num_edges: 2
```
"""
add_edges(g::GNNGraph{<:COO_T}, snew::AbstractVector, tnew::AbstractVector; kws...) = add_edges(g, (snew, tnew, nothing); kws...)
add_edges(g, data::Tuple{<:AbstractVector, <:AbstractVector}; kws...) = add_edges(g, (data..., nothing); kws...)
function add_edges(g::GNNGraph{<:COO_T}, data::COO_T; edata = nothing)
snew, tnew, wnew = data
@assert length(snew) == length(tnew)
@assert isnothing(wnew) || length(wnew) == length(snew)
if length(snew) == 0
return g
end
@assert minimum(snew) >= 1
@assert minimum(tnew) >= 1
num_new = length(snew)
edata = normalize_graphdata(edata, default_name = :e, n = num_new)
edata = cat_features(g.edata, edata)
s, t = edge_index(g)
s = [s; snew]
t = [t; tnew]
w = get_edge_weight(g)
w = cat_features(w, wnew, g.num_edges, num_new)
num_nodes = max(maximum(snew), maximum(tnew), g.num_nodes)
if num_nodes > g.num_nodes
ndata_new = normalize_graphdata((;), default_name = :x, n = num_nodes - g.num_nodes)
ndata = cat_features(g.ndata, ndata_new)
else
ndata = g.ndata
end
return GNNGraph((s, t, w),
num_nodes, length(s), g.num_graphs,
g.graph_indicator,
ndata, edata, g.gdata)
end
"""
add_edges(g::GNNHeteroGraph, edge_t, s, t; [edata, num_nodes])
add_edges(g::GNNHeteroGraph, edge_t => (s, t); [edata, num_nodes])
add_edges(g::GNNHeteroGraph, edge_t => (s, t, w); [edata, num_nodes])
Add to heterograph `g` edges of type `edge_t` with source node vector `s` and target node vector `t`.
Optionally, pass the edge weights `w` or the features `edata` for the new edges.
`edge_t` is a triplet of symbols `(src_t, rel_t, dst_t)`.
If the edge type is not already present in the graph, it is added.
If it involves new node types, they are added to the graph as well.
In this case, a dictionary or named tuple of `num_nodes` can be passed to specify the number of nodes of the new types,
otherwise the number of nodes is inferred from the maximum node id in `s` and `t`.
"""
add_edges(g::GNNHeteroGraph{<:COO_T}, edge_t::EType, snew::AbstractVector, tnew::AbstractVector; kws...) = add_edges(g, edge_t => (snew, tnew, nothing); kws...)
add_edges(g::GNNHeteroGraph{<:COO_T}, data::Pair{EType, <:Tuple{<:AbstractVector, <:AbstractVector}}; kws...) = add_edges(g, data.first => (data.second..., nothing); kws...)
function add_edges(g::GNNHeteroGraph{<:COO_T},
data::Pair{EType, <:COO_T};
edata = nothing,
num_nodes = Dict{Symbol,Int}())
edge_t, (snew, tnew, wnew) = data
@assert length(snew) == length(tnew)
if length(snew) == 0
return g
end
@assert minimum(snew) >= 1
@assert minimum(tnew) >= 1
is_existing_rel = haskey(g.graph, edge_t)
edata = normalize_graphdata(edata, default_name = :e, n = length(snew))
_edata = g.edata |> copy
if haskey(_edata, edge_t)
_edata[edge_t] = cat_features(g.edata[edge_t], edata)
else
_edata[edge_t] = edata
end
graph = g.graph |> copy
etypes = g.etypes |> copy
ntypes = g.ntypes |> copy
_num_nodes = g.num_nodes |> copy
ndata = g.ndata |> copy
if !is_existing_rel
for (node_t, st) in [(edge_t[1], snew), (edge_t[3], tnew)]
if node_t ∉ ntypes
push!(ntypes, node_t)
if haskey(num_nodes, node_t)
_num_nodes[node_t] = num_nodes[node_t]
else
_num_nodes[node_t] = maximum(st)
end
ndata[node_t] = DataStore(_num_nodes[node_t])
end
end
push!(etypes, edge_t)
else
s, t = edge_index(g, edge_t)
snew = [s; snew]
tnew = [t; tnew]
w = get_edge_weight(g, edge_t)
wnew = cat_features(w, wnew, length(s), length(snew))
end
if maximum(snew) > _num_nodes[edge_t[1]]
ndata_new = normalize_graphdata((;), default_name = :x, n = maximum(snew) - _num_nodes[edge_t[1]])
ndata[edge_t[1]] = cat_features(ndata[edge_t[1]], ndata_new)
_num_nodes[edge_t[1]] = maximum(snew)
end
if maximum(tnew) > _num_nodes[edge_t[3]]
ndata_new = normalize_graphdata((;), default_name = :x, n = maximum(tnew) - _num_nodes[edge_t[3]])
ndata[edge_t[3]] = cat_features(ndata[edge_t[3]], ndata_new)
_num_nodes[edge_t[3]] = maximum(tnew)
end
graph[edge_t] = (snew, tnew, wnew)
num_edges = g.num_edges |> copy
num_edges[edge_t] = length(graph[edge_t][1])
return GNNHeteroGraph(graph,
_num_nodes, num_edges, g.num_graphs,
g.graph_indicator,
ndata, _edata, g.gdata,
ntypes, etypes)
end
"""
perturb_edges([rng], g::GNNGraph, perturb_ratio)
Return a new graph obtained from `g` by adding random edges, based on a specified `perturb_ratio`.
The `perturb_ratio` determines the fraction of new edges to add relative to the current number of edges in the graph.
These new edges are added without creating self-loops.
The function returns a new `GNNGraph` instance that shares some of the underlying data with `g` but includes the additional edges.
The nodes for the new edges are selected randomly, and no edge data (`edata`) or weights (`w`) are assigned to these new edges.
# Arguments
- `g::GNNGraph`: The graph to be perturbed.
- `perturb_ratio`: The ratio of the number of new edges to add relative to the current number of edges in the graph. For example, a `perturb_ratio` of 0.1 means that 10% of the current number of edges will be added as new random edges.
- `rng`: An optionalrandom number generator to ensure reproducible results.
# Examples
```julia
julia> g = GNNGraph((s, t, w))
GNNGraph:
num_nodes: 4
num_edges: 5
julia> perturbed_g = perturb_edges(g, 0.2)
GNNGraph:
num_nodes: 4
num_edges: 6
```
"""
perturb_edges(g::GNNGraph{<:COO_T}, perturb_ratio::AbstractFloat) =
perturb_edges(Random.default_rng(), g, perturb_ratio)
function perturb_edges(rng::AbstractRNG, g::GNNGraph{<:COO_T}, perturb_ratio::AbstractFloat)
@assert perturb_ratio >= 0 && perturb_ratio <= 1 "perturb_ratio must be between 0 and 1"
num_current_edges = g.num_edges
num_edges_to_add = ceil(Int, num_current_edges * perturb_ratio)
if num_edges_to_add == 0
return g
end
num_nodes = g.num_nodes
@assert num_nodes > 1 "Graph must contain at least 2 nodes to add edges"
snew = ceil.(Int, rand_like(rng, ones(num_nodes), Float32, num_edges_to_add) .* num_nodes)
tnew = ceil.(Int, rand_like(rng, ones(num_nodes), Float32, num_edges_to_add) .* num_nodes)
mask_loops = snew .!= tnew
snew = snew[mask_loops]
tnew = tnew[mask_loops]
while length(snew) < num_edges_to_add
n = num_edges_to_add - length(snew)
snewnew = ceil.(Int, rand_like(rng, ones(num_nodes), Float32, n) .* num_nodes)
tnewnew = ceil.(Int, rand_like(rng, ones(num_nodes), Float32, n) .* num_nodes)
mask_new_loops = snewnew .!= tnewnew
snewnew = snewnew[mask_new_loops]
tnewnew = tnewnew[mask_new_loops]
snew = [snew; snewnew]
tnew = [tnew; tnewnew]
end
return add_edges(g, (snew, tnew, nothing))
end
### TODO Cannot implement this since GNNGraph is immutable (cannot change num_edges). make it mutable
# function Graphs.add_edge!(g::GNNGraph{<:COO_T}, snew::T, tnew::T; edata=nothing) where T<:Union{Integer, AbstractVector}
# s, t = edge_index(g)
# @assert length(snew) == length(tnew)
# # TODO remove this constraint
# @assert get_edge_weight(g) === nothing
# edata = normalize_graphdata(edata, default_name=:e, n=length(snew))
# edata = cat_features(g.edata, edata)
# s, t = edge_index(g)
# append!(s, snew)
# append!(t, tnew)
# g.num_edges += length(snew)
# return true
# end
"""
to_bidirected(g)
Adds a reverse edge for each edge in the graph, then calls
[`remove_multi_edges`](@ref) with `mean` aggregation to simplify the graph.
See also [`is_bidirected`](@ref).
# Examples
```jldoctest
julia> s, t = [1, 2, 3, 3, 4], [2, 3, 4, 4, 4];
julia> w = [1.0, 2.0, 3.0, 4.0, 5.0];
julia> e = [10.0, 20.0, 30.0, 40.0, 50.0];
julia> g = GNNGraph(s, t, w, edata = e)
GNNGraph:
num_nodes = 4
num_edges = 5
edata:
e => (5,)
julia> g2 = to_bidirected(g)
GNNGraph:
num_nodes = 4
num_edges = 7
edata:
e => (7,)
julia> edge_index(g2)
([1, 2, 2, 3, 3, 4, 4], [2, 1, 3, 2, 4, 3, 4])
julia> get_edge_weight(g2)
7-element Vector{Float64}:
1.0
1.0
2.0
2.0
3.5
3.5
5.0
julia> g2.edata.e
7-element Vector{Float64}:
10.0
10.0
20.0
20.0
35.0
35.0
50.0
```
"""
function to_bidirected(g::GNNGraph{<:COO_T})
s, t = edge_index(g)
w = get_edge_weight(g)
snew = [s; t]
tnew = [t; s]
w = cat_features(w, w)
edata = cat_features(g.edata, g.edata)
g = GNNGraph((snew, tnew, w),
g.num_nodes, length(snew), g.num_graphs,
g.graph_indicator,
g.ndata, edata, g.gdata)
return remove_multi_edges(g; aggr = mean)
end
"""
to_unidirected(g::GNNGraph)
Return a graph that for each multiple edge between two nodes in `g`
keeps only an edge in one direction.
"""
function to_unidirected(g::GNNGraph{<:COO_T})
s, t = edge_index(g)
w = get_edge_weight(g)
idxs, _ = edge_encoding(s, t, g.num_nodes, directed = false)
snew, tnew = edge_decoding(idxs, g.num_nodes, directed = false)
g = GNNGraph((snew, tnew, w),
g.num_nodes, g.num_edges, g.num_graphs,
g.graph_indicator,
g.ndata, g.edata, g.gdata)
return remove_multi_edges(g; aggr = mean)
end
function Graphs.SimpleGraph(g::GNNGraph)
G = Graphs.SimpleGraph(g.num_nodes)
for e in Graphs.edges(g)
Graphs.add_edge!(G, e)
end
return G
end
function Graphs.SimpleDiGraph(g::GNNGraph)
G = Graphs.SimpleDiGraph(g.num_nodes)
for e in Graphs.edges(g)
Graphs.add_edge!(G, e)
end
return G
end
"""
add_nodes(g::GNNGraph, n; [ndata])
Add `n` new nodes to graph `g`. In the
new graph, these nodes will have indexes from `g.num_nodes + 1`
to `g.num_nodes + n`.
"""
function add_nodes(g::GNNGraph{<:COO_T}, n::Integer; ndata = (;))
ndata = normalize_graphdata(ndata, default_name = :x, n = n)
ndata = cat_features(g.ndata, ndata)
GNNGraph(g.graph,
g.num_nodes + n, g.num_edges, g.num_graphs,
g.graph_indicator,
ndata, g.edata, g.gdata)
end
"""
set_edge_weight(g::GNNGraph, w::AbstractVector)
Set `w` as edge weights in the returned graph.
"""
function set_edge_weight(g::GNNGraph, w::AbstractVector)
s, t = edge_index(g)
@assert length(w) == length(s)
return GNNGraph((s, t, w),
g.num_nodes, g.num_edges, g.num_graphs,
g.graph_indicator,
g.ndata, g.edata, g.gdata)
end
function SparseArrays.blockdiag(g1::GNNGraph, g2::GNNGraph)
nv1, nv2 = g1.num_nodes, g2.num_nodes
if g1.graph isa COO_T
s1, t1 = edge_index(g1)
s2, t2 = edge_index(g2)
s = vcat(s1, nv1 .+ s2)
t = vcat(t1, nv1 .+ t2)
w = cat_features(get_edge_weight(g1), get_edge_weight(g2))
graph = (s, t, w)
ind1 = isnothing(g1.graph_indicator) ? ones_like(s1, nv1) : g1.graph_indicator
ind2 = isnothing(g2.graph_indicator) ? ones_like(s2, nv2) : g2.graph_indicator
elseif g1.graph isa ADJMAT_T
graph = blockdiag(g1.graph, g2.graph)
ind1 = isnothing(g1.graph_indicator) ? ones_like(graph, nv1) : g1.graph_indicator
ind2 = isnothing(g2.graph_indicator) ? ones_like(graph, nv2) : g2.graph_indicator
end
graph_indicator = vcat(ind1, g1.num_graphs .+ ind2)
GNNGraph(graph,
nv1 + nv2, g1.num_edges + g2.num_edges, g1.num_graphs + g2.num_graphs,
graph_indicator,
cat_features(g1.ndata, g2.ndata),
cat_features(g1.edata, g2.edata),
cat_features(g1.gdata, g2.gdata))
end
# PIRACY
function SparseArrays.blockdiag(A1::AbstractMatrix, A2::AbstractMatrix)
m1, n1 = size(A1)
@assert m1 == n1
m2, n2 = size(A2)
@assert m2 == n2
O1 = fill!(similar(A1, eltype(A1), (m1, n2)), 0)
O2 = fill!(similar(A1, eltype(A1), (m2, n1)), 0)
return [A1 O1
O2 A2]
end
"""
blockdiag(xs::GNNGraph...)
Equivalent to [`MLUtils.batch`](@ref).
"""
function SparseArrays.blockdiag(g1::GNNGraph, gothers::GNNGraph...)
g = g1
for go in gothers
g = blockdiag(g, go)
end
return g
end
"""
batch(gs::Vector{<:GNNGraph})
Batch together multiple `GNNGraph`s into a single one
containing the total number of original nodes and edges.
Equivalent to [`SparseArrays.blockdiag`](@ref).
See also [`MLUtils.unbatch`](@ref).
# Examples
```jldoctest
julia> g1 = rand_graph(4, 6, ndata=ones(8, 4))
GNNGraph:
num_nodes = 4
num_edges = 6
ndata:
x => (8, 4)
julia> g2 = rand_graph(7, 4, ndata=zeros(8, 7))
GNNGraph:
num_nodes = 7
num_edges = 4
ndata:
x => (8, 7)
julia> g12 = MLUtils.batch([g1, g2])
GNNGraph:
num_nodes = 11
num_edges = 10
num_graphs = 2
ndata:
x => (8, 11)
julia> g12.ndata.x
8×11 Matrix{Float64}:
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
```
"""
function MLUtils.batch(gs::AbstractVector{<:GNNGraph})
Told = eltype(gs)
# try to restrict the eltype
gs = [g for g in gs]
if eltype(gs) != Told
return MLUtils.batch(gs)
else
return blockdiag(gs...)
end
end
function MLUtils.batch(gs::AbstractVector{<:GNNGraph{T}}) where {T <: COO_T}
v_num_nodes = [g.num_nodes for g in gs]
edge_indices = [edge_index(g) for g in gs]
nodesum = cumsum([0; v_num_nodes])[1:(end - 1)]
s = cat_features([ei[1] .+ nodesum[ii] for (ii, ei) in enumerate(edge_indices)])
t = cat_features([ei[2] .+ nodesum[ii] for (ii, ei) in enumerate(edge_indices)])
w = cat_features([get_edge_weight(g) for g in gs])
graph = (s, t, w)
function materialize_graph_indicator(g)
g.graph_indicator === nothing ? ones_like(s, g.num_nodes) : g.graph_indicator
end
v_gi = materialize_graph_indicator.(gs)
v_num_graphs = [g.num_graphs for g in gs]
graphsum = cumsum([0; v_num_graphs])[1:(end - 1)]
v_gi = [ng .+ gi for (ng, gi) in zip(graphsum, v_gi)]
graph_indicator = cat_features(v_gi)
GNNGraph(graph,
sum(v_num_nodes),
sum([g.num_edges for g in gs]),
sum(v_num_graphs),
graph_indicator,
cat_features([g.ndata for g in gs]),
cat_features([g.edata for g in gs]),
cat_features([g.gdata for g in gs]))
end
function MLUtils.batch(g::GNNGraph)
throw(ArgumentError("Cannot batch a `GNNGraph` (containing $(g.num_graphs) graphs). Pass a vector of `GNNGraph`s instead."))
end
function MLUtils.batch(gs::AbstractVector{<:GNNHeteroGraph})
function edge_index_nullable(g::GNNHeteroGraph{<:COO_T}, edge_t::EType)
if haskey(g.graph, edge_t)
g.graph[edge_t][1:2]
else
nothing
end
end
function get_edge_weight_nullable(g::GNNHeteroGraph{<:COO_T}, edge_t::EType)
get(g.graph, edge_t, (nothing, nothing, nothing))[3]
end
@assert length(gs) > 0
ntypes = union([g.ntypes for g in gs]...)
etypes = union([g.etypes for g in gs]...)
v_num_nodes = Dict(node_t => [get(g.num_nodes, node_t, 0) for g in gs] for node_t in ntypes)
num_nodes = Dict(node_t => sum(v_num_nodes[node_t]) for node_t in ntypes)
num_edges = Dict(edge_t => sum(get(g.num_edges, edge_t, 0) for g in gs) for edge_t in etypes)
edge_indices = edge_indices = Dict(edge_t => [edge_index_nullable(g, edge_t) for g in gs] for edge_t in etypes)
nodesum = Dict(node_t => cumsum([0; v_num_nodes[node_t]])[1:(end - 1)] for node_t in ntypes)
graphs = []
for edge_t in etypes
src_t, _, dst_t = edge_t
# @show edge_t edge_indices[edge_t] first(edge_indices[edge_t])
# for ei in edge_indices[edge_t]
# @show ei[1]
# end
# # [ei[1] for (ii, ei) in enumerate(edge_indices[edge_t])]
s = cat_features([ei[1] .+ nodesum[src_t][ii] for (ii, ei) in enumerate(edge_indices[edge_t]) if ei !== nothing])
t = cat_features([ei[2] .+ nodesum[dst_t][ii] for (ii, ei) in enumerate(edge_indices[edge_t]) if ei !== nothing])
w = cat_features(filter(x -> x !== nothing, [get_edge_weight_nullable(g, edge_t) for g in gs]))
push!(graphs, edge_t => (s, t, w))
end
graph = Dict(graphs...)
#TODO relax this restriction
@assert all(g -> g.num_graphs == 1, gs)
s = edge_index(gs[1], gs[1].etypes[1])[1] # grab any source vector
function materialize_graph_indicator(g, node_t)
n = get(g.num_nodes, node_t, 0)
return ones_like(s, n)
end
v_gi = Dict(node_t => [materialize_graph_indicator(g, node_t) for g in gs] for node_t in ntypes)
v_num_graphs = [g.num_graphs for g in gs]
graphsum = cumsum([0; v_num_graphs])[1:(end - 1)]
v_gi = Dict(node_t => [ng .+ gi for (ng, gi) in zip(graphsum, v_gi[node_t])] for node_t in ntypes)
graph_indicator = Dict(node_t => cat_features(v_gi[node_t]) for node_t in ntypes)
function data_or_else(data, types)
Dict(type => get(data, type, DataStore(0)) for type in types)
end
return GNNHeteroGraph(graph,
num_nodes,
num_edges,
sum(v_num_graphs),
graph_indicator,
cat_features([data_or_else(g.ndata, ntypes) for g in gs]),
cat_features([data_or_else(g.edata, etypes) for g in gs]),
cat_features([g.gdata for g in gs]),
ntypes, etypes)
end
"""
unbatch(g::GNNGraph)
Opposite of the [`MLUtils.batch`](@ref) operation, returns
an array of the individual graphs batched together in `g`.
See also [`MLUtils.batch`](@ref) and [`getgraph`](@ref).
# Examples
```jldoctest
julia> gbatched = MLUtils.batch([rand_graph(5, 6), rand_graph(10, 8), rand_graph(4,2)])
GNNGraph:
num_nodes = 19
num_edges = 16
num_graphs = 3
julia> MLUtils.unbatch(gbatched)
3-element Vector{GNNGraph{Tuple{Vector{Int64}, Vector{Int64}, Nothing}}}:
GNNGraph:
num_nodes = 5
num_edges = 6
GNNGraph:
num_nodes = 10
num_edges = 8
GNNGraph:
num_nodes = 4
num_edges = 2
```
"""
function MLUtils.unbatch(g::GNNGraph{T}) where {T <: COO_T}
g.num_graphs == 1 && return [g]
nodemasks = _unbatch_nodemasks(g.graph_indicator, g.num_graphs)
num_nodes = length.(nodemasks)
cumnum_nodes = [0; cumsum(num_nodes)]
s, t = edge_index(g)
w = get_edge_weight(g)
edgemasks = _unbatch_edgemasks(s, t, g.num_graphs, cumnum_nodes)
num_edges = length.(edgemasks)
@assert sum(num_edges)==g.num_edges "Error in unbatching, likely the edges are not sorted (first edges belong to the first graphs, then edges in the second graph and so on)"
function build_graph(i)
node_mask = nodemasks[i]
edge_mask = edgemasks[i]