Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / bipartite / tests / test_cluster.py @ 5cef0f13

History | View | Annotate | Download (2.77 KB)

1
import networkx as nx
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
5

    
6

    
7
def test_pairwise_bipartite_cc_functions():
8
    # Test functions for different kinds of bipartite clustering coefficients
9
    # between pairs of nodes using 3 example graphs from figure 5 p. 40
10
    # Latapy et al (2008)
11
    G1 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7)])
12
    G2 = nx.Graph([(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (1, 5)])
13
    G3 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)])
14
    result = {0: [1 / 3.0, 2 / 3.0, 2 / 5.0],
15
              1: [1 / 2.0, 2 / 3.0, 2 / 3.0],
16
              2: [2 / 8.0, 2 / 5.0, 2 / 5.0]}
17
    for i, G in enumerate([G1, G2, G3]):
18
        assert(bipartite.is_bipartite(G))
19
        assert(cc_dot(set(G[0]), set(G[1])) == result[i][0])
20
        assert(cc_min(set(G[0]), set(G[1])) == result[i][1])
21
        assert(cc_max(set(G[0]), set(G[1])) == result[i][2])
22

    
23

    
24
def test_star_graph():
25
    G = nx.star_graph(3)
26
    # all modes are the same
27
    answer = {0: 0, 1: 1, 2: 1, 3: 1}
28
    assert_equal(bipartite.clustering(G, mode='dot'), answer)
29
    assert_equal(bipartite.clustering(G, mode='min'), answer)
30
    assert_equal(bipartite.clustering(G, mode='max'), answer)
31

    
32

    
33
@raises(nx.NetworkXError)
34
def test_not_bipartite():
35
    bipartite.clustering(nx.complete_graph(4))
36

    
37

    
38
@raises(nx.NetworkXError)
39
def test_bad_mode():
40
    bipartite.clustering(nx.path_graph(4), mode='foo')
41

    
42

    
43
def test_path_graph():
44
    G = nx.path_graph(4)
45
    answer = {0: 0.5, 1: 0.5, 2: 0.5, 3: 0.5}
46
    assert_equal(bipartite.clustering(G, mode='dot'), answer)
47
    assert_equal(bipartite.clustering(G, mode='max'), answer)
48
    answer = {0: 1, 1: 1, 2: 1, 3: 1}
49
    assert_equal(bipartite.clustering(G, mode='min'), answer)
50

    
51

    
52
def test_average_path_graph():
53
    G = nx.path_graph(4)
54
    assert_equal(bipartite.average_clustering(G, mode='dot'), 0.5)
55
    assert_equal(bipartite.average_clustering(G, mode='max'), 0.5)
56
    assert_equal(bipartite.average_clustering(G, mode='min'), 1)
57

    
58

    
59
def test_ra_clustering_davis():
60
    G = nx.davis_southern_women_graph()
61
    cc4 = round(bipartite.robins_alexander_clustering(G), 3)
62
    assert_equal(cc4, 0.468)
63

    
64

    
65
def test_ra_clustering_square():
66
    G = nx.path_graph(4)
67
    G.add_edge(0, 3)
68
    assert_equal(bipartite.robins_alexander_clustering(G), 1.0)
69

    
70

    
71
def test_ra_clustering_zero():
72
    G = nx.Graph()
73
    assert_equal(bipartite.robins_alexander_clustering(G), 0)
74
    G.add_nodes_from(range(4))
75
    assert_equal(bipartite.robins_alexander_clustering(G), 0)
76
    G.add_edges_from([(0, 1), (2, 3), (3, 4)])
77
    assert_equal(bipartite.robins_alexander_clustering(G), 0)
78
    G.add_edge(1, 2)
79
    assert_equal(bipartite.robins_alexander_clustering(G), 0)