Skip to content

Commit 726aec6

Browse files
simonschoellysbromberger
authored andcommitted
Some fixes for v1.0 (#66)
* fix for function gplot (#63) * upgraded .travis.yml to v0.7 * added a minimum version for VisualRegressionTests for the tests * using builtin function LightGraphs.LinAlg.eigs instead uf Arpack.eigs * further fixes for problems that are not covered by tests * added min requirement for Compose.jl * added VisualRegresionTests to the requirements * some changes to deal with an error on travis
1 parent bed84d9 commit 726aec6

13 files changed

+120
-172
lines changed

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.6
6+
- 0.7
7+
- 1.0
78
- nightly
89

910
matrix:
@@ -16,3 +17,6 @@ notifications:
1617
#script:
1718
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
1819
# - julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("GraphPlot"); Pkg.test("GraphPlot"; coverage=true)'
20+
after_success:
21+
# push coverage results to Codecov
22+
- julia -e 'cd(using Pkg; Pkg.dir("GraphPlot")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

REQUIRE

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
julia 0.6
1+
julia 0.7
2+
ArnoldiMethod
23
Colors
34
ColorTypes
4-
Compose
5-
LightGraphs
5+
Compose 0.7.0
6+
LightGraphs 1.1.0
7+
VisualRegressionTests 0.2.2 # this shouldn't be necessary here as it is only used by the tests

src/GraphPlot.jl

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
__precompile__(true)
2-
31
module GraphPlot
42

53
using Compose # for plotting features
@@ -12,6 +10,8 @@ export
1210
gplothtml,
1311
random_layout,
1412
circular_layout,
13+
collapse_layout,
14+
community_layout,
1515
spring_layout,
1616
spectral_layout,
1717
shell_layout,
@@ -27,14 +27,11 @@ include("stress.jl")
2727
include("shape.jl")
2828
include("lines.jl")
2929
include("plot.jl")
30+
include("collapse_plot.jl")
3031

3132
# read graph
3233
include("graphio.jl")
3334

34-
function test()
35-
include(joinpath(dirname(@__DIR__), "test", "runtests.jl"))
36-
end
37-
3835

3936
# These functions are mappings to various graph packages.
4037
# Currently only LightGraphs is supported.

src/collapse_plot.jl

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
using Graphs
21
using GraphPlot
32

4-
function collapse_graph{V}(g::AbstractGraph{V}, membership::Vector{Int})
3+
function collapse_graph(g::AbstractGraph, membership::Vector{Int})
54
nb_comm = maximum(membership)
65

7-
collapsed_edge_weights = Array(Dict{Int,Float64}, nb_comm)
6+
collapsed_edge_weights = Vector{Dict{Int,Float64}}(undef, nb_comm)
87
for i=1:nb_comm
98
collapsed_edge_weights[i] = Dict{Int,Float64}()
109
end
1110

12-
for e in Graphs.edges(g)
13-
u = source(e,g)
14-
v = target(e,g)
15-
u_idx = vertex_index(u,g)
16-
v_idx = vertex_index(v,g)
17-
u_comm = membership[u_idx]
18-
v_comm = membership[v_idx]
11+
for e in _edges(g)
12+
u = _src_index(e,g)
13+
v = _dst_index(e,g)
14+
u_comm = membership[u]
15+
v_comm = membership[v]
1916

2017
# for special case of undirected network
21-
if !Graphs.is_directed(g)
18+
if !_is_directed(g)
2219
u_comm, v_comm = minmax(u_comm, v_comm)
2320
end
2421

@@ -29,20 +26,20 @@ function collapse_graph{V}(g::AbstractGraph{V}, membership::Vector{Int})
2926
end
3027
end
3128

32-
collapsed_graph = simple_graph(nb_comm, is_directed=false)
29+
collapsed_graph = SimpleGraph(nb_comm)
3330
collapsed_weights = Float64[]
3431

3532
for u=1:nb_comm
3633
for (v,w) in collapsed_edge_weights[u]
37-
Graphs.add_edge!(collapsed_graph, u, v)
34+
add_edge!(collapsed_graph, u, v)
3835
push!(collapsed_weights, w)
3936
end
4037
end
4138

4239
collapsed_graph, collapsed_weights
4340
end
4441

45-
function community_layout{V}(g::AbstractGraph{V}, membership::Vector{Int})
42+
function community_layout(g::AbstractGraph, membership::Vector{Int})
4643
N = length(membership)
4744
lx = zeros(N)
4845
ly = zeros(N)
@@ -51,13 +48,13 @@ function community_layout{V}(g::AbstractGraph{V}, membership::Vector{Int})
5148
if haskey(comms, lbl)
5249
push!(comms[lbl], idx)
5350
else
54-
comms[lbl] = collect(idx)
51+
comms[lbl] = Int[idx]
5552
end
5653
end
5754
h, w = collapse_graph(g, membership)
5855
clx, cly = spring_layout(h)
5956
for (lbl, nodes) in comms
60-
θ = linspace(0, 2pi, length(nodes) + 1)[1:end-1]
57+
θ = range(0, stop=2pi, length=(length(nodes) + 1))[1:end-1]
6158
for (idx, node) in enumerate(nodes)
6259
lx[node] = 1.8*length(nodes)/N*cos(θ[idx]) + clx[lbl]
6360
ly[node] = 1.8*length(nodes)/N*sin(θ[idx]) + cly[lbl]
@@ -66,11 +63,11 @@ function community_layout{V}(g::AbstractGraph{V}, membership::Vector{Int})
6663
lx, ly
6764
end
6865

69-
function collapse_layout{V}(g::AbstractGraph{V}, membership::Vector{Int})
70-
lightg = LightGraphs.Graph(num_vertices(g))
71-
for e in Graphs.edges(g)
72-
u = vertex_index(source(e,g), g)
73-
v = vertex_index(target(e,g), g)
66+
function collapse_layout(g::AbstractGraph, membership::Vector{Int})
67+
lightg = LightGraphs.SimpleGraph(_nv(g))
68+
for e in _edges(g)
69+
u = _src_index(e, g)
70+
v = _dst_index(e, g)
7471
LightGraphs.add_edge!(lightg, u, v)
7572
end
7673
N = length(membership)
@@ -81,15 +78,15 @@ function collapse_layout{V}(g::AbstractGraph{V}, membership::Vector{Int})
8178
if haskey(comms, lbl)
8279
push!(comms[lbl], idx)
8380
else
84-
comms[lbl] = collect(idx)
81+
comms[lbl] = Int[idx]
8582
end
8683
end
8784
h, w = collapse_graph(g, membership)
8885
clx, cly = spring_layout(h)
8986
for (lbl, nodes) in comms
9087
subg = lightg[nodes]
9188
sublx, subly = spring_layout(subg)
92-
θ = linspace(0, 2pi, length(nodes) + 1)[1:end-1]
89+
θ = range(0, stop=2pi, length=(length(nodes) + 1))[1:end-1]
9390
for (idx, node) in enumerate(nodes)
9491
lx[node] = 1.8*length(nodes)/N*sublx[idx] + clx[lbl]
9592
ly[node] = 1.8*length(nodes)/N*subly[idx] + cly[lbl]

src/graphio.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using DelimitedFiles: readdlm
2+
13
"""
24
read some famous graphs
35
@@ -21,9 +23,9 @@ end
2123
"""read graph from in edgelist format"""
2224
function readedgelist(filename; is_directed::Bool=false, start_index::Int=0, delim::Char=' ')
2325
es = readdlm(filename, delim, Int)
24-
es = unique(es, 1)
26+
es = unique(es, dims=1)
2527
if start_index == 0
26-
es = es + 1
28+
es = es .+ 1
2729
end
2830
N = maximum(es)
2931
if is_directed
@@ -38,7 +40,7 @@ function readedgelist(filename; is_directed::Bool=false, start_index::Int=0, del
3840
es[i,1], es[i,2] = es[i,2], es[i,1]
3941
end
4042
end
41-
es = unique(es, 1)
43+
es = unique(es, dims=1)
4244
g = Graph(N)
4345
for i=1:size(es,1)
4446
add_edge!(g, es[i,1], es[i,2])

src/layout.jl

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using SparseArrays: SparseMatrixCSC, sparse
2+
using ArnoldiMethod: LR
3+
using LinearAlgebra: eigen
4+
15
"""
26
Position nodes uniformly at random in the unit square.
37
For every node, a position is generated by choosing each of dim
@@ -56,7 +60,7 @@ function circular_layout(G)
5660
return [0.0], [0.0]
5761
else
5862
# Discard the extra angle since it matches 0 radians.
59-
θ = linspace(0, 2pi, _nv(G) + 1)[1:end-1]
63+
θ = range(0, stop=2pi, length=_nv(G)+1)[1:end-1]
6064
return cos.(θ), sin.(θ)
6165
end
6266
end
@@ -96,11 +100,11 @@ julia> locs_x, locs_y = spring_layout(g)
96100
function spring_layout(G, locs_x=2*rand(_nv(G)).-1.0, locs_y=2*rand(_nv(G)).-1.0; C=2.0, MAXITER=100, INITTEMP=2.0)
97101

98102
#size(adj_matrix, 1) != size(adj_matrix, 2) && error("Adj. matrix must be square.")
99-
const N = _nv(G)
103+
N = _nv(G)
100104
adj_matrix = _adjacency_matrix(G)
101105

102106
# The optimal distance bewteen vertices
103-
const K = C * sqrt(4.0 / N)
107+
K = C * sqrt(4.0 / N)
104108

105109
# Store forces and apply at end of iteration all at once
106110
force_x = zeros(N)
@@ -183,7 +187,7 @@ julia> nlist[2] = [6:num_vertiecs(g)]
183187
julia> locs_x, locs_y = shell_layout(g, nlist)
184188
```
185189
"""
186-
function shell_layout(G, nlist::Union{Void, Vector{Vector{Int}}} = nothing)
190+
function shell_layout(G, nlist::Union{Nothing, Vector{Vector{Int}}} = nothing)
187191
if _nv(G) == 1
188192
return [0.0], [0.0]
189193
end
@@ -199,7 +203,7 @@ function shell_layout(G, nlist::Union{Void, Vector{Vector{Int}}} = nothing)
199203
locs_y = Float64[]
200204
for nodes in nlist
201205
# Discard the extra angle since it matches 0 radians.
202-
θ = linspace(0, 2pi, length(nodes) + 1)[1:end-1]
206+
θ = range(0, stop=2pi, length=length(nodes)+1)[1:end-1]
203207
append!(locs_x, radius*cos.(θ))
204208
append!(locs_y, radius*sin.(θ))
205209
radius += 1.0
@@ -229,37 +233,41 @@ julia> weight = rand(num_edges(g))
229233
julia> locs_x, locs_y = spectral_layout(g, weight)
230234
```
231235
"""
232-
function spectral_layout(G)
236+
function spectral_layout(G, weight=nothing)
233237
if _nv(G) == 1
234238
return [0.0], [0.0]
239+
elseif _nv(G) == 2
240+
return [0.0, 1.0], [0.0, 0.0]
235241
end
236242

243+
if weight == nothing
244+
weight = ones(length(_edges(G)))
245+
end
237246
if _nv(G) > 500
238-
A = sparse(Int[_src_index(e) for e in _edges(G)],
239-
Int[_dst_index(e) for e in _edges(G)],
247+
A = sparse(Int[_src_index(e, G) for e in _edges(G)],
248+
Int[_dst_index(e, G) for e in _edges(G)],
240249
weight, _nv(G), _nv(G))
241250
if _is_directed(G)
242251
A = A + A'
243252
end
244253
return _spectral(A)
245254
else
246255
L = _laplacian_matrix(G)
247-
return _spectral(full(L))
256+
return _spectral(Matrix(L))
248257
end
249258
end
250259

251260
function _spectral(L::Matrix)
252-
eigenvalues, eigenvectors = eig(L)
261+
eigenvalues, eigenvectors = eigen(L)
253262
index = sortperm(eigenvalues)[2:3]
254263
eigenvectors[:, index[1]], eigenvectors[:, index[2]]
255264
end
256265

257266
function _spectral(A::SparseMatrixCSC)
258-
data = collect(sum(A, 1))
259-
D = sparse([1:length(data)], [1:length(data)], data)
267+
data = vec(sum(A, dims=1))
268+
D = sparse(Base.OneTo(length(data)), Base.OneTo(length(data)), data)
260269
L = D - A
261-
ncv = max(7, int(sqrt(length(data))))
262-
eigenvalues, eigenvectors = eigs(L, nev=3, ncv=ncv)
270+
eigenvalues, eigenvectors = LightGraphs.LinAlg.eigs(L, nev=3, which=LR())
263271
index = sortperm(real(eigenvalues))[2:3]
264272
real(eigenvectors[:, index[1]]), real(eigenvectors[:, index[2]])
265273
end

0 commit comments

Comments
 (0)