Skip to content

Commit 301e7e9

Browse files
committed
Shell: new layout + slightly more general
1 parent 8d413b3 commit 301e7e9

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

src/NetworkLayout.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ using LinearAlgebra: norm
77

88
abstract type AbstractLayout{Dim,Ptype} end
99

10-
dim(::AbstractLayout{Dim, Ptype}) where {Dim,Ptype} = Dim
11-
ptype(::AbstractLayout{Dim, Ptype}) where {Dim,Ptype} = Ptype
10+
dim(::AbstractLayout{Dim,Ptype}) where {Dim,Ptype} = Dim
11+
ptype(::AbstractLayout{Dim,Ptype}) where {Dim,Ptype} = Ptype
1212

1313
(lay::AbstractLayout)(adj_matrix) = layout(lay, adj_matrix)
1414

@@ -37,6 +37,6 @@ include("spring.jl")
3737
include("stress.jl")
3838
include("spectral.jl")
3939
include("circular.jl")
40-
# include("shell.jl")
40+
include("shell.jl")
4141

4242
end

src/shell.jl

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,41 @@ julia> nlist[2] = [6:num_vertiecs(g)]
1515
julia> locs_x, locs_y = shell_layout(g, nlist)
1616
```
1717
"""
18-
module Shell
19-
20-
using GeometryBasics
21-
22-
function layout(adj_matrix::AbstractMatrix; nlist::Union{Nothing,Vector{Vector{Int}}}=nothing)
23-
return layout!(adj_matrix, nlist)
18+
struct Shell{Ptype} <: AbstractLayout{2,Ptype}
19+
nlist::Vector{Vector{Int}}
2420
end
2521

26-
function layout!(adj_matrix::AbstractMatrix, nlist::Union{Nothing,Vector{Vector{Int}}})
22+
Shell(; Ptype=Float64, nlist=Int[]) = Shell{Ptype}(nlist)
23+
24+
function layout(algo::Shell{Ptype}, adj_matrix) where {Ptype}
2725
if size(adj_matrix, 1) == 1
2826
return Point{2,Float64}[Point(0.0, 0.0)]
2927
end
30-
if nlist == nothing
31-
nlist = Array{Vector{Int}}(undef, 1)
32-
nlist[1] = collect(1:size(adj_matrix, 1))
28+
29+
N = size(adj_matrix, 1)
30+
nlist = copy(algo.nlist)
31+
32+
# if the list does not contain all the nodes push missing nodes to new shell
33+
listed_nodes = Iterators.flatten(nlist)
34+
@assert allunique(listed_nodes)
35+
diff = setdiff(1:N, listed_nodes)
36+
if !isempty(diff)
37+
push!(nlist, diff)
3338
end
39+
3440
radius = 0.0
3541
if length(nlist[1]) > 1
3642
radius = 1.0
3743
end
38-
T = Point{2,Float64}
39-
locs = T[]
44+
45+
T = Point{2,Ptype}
46+
locs = Vector{T}(undef, N)
4047
for nodes in nlist
4148
# Discard the extra angle since it matches 0 radians.
42-
θ = range(0, stop=2pi, length=length(nodes) + 1)[1:(end - 1)]
49+
θ = range(0; stop=2pi, length=length(nodes) + 1)[1:(end - 1)]
4350
x = T[(radius * cos(o), radius * sin(o)) for o in θ]
44-
append!(locs, x)
51+
locs[nodes] = x
4552
radius += 1.0
4653
end
4754
return locs
4855
end
49-
50-
end # end of module

test/runtests.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using NetworkLayout.Spectral
2-
using NetworkLayout.Shell
3-
using NetworkLayout.Circular
4-
51
using NetworkLayout
62
using LightGraphs
73
using GeometryBasics
@@ -163,7 +159,7 @@ jagmesh_adj = jagmesh()
163159
adj_matrix = adjacency_matrix(g)
164160
positions = @time Spectral()(adj_matrix)
165161
@test typeof(positions) == Vector{Point{3,Float64}}
166-
positions = @time Spectral(;Ptype=Float32)(adj_matrix)
162+
positions = @time Spectral(; Ptype=Float32)(adj_matrix)
167163
@test typeof(positions) == Vector{Point{3,Float32}}
168164
end
169165
end
@@ -176,7 +172,7 @@ jagmesh_adj = jagmesh()
176172
adj_matrix = adjacency_matrix(g)
177173
positions = @time Circular()(adj_matrix)
178174
@test typeof(positions) == Vector{Point{2,Float64}}
179-
positions = @time Circular(Ptype=Float32)(adj_matrix)
175+
positions = @time Circular(; Ptype=Float32)(adj_matrix)
180176
@test typeof(positions) == Vector{Point{2,Float32}}
181177
end
182178
@testset "Testing Base Case" begin
@@ -188,18 +184,19 @@ jagmesh_adj = jagmesh()
188184
end
189185

190186
@testset "Testing Shell Layout Algorithm" begin
187+
using NetworkLayout: Shell
191188
println("Shell wheel_graph")
192189

193190
@testset "Testing wheel_graph" begin
194191
g = wheel_graph(10)
195192
adj_matrix = adjacency_matrix(g)
196-
positions = @time Shell.layout(adj_matrix)
193+
positions = @time Shell()(adj_matrix)
197194
@test typeof(positions) == Vector{Point{2,Float64}}
198195
end
199196
@testset "Testing Base Case" begin
200197
g = Graph(1)
201198
adj_matrix = adjacency_matrix(g)
202-
positions = @time Shell.layout(adj_matrix)
199+
positions = @time Shell()(adj_matrix)
203200
@test typeof(positions) == Vector{Point{2,Float64}}
204201
end
205202
end

0 commit comments

Comments
 (0)