Skip to content

Commit 403b423

Browse files
fix many gnngraph doctests
1 parent af0455a commit 403b423

File tree

10 files changed

+98
-113
lines changed

10 files changed

+98
-113
lines changed

GNNGraphs/docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Documenter
22
using DocumenterInterLinks
33
using GNNGraphs
4+
using MLUtils # this is needed by setdocmeta!
45
import Graphs
56
using Graphs: induced_subgraph
67

GNNGraphs/docs/src/guides/heterograph.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
```@meta
2+
CurrentModule = GNNGraphs
3+
```
4+
15
# Heterogeneous Graphs
26

37
Heterogeneous graphs (also called heterographs), are graphs where each node has a type,
@@ -67,18 +71,15 @@ julia> g.num_edges
6771
Dict{Tuple{Symbol, Symbol, Symbol}, Int64} with 1 entry:
6872
(:user, :rate, :movie) => 4
6973
70-
# source and target node for a given relation
71-
julia> edge_index(g, (:user, :rate, :movie))
74+
julia> edge_index(g, (:user, :rate, :movie)) # source and target node for a given relation
7275
([1, 1, 2, 3], [7, 13, 5, 7])
7376
74-
# node types
75-
julia> g.ntypes
77+
julia> g.ntypes # node types
7678
2-element Vector{Symbol}:
7779
:user
7880
:movie
7981
80-
# edge types
81-
julia> g.etypes
82+
julia> g.etypes # edge types
8283
1-element Vector{Tuple{Symbol, Symbol, Symbol}}:
8384
(:user, :rate, :movie)
8485
```
@@ -120,8 +121,8 @@ GNNHeteroGraph:
120121
Batching is automatically performed by the [`DataLoader`](https://fluxml.ai/Flux.jl/stable/data/mlutils/#MLUtils.DataLoader) iterator
121122
when the `collate` option is set to `true`.
122123

123-
```jldoctest hetero
124-
using Flux: DataLoader
124+
```julia
125+
using MLUtils: DataLoader
125126

126127
data = [rand_bipartite_heterograph((5, 10), 20,
127128
ndata=Dict(:A=>rand(Float32, 3, 5)))

GNNGraphs/docs/src/guides/temporalgraph.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
```@meta
2+
CurrentModule = GNNGraphs
3+
```
4+
15
# Temporal Graphs
26

37
Temporal Graphs are graphs with time varying topologies and features. In GNNGraphs.jl, temporal graphs with fixed number of nodes over time are supported by the [`TemporalSnapshotsGNNGraph`](@ref) type.
@@ -45,7 +49,7 @@ TemporalSnapshotsGNNGraph:
4549

4650
See [`rand_temporal_radius_graph`](@ref) and [`rand_temporal_hyperbolic_graph`](@ref) for generating random temporal graphs.
4751

48-
```jldoctest temporal
52+
```julia
4953
julia> tg = rand_temporal_radius_graph(10, 3, 0.1, 0.5)
5054
TemporalSnapshotsGNNGraph:
5155
num_nodes: [10, 10, 10]
@@ -97,28 +101,30 @@ A temporal graph can store global feature for the entire time series in the `tgd
97101
Also, each snapshot can store node, edge, and graph features in the `ndata`, `edata`, and `gdata` fields, respectively.
98102

