Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.3 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.communicability_alg import *
6

    
7

    
8
class TestCommunicability:
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_communicability(self):
23
        answer = {0: {0: 1.5430806348152435,
24
                      1: 1.1752011936438012
25
                      },
26
                  1: {0: 1.1752011936438012,
27
                      1: 1.5430806348152435
28
                      }
29
                  }
30
#        answer={(0, 0): 1.5430806348152435,
31
#                (0, 1): 1.1752011936438012,
32
#                (1, 0): 1.1752011936438012,
33
#                (1, 1): 1.5430806348152435}
34

    
35
        result = communicability(nx.path_graph(2))
36
        for k1, val in result.items():
37
            for k2 in val:
38
                assert_almost_equal(answer[k1][k2], result[k1][k2], places=7)
39

    
40
    def test_communicability2(self):
41

    
42
        answer_orig = {('1', '1'): 1.6445956054135658,
43
                       ('1', 'Albert'): 0.7430186221096251,
44
                       ('1', 'Aric'): 0.7430186221096251,
45
                       ('1', 'Dan'): 1.6208126320442937,
46
                       ('1', 'Franck'): 0.42639707170035257,
47
                       ('Albert', '1'): 0.7430186221096251,
48
                       ('Albert', 'Albert'): 2.4368257358712189,
49
                       ('Albert', 'Aric'): 1.4368257358712191,
50
                       ('Albert', 'Dan'): 2.0472097037446453,
51
                       ('Albert', 'Franck'): 1.8340111678944691,
52
                       ('Aric', '1'): 0.7430186221096251,
53
                       ('Aric', 'Albert'): 1.4368257358712191,
54
                       ('Aric', 'Aric'): 2.4368257358712193,
55
                       ('Aric', 'Dan'): 2.0472097037446457,
56
                       ('Aric', 'Franck'): 1.8340111678944691,
57
                       ('Dan', '1'): 1.6208126320442937,
58
                       ('Dan', 'Albert'): 2.0472097037446453,
59
                       ('Dan', 'Aric'): 2.0472097037446457,
60
                       ('Dan', 'Dan'): 3.1306328496328168,
61
                       ('Dan', 'Franck'): 1.4860372442192515,
62
                       ('Franck', '1'): 0.42639707170035257,
63
                       ('Franck', 'Albert'): 1.8340111678944691,
64
                       ('Franck', 'Aric'): 1.8340111678944691,
65
                       ('Franck', 'Dan'): 1.4860372442192515,
66
                       ('Franck', 'Franck'): 2.3876142275231915}
67

    
68
        answer = defaultdict(dict)
69
        for (k1, k2), v in answer_orig.items():
70
            answer[k1][k2] = v
71

    
72
        G1 = nx.Graph([('Franck', 'Aric'), ('Aric', 'Dan'), ('Dan', 'Albert'),
73
                       ('Albert', 'Franck'), ('Dan', '1'), ('Franck', 'Albert')])
74

    
75
        result = communicability(G1)
76
        for k1, val in result.items():
77
            for k2 in val:
78
                assert_almost_equal(answer[k1][k2], result[k1][k2], places=7)
79

    
80
        result = communicability_exp(G1)
81
        for k1, val in result.items():
82
            for k2 in val:
83
                assert_almost_equal(answer[k1][k2], result[k1][k2], places=7)