Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7.39 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from nose import SkipTest
4
import networkx as nx
5
from nose.plugins.attrib import attr
6

    
7
from networkx import edge_current_flow_betweenness_centrality \
8
    as edge_current_flow
9

    
10
from networkx import edge_current_flow_betweenness_centrality_subset \
11
    as edge_current_flow_subset
12

    
13

    
14
class TestFlowBetweennessCentrality(object):
15
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
16

    
17
    @classmethod
18
    def setupClass(cls):
19
        global np
20
        try:
21
            import numpy as np
22
            import scipy
23
        except ImportError:
24
            raise SkipTest('NumPy not available.')
25

    
26
    def test_K4_normalized(self):
27
        """Betweenness centrality: K4"""
28
        G = nx.complete_graph(4)
29
        b = nx.current_flow_betweenness_centrality_subset(G,
30
                                                          list(G),
31
                                                          list(G),
32
                                                          normalized=True)
33
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
34
        for n in sorted(G):
35
            assert_almost_equal(b[n], b_answer[n])
36

    
37
    def test_K4(self):
38
        """Betweenness centrality: K4"""
39
        G = nx.complete_graph(4)
40
        b = nx.current_flow_betweenness_centrality_subset(G,
41
                                                          list(G),
42
                                                          list(G),
43
                                                          normalized=True)
44
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
45
        for n in sorted(G):
46
            assert_almost_equal(b[n], b_answer[n])
47
        # test weighted network
48
        G.add_edge(0, 1, weight=0.5, other=0.3)
49
        b = nx.current_flow_betweenness_centrality_subset(G,
50
                                                          list(G),
51
                                                          list(G),
52
                                                          normalized=True,
53
                                                          weight=None)
54
        for n in sorted(G):
55
            assert_almost_equal(b[n], b_answer[n])
56
        b = nx.current_flow_betweenness_centrality_subset(G,
57
                                                          list(G),
58
                                                          list(G),
59
                                                          normalized=True)
60
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
61
        for n in sorted(G):
62
            assert_almost_equal(b[n], b_answer[n])
63
        b = nx.current_flow_betweenness_centrality_subset(G,
64
                                                          list(G),
65
                                                          list(G),
66
                                                          normalized=True,
67
                                                          weight='other')
68
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True, weight='other')
69
        for n in sorted(G):
70
            assert_almost_equal(b[n], b_answer[n])
71

    
72
    def test_P4_normalized(self):
73
        """Betweenness centrality: P4 normalized"""
74
        G = nx.path_graph(4)
75
        b = nx.current_flow_betweenness_centrality_subset(G,
76
                                                          list(G),
77
                                                          list(G),
78
                                                          normalized=True)
79
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
80
        for n in sorted(G):
81
            assert_almost_equal(b[n], b_answer[n])
82

    
83
    def test_P4(self):
84
        """Betweenness centrality: P4"""
85
        G = nx.path_graph(4)
86
        b = nx.current_flow_betweenness_centrality_subset(G,
87
                                                          list(G),
88
                                                          list(G),
89
                                                          normalized=True)
90
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
91
        for n in sorted(G):
92
            assert_almost_equal(b[n], b_answer[n])
93

    
94
    def test_star(self):
95
        """Betweenness centrality: star """
96
        G = nx.Graph()
97
        nx.add_star(G, ['a', 'b', 'c', 'd'])
98
        b = nx.current_flow_betweenness_centrality_subset(G,
99
                                                          list(G),
100
                                                          list(G),
101
                                                          normalized=True)
102
        b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
103
        for n in sorted(G):
104
            assert_almost_equal(b[n], b_answer[n])
105

    
106

    
107
# class TestWeightedFlowBetweennessCentrality():
108
#     pass
109

    
110

    
111
class TestEdgeFlowBetweennessCentrality(object):
112
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
113

    
114
    @classmethod
115
    def setupClass(cls):
116
        global np
117
        try:
118
            import numpy as np
119
            import scipy
120
        except ImportError:
121
            raise SkipTest('NumPy not available.')
122

    
123
    def test_K4_normalized(self):
124
        """Betweenness centrality: K4"""
125
        G = nx.complete_graph(4)
126
        b = edge_current_flow_subset(G, list(G), list(G), normalized=True)
127
        b_answer = edge_current_flow(G, normalized=True)
128
        for (s, t), v1 in b_answer.items():
129
            v2 = b.get((s, t), b.get((t, s)))
130
            assert_almost_equal(v1, v2)
131

    
132
    def test_K4(self):
133
        """Betweenness centrality: K4"""
134
        G = nx.complete_graph(4)
135
        b = edge_current_flow_subset(G, list(G), list(G), normalized=False)
136
        b_answer = edge_current_flow(G, normalized=False)
137
        for (s, t), v1 in b_answer.items():
138
            v2 = b.get((s, t), b.get((t, s)))
139
            assert_almost_equal(v1, v2)
140
        # test weighted network
141
        G.add_edge(0, 1, weight=0.5, other=0.3)
142
        b = edge_current_flow_subset(G, list(G), list(G), normalized=False, weight=None)
143
        # weight is None => same as unweighted network
144
        for (s, t), v1 in b_answer.items():
145
            v2 = b.get((s, t), b.get((t, s)))
146
            assert_almost_equal(v1, v2)
147

    
148
        b = edge_current_flow_subset(G, list(G), list(G), normalized=False)
149
        b_answer = edge_current_flow(G, normalized=False)
150
        for (s, t), v1 in b_answer.items():
151
            v2 = b.get((s, t), b.get((t, s)))
152
            assert_almost_equal(v1, v2)
153

    
154
        b = edge_current_flow_subset(G, list(G), list(G), normalized=False, weight='other')
155
        b_answer = edge_current_flow(G, normalized=False, weight='other')
156
        for (s, t), v1 in b_answer.items():
157
            v2 = b.get((s, t), b.get((t, s)))
158
            assert_almost_equal(v1, v2)
159

    
160
    def test_C4(self):
161
        """Edge betweenness centrality: C4"""
162
        G = nx.cycle_graph(4)
163
        b = edge_current_flow_subset(G, list(G), list(G), normalized=True)
164
        b_answer = edge_current_flow(G, normalized=True)
165
        for (s, t), v1 in b_answer.items():
166
            v2 = b.get((s, t), b.get((t, s)))
167
            assert_almost_equal(v1, v2)
168

    
169
    def test_P4(self):
170
        """Edge betweenness centrality: P4"""
171
        G = nx.path_graph(4)
172
        b = edge_current_flow_subset(G, list(G), list(G), normalized=True)
173
        b_answer = edge_current_flow(G, normalized=True)
174
        for (s, t), v1 in b_answer.items():
175
            v2 = b.get((s, t), b.get((t, s)))
176
            assert_almost_equal(v1, v2)