99103
```jldoctest temporal
100-
julia> snapshots = [rand_graph(10,20; ndata = rand(3,10)), rand_graph(10,14; ndata = rand(4,10)), rand_graph(10,22; ndata = rand(5,10))]; # node features at construction time
104+
julia> snapshots = [rand_graph(10, 20; ndata = rand(Float32, 3, 10)),
105+
rand_graph(10, 14; ndata = rand(Float32, 4, 10)),
106+
rand_graph(10, 22; ndata = rand(Float32, 5, 10))]; # node features at construction time
101107
102108
julia> tg = TemporalSnapshotsGNNGraph(snapshots);
103109
104-
julia> tg.tgdata.y = rand(3,1); # add global features after construction
110+
julia> tg.tgdata.y = rand(Float32, 3, 1); # add global features after construction
105111
106112
julia> tg
107113
TemporalSnapshotsGNNGraph:
108114
num_nodes: [10, 10, 10]
109115
num_edges: [20, 14, 22]
110116
num_snapshots: 3
111117
tgdata:
112-
y = 3×1 Matrix{Float64}
118+
y = 3×1 Matrix{Float32}
113119
114120
julia> tg.ndata # vector of DataStore containing node features for each snapshot
115121
3-element Vector{DataStore}:
116122
DataStore(10) with 1 element:
117-
x = 3×10 Matrix{Float64}
123+
x = 3×10 Matrix{Float32}
118124
DataStore(10) with 1 element:
119-
x = 4×10 Matrix{Float64}
125+
x = 4×10 Matrix{Float32}
120126
DataStore(10) with 1 element:
121-
x = 5×10 Matrix{Float64}
127+
x = 5×10 Matrix{Float32}
122128
123129
julia> [ds.x for ds in tg.ndata]; # vector containing the x feature of each snapshot
124130

GNNGraphs/src/GNNGraphs.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using SparseArrays
44
using Functors: @functor
55
import Graphs
66
using Graphs: AbstractGraph, outneighbors, inneighbors, adjacency_matrix, degree,
7-
has_self_loops, is_directed, induced_subgraph
7+
has_self_loops, is_directed, induced_subgraph, has_edge
88
import NearestNeighbors
99
import NNlib
1010
import StatsBase
@@ -58,8 +58,9 @@ export adjacency_list,
5858
# from Graphs
5959
adjacency_matrix,
6060
degree,
61-
has_self_loops,
61+
has_edge,
6262
has_isolated_nodes,
63+
has_self_loops,
6364
inneighbors,
6465
outneighbors,
6566
khop_adj

GNNGraphs/src/datastore.jl

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,12 @@ DataStore(3) with 2 elements:
1515
x = 2×3 Matrix{Float32}
1616
1717
julia> ds = DataStore(3, Dict(:x => rand(Float32, 2, 3), :y => rand(Float32, 3))); # equivalent to above
18-
19-
julia> ds = DataStore(3, (x = rand(Float32, 2, 3), y = rand(Float32, 30)))
20-
ERROR: AssertionError: DataStore: data[y] has 30 observations, but n = 3
21-
Stacktrace:
22-
[1] DataStore(n::Int64, data::Dict{Symbol, Any})
23-
@ GNNGraphs ~/.julia/dev/GNNGraphs/datastore.jl:54
24-
[2] DataStore(n::Int64, data::NamedTuple{(:x, :y), Tuple{Matrix{Float32}, Vector{Float32}}})
25-
@ GNNGraphs ~/.julia/dev/GNNGraphs/datastore.jl:73
26-
[3] top-level scope
27-
@ REPL[13]:1
28-
29-
julia> ds = DataStore(x = randFloat32, 2, 3), y = rand(Float32, 30)) # no checks
30-
DataStore() with 2 elements:
31-
y = 30-element Vector{Float32}
32-
x = 2×3 Matrix{Float32}
33-
y = 30-element Vector{Float64}
34-
x = 2×3 Matrix{Float64}
3518
```
3619
3720
The `DataStore` has an interface similar to both dictionaries and named tuples.
3821
Arrays can be accessed and added using either the indexing or the property syntax:
3922
40-
```jldoctest datastore
23+
```jldoctest docstr_datastore
4124
julia> ds = DataStore(x = ones(Float32, 2, 3), y = zeros(Float32, 3))
4225
DataStore() with 2 elements:
4326
y = 3-element Vector{Float32}
@@ -59,14 +42,16 @@ The `DataStore` can be iterated over, and the keys and values can be accessed
5942
using `keys(ds)` and `values(ds)`. `map(f, ds)` applies the function `f`
6043
to each feature array:
6144
62-
```jldoctest datastore
45+
```jldoctest docstr_datastore
6346
julia> ds2 = map(x -> x .+ 1, ds)
64-
DataStore() with 2 elements:
65-
a = 2-element Vector{Float64}
66-
b = 2-element Vector{Float64}
47+
DataStore() with 3 elements:
48+
y = 3-element Vector{Float32}
49+
z = 3-element Vector{Float32}
50+
x = 2×3 Matrix{Float32}
6751
68-
julia> ds2.a
69-
2-element Vector{Float64}:
52+
julia> ds2.z
53+
3-element Vector{Float32}:
54+
1.0
7055
1.0
7156
1.0
7257
```

GNNGraphs/src/generate.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ Additional keyword arguments will be passed to the [`GNNGraph`](@ref) constructo
1616
1717
# Examples
1818
19-
```jldoctest
19+
```julia
2020
julia> g = rand_graph(5, 4, bidirected=false)
2121
GNNGraph:
22-
num_nodes = 5
23-
num_edges = 4
22+
num_nodes: 5
23+
num_edges: 4
2424
2525
julia> edge_index(g)
26-
([1, 3, 3, 4], [5, 4, 5, 2])
26+
([4, 3, 2, 1], [5, 4, 3, 2])
2727
2828
# In the bidirected case, edge data will be duplicated on the reverse edges if needed.
2929
julia> g = rand_graph(5, 4, edata=rand(Float32, 16, 2))
3030
GNNGraph:
31-
num_nodes = 5
32-
num_edges = 4
33-
edata:
34-
e => (16, 4)
31+
num_nodes: 5
32+
num_edges: 4
33+
edata:
34+
e = 16×4 Matrix{Float32}
3535
3636
# Each edge has a reverse
3737
julia> edge_index(g)
38-
([1, 3, 3, 4], [3, 4, 1, 3])
38+
([1, 1, 5, 3], [5, 3, 1, 1])
3939
```
4040
"""
4141
function rand_graph(n::Integer, m::Integer; seed=-1, kws...)
@@ -85,8 +85,8 @@ Additional keyword arguments will be passed to the [`GNNHeteroGraph`](@ref) cons
8585
julia> g = rand_heterograph((:user => 10, :movie => 20),
8686
(:user, :rate, :movie) => 30)
8787
GNNHeteroGraph:
88-
num_nodes: (:user => 10, :movie => 20)
89-
num_edges: ((:user, :rate, :movie) => 30,)
88+
num_nodes: Dict(:movie => 20, :user => 10)
89+
num_edges: Dict((:user, :rate, :movie) => 30)
9090
```
9191
"""
9292
function rand_heterograph end
@@ -161,7 +161,7 @@ See [`rand_heterograph`](@ref) for a more general version.
161161
162162
# Examples
163163
164-
```julia-repl
164+
```julia
165165
julia> g = rand_bipartite_heterograph((10, 15), 20)
166166
GNNHeteroGraph:
167167
num_nodes: (:A => 10, :B => 15)
@@ -214,7 +214,7 @@ to its `k` closest `points`.
214214
215215
# Examples
216216
217-
```jldoctest
217+
```julia
218218
julia> n, k = 10, 3;
219219
220220
julia> x = rand(Float32, 3, n);
@@ -231,7 +231,6 @@ GNNGraph:
231231
num_nodes = 10
232232
num_edges = 30
233233
num_graphs = 2
234-
235234
```
236235
"""
237236
function knn_graph(points::AbstractMatrix, k::Int;
@@ -295,7 +294,7 @@ to its neighbors within a given distance `r`.
295294
296295
# Examples
297296
298-
```jldoctest
297+
```julia
299298
julia> n, r = 10, 0.75;
300299
301300
julia> x = rand(Float32, 3, n);
@@ -312,9 +311,10 @@ GNNGraph:
312311
num_nodes = 10
313312
num_edges = 20
314313
num_graphs = 2
315-
316314
```
315+
317316
# References
317+
318318
Section B paragraphs 1 and 2 of the paper [Dynamic Hidden-Variable Network Models](https://arxiv.org/pdf/2101.00414.pdf)
319319
"""
320320
function radius_graph(points::AbstractMatrix, r::AbstractFloat;
@@ -447,7 +447,7 @@ First, the positions of the nodes are generated with a quasi-uniform distributio
447447
448448
# Example
449449
450-
```jldoctest
450+
```julia
451451
julia> n, snaps, α, R, speed, ζ = 10, 5, 1.0, 4.0, 0.1, 1.0;
452452
453453
julia> thg = rand_temporal_hyperbolic_graph(n, snaps; α, R, speed, ζ)

GNNGraphs/src/mldatasets.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ julia> using MLDatasets, GNNGraphs
1212
1313
julia> mldataset2gnngraph(Cora())
1414
GNNGraph:
15-
num_nodes = 2708
16-
num_edges = 10556
17-
ndata:
18-
features => 1433×2708 Matrix{Float32}
19-
targets => 2708-element Vector{Int64}
20-
train_mask => 2708-element BitVector
21-
val_mask => 2708-element BitVector
22-
test_mask => 2708-element BitVector
15+
num_nodes: 2708
16+
num_edges: 10556
17+
ndata:
18+
val_mask = 2708-element BitVector
19+
targets = 2708-element Vector{Int64}
20+
test_mask = 2708-element BitVector
21+
features = 1433×2708 Matrix{Float32}
22+
train_mask = 2708-element BitVector
2323
```
2424
"""
2525
function mldataset2gnngraph(dataset::D) where {D}

GNNGraphs/src/query.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ Return `true` if there is an edge of type `edge_t` from node `i` to node `j` in
6565
```jldoctest
6666
julia> g = rand_bipartite_heterograph((2, 2), (4, 0), bidirected=false)
6767
GNNHeteroGraph:
68-
num_nodes: (:A => 2, :B => 2)
69-
num_edges: ((:A, :to, :B) => 4, (:B, :to, :A) => 0)
68+
num_nodes: Dict(:A => 2, :B => 2)
69+
num_edges: Dict((:A, :to, :B) => 4, (:B, :to, :A) => 0)
7070
7171
julia> has_edge(g, (:A,:to,:B), 1, 1)
7272
true

GNNGraphs/src/samplers.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ originally introduced in ["Inductive Representation Learning on Large Graphs"}(h
1212
- `num_layers::Int`: The number of layers for neighborhood expansion (how far to sample neighbors).
1313
- `batch_size::Union{Int, Nothing}`: The size of the batch. If not specified, it defaults to the number of `input_nodes`.
1414
15-
# Usage
16-
```jldoctest
17-
julia> loader = NeighborLoader(graph; num_neighbors=[10, 5], input_nodes=[1, 2, 3], num_layers=2)
15+
# Examples
16+
17+
```julia
18+
julia> loader = NeighborLoader(graph; num_neighbors=[10, 5], input_nodes=[1, 2, 3], num_layers=2)
1819
1920
julia> batch_counter = 0
21+
2022
julia> for mini_batch_gnn in loader
2123
batch_counter += 1
2224
println("Batch ", batch_counter, ": Nodes in mini-batch graph: ", nv(mini_batch_gnn))

0 commit comments

Comments
 (0)