Skip to content

Commit 333d74a

Browse files
Add Temporal random radius graph generator (#305)
* Add `temporal_rand_radius_graph` function * New version * Add test for `temporal_rand_radius_graph` * Update on a square with border * Add docstring * Export `temporal_rand_radius_graph` * Add more spaces Co-authored-by: Carlo Lucibello <[email protected]> * Improved docstring Co-authored-by: Carlo Lucibello <[email protected]> * Rename * Add ref --------- Co-authored-by: Carlo Lucibello <[email protected]>
1 parent 7a31d3d commit 333d74a

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/GNNGraphs/GNNGraphs.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export rand_graph,
8989
rand_heterograph,
9090
rand_bipartite_heterograph,
9191
knn_graph,
92-
radius_graph
92+
radius_graph,
93+
rand_temporal_radius_graph
9394

9495
include("sampling.jl")
9596
export sample_neighbors

src/GNNGraphs/generate.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ GNNGraph:
270270
num_graphs = 2
271271
272272
```
273+
# References
274+
Section B paragraphs 1 and 2 of the paper [Dynamic Hidden-Variable Network Models](https://arxiv.org/pdf/2101.00414.pdf)
273275
"""
274276
function radius_graph(points::AbstractMatrix, r::AbstractFloat;
275277
graph_indicator = nothing,
@@ -298,3 +300,65 @@ function radius_graph(points::AbstractMatrix, r::AbstractFloat;
298300
end
299301
return g
300302
end
303+
304+
"""
305+
rand_temporal_radius_graph(number_nodes::Int,
306+
number_snapshots::Int,
307+
speed::AbstractFloat,
308+
r::AbstractFloat;
309+
self_loops = false,
310+
dir = :in,
311+
kws...)
312+
313+
Create a random temporal graph given `number_nodes` nodes and `number_snapshots` snapshots.
314+
First, the positions of the nodes are randomly generated in the unit square. Two nodes are connected if their distance is less than a given radius `r`.
315+
Each following snapshot is obtained by applying the same construction to new positions obtained as follows.
316+
For each snapshot, the new positions of the points are determined by applying random independent displacement vectors to the previous positions. The direction of the displacement is chosen uniformly at random and its length is chosen uniformly in `[0, speed]`. Then the connections are recomputed.
317+
If a point happens to move outside the boundary, its position is updated as if it had bounced off the boundary.
318+
319+
# Arguments
320+
321+
- `number_nodes`: The number of nodes of each snapshot.
322+
- `number_snapshots`: The number of snapshots.
323+
- `speed`: The speed to update the nodes.
324+
- `r`: The radius of connection.
325+
- `self_loops`: If `true`, consider the node itself among its neighbors, in which
326+
case the graph will contain self-loops.
327+
- `dir`: The direction of the edges. If `dir=:in` edges go from the
328+
neighbors to the central node. If `dir=:out` we have the opposite
329+
direction.
330+
- `kws`: Further keyword arguments will be passed to the [`GNNGraph`](@ref) constructor of each snapshot.
331+
332+
# Example
333+
334+
```julia-repl
335+
julia> n, snaps, s, r = 10, 5, 0.1, 1.5;
336+
337+
julia> tg = rand_temporal_radius_graph(n,snaps,s,r) # complete graph at each snapshot
338+
TemporalSnapshotsGNNGraph:
339+
num_nodes: [10, 10, 10, 10, 10]
340+
num_edges: [90, 90, 90, 90, 90]
341+
num_snapshots: 5
342+
```
343+
344+
"""
345+
function rand_temporal_radius_graph(number_nodes::Int,
346+
number_snapshots::Int,
347+
speed::AbstractFloat,
348+
r::AbstractFloat;
349+
self_loops = false,
350+
dir = :in,
351+
kws...)
352+
points=rand(2, number_nodes)
353+
tg = Vector{GNNGraph}(undef, number_snapshots)
354+
for t in 1:number_snapshots
355+
tg[t] = radius_graph(points, r; graph_indicator = nothing, self_loops, dir, kws...)
356+
for i in 1:number_nodes
357+
ρ = 2 * speed * rand() - speed
358+
theta=2*pi*rand()
359+
points[1,i]=1-abs(1-(abs(points[1,i]+ρ*cos(theta))))
360+
points[2,i]=1-abs(1-(abs(points[2,i]+ρ*sin(theta))))
361+
end
362+
end
363+
return TemporalSnapshotsGNNGraph(tg)
364+
end

test/GNNGraphs/generate.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ end
9292
@test has_edge(g, (:A,:to,:B), 1, 1)
9393
@test !has_edge(g, (:B,:to,:A), 1, 1)
9494
end
95+
96+
@testset "rand_temporal_radius_graph" begin
97+
number_nodes = 30
98+
number_snapshots = 5
99+
r = 0.1
100+
speed = 0.1
101+
tg = rand_temporal_radius_graph(number_nodes, number_snapshots, speed, r)
102+
@test tg.num_nodes == [number_nodes for i in 1:number_snapshots]
103+
@test tg.num_snapshots == number_snapshots
104+
r2 = 0.95
105+
tg2 = rand_temporal_radius_graph(number_nodes, number_snapshots, speed, r2)
106+
@test mean(mean(degree.(tg.snapshots)))<=mean(mean(degree.(tg2.snapshots)))
107+
end

0 commit comments

Comments
 (0)