-
Notifications
You must be signed in to change notification settings - Fork 62
Kamada kawai #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Kamada kawai #89
Changes from 2 commits
b823013
e6da683
5054320
b4f0c13
f890311
695feb2
25e156a
a2170ae
394a3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ Colors | |
ColorTypes | ||
Compose 0.7.0 | ||
LightGraphs 1.1.0 | ||
Optim | ||
Distances |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import Optim | ||
using Distances | ||
function kamada_kawai_layout(G, X=nothing; C= 1.0, MAXITER=100 ) | ||
Offset = 0.0 | ||
if X===nothing | ||
locs_x = zeros(nv(G)) | ||
locs_y = zeros(nv(G)) | ||
else | ||
locs_x = X[1] | ||
locs_y = X[2] | ||
end | ||
|
||
function Objective(M,K) | ||
D = pairwise(Euclidean(),M, dims=2) | ||
D-= K | ||
R = sum(D.^2)/2 | ||
return R | ||
end | ||
|
||
function dObjective!(dR,M,K) | ||
dR .= zeros(size(M)) | ||
Vs = size(M,2) | ||
D = pairwise(Euclidean(),M, dims=2) | ||
D += I # Prevent division by zero | ||
D .= K./D # Use negative for simplicity, since diag K = 0 everything is fine. | ||
D .-= 1.0 # (K-(D+I))./(D+I) = K./(D+I) .- 1.0 | ||
D += I # Remove the false diagonal | ||
for v1 in 1:Vs | ||
dR[:,v1] .= -M[:,v1]*sum(D[:,v1]) | ||
end | ||
dR .+= M*D | ||
dR .*=2 | ||
return dR | ||
end | ||
|
||
function scaler(z, a, b) | ||
2.0*((z - a)/(b - a)) - 1.0 | ||
end | ||
# Stack individual graphs next to each other | ||
for SubGraphVertices in connected_components(G) | ||
|
||
SubGraph = induced_subgraph(G,SubGraphVertices)[1] | ||
N = nv(SubGraph) | ||
if X !== nothing | ||
_locs_x = locs_x[SubGraphVertices] | ||
_locs_y = locs_y[SubGraphVertices] | ||
else | ||
Vertices=collect(vertices(SubGraph)) | ||
Vmax=findmax([degree(SubGraph,x) for x in vertices(SubGraph)])[2] | ||
filter!(x->x!=Vmax, Vertices) | ||
|
||
Shells=[[Vmax]] | ||
VComplement = copy(Shells[1]) | ||
while length(Vertices)>0 | ||
|
||
Interim = filter(x->!(x ∈ VComplement),vcat([collect(neighbors(SubGraph,s)) for s in Shells[end]]...)) | ||
unique!(Interim) | ||
push!(Shells,Interim) | ||
filter!(x->!(x ∈ Shells[end]),Vertices) | ||
|
||
append!(VComplement,Shells[end]) | ||
end | ||
_locs_x, _locs_y = shell_layout(SubGraph,Shells) | ||
end | ||
|
||
# The optimal distance between vertices | ||
# Currently only LightGraphs are supported using the Dijkstra shortest path algorithm | ||
K = zeros(N,N) | ||
for v in 1:N | ||
K[:,v] = dijkstra_shortest_paths(SubGraph,v).dists | ||
|
||
end | ||
|
||
M0 = vcat(_locs_x',_locs_y') | ||
OptResult = Optim.optimize(x->Objective(x,K),(x,y) -> dObjective!(x,y,K), M0, method=Optim.LBFGS(),iterations = MAXITER ) | ||
M0 = Optim.minimizer(OptResult) | ||
min_x, max_x = minimum(M0[1,:]), maximum(M0[1,:]) | ||
min_y, max_y = minimum(M0[2,:]), maximum(M0[2,:]) | ||
|
||
map!(z -> scaler(z, min_x, max_x), M0[1,:], M0[1,:]) | ||
map!(z -> scaler(z, min_y, max_y), M0[2,:], M0[2,:]) | ||
locs_x[SubGraphVertices] .= M0[1,:] .+ Offset | ||
locs_y[SubGraphVertices] .= M0[2,:] | ||
Offset += maximum(M0[1,:])+C | ||
|
||
end | ||
# Scale to unit square | ||
min_x, max_x = minimum(locs_x), maximum(locs_x) | ||
min_y, max_y = minimum(locs_y), maximum(locs_y) | ||
map!(z -> scaler(z, min_x, max_x), locs_x, locs_x) | ||
map!(z -> scaler(z, min_y, max_y), locs_y, locs_y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably also some function that we can generalize eventually (also does not have to be in this PR) |
||
|
||
return locs_x,locs_y | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,7 +192,7 @@ Vector of Vector, Vector of node Vector for each shell. | |
julia> g = graphfamous("karate") | ||
|
||
julia> nlist = Array{Vector{Int}}(2) | ||
julia> nlist[1] = [1:5] | ||
julia> nlist[2] = [6:num_vertiecs(g)] | ||
julia> nlist[2] = [6:num_vertices(g)] | ||
julia> locs_x, locs_y = shell_layout(g, nlist) | ||
``` | ||
""" | ||
|
@@ -280,3 +280,5 @@ function _spectral(A::SparseMatrixCSC) | |
index = sortperm(real(eigenvalues))[2:3] | ||
return real(eigenvectors[:, index[1]]), real(eigenvectors[:, index[2]]) | ||
end | ||
|
||
include("KamadaKawai.jl") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need some kind of docstring here, explaining what happens.
Also we should use lower capitel letters for variable names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a problem. I've never done a non-trivial pull-request, so bear with me as I work my way through the process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that
stressmajorize_layout
using upper case variable names. I personally prefer CamelCase for Variables and camelCase for functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably mean
MAXITER
more so thanX
orG
. Done.