Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (6.28 KB)

1
from nose.tools import *
2
import networkx as nx
3
from networkx.algorithms import bipartite
4

    
5

    
6
class TestBipartiteCentrality(object):
7

    
8
    def setUp(self):
9
        self.P4 = nx.path_graph(4)
10
        self.K3 = nx.complete_bipartite_graph(3, 3)
11
        self.C4 = nx.cycle_graph(4)
12
        self.davis = nx.davis_southern_women_graph()
13
        self.top_nodes = [n for n, d in self.davis.nodes(data=True)
14
                          if d['bipartite'] == 0]
15

    
16
    def test_degree_centrality(self):
17
        d = bipartite.degree_centrality(self.P4, [1, 3])
18
        answer = {0: 0.5, 1: 1.0,  2: 1.0, 3:  0.5}
19
        assert_equal(d, answer)
20
        d = bipartite.degree_centrality(self.K3, [0, 1, 2])
21
        answer = {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0, 5: 1.0}
22
        assert_equal(d, answer)
23
        d = bipartite.degree_centrality(self.C4, [0, 2])
24
        answer = {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0}
25
        assert_equal(d, answer)
26

    
27
    def test_betweenness_centrality(self):
28
        c = bipartite.betweenness_centrality(self.P4, [1, 3])
29
        answer = {0: 0.0, 1: 1.0, 2: 1.0, 3: 0.0}
30
        assert_equal(c, answer)
31
        c = bipartite.betweenness_centrality(self.K3, [0, 1, 2])
32
        answer = {0: 0.125, 1: 0.125, 2: 0.125, 3: 0.125, 4: 0.125, 5: 0.125}
33
        assert_equal(c, answer)
34
        c = bipartite.betweenness_centrality(self.C4, [0, 2])
35
        answer = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
36
        assert_equal(c, answer)
37

    
38
    def test_closeness_centrality(self):
39
        c = bipartite.closeness_centrality(self.P4, [1, 3])
40
        answer = {0: 2.0 / 3, 1: 1.0, 2: 1.0, 3: 2.0 / 3}
41
        assert_equal(c, answer)
42
        c = bipartite.closeness_centrality(self.K3, [0, 1, 2])
43
        answer = {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0, 4: 1.0, 5: 1.0}
44
        assert_equal(c, answer)
45
        c = bipartite.closeness_centrality(self.C4, [0, 2])
46
        answer = {0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0}
47
        assert_equal(c, answer)
48
        G = nx.Graph()
49
        G.add_node(0)
50
        G.add_node(1)
51
        c = bipartite.closeness_centrality(G, [0])
52
        assert_equal(c, {1: 0.0})
53
        c = bipartite.closeness_centrality(G, [1])
54
        assert_equal(c, {1: 0.0})
55

    
56
    def test_davis_degree_centrality(self):
57
        G = self.davis
58
        deg = bipartite.degree_centrality(G, self.top_nodes)
59
        answer = {'E8': 0.78,
60
                  'E9': 0.67,
61
                  'E7': 0.56,
62
                  'Nora Fayette': 0.57,
63
                  'Evelyn Jefferson': 0.57,
64
                  'Theresa Anderson': 0.57,
65
                  'E6': 0.44,
66
                  'Sylvia Avondale': 0.50,
67
                  'Laura Mandeville': 0.50,
68
                  'Brenda Rogers': 0.50,
69
                  'Katherina Rogers': 0.43,
70
                  'E5': 0.44,
71
                  'Helen Lloyd': 0.36,
72
                  'E3': 0.33,
73
                  'Ruth DeSand': 0.29,
74
                  'Verne Sanderson': 0.29,
75
                  'E12': 0.33,
76
                  'Myra Liddel': 0.29,
77
                  'E11': 0.22,
78
                  'Eleanor Nye': 0.29,
79
                  'Frances Anderson': 0.29,
80
                  'Pearl Oglethorpe': 0.21,
81
                  'E4': 0.22,
82
                  'Charlotte McDowd': 0.29,
83
                  'E10': 0.28,
84
                  'Olivia Carleton': 0.14,
85
                  'Flora Price': 0.14,
86
                  'E2': 0.17,
87
                  'E1': 0.17,
88
                  'Dorothy Murchison': 0.14,
89
                  'E13': 0.17,
90
                  'E14': 0.17}
