-
Notifications
You must be signed in to change notification settings - Fork 62
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
Kamada kawai #89
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) | ||
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. Good call on that, I think that is something that we might have to tackle for graph layouts that we have here. Should probably not happen in this PR, but I think we could have some shared function that deals with multiple components. 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. I can remove and just assume that the graph is connected. Then a composition of graph layouts could be used to discern what happens with connected components. 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. Maybe there should be a warning, if the graph isn't connected? |
||
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) | ||
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. Maybe you could use 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. No problem. |
||
Shells=[[Vmax]] | ||
VComplement = copy(Shells[1]) | ||
while length(Vertices)>0 | ||
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. Why not |
||
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) | ||
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. You can use |
||
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 | ||
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. I wonder, if we should have the option to input a custom weight matrix. 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. Does this also work for directed graphs? (In general, I think for a lot of graph layouts, directed graphs should be seen as as undirected graphs though). Also, you seem to do that for every vertex? Maybe consider using an algorithm for calculating all shortest paths at the same time, LightGraphs should contain an implementation of floyd-warshall, that could solve that. 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. I looked for a call that would do that, but didn't find it. This was expensive, but it works. I searched explicitly for Floyd-Warshall and it is undocumented, but |
||
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,:]) | ||
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. Maybe you can us e Otherwise, you should at least use the `@views macro, so that Julia does not allocate space for the subarray. |
||
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 | ||
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. Use the view macro here:
The problem is, that Julia by default does allocate a new Array, when you use a slice (There might be some exceptions to this rule, when using broadcasting, but I am not that familiar yet with that topic). When you put the 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. It shouldn't allocate anything here, but since I'm not concerned with the offset in this particular function anymore, I'll just try to remember for future reference ;-) |
||
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") | ||
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. We should stop using graphfamous eventually, because it is taken out of this package. The karate graph can then be generated using 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. Ah, just realised that was not put in by your pr, so forget about that. |
||
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.