Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.08 KB)

1
# test_structuralholes.py - unit tests for the structuralholes module
2
#
3
# Copyright 2017 NetworkX developers.
4
#
5
# This file is part of NetworkX.
6
#
7
# NetworkX is distributed under a BSD license; see LICENSE.txt for more
8
# information.
9
"""Unit tests for the :mod:`networkx.algorithms.structuralholes` module."""
10
from nose.tools import assert_almost_equal, assert_true
11

    
12
import math
13
import networkx as nx
14

    
15

    
16
class TestStructuralHoles(object):
17
    """Unit tests for computing measures of structural holes.
18

19
    The expected values for these functions were originally computed using the
20
    proprietary software `UCINET`_ and the free software `IGraph`_ , and then
21
    computed by hand to make sure that the results are correct.
22

23
    .. _UCINET: https://sites.google.com/site/ucinetsoftware/home
24
    .. _IGraph: http://igraph.org/
25

26
    """
27

    
28
    def setup(self):
29
        self.D = nx.DiGraph()
30
        self.D.add_edges_from([(0, 1), (0, 2), (1, 0), (2, 1)])
31
        self.D_weights = {(0, 1): 2, (0, 2): 2, (1, 0): 1, (2, 1): 1}
32
        # Example from http://www.analytictech.com/connections/v20(1)/holes.htm
33
        self.G = nx.Graph()
34
        self.G.add_edges_from([
35
            ('A', 'B'), ('A', 'F'), ('A', 'G'), ('A', 'E'), ('E', 'G'),
36
            ('F', 'G'), ('B', 'G'), ('B', 'D'), ('D', 'G'), ('G', 'C'),
37
        ])
38
        self.G_weights = {
39
            ('A', 'B'): 2, ('A', 'F'): 3, ('A', 'G'): 5, ('A', 'E'): 2,
40
            ('E', 'G'): 8, ('F', 'G'): 3, ('B', 'G'): 4, ('B', 'D'): 1,
41
            ('D', 'G'): 3, ('G', 'C'): 10,
42
        }
43

    
44
    def test_constraint_directed(self):
45
        constraint = nx.constraint(self.D)
46
        assert_almost_equal(round(constraint[0], 3), 1.003)
47
        assert_almost_equal(round(constraint[1], 3), 1.003)
48
        assert_almost_equal(round(constraint[2], 3), 1.389)
49

    
50
    def test_effective_size_directed(self):
51
        effective_size = nx.effective_size(self.D)
52
        assert_almost_equal(round(effective_size[0], 3), 1.167)
53
        assert_almost_equal(round(effective_size[1], 3), 1.167)
54
        assert_almost_equal(round(effective_size[2], 3), 1)
55

    
56
    def test_constraint_weighted_directed(self):
57
        D = self.D.copy()
58
        nx.set_edge_attributes(D, self.D_weights, 'weight')
59
        constraint = nx.constraint(D, weight='weight')
60
        assert_almost_equal(round(constraint[0], 3), 0.840)
61
        assert_almost_equal(round(constraint[1], 3), 1.143)
62
        assert_almost_equal(round(constraint[2], 3), 1.378)
63

    
64
    def test_effective_size_weighted_directed(self):
65
        D = self.D.copy()
66
        nx.set_edge_attributes(D, self.D_weights, 'weight')
67
        effective_size = nx.effective_size(D, weight='weight')
68
        assert_almost_equal(round(effective_size[0], 3), 1.567)
69
        assert_almost_equal(round(effective_size[1], 3), 1.083)
70
        assert_almost_equal(round(effective_size[2], 3), 1)
71

    
72
    def test_constraint_undirected(self):
73
        constraint = nx.constraint(self.G)
74
        assert_almost_equal(round(constraint['G'], 3), 0.400)
75
        assert_almost_equal(round(constraint['A'], 3), 0.595)
76
        assert_almost_equal(round(constraint['C'], 3), 1)
77

    
78
    def test_effective_size_undirected_borgatti(self):
79
        effective_size = nx.effective_size(self.G)
80
        assert_almost_equal(round(effective_size['G'], 2), 4.67)
81
        assert_almost_equal(round(effective_size['A'], 2), 2.50)
82
        assert_almost_equal(round(effective_size['C'], 2), 1)
83

    
84
    def test_effective_size_undirected(self):
85
        G = self.G.copy()
86
        nx.set_edge_attributes(G, 1, 'weight')
87
        effective_size = nx.effective_size(G, weight='weight')
88
        assert_almost_equal(round(effective_size['G'], 2), 4.67)
89
        assert_almost_equal(round(effective_size['A'], 2), 2.50)
90
        assert_almost_equal(round(effective_size['C'], 2), 1)
91

    
92
    def test_constraint_weighted_undirected(self):
93
        G = self.G.copy()
94
        nx.set_edge_attributes(G, self.G_weights, 'weight')
95
        constraint = nx.constraint(G, weight='weight')
96
        assert_almost_equal(round(constraint['G'], 3), 0.299)
97
        assert_almost_equal(round(constraint['A'], 3), 0.795)
98
        assert_almost_equal(round(constraint['C'], 3), 1)
99

    
100
    def test_effective_size_weighted_undirected(self):
101
        G = self.G.copy()
102
        nx.set_edge_attributes(G, self.G_weights, 'weight')
103
        effective_size = nx.effective_size(G, weight='weight')
104
        assert_almost_equal(round(effective_size['G'], 2), 5.47)
105
        assert_almost_equal(round(effective_size['A'], 2), 2.47)
106
        assert_almost_equal(round(effective_size['C'], 2), 1)
107

    
108
    def test_constraint_isolated(self):
109
        G = self.G.copy()
110
        G.add_node(1)
111
        constraint = nx.constraint(G)
112
        assert_true(math.isnan(constraint[1]))
113

    
114
    def test_effective_size_isolated(self):
115
        G = self.G.copy()
116
        G.add_node(1)
117
        nx.set_edge_attributes(G, self.G_weights, 'weight')
118
        effective_size = nx.effective_size(G, weight='weight')
119
        assert_true(math.isnan(effective_size[1]))
120

    
121
    def test_effective_size_borgatti_isolated(self):
122
        G = self.G.copy()
123
        G.add_node(1)
124
        effective_size = nx.effective_size(G)
125
        assert_true(math.isnan(effective_size[1]))