Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.39 KB)

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

    
6

    
7
class TestFlowClosenessCentrality(object):
8
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
9

    
10
    @classmethod
11
    def setupClass(cls):
12
        global np
13
        try:
14
            import numpy as np
15
            import scipy
16
        except ImportError:
17
            raise SkipTest('NumPy not available.')
18

    
19
    def test_K4(self):
20
        """Closeness centrality: K4"""
21
        G = nx.complete_graph(4)
22
        b = nx.current_flow_closeness_centrality(G)
23
        b_answer = {0: 2.0 / 3, 1: 2.0 / 3, 2: 2.0 / 3, 3: 2.0 / 3}
24
        for n in sorted(G):
25
            assert_almost_equal(b[n], b_answer[n])
26

    
27
    def test_P4(self):
28
        """Closeness centrality: P4"""
29
        G = nx.path_graph(4)
30
        b = nx.current_flow_closeness_centrality(G)
31
        b_answer = {0: 1.0 / 6, 1: 1.0 / 4, 2: 1.0 / 4, 3: 1.0 / 6}
32
        for n in sorted(G):
33
            assert_almost_equal(b[n], b_answer[n])
34

    
35
    def test_star(self):
36
        """Closeness centrality: star """
37
        G = nx.Graph()
38
        nx.add_star(G, ['a', 'b', 'c', 'd'])
39
        b = nx.current_flow_closeness_centrality(G)
40
        b_answer = {'a': 1.0 / 3, 'b': 0.6 / 3, 'c': 0.6 / 3, 'd': 0.6 / 3}
41
        for n in sorted(G):
42
            assert_almost_equal(b[n], b_answer[n])
43

    
44

    
45
class TestWeightedFlowClosenessCentrality(object):
46
    pass