Skip to content

Commit 26ce7d3

Browse files
committed
fix Spectral and add 2d option
1 parent 57586a1 commit 26ce7d3

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

docs/src/index.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,21 @@ f, ax, p = graphplot(g, layout=layout, nlabels=repr.(1:nv(g)), nlabels_textsize=
150150
ylims!(-4.5,.5); hidedecorations!(ax); hidespines!(ax); ax.aspect = DataAspect(); f #hide
151151
```
152152

153-
## Spectral
153+
## Spectral Layout
154154
```@docs
155155
Spectral
156156
```
157157
```@example layouts
158+
g = watts_strogatz(1000, 5, 0.03; seed=5)
159+
layout = Spectral(dim=2)
160+
f, ax, p = graphplot(g, layout=layout, node_size=0.0, edge_width=1.0)
161+
hidedecorations!(ax); hidespines!(ax); f #hide
162+
f #hide
163+
```
164+
```@example layouts
158165
set_theme!(resolution=(800, 800)) #hide
159166
using Random; Random.seed!(5) # hide
160-
g = watts_strogatz(30, 5, 1)
161167
layout = Spectral()
162-
f, ax, p = graphplot(g, layout=layout, node_size=5, edge_width=1)
168+
f, ax, p = graphplot(g, layout=layout, node_size=0.0, edge_width=1.0)
163169
f #hide
164170
```

src/spectral.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@ export Spectral, spectral
77
spectral(adj_matrix; kwargs...)
88
99
This algorithm uses the technique of Spectral Graph Drawing, which is an
10-
under-appreciated method of graph layouts; easier, simpler, and faster
11-
than the more common spring-based methods. For reference see
12-
13-
- <http://www.research.att.com/export/sites/att_labs/groups/infovis/res/legacy_papers/DBLP-journals-camwa-Koren05.pdf>
14-
- <http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.3.2055&rep=rep1&type=pdf>
10+
under-appreciated method of graph layouts; easier, simpler, and faster than the
11+
more common spring-based methods. For reference see [Yehuda Koren,
12+
2002](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.3.2055&rep=rep1&type=pdf).
1513
1614
Takes adjacency matrix representation of a network and returns coordinates of
1715
the nodes.
1816
1917
## Keyword Arguments
20-
- `Ptype=Float64`: Determines the output type `Point{3,Ptype}`.
21-
- `nodeweights=Float64[]`
22-
23-
Vector of weights. If network size does not match the length of `nodesize` use
24-
`ones` instead.
18+
- `dim=3`, `Ptype=Float64`: Determines dimension and output type `Point{dim,Ptype}`.
19+
- `nodeweights=Float64[]`: Vector of weights. If network size does not match the
20+
length of `nodesize` use `ones` instead.
2521
"""
26-
@addcall struct Spectral{Ptype,FT<:AbstractFloat} <: AbstractLayout{3,Ptype}
22+
@addcall struct Spectral{Dim,Ptype,FT<:AbstractFloat} <: AbstractLayout{Dim,Ptype}
2723
nodeweights::Vector{FT}
2824
end
2925

30-
Spectral(; Ptype=Float64, nodeweights=Float64[]) = Spectral{Ptype,eltype(nodeweights)}(nodeweights)
26+
Spectral(; dim=3, Ptype=Float64, nodeweights=Float64[]) = Spectral{dim,Ptype,eltype(nodeweights)}(nodeweights)
3127

3228
function make_symmetric(adj_matrix::AbstractMatrix)
3329
adj_matrix = copy(adj_matrix)
@@ -55,7 +51,7 @@ function compute_laplacian(adj_matrix, node_weights)
5551
return L, D
5652
end
5753

58-
function layout(algo::Spectral{Ptype,FT}, adj_matrix::AbstractMatrix) where {Ptype,FT}
54+
function layout(algo::Spectral{Dim,Ptype,FT}, adj_matrix::AbstractMatrix) where {Dim,Ptype,FT}
5955
N = assertsquare(adj_matrix)
6056
# try to use user provided nodeweights
6157
nodeweights = if length(algo.nodeweights) == N
@@ -70,5 +66,5 @@ function layout(algo::Spectral{Ptype,FT}, adj_matrix::AbstractMatrix) where {Pty
7066
v = eigen(L, D).vectors
7167
# x, y, and z are the 2nd through 4th eigenvectors of the solution to the
7268
# generalized eigenvalue problem Lv = λDv
73-
return [Point{3,Ptype}(v[2, i], v[3, i], v[4, i]) for i in 1:size(v, 2)]
69+
return map(x -> Point{Dim,Ptype}(x[2:(Dim + 1)]...), eachrow(v))
7470
end

0 commit comments

Comments
 (0)