Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.86 KB)

1
from collections import defaultdict
2
from nose.tools import *
3
from nose import SkipTest
4
import networkx as nx
5
from networkx.algorithms.centrality.subgraph_alg import *
6

    
7

    
8
class TestSubgraph:
9
    @classmethod
10
    def setupClass(cls):
11
        global numpy
12
        global scipy
13
        try:
14
            import numpy
15
        except ImportError:
16
            raise SkipTest('NumPy not available.')
17
        try:
18
            import scipy
19
        except ImportError:
20
            raise SkipTest('SciPy not available.')
21

    
22
    def test_subgraph_centrality(self):
23
        answer = {0: 1.5430806348152433, 1: 1.5430806348152433}
24
        result = subgraph_centrality(nx.path_graph(2))
25
        for k, v in result.items():
26
            assert_almost_equal(answer[k], result[k], places=7)
27

    
28
        answer1 = {'1': 1.6445956054135658,
29
                   'Albert': 2.4368257358712189,
30
                   'Aric': 2.4368257358712193,
31
                   'Dan': 3.1306328496328168,
32
                   'Franck': 2.3876142275231915}
33
        G1 = nx.Graph([('Franck', 'Aric'), ('Aric', 'Dan'), ('Dan', 'Albert'),
34
                       ('Albert', 'Franck'), ('Dan', '1'), ('Franck', 'Albert')])
35
        result1 = subgraph_centrality(G1)
36
        for k, v in result1.items():
37
            assert_almost_equal(answer1[k], result1[k], places=7)
38
        result1 = subgraph_centrality_exp(G1)
39
        for k, v in result1.items():
40
            assert_almost_equal(answer1[k], result1[k], places=7)
41

    
42
    def test_subgraph_centrality_big_graph(self):
43
        g199 = nx.complete_graph(199)
44
        g200 = nx.complete_graph(200)
45

    
46
        comm199 = nx.subgraph_centrality(g199)
47
        comm199_exp = nx.subgraph_centrality_exp(g199)
48

    
49
        comm200 = nx.subgraph_centrality(g200)
50
        comm200_exp = nx.subgraph_centrality_exp(g200)
51

    
52
    def test_communicability_betweenness_centrality(self):
53
        answer = {0: 0.07017447951484615, 1: 0.71565598701107991,
54
                  2: 0.71565598701107991, 3: 0.07017447951484615}
55
        result = communicability_betweenness_centrality(nx.path_graph(4))
56
        for k, v in result.items():
57
            assert_almost_equal(answer[k], result[k], places=7)
58

    
59
        answer1 = {'1': 0.060039074193949521,
60
                   'Albert': 0.315470761661372,
61
                   'Aric': 0.31547076166137211,
62
                   'Dan': 0.68297778678316201,
63
                   'Franck': 0.21977926617449497}
64
        G1 = nx.Graph([('Franck', 'Aric'),
65
                       ('Aric', 'Dan'), ('Dan', 'Albert'), ('Albert', 'Franck'),
66
                       ('Dan', '1'), ('Franck', 'Albert')])
67
        result1 = communicability_betweenness_centrality(G1)
68
        for k, v in result1.items():
69
            assert_almost_equal(answer1[k], result1[k], places=7)
70

    
71
    def test_estrada_index(self):
72
        answer = 1041.2470334195475
73
        result = estrada_index(nx.karate_club_graph())
74
        assert_almost_equal(answer, result, places=7)