Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Compose = "0.7"
LightGraphs = "1.1"
VisualRegressionTests = "0.2"

[extras]
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Expand All @@ -22,8 +29,3 @@ VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

[targets]
test = ["Test", "Cairo", "ImageMagick", "VisualRegressionTests"]

[compat]
"Compose" = "0.7"
"LightGraphs" = "1.1"
"VisualRegressionTests" = "0.2"
2 changes: 2 additions & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Colors
ColorTypes
Compose 0.7.0
LightGraphs 1.1.0
Optim
Distances
87 changes: 87 additions & 0 deletions src/KamadaKawai.jl
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 )
Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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.

Copy link
Author

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 than X or G. Done.

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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could use filter!( !isequal(Vmax), Vertices) here or ``filter!( !(==(Vmax)), Vertices)`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem.

Shells=[[Vmax]]
VComplement = copy(Shells[1])
while length(Vertices)>0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not while !isempty(Vertices)

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)
Copy link
Member

@simonschoelly simonschoelly Jun 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use filter!( !∈(Shells[end]),Vertices) here, or filter!( !in(Shells[end]),Vertices). Unfortunately filter!( ∉(Shells[end]),Vertices) does not work.

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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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 floyd_warshall_shortest_paths exists. I'll check whether this works as well.

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,:])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can us eextrema(M0, dims=2) here. Or minimum(M0, dims=2) and maximum(m0, dims=2).

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the view macro here:

Offset += maximum(@view M0[1,:]) + C

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 @view macro in front, you only get a view of the array instead. This also causes some allocations , where the garbage collector is involved atm, which is actually a bit of a problem when you use it in a loop, but it is still much faster.

Copy link
Author

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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
4 changes: 3 additions & 1 deletion src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Vector of Vector, Vector of node Vector for each shell.
julia> g = graphfamous("karate")
Copy link
Member

Choose a reason for hiding this comment

The 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 smallgraphs(:karate). Unfortunately this does not work yet, because we have to wait until a new version of LightGraphs is getting tagged.

Copy link
Member

Choose a reason for hiding this comment

The 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)
```
"""
Expand Down Expand Up @@ -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")