91
        for node, value in answer.items():
92
            assert_almost_equal(value, deg[node], places=2)
93

    
94
    def test_davis_betweenness_centrality(self):
95
        G = self.davis
96
        bet = bipartite.betweenness_centrality(G, self.top_nodes)
97
        answer = {'E8': 0.24,
98
                  'E9': 0.23,
99
                  'E7': 0.13,
100
                  'Nora Fayette': 0.11,
101
                  'Evelyn Jefferson': 0.10,
102
                  'Theresa Anderson': 0.09,
103
                  'E6': 0.07,
104
                  'Sylvia Avondale': 0.07,
105
                  'Laura Mandeville': 0.05,
106
                  'Brenda Rogers': 0.05,
107
                  'Katherina Rogers': 0.05,
108
                  'E5': 0.04,
109
                  'Helen Lloyd': 0.04,
110
                  'E3': 0.02,
111
                  'Ruth DeSand': 0.02,
112
                  'Verne Sanderson': 0.02,
113
                  'E12': 0.02,
114
                  'Myra Liddel': 0.02,
115
                  'E11': 0.02,
116
                  'Eleanor Nye': 0.01,
117
                  'Frances Anderson': 0.01,
118
                  'Pearl Oglethorpe': 0.01,
119
                  'E4': 0.01,
120
                  'Charlotte McDowd': 0.01,
121
                  'E10': 0.01,
122
                  'Olivia Carleton': 0.01,
123
                  'Flora Price': 0.01,
124
                  'E2': 0.00,
125
                  'E1': 0.00,
126
                  'Dorothy Murchison': 0.00,
127
                  'E13': 0.00,
128
                  'E14': 0.00}
129
        for node, value in answer.items():
130
            assert_almost_equal(value, bet[node], places=2)
131

    
132
    def test_davis_closeness_centrality(self):
133
        G = self.davis
134
        clos = bipartite.closeness_centrality(G, self.top_nodes)
135
        answer = {'E8': 0.85,
136
                  'E9': 0.79,
137
                  'E7': 0.73,
138
                  'Nora Fayette': 0.80,
139
                  'Evelyn Jefferson': 0.80,
140
                  'Theresa Anderson': 0.80,
141
                  'E6': 0.69,
142
                  'Sylvia Avondale': 0.77,
143
                  'Laura Mandeville': 0.73,
144
                  'Brenda Rogers': 0.73,
145
                  'Katherina Rogers': 0.73,
146
                  'E5': 0.59,
147
                  'Helen Lloyd': 0.73,
148
                  'E3': 0.56,
149
                  'Ruth DeSand': 0.71,
150
                  'Verne Sanderson': 0.71,
151
                  'E12': 0.56,
152
                  'Myra Liddel': 0.69,
153
                  'E11': 0.54,
154
                  'Eleanor Nye': 0.67,
155
                  'Frances Anderson': 0.67,
156
                  'Pearl Oglethorpe': 0.67,
157
                  'E4': 0.54,
158
                  'Charlotte McDowd': 0.60,
159
                  'E10': 0.55,
160
                  'Olivia Carleton': 0.59,
161
                  'Flora Price': 0.59,
162
                  'E2': 0.52,
163
                  'E1': 0.52,
164
                  'Dorothy Murchison': 0.65,
165
                  'E13': 0.52,
166
                  'E14': 0.52}
167
        for node, value in answer.items():
168
            assert_almost_equal(value, clos[node], places=2)