Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.01 KB)

1
#    Copyright (C) 2015-2019 by
2
#    Aric Hagberg <hagberg@lanl.gov>
3
#    Dan Schult <dschult@colgate.edu>
4
#    Pieter Swart <swart@lanl.gov>
5
#    All rights reserved.
6
#    BSD license.
7
"""Unit tests for the :mod:`networkx.algorithms.centrality.reaching` module."""
8
from __future__ import division
9
from nose.tools import assert_almost_equal, assert_equal, raises
10
from unittest import TestCase
11

    
12
from networkx import nx
13

    
14

    
15
class TestGlobalReachingCentrality(TestCase):
16
    """Unit tests for the global reaching centrality function."""
17

    
18
    @raises(nx.NetworkXError)
19
    def test_non_positive_weights(self):
20
        G = nx.DiGraph()
21
        nx.global_reaching_centrality(G, weight='weight')
22

    
23
    @raises(nx.NetworkXError)
24
    def test_negatively_weighted(self):
25
        G = nx.Graph()
26
        G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
27
        nx.global_reaching_centrality(G, weight='weight')
28

    
29
    def test_directed_star(self):
30
        G = nx.DiGraph()
31
        G.add_weighted_edges_from([(1, 2, 0.5), (1, 3, 0.5)])
32
        grc = nx.global_reaching_centrality
33
        assert_equal(grc(G, normalized=False, weight='weight'), 0.5)
34
        assert_equal(grc(G), 1)
35

    
36
    def test_undirected_unweighted_star(self):
37
        G = nx.star_graph(2)
38
        grc = nx.global_reaching_centrality
39
        assert_equal(grc(G, normalized=False, weight=None), 0.25)
40

    
41
    def test_undirected_weighted_star(self):
42
        G = nx.Graph()
43
        G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
44
        grc = nx.global_reaching_centrality
45
        assert_equal(grc(G, normalized=False, weight='weight'), 0.375)
46

    
47
    def test_cycle_directed_unweighted(self):
48
        G = nx.DiGraph()
49
        G.add_edge(1, 2)
50
        G.add_edge(2, 1)
51
        assert_equal(nx.global_reaching_centrality(G, weight=None), 0)
52

    
53
    def test_cycle_undirected_unweighted(self):
54
        G = nx.Graph()
55
        G.add_edge(1, 2)
56
        assert_equal(nx.global_reaching_centrality(G, weight=None), 0)
57

    
58
    def test_cycle_directed_weighted(self):
59
        G = nx.DiGraph()
60
        G.add_weighted_edges_from([(1, 2, 1), (2, 1, 1)])
61
        assert_equal(nx.global_reaching_centrality(G), 0)
62

    
63
    def test_cycle_undirected_weighted(self):
64
        G = nx.Graph()
65
        G.add_edge(1, 2, weight=1)
66
        grc = nx.global_reaching_centrality
67
        assert_equal(grc(G, normalized=False), 0)
68

    
69
    def test_directed_weighted(self):
70
        G = nx.DiGraph()
71
        G.add_edge("A", "B", weight=5)
72
        G.add_edge("B", "C", weight=1)
73
        G.add_edge("B", "D", weight=0.25)
74
        G.add_edge("D", "E", weight=1)
75

    
76
        denom = len(G) - 1
77
        A_local = sum([5, 3, 2.625, 2.0833333333333]) / denom
78
        B_local = sum([1, 0.25, 0.625]) / denom
79
        C_local = 0
80
        D_local = sum([1]) / denom
81
        E_local = 0
82

    
83
        local_reach_ctrs = [A_local, C_local, B_local, D_local, E_local]
84
        max_local = max(local_reach_ctrs)
85
        expected = sum(max_local - lrc for lrc in local_reach_ctrs) / denom
86
        grc = nx.global_reaching_centrality
87
        actual = grc(G, normalized=False, weight='weight')
88
        assert_almost_equal(expected, actual, places=7)
89

    
90

    
91
class TestLocalReachingCentrality(TestCase):
92
    """Unit tests for the local reaching centrality function."""
93

    
94
    @raises(nx.NetworkXError)
95
    def test_non_positive_weights(self):
96
        G = nx.DiGraph()
97
        G.add_weighted_edges_from([(0, 1, 0)])
98
        nx.local_reaching_centrality(G, 0, weight='weight')
99

    
100
    @raises(nx.NetworkXError)
101
    def test_negatively_weighted(self):
102
        G = nx.Graph()
103
        G.add_weighted_edges_from([(0, 1, -2), (1, 2, +1)])
104
        nx.local_reaching_centrality(G, 0, weight='weight')
105

    
106
    def test_undirected_unweighted_star(self):
107
        G = nx.star_graph(2)
108
        grc = nx.local_reaching_centrality
109
        assert_equal(grc(G, 1, weight=None, normalized=False), 0.75)
110

    
111
    def test_undirected_weighted_star(self):
112
        G = nx.Graph()
113
        G.add_weighted_edges_from([(1, 2, 1), (1, 3, 2)])
114
        centrality = nx.local_reaching_centrality(G, 1, normalized=False, weight='weight')
115
        assert_equal(centrality, 1.5)