Skip to content

Commit

Permalink
fix many gnngraph doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Dec 2, 2024
1 parent af0455a commit 403b423
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 113 deletions.
1 change: 1 addition & 0 deletions GNNGraphs/docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Documenter
using DocumenterInterLinks
using GNNGraphs
using MLUtils # this is needed by setdocmeta!
import Graphs
using Graphs: induced_subgraph

Expand Down
17 changes: 9 additions & 8 deletions GNNGraphs/docs/src/guides/heterograph.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
```@meta
CurrentModule = GNNGraphs
```

# Heterogeneous Graphs

Heterogeneous graphs (also called heterographs), are graphs where each node has a type,
Expand Down Expand Up @@ -67,18 +71,15 @@ julia> g.num_edges
Dict{Tuple{Symbol, Symbol, Symbol}, Int64} with 1 entry:
(:user, :rate, :movie) => 4
# source and target node for a given relation
julia> edge_index(g, (:user, :rate, :movie))
julia> edge_index(g, (:user, :rate, :movie)) # source and target node for a given relation
([1, 1, 2, 3], [7, 13, 5, 7])
# node types
julia> g.ntypes
julia> g.ntypes # node types
2-element Vector{Symbol}:
:user
:movie
# edge types
julia> g.etypes
julia> g.etypes # edge types
1-element Vector{Tuple{Symbol, Symbol, Symbol}}:
(:user, :rate, :movie)
```
Expand Down Expand Up @@ -120,8 +121,8 @@ GNNHeteroGraph:
Batching is automatically performed by the [`DataLoader`](https://fluxml.ai/Flux.jl/stable/data/mlutils/#MLUtils.DataLoader) iterator
when the `collate` option is set to `true`.

```jldoctest hetero
using Flux: DataLoader
```julia
using MLUtils: DataLoader

data = [rand_bipartite_heterograph((5, 10), 20,
ndata=Dict(:A=>rand(Float32, 3, 5)))
Expand Down
20 changes: 13 additions & 7 deletions GNNGraphs/docs/src/guides/temporalgraph.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
```@meta
CurrentModule = GNNGraphs
```

# Temporal Graphs

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.
Expand Down Expand Up @@ -45,7 +49,7 @@ TemporalSnapshotsGNNGraph:

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

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

```jldoctest temporal
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
julia> snapshots = [rand_graph(10, 20; ndata = rand(Float32, 3, 10)),
rand_graph(10, 14; ndata = rand(Float32, 4, 10)),
rand_graph(10, 22; ndata = rand(Float32, 5, 10))]; # node features at construction time
julia> tg = TemporalSnapshotsGNNGraph(snapshots);
julia> tg.tgdata.y = rand(3,1); # add global features after construction
julia> tg.tgdata.y = rand(Float32, 3, 1); # add global features after construction
julia> tg
TemporalSnapshotsGNNGraph:
num_nodes: [10, 10, 10]
num_edges: [20, 14, 22]
num_snapshots: 3
tgdata:
y = 3×1 Matrix{Float64}
y = 3×1 Matrix{Float32}
julia> tg.ndata # vector of DataStore containing node features for each snapshot
3-element Vector{DataStore}:
DataStore(10) with 1 element:
x = 3×10 Matrix{Float64}
x = 3×10 Matrix{Float32}
DataStore(10) with 1 element:
x = 4×10 Matrix{Float64}
x = 4×10 Matrix{Float32}
DataStore(10) with 1 element:
x = 5×10 Matrix{Float64}
x = 5×10 Matrix{Float32}
julia> [ds.x for ds in tg.ndata]; # vector containing the x feature of each snapshot
Expand Down
5 changes: 3 additions & 2 deletions GNNGraphs/src/GNNGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using SparseArrays
using Functors: @functor
import Graphs
using Graphs: AbstractGraph, outneighbors, inneighbors, adjacency_matrix, degree,
has_self_loops, is_directed, induced_subgraph
has_self_loops, is_directed, induced_subgraph, has_edge
import NearestNeighbors
import NNlib
import StatsBase
Expand Down Expand Up @@ -58,8 +58,9 @@ export adjacency_list,
# from Graphs
adjacency_matrix,
degree,
has_self_loops,
has_edge,
has_isolated_nodes,
has_self_loops,
inneighbors,
outneighbors,
khop_adj
Expand Down
33 changes: 9 additions & 24 deletions GNNGraphs/src/datastore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,12 @@ DataStore(3) with 2 elements:
x = 2×3 Matrix{Float32}
julia> ds = DataStore(3, Dict(:x => rand(Float32, 2, 3), :y => rand(Float32, 3))); # equivalent to above
julia> ds = DataStore(3, (x = rand(Float32, 2, 3), y = rand(Float32, 30)))
ERROR: AssertionError: DataStore: data[y] has 30 observations, but n = 3
Stacktrace:
[1] DataStore(n::Int64, data::Dict{Symbol, Any})
@ GNNGraphs ~/.julia/dev/GNNGraphs/datastore.jl:54
[2] DataStore(n::Int64, data::NamedTuple{(:x, :y), Tuple{Matrix{Float32}, Vector{Float32}}})
@ GNNGraphs ~/.julia/dev/GNNGraphs/datastore.jl:73
[3] top-level scope
@ REPL[13]:1
julia> ds = DataStore(x = randFloat32, 2, 3), y = rand(Float32, 30)) # no checks
DataStore() with 2 elements:
y = 30-element Vector{Float32}
x = 2×3 Matrix{Float32}
y = 30-element Vector{Float64}
x = 2×3 Matrix{Float64}
```
The `DataStore` has an interface similar to both dictionaries and named tuples.
Arrays can be accessed and added using either the indexing or the property syntax:
```jldoctest datastore
```jldoctest docstr_datastore
julia> ds = DataStore(x = ones(Float32, 2, 3), y = zeros(Float32, 3))
DataStore() with 2 elements:
y = 3-element Vector{Float32}
Expand All @@ -59,14 +42,16 @@ The `DataStore` can be iterated over, and the keys and values can be accessed
using `keys(ds)` and `values(ds)`. `map(f, ds)` applies the function `f`
to each feature array:
```jldoctest datastore
```jldoctest docstr_datastore
julia> ds2 = map(x -> x .+ 1, ds)
DataStore() with 2 elements:
a = 2-element Vector{Float64}
b = 2-element Vector{Float64}
DataStore() with 3 elements:
y = 3-element Vector{Float32}
z = 3-element Vector{Float32}
x = 2×3 Matrix{Float32}
julia> ds2.a
2-element Vector{Float64}:
julia> ds2.z
3-element Vector{Float32}:
1.0
1.0
1.0
```
Expand Down
34 changes: 17 additions & 17 deletions GNNGraphs/src/generate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ Additional keyword arguments will be passed to the [`GNNGraph`](@ref) constructo
# Examples
```jldoctest
```julia
julia> g = rand_graph(5, 4, bidirected=false)
GNNGraph:
num_nodes = 5
num_edges = 4
num_nodes: 5
num_edges: 4
julia> edge_index(g)
([1, 3, 3, 4], [5, 4, 5, 2])
([4, 3, 2, 1], [5, 4, 3, 2])
# In the bidirected case, edge data will be duplicated on the reverse edges if needed.
julia> g = rand_graph(5, 4, edata=rand(Float32, 16, 2))
GNNGraph:
num_nodes = 5
num_edges = 4
edata:
e => (16, 4)
num_nodes: 5
num_edges: 4
edata:
e = 16×4 Matrix{Float32}
# Each edge has a reverse
julia> edge_index(g)
([1, 3, 3, 4], [3, 4, 1, 3])
([1, 1, 5, 3], [5, 3, 1, 1])
```
"""
function rand_graph(n::Integer, m::Integer; seed=-1, kws...)
Expand Down Expand Up @@ -85,8 +85,8 @@ Additional keyword arguments will be passed to the [`GNNHeteroGraph`](@ref) cons
julia> g = rand_heterograph((:user => 10, :movie => 20),
(:user, :rate, :movie) => 30)
GNNHeteroGraph:
num_nodes: (:user => 10, :movie => 20)
num_edges: ((:user, :rate, :movie) => 30,)
num_nodes: Dict(:movie => 20, :user => 10)
num_edges: Dict((:user, :rate, :movie) => 30)
```
"""
function rand_heterograph end
Expand Down Expand Up @@ -161,7 +161,7 @@ See [`rand_heterograph`](@ref) for a more general version.
# Examples
```julia-repl
```julia
julia> g = rand_bipartite_heterograph((10, 15), 20)
GNNHeteroGraph:
num_nodes: (:A => 10, :B => 15)
Expand Down Expand Up @@ -214,7 +214,7 @@ to its `k` closest `points`.
# Examples
```jldoctest
```julia
julia> n, k = 10, 3;
julia> x = rand(Float32, 3, n);
Expand All @@ -231,7 +231,6 @@ GNNGraph:
num_nodes = 10
num_edges = 30
num_graphs = 2
```
"""
function knn_graph(points::AbstractMatrix, k::Int;
Expand Down Expand Up @@ -295,7 +294,7 @@ to its neighbors within a given distance `r`.
# Examples
```jldoctest
```julia
julia> n, r = 10, 0.75;
julia> x = rand(Float32, 3, n);
Expand All @@ -312,9 +311,10 @@ GNNGraph:
num_nodes = 10
num_edges = 20
num_graphs = 2
```
# References
Section B paragraphs 1 and 2 of the paper [Dynamic Hidden-Variable Network Models](https://arxiv.org/pdf/2101.00414.pdf)
"""
function radius_graph(points::AbstractMatrix, r::AbstractFloat;
Expand Down Expand Up @@ -447,7 +447,7 @@ First, the positions of the nodes are generated with a quasi-uniform distributio
# Example
```jldoctest
```julia
julia> n, snaps, α, R, speed, ζ = 10, 5, 1.0, 4.0, 0.1, 1.0;
julia> thg = rand_temporal_hyperbolic_graph(n, snaps; α, R, speed, ζ)
Expand Down
16 changes: 8 additions & 8 deletions GNNGraphs/src/mldatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ julia> using MLDatasets, GNNGraphs
julia> mldataset2gnngraph(Cora())
GNNGraph:
num_nodes = 2708
num_edges = 10556
ndata:
features => 1433×2708 Matrix{Float32}
targets => 2708-element Vector{Int64}
train_mask => 2708-element BitVector
val_mask => 2708-element BitVector
test_mask => 2708-element BitVector
num_nodes: 2708
num_edges: 10556
ndata:
val_mask = 2708-element BitVector
targets = 2708-element Vector{Int64}
test_mask = 2708-element BitVector
features = 1433×2708 Matrix{Float32}
train_mask = 2708-element BitVector
```
"""
function mldataset2gnngraph(dataset::D) where {D}
Expand Down
4 changes: 2 additions & 2 deletions GNNGraphs/src/query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Return `true` if there is an edge of type `edge_t` from node `i` to node `j` in
```jldoctest
julia> g = rand_bipartite_heterograph((2, 2), (4, 0), bidirected=false)
GNNHeteroGraph:
num_nodes: (:A => 2, :B => 2)
num_edges: ((:A, :to, :B) => 4, (:B, :to, :A) => 0)
num_nodes: Dict(:A => 2, :B => 2)
num_edges: Dict((:A, :to, :B) => 4, (:B, :to, :A) => 0)
julia> has_edge(g, (:A,:to,:B), 1, 1)
true
Expand Down
8 changes: 5 additions & 3 deletions GNNGraphs/src/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ originally introduced in ["Inductive Representation Learning on Large Graphs"}(h
- `num_layers::Int`: The number of layers for neighborhood expansion (how far to sample neighbors).
- `batch_size::Union{Int, Nothing}`: The size of the batch. If not specified, it defaults to the number of `input_nodes`.
# Usage
```jldoctest
julia> loader = NeighborLoader(graph; num_neighbors=[10, 5], input_nodes=[1, 2, 3], num_layers=2)
# Examples
```julia
julia> loader = NeighborLoader(graph; num_neighbors=[10, 5], input_nodes=[1, 2, 3], num_layers=2)
julia> batch_counter = 0
julia> for mini_batch_gnn in loader
batch_counter += 1
println("Batch ", batch_counter, ": Nodes in mini-batch graph: ", nv(mini_batch_gnn))
Expand Down
Loading

0 comments on commit 403b423

Please sign in to comment.