Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / community / tests / test_asyn_fluid.py @ 5cef0f13

History | View | Annotate | Download (3.25 KB)

1
from nose.tools import assert_equal, assert_raises
2
from networkx import Graph, NetworkXError
3
from networkx.algorithms.community.asyn_fluid import *
4
import random
5

    
6

    
7
def test_exceptions():
8
    test = Graph()
9
    test.add_node('a')
10
    assert_raises(NetworkXError, asyn_fluidc, test, 'hi')
11
    assert_raises(NetworkXError, asyn_fluidc, test, -1)
12
    assert_raises(NetworkXError, asyn_fluidc, test, 3)
13
    test.add_node('b')
14
    assert_raises(NetworkXError, asyn_fluidc, test, 1)
15

    
16

    
17
def test_single_node():
18
    test = Graph()
19

    
20
    test.add_node('a')
21

    
22
    # ground truth
23
    ground_truth = set([frozenset(['a'])])
24

    
25
    communities = asyn_fluidc(test, 1)
26
    result = {frozenset(c) for c in communities}
27
    assert_equal(result, ground_truth)
28

    
29

    
30
def test_two_nodes():
31
    test = Graph()
32

    
33
    test.add_edge('a', 'b')
34

    
35
    # ground truth
36
    ground_truth = set([frozenset(['a']), frozenset(['b'])])
37

    
38
    communities = asyn_fluidc(test, 2)
39
    result = {frozenset(c) for c in communities}
40
    assert_equal(result, ground_truth)
41

    
42

    
43
def test_two_clique_communities():
44
    test = Graph()
45

    
46
    # c1
47
    test.add_edge('a', 'b')
48
    test.add_edge('a', 'c')
49
    test.add_edge('b', 'c')
50

    
51
    # connection
52
    test.add_edge('c', 'd')
53

    
54
    # c2
55
    test.add_edge('d', 'e')
56
    test.add_edge('d', 'f')
57
    test.add_edge('f', 'e')
58

    
59
    # ground truth
60
    ground_truth = set([frozenset(['a', 'c', 'b']),
61
                        frozenset(['e', 'd', 'f'])])
62

    
63
    communities = asyn_fluidc(test, 2, seed=7)
64
    result = {frozenset(c) for c in communities}
65
    assert_equal(result, ground_truth)
66

    
67

    
68
def five_clique_ring():
69
    """Not auto-tested (not named test_...) due to cross-version seed issues
70
    python3.4 in particular gives different results.
71
    """
72
    test = Graph()
73

    
74
    # c1
75
    test.add_edge('1a', '1b')
76
    test.add_edge('1a', '1c')
77
    test.add_edge('1a', '1d')
78
    test.add_edge('1b', '1c')
79
    test.add_edge('1b', '1d')
80
    test.add_edge('1c', '1d')
81

    
82
    # c2
83
    test.add_edge('2a', '2b')
84
    test.add_edge('2a', '2c')
85
    test.add_edge('2a', '2d')
86
    test.add_edge('2b', '2c')
87
    test.add_edge('2b', '2d')
88
    test.add_edge('2c', '2d')
89

    
90
    # c3
91
    test.add_edge('3a', '3b')
92
    test.add_edge('3a', '3c')
93
    test.add_edge('3a', '3d')
94
    test.add_edge('3b', '3c')
95
    test.add_edge('3b', '3d')
96
    test.add_edge('3c', '3d')
97

    
98
    # c4
99
    test.add_edge('4a', '4b')
100
    test.add_edge('4a', '4c')
101
    test.add_edge('4a', '4d')
102
    test.add_edge('4b', '4c')
103
    test.add_edge('4b', '4d')
104
    test.add_edge('4c', '4d')
105

    
106
    # c5
107
    test.add_edge('5a', '5b')
108
    test.add_edge('5a', '5c')
109
    test.add_edge('5a', '5d')
110
    test.add_edge('5b', '5c')
111
    test.add_edge('5b', '5d')
112
    test.add_edge('5c', '5d')
113

    
114
    # connections
115
    test.add_edge('1a', '2c')
116
    test.add_edge('2a', '3c')
117
    test.add_edge('3a', '4c')
118
    test.add_edge('4a', '5c')
119
    test.add_edge('5a', '1c')
120

    
121
    # ground truth
122
    ground_truth = set([frozenset(['1a', '1b', '1c', '1d']),
123
                        frozenset(['2a', '2b', '2c', '2d']),
124
                        frozenset(['3a', '3b', '3c', '3d']),
125
                        frozenset(['4a', '4b', '4c', '4d']),
126
                        frozenset(['5a', '5b', '5c', '5d'])])
127

    
128
    communities = asyn_fluidc(test, 5, seed=9)
129
    result = {frozenset(c) for c in communities}
130
    assert_equal(result, ground_truth)