-
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
Open
BCRARL
wants to merge
9
commits into
JuliaGraphs:master
Choose a base branch
from
BCRARL:KamadaKawai
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kamada kawai #89
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b823013
Kamada Kawai visualization layout using Optim and Distances
crinders e6da683
Forgot to add REQUIRE and tomls
crinders 5054320
Updated kamada_kawai_layout
crinders b4f0c13
Removed unnecessary dyjkstra component.
crinders f890311
Merge branch 'master' into KamadaKawai
simonschoelly 695feb2
Update Project.toml
simonschoelly 25e156a
Merge pull request #1 from JuliaGraphs/master
BCRARL a2170ae
Merge branch 'KamadaKawai' into master
BCRARL 394a3cb
Merge pull request #2 from BCRARL/master
BCRARL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import Optim | ||
using Distances | ||
@doc raw""" | ||
Computes the layout corresponding to | ||
|
||
```math | ||
\min \sum_{i,j} (K_{ij} - \|x_i - x_j\|)^\frac{1}{2}, | ||
``` | ||
where $K_{ij}$ is the shortest path distance between vertices `i` abd `j`, and $x_i$ and $x_j$ are the positions of vertices `i` and `j` in the layout, respectively. | ||
|
||
Inputs: | ||
|
||
- G: Graph to layout | ||
- X: starting positions. If no starting positions are provided a shell_layout is constructed. | ||
|
||
Optional keyword arguments: | ||
|
||
- maxiter: maximum number of iterations Optim.optimize will perform in an attempt to optimize the layout. | ||
- distmx: Distance matrix for the Floyd-Warshall algorithm to discern shortest path distances on the graph. By default the edge weights of the graph. | ||
|
||
Outputs: | ||
|
||
- (`locs_x`, `locs_y`) positions in the x and y directions, respectively. | ||
|
||
""" | ||
function kamada_kawai_layout(G, X=nothing; maxiter=100, distmx=weights(G) ) | ||
if !is_connected(G) | ||
@warn "This graph is disconnected. Results may not be reasonable." | ||
end | ||
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 | ||
N = nv(G) | ||
Vertices=collect(vertices(G)) | ||
|
||
if X !== nothing | ||
_locs_x = locs_x | ||
_locs_y = locs_y | ||
else | ||
Vmax=findmax([degree(G,x) for x in vertices(G)])[2] | ||
filter!(!isequal(Vmax), Vertices) | ||
Shells=[[Vmax]] | ||
VComplement = copy(Shells[1]) | ||
while !isempty(Vertices) | ||
Interim = filter(!∈(VComplement),vcat([collect(neighbors(G,s)) for s in Shells[end]]...)) | ||
unique!(Interim) | ||
push!(Shells,Interim) | ||
filter!(!∈(Shells[end]),Vertices) | ||
append!(VComplement,Shells[end]) | ||
end | ||
_locs_x, _locs_y = shell_layout(G,Shells) | ||
end | ||
|
||
# The optimal distance between vertices | ||
# Currently only LightGraphs are supported using the Dijkstra shortest path algorithm | ||
|
||
K = floyd_warshall_shortest_paths(G,distmx).dists | ||
|
||
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), (min_y, max_y) = extrema(M0,dims=2) | ||
locs_x .= M0[1,:] | ||
locs_y .= M0[2,:] | ||
|
||
# Scale to unit square | ||
map!(z -> scaler(z, min_x, max_x), locs_x, locs_x) | ||
map!(z -> scaler(z, min_y, max_y), locs_y, locs_y) | ||
|
||
return locs_x,locs_y | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is probably also some function that we can generalize eventually (also does not have to be in this PR)