Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.1 KB)

1
"""
2
Tests for second order centrality.
3
"""
4

    
5
import networkx as nx
6
from nose import SkipTest
7
from nose.tools import raises, assert_almost_equal
8

    
9

    
10
class TestSecondOrderCentrality(object):
11
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
12

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

    
22
    @raises(nx.NetworkXException)
23
    def test_empty(self):
24
        G = nx.empty_graph()
25
        nx.second_order_centrality(G)
26

    
27
    @raises(nx.NetworkXException)
28
    def test_non_connected(self):
29
        G = nx.Graph()
30
        G.add_node(0)
31
        G.add_node(1)
32
        nx.second_order_centrality(G)
33

    
34
    @raises(nx.NetworkXException)
35
    def test_non_negative_edge_weights(self):
36
        G = nx.path_graph(2)
37
        G.add_edge(0, 1, weight=-1)
38
        nx.second_order_centrality(G)
39

    
40
    def test_one_node_graph(self):
41
        """Second order centrality: single node"""
42
        G = nx.Graph()
43
        G.add_node(0)
44
        G.add_edge(0, 0)
45
        assert nx.second_order_centrality(G)[0] == 0
46

    
47
    def test_P3(self):
48
        """Second order centrality: line graph, as defined in paper"""
49
        G = nx.path_graph(3)
50
        b_answer = {0: 3.741, 1: 1.414, 2: 3.741}
51

    
52
        b = nx.second_order_centrality(G)
53

    
54
        for n in sorted(G):
55
            assert_almost_equal(b[n], b_answer[n], places=2)
56

    
57
    def test_K3(self):
58
        """Second order centrality: complete graph, as defined in paper"""
59
        G = nx.complete_graph(3)
60
        b_answer = {0: 1.414, 1: 1.414, 2: 1.414}
61

    
62
        b = nx.second_order_centrality(G)
63

    
64
        for n in sorted(G):
65
            assert_almost_equal(b[n], b_answer[n], places=2)
66

    
67
    def test_ring_graph(self):
68
        """Second order centrality: ring graph, as defined in paper"""
69
        G = nx.cycle_graph(5)
70
        b_answer = {0: 4.472, 1: 4.472, 2: 4.472,
71
                    3: 4.472, 4: 4.472}
72

    
73
        b = nx.second_order_centrality(G)
74

    
75
        for n in sorted(G):
76
            assert_almost_equal(b[n], b_answer[n], places=2)