2
from nose.tools import *
3
from networkx.algorithms.bipartite.cluster import cc_dot,cc_min,cc_max
4
import networkx.algorithms.bipartite as bipartite
6
def test_pairwise_bipartite_cc_functions():
7
# Test functions for different kinds of bipartite clustering coefficients
8
# between pairs of nodes using 3 example graphs from figure 5 p. 40
10
G1 = nx.Graph([(0,2),(0,3),(0,4),(0,5),(0,6),(1,5),(1,6),(1,7)])
11
G2 = nx.Graph([(0,2),(0,3),(0,4),(1,3),(1,4),(1,5)])
12
G3 = nx.Graph([(0,2),(0,3),(0,4),(0,5),(0,6),(1,5),(1,6),(1,7),(1,8),(1,9)])
13
result = {0:[1/3.0, 2/3.0, 2/5.0], 1:[1/2.0, 2/3.0, 2/3.0], 2:[2/8.0, 2/5.0, 2/5.0]}
14
for i, G in enumerate([G1, G2, G3]):
15
assert(bipartite.is_bipartite(G))
16
assert(cc_dot(set(G[0]), set(G[1])) == result[i][0])
17
assert(cc_min(set(G[0]), set(G[1])) == result[i][1])
18
assert(cc_max(set(G[0]), set(G[1])) == result[i][2])
20
def test_star_graph():
22
# all modes are the same
23
answer={0:0,1:1,2:1,3:1}
24
assert_equal(bipartite.clustering(G,mode='dot'),answer)
25
assert_equal(bipartite.clustering(G,mode='min'),answer)
26
assert_equal(bipartite.clustering(G,mode='max'),answer)
28
@raises(nx.NetworkXError)
29
def test_not_bipartite():
30
bipartite.clustering(nx.complete_graph(4))
32
@raises(nx.NetworkXError)
34
bipartite.clustering(nx.path_graph(4),mode='foo')
36
def test_path_graph():
38
answer={0:0.5,1:0.5,2:0.5,3:0.5}
39
assert_equal(bipartite.clustering(G,mode='dot'),answer)
40
assert_equal(bipartite.clustering(G,mode='max'),answer)
41
answer={0:1,1:1,2:1,3:1}
42
assert_equal(bipartite.clustering(G,mode='min'),answer)
45
def test_average_path_graph():
47
assert_equal(bipartite.average_clustering(G,mode='dot'),0.5)
48
assert_equal(bipartite.average_clustering(G,mode='max'),0.5)
49
assert_equal(bipartite.average_clustering(G,mode='min'),1)