~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/algorithms/bipartite/tests/test_cluster.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
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 
 
9
    # Latapy et al (2008)
 
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])
 
19
 
 
20
def test_star_graph():
 
21
    G=nx.star_graph(3)
 
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)
 
27
 
 
28
@raises(nx.NetworkXError)
 
29
def test_not_bipartite():
 
30
    bipartite.clustering(nx.complete_graph(4))
 
31
 
 
32
@raises(nx.NetworkXError)
 
33
def test_bad_mode():
 
34
    bipartite.clustering(nx.path_graph(4),mode='foo')
 
35
 
 
36
def test_path_graph():
 
37
    G=nx.path_graph(4)
 
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)
 
43
 
 
44
 
 
45
def test_average_path_graph():
 
46
    G=nx.path_graph(4)
 
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)
 
50
 
 
51
 
 
52
 
 
53