Skip to content

Commit d6b2d14

Browse files
Merge pull request #78 from stevengj/patch-1
make default spring_layout deterministic
2 parents db1c993 + 300235a commit d6b2d14

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/layout.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ Number of iterations we apply the forces
9191
*INITTEMP*
9292
Initial "temperature", controls movement per iteration
9393
94+
*seed*
95+
Integer seed for pseudorandom generation of locations (default = 0).
96+
9497
**Examples**
9598
```
9699
julia> g = graphfamous("karate")
97100
julia> locs_x, locs_y = spring_layout(g)
98101
```
99102
"""
100-
function spring_layout(g::AbstractGraph{T}, locs_x=2*rand(nv(g)).-1.0, locs_y=2*rand(nv(g)).-1.0; C=2.0, MAXITER=100, INITTEMP=2.0) where {T<:Integer}
103+
function spring_layout(g::AbstractGraph{T}, locs_x, locs_y; C=2.0, MAXITER=100, INITTEMP=2.0) where {T<:Integer}
101104

102105
#size(adj_matrix, 1) != size(adj_matrix, 2) && error("Adj. matrix must be square.")
103106
N = nv(g)
@@ -165,6 +168,12 @@ function spring_layout(g::AbstractGraph{T}, locs_x=2*rand(nv(g)).-1.0, locs_y=2*
165168
return locs_x,locs_y
166169
end
167170

171+
using Random: MersenneTwister
172+
function spring_layout(g::AbstractGraph; seed::Integer=0, kws...)
173+
rng = MersenneTwister(seed)
174+
spring_layout(g, 2 .* rand(rng, nv(g)) .- 1.0, 2 .* rand(rng,nv(g)) .- 1.0; kws...)
175+
end
176+
168177
"""
169178
This function is copy from [IainNZ](https://github.com/IainNZ)'s [GraphLayout.jl](https://github.com/IainNZ/GraphLayout.jl)
170179

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ karate_edges = Edge.([
3535
g = SimpleGraph(karate_edges)
3636
h = LightGraphs.WheelGraph(10)
3737

38+
test_layout(g::AbstractGraph; kws...) = spring_layout(g; seed=2017, kws...)
39+
3840
# plot and save function for visual regression tests
3941
function plot_and_save(fname, g; gplot_kwargs...)
40-
Random.seed!(2017)
41-
draw(PNG(fname, 8inch, 8inch), gplot(g; gplot_kwargs...))
42+
draw(PNG(fname, 8inch, 8inch), gplot(g; layout=test_layout, gplot_kwargs...))
4243
end
4344

4445
@testset "Karate Net" begin

0 commit comments

Comments
 (0)