Skip to content

Commit 27d9763

Browse files
Use GenericGraph for testing community algorithms (#273)
1 parent 57cd3ac commit 27d9763

File tree

9 files changed

+32
-37
lines changed

9 files changed

+32
-37
lines changed

src/community/clustering.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
function local_clustering!(storage::AbstractVector{Bool}, g::AbstractGraph, v::Integer)
3939
k = degree(g, v)
4040
k <= 1 && return (0, 0)
41-
neighs = neighbors(g, v)
41+
neighs = collect_if_not_vector(neighbors(g, v))
4242
tcount = 0
4343
storage[neighs] .= true
4444

@@ -63,7 +63,7 @@ function local_clustering!(
6363
i = 0
6464
for (i, v) in enumerate(vs)
6565
ntriang[i], nalltriang[i] = local_clustering!(storage, g, v)
66-
storage[neighbors(g, v)] .= false
66+
storage[collect_if_not_vector(neighbors(g, v))] .= false
6767
end
6868
return ntriang, nalltriang
6969
end

test/community/assortativity.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ using Statistics
44
# Test definition of assortativity as Pearson correlation coefficient
55
# between excess of degrees
66
@testset "Small graphs" for n in 5:10
7-
@test @inferred assortativity(wheel_graph(n)) -1 / 3
7+
@test @inferred assortativity(GenericGraph(wheel_graph(n))) -1 / 3
88
end
99
@testset "Directed ($seed)" for seed in [1, 2, 3],
1010
(n, ne) in [(14, 18), (10, 22), (7, 16)]
1111

12-
g = erdos_renyi(n, ne; is_directed=true, rng=StableRNG(seed))
12+
g = GenericDiGraph(erdos_renyi(n, ne; is_directed=true, rng=StableRNG(seed)))
1313
assort = assortativity(g)
1414
x = [outdegree(g, src(d)) - 1 for d in edges(g)]
1515
y = [indegree(g, dst(d)) - 1 for d in edges(g)]
@@ -18,7 +18,7 @@ using Statistics
1818
@testset "Undirected ($seed)" for seed in [1, 2, 3],
1919
(n, ne) in [(14, 18), (10, 22), (7, 16)]
2020

21-
g = erdos_renyi(n, ne; is_directed=false, rng=StableRNG(seed))
21+
g = GenericGraph(erdos_renyi(n, ne; is_directed=false, rng=StableRNG(seed)))
2222
assort = assortativity(g)
2323
x = [outdegree(g, src(d)) - 1 for d in edges(g)]
2424
y = [indegree(g, dst(d)) - 1 for d in edges(g)]

test/community/clique_percolation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
add_edge!(g, 1, 4)
1616
add_edge!(g, 4, 5)
1717
add_edge!(g, 5, 1)
18-
@test test_cliques(g, Array[[1, 2, 3], [1, 4, 5]])
18+
@test test_cliques(GenericGraph(g), Array[[1, 2, 3], [1, 4, 5]])
1919
end

test/community/cliques.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
gx = SimpleGraph(3)
1919
add_edge!(gx, 1, 2)
20-
for g in testgraphs(gx)
20+
for g in test_generic_graphs(gx)
2121
@test test_cliques(g, Array[[1, 2], [3]])
2222
end
2323
add_edge!(gx, 2, 3)
24-
for g in testgraphs(gx)
24+
for g in test_generic_graphs(gx)
2525
@test test_cliques(g, Array[[1, 2], [2, 3]])
2626
end
2727
# Test for "pivotdonenbrs not defined" bug
@@ -35,7 +35,7 @@
3535
add_edge!(h, 3, 6)
3636
add_edge!(h, 5, 6)
3737

38-
for g in testgraphs(h)
38+
for g in test_generic_graphs(h)
3939
@test !isempty(@inferred(maximal_cliques(g)))
4040
end
4141

@@ -49,7 +49,7 @@
4949
add_edge!(h, 4, 5)
5050
add_edge!(h, 4, 7)
5151
add_edge!(h, 5, 7)
52-
for g in testgraphs(h)
52+
for g in test_generic_graphs(h)
5353
@test test_cliques(h, Array[[7, 4, 5], [2, 6], [3, 5], [3, 6], [3, 1]])
5454
end
5555
end

test/community/clustering.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "Clustering" begin
22
g10 = complete_graph(10)
3-
for g in testgraphs(g10)
3+
for g in test_generic_graphs(g10)
44
@test @inferred(local_clustering_coefficient(g, 1)) == 1.0
55
@test @inferred(local_clustering_coefficient(g)) == ones(10)
66
@test @inferred(global_clustering_coefficient(g)) == 1.0
@@ -11,5 +11,5 @@
1111
# 1265 / 1266
1212
g = complete_graph(3)
1313
add_edge!(g, 2, 2)
14-
@test @inferred(triangles(g)) == fill(1, 3)
14+
@test @inferred(triangles(GenericGraph(g))) == fill(1, 3)
1515
end

test/community/core-periphery.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "Core periphery" begin
22
g10 = star_graph(10)
3-
for g in testgraphs(g10)
3+
for g in test_generic_graphs(g10)
44
c = core_periphery_deg(g)
55
@test @inferred(degree(g, 1)) == 9
66
@test c[1] == 1
@@ -12,7 +12,7 @@
1212
g10 = star_graph(10)
1313
g10 = blockdiag(g10, g10)
1414
add_edge!(g10, 1, 11)
15-
for g in testgraphs(g10)
15+
for g in test_generic_graphs(g10)
1616
c = @inferred(core_periphery_deg(g))
1717
@test c[1] == 1
1818
@test c[11] == 1

test/community/label_propagation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
for k in 2:5
88
z = blockdiag(z, g)
99
add_edge!(z, (k - 1) * n, k * n)
10-
c, ch = @inferred(label_propagation(z; rng=rng))
10+
c, ch = @inferred(label_propagation(GenericGraph(z); rng=rng))
1111
a = collect(n:n:(k * n))
1212
a = Int[div(i - 1, n) + 1 for i in 1:(k * n)]
1313
# check the number of communities

test/community/modularity.jl

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
m = n * (n - 1) / 2
66
c = ones(Int, n)
77
gint = complete_graph(n)
8-
for g in testgraphs(gint)
8+
for g in test_generic_graphs(gint)
99
@test @inferred(modularity(g, c)) == 0
1010
end
1111

1212
gint = SimpleGraph(n)
13-
for g in testgraphs(gint)
13+
for g in test_generic_graphs(gint)
1414
@test @inferred(modularity(g, c)) == 0
1515
end
1616

17-
barbell = blockdiag(complete_graph(3), complete_graph(3))
18-
add_edge!(barbell, 1, 4)
17+
barbell = barbell_graph(3, 3)
1918
c = [1, 1, 1, 2, 2, 2]
2019

21-
for g in testgraphs(barbell)
20+
for g in test_generic_graphs(barbell)
2221
Q1 = @inferred(modularity(g, c))
2322
@test isapprox(Q1, 0.35714285714285715, atol=1e-3)
2423
Q2 = @inferred(modularity(g, c, γ=0.5))
@@ -35,7 +34,7 @@
3534
add_edge!(barbell, 1, 4)
3635
c = [1, 1, 1, 2, 2, 2]
3736

38-
for g in testdigraphs(barbell)
37+
for g in test_generic_graphs(barbell)
3938
Q1 = @inferred(modularity(g, c))
4039
@test isapprox(Q1, 0.3673469387755103, atol=1e-3)
4140

@@ -44,29 +43,25 @@
4443
end
4544

4645
add_edge!(barbell, 4, 1)
47-
for g in testdigraphs(barbell)
46+
for g in test_generic_graphs(barbell)
4847
@test @inferred(modularity(g, c)) == 0.25
4948
end
5049

5150
# 3. weighted test cases
5251
# 3.1. undirected and weighted test cases
53-
triangle = SimpleGraph(3)
54-
add_edge!(triangle, 1, 2)
55-
add_edge!(triangle, 2, 3)
56-
add_edge!(triangle, 3, 1)
5752

58-
barbell = blockdiag(triangle, triangle)
59-
add_edge!(barbell, 1, 4) # this edge has a weight of 5
53+
# the "handle" of the barbell 3--4 gets a weight of 5
54+
barbell = barbell_graph(3, 3)
6055
c = [1, 1, 1, 2, 2, 2]
6156
d = [
62-
[0 1 1 5 0 0]
57+
[0 1 1 0 0 0]
6358
[1 0 1 0 0 0]
64-
[1 1 0 0 0 0]
65-
[5 0 0 0 1 1]
59+
[1 1 0 5 0 0]
60+
[0 0 5 0 1 1]
6661
[0 0 0 1 0 1]
6762
[0 0 0 1 1 0]
6863
]
69-
for g in testgraphs(barbell)
64+
for g in test_generic_graphs(barbell)
7065
Q = @inferred(modularity(g, c, distmx=d))
7166
@test isapprox(Q, 0.045454545454545456, atol=1e-3)
7267
end
@@ -88,7 +83,7 @@
8883
[0 0 0 0 0 1]
8984
[0 0 0 1 0 0]
9085
]
91-
for g in testdigraphs(barbell)
86+
for g in test_generic_graphs(barbell)
9287
Q = @inferred(modularity(g, c, distmx=d))
9388
@test isapprox(Q, 0.1487603305785124, atol=1e-3)
9489
end

test/community/rich_club.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
@testset "Rich club coefficient" begin
33
rng = StableRNG(1)
44
@testset "Small graphs" for _n in 5:10
5-
@test @inferred rich_club(star_graph(_n), 1) 2 / _n
6-
@test @inferred rich_club(DiGraph(star_graph(_n)), 1) 2 / _n
5+
@test @inferred rich_club(GenericGraph(star_graph(_n)), 1) 2 / _n
6+
@test @inferred rich_club(GenericDiGraph(DiGraph(star_graph(_n))), 1) 2 / _n
77
end
88
@testset "Directed ($seed)" for seed in [1, 2, 3],
99
(n, ne) in [(14, 18), (10, 22), (7, 16)]
1010

11-
g = erdos_renyi(n, ne; is_directed=true, rng=StableRNG(seed))
11+
g = GenericDiGraph(erdos_renyi(n, ne; is_directed=true, rng=StableRNG(seed)))
1212
_r = rich_club(g, 1)
1313
@test @inferred rich_club(g, 1) > 0.0
1414
end
1515
@testset "Undirected ($seed)" for seed in [1, 2, 3],
1616
(n, ne) in [(14, 18), (10, 22), (7, 16)]
1717

18-
g = erdos_renyi(n, ne; is_directed=false, rng=StableRNG(seed))
18+
g = GenericGraph(erdos_renyi(n, ne; is_directed=false, rng=StableRNG(seed)))
1919
_r = rich_club(g, 1)
2020
@test @inferred rich_club(g, 1) > 0.0
2121
end

0 commit comments

Comments
 (0)