Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / centrality / tests / test_group.py @ 5cef0f13

History | View | Annotate | Download (4.76 KB)

1
"""
2
    Tests for Group Centrality Measures
3
"""
4

    
5

    
6
from nose.tools import *
7
import networkx as nx
8

    
9

    
10
class TestGroupBetweennessCentrality:
11

    
12
    def test_group_betweenness_single_node(self):
13
        """
14
            Group betweenness centrality for single node group
15
        """
16
        G = nx.path_graph(5)
17
        C = [1]
18
        b = nx.group_betweenness_centrality(G, C,
19
                                            weight=None, normalized=False)
20
        b_answer = 3.0
21
        assert_equal(b, b_answer)
22

    
23
    def test_group_betweenness_normalized(self):
24
        """
25
            Group betweenness centrality for group with more than
26
            1 node and normalized
27
        """
28
        G = nx.path_graph(5)
29
        C = [1, 3]
30
        b = nx.group_betweenness_centrality(G, C,
31
                                            weight=None, normalized=True)
32
        b_answer = 1.0
33
        assert_equal(b, b_answer)
34

    
35
    def test_group_betweenness_value_zero(self):
36
        """
37
            Group betweenness centrality value of 0
38
        """
39
        G = nx.cycle_graph(6)
40
        C = [0, 1, 5]
41
        b = nx.group_betweenness_centrality(G, C, weight=None)
42
        b_answer = 0.0
43
        assert_equal(b, b_answer)
44

    
45
    def test_group_betweenness_disconnected_graph(self):
46
        """
47
            Group betweenness centrality in a disconnected graph
48
        """
49
        G = nx.path_graph(5)
50
        G.remove_edge(0, 1)
51
        C = [1]
52
        b = nx.group_betweenness_centrality(G, C, weight=None)
53
        b_answer = 0.0
54
        assert_equal(b, b_answer)
55

    
56
    @raises(nx.NodeNotFound)
57
    def test_group_betweenness_node_not_in_graph(self):
58
        """
59
            Node(s) in C not in graph, raises NodeNotFound exception
60
        """
61
        b = nx.group_betweenness_centrality(nx.path_graph(5), [6, 7, 8])
62

    
63

    
64
class TestGroupClosenessCentrality:
65

    
66
    def test_group_closeness_single_node(self):
67
        """
68
            Group closeness centrality for a single node group
69
        """
70
        G = nx.path_graph(5)
71
        c = nx.group_closeness_centrality(G, [1])
72
        c_answer = nx.closeness_centrality(G, 1)
73
        assert_equal(c, c_answer)
74

    
75
    def test_group_closeness_disconnected(self):
76
        """
77
            Group closeness centrality for a disconnected graph
78
        """
79
        G = nx.Graph()
80
        G.add_nodes_from([1, 2, 3, 4])
81
        c = nx.group_closeness_centrality(G, [1, 2])
82
        c_answer = 0
83
        assert_equal(c, c_answer)
84

    
85
    def test_group_closeness_multiple_node(self):
86
        """
87
            Group closeness centrality for a group with more than
88
            1 node
89
        """
90
        G = nx.path_graph(4)
91
        c = nx.group_closeness_centrality(G, [1, 2])
92
        c_answer = 1
93
        assert_equal(c, c_answer)
94

    
95
    @raises(nx.NodeNotFound)
96
    def test_group_closeness_node_not_in_graph(self):
97
        """
98
            Node(s) in S not in graph, raises NodeNotFound exception
99
        """
100
        c = nx.group_closeness_centrality(nx.path_graph(5), [6, 7, 8])
101

    
102

    
103
class TestGroupDegreeCentrality:
104

    
105
    def test_group_degree_centrality_single_node(self):
106
        """
107
            Group degree centrality for a single node group
108
        """
109
        G = nx.path_graph(4)
110
        d = nx.group_degree_centrality(G, [1])
111
        d_answer = nx.degree_centrality(G)[1]
112
        assert_equal(d, d_answer)
113

    
114
    def test_group_degree_centrality_multiple_node(self):
115
        """
116
            Group degree centrality for group with more than
117
            1 node
118
        """
119
        G = nx.Graph()
120
        G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
121
        G.add_edges_from([(1, 2), (1, 3), (1, 6), (1, 7), (1, 8),
122
                          (2, 3), (2, 4), (2, 5)])
123
        d = nx.group_degree_centrality(G, [1, 2])
124
        d_answer = 1
125
        assert_equal(d, d_answer)
126

    
127
    def test_group_in_degree_centrality(self):
128
        """
129
            Group in-degree centrality in a DiGraph
130
        """
131
        G = nx.DiGraph()
132
        G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
133
        G.add_edges_from([(1, 2), (1, 3), (1, 6), (1, 7), (1, 8),
134
                          (2, 3), (2, 4), (2, 5)])
135
        d = nx.group_in_degree_centrality(G, [1, 2])
136
        d_answer = 0
137
        assert_equal(d, d_answer)
138

    
139
    def test_group_out_degree_centrality(self):
140
        """
141
            Group out-degree centrality in a DiGraph
142
        """
143
        G = nx.DiGraph()
144
        G.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8])
145
        G.add_edges_from([(1, 2), (1, 3), (1, 6), (1, 7), (1, 8),
146
                          (2, 3), (2, 4), (2, 5)])
147
        d = nx.group_out_degree_centrality(G, [1, 2])
148
        d_answer = 1
149
        assert_equal(d, d_answer)
150

    
151
    @raises(nx.NetworkXError)
152
    def test_group_degree_centrality_node_not_in_graph(self):
153
        """
154
            Node(s) in S not in graph, raises NetworkXError
155
        """
156
        b = nx.group_degree_centrality(nx.path_graph(5), [6, 7, 8])