comparison env/lib/python3.9/site-packages/networkx/algorithms/tests/test_structuralholes.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.algorithms.structuralholes` module."""
2 import math
3 import networkx as nx
4 from networkx.testing import almost_equal
5
6
7 class TestStructuralHoles:
8 """Unit tests for computing measures of structural holes.
9
10 The expected values for these functions were originally computed using the
11 proprietary software `UCINET`_ and the free software `IGraph`_ , and then
12 computed by hand to make sure that the results are correct.
13
14 .. _UCINET: https://sites.google.com/site/ucinetsoftware/home
15 .. _IGraph: http://igraph.org/
16
17 """
18
19 def setup(self):
20 self.D = nx.DiGraph()
21 self.D.add_edges_from([(0, 1), (0, 2), (1, 0), (2, 1)])
22 self.D_weights = {(0, 1): 2, (0, 2): 2, (1, 0): 1, (2, 1): 1}
23 # Example from http://www.analytictech.com/connections/v20(1)/holes.htm
24 self.G = nx.Graph()
25 self.G.add_edges_from(
26 [
27 ("A", "B"),
28 ("A", "F"),
29 ("A", "G"),
30 ("A", "E"),
31 ("E", "G"),
32 ("F", "G"),
33 ("B", "G"),
34 ("B", "D"),
35 ("D", "G"),
36 ("G", "C"),
37 ]
38 )
39 self.G_weights = {
40 ("A", "B"): 2,
41 ("A", "F"): 3,
42 ("A", "G"): 5,
43 ("A", "E"): 2,
44 ("E", "G"): 8,
45 ("F", "G"): 3,
46 ("B", "G"): 4,
47 ("B", "D"): 1,
48 ("D", "G"): 3,
49 ("G", "C"): 10,
50 }
51
52 def test_constraint_directed(self):
53 constraint = nx.constraint(self.D)
54 assert almost_equal(constraint[0], 1.003, places=3)
55 assert almost_equal(constraint[1], 1.003, places=3)
56 assert almost_equal(constraint[2], 1.389, places=3)
57
58 def test_effective_size_directed(self):
59 effective_size = nx.effective_size(self.D)
60 assert almost_equal(effective_size[0], 1.167, places=3)
61 assert almost_equal(effective_size[1], 1.167, places=3)
62 assert almost_equal(effective_size[2], 1, places=3)
63
64 def test_constraint_weighted_directed(self):
65 D = self.D.copy()
66 nx.set_edge_attributes(D, self.D_weights, "weight")
67 constraint = nx.constraint(D, weight="weight")
68 assert almost_equal(constraint[0], 0.840, places=3)
69 assert almost_equal(constraint[1], 1.143, places=3)
70 assert almost_equal(constraint[2], 1.378, places=3)
71
72 def test_effective_size_weighted_directed(self):
73 D = self.D.copy()
74 nx.set_edge_attributes(D, self.D_weights, "weight")
75 effective_size = nx.effective_size(D, weight="weight")
76 assert almost_equal(effective_size[0], 1.567, places=3)
77 assert almost_equal(effective_size[1], 1.083, places=3)
78 assert almost_equal(effective_size[2], 1, places=3)
79
80 def test_constraint_undirected(self):
81 constraint = nx.constraint(self.G)
82 assert almost_equal(constraint["G"], 0.400, places=3)
83 assert almost_equal(constraint["A"], 0.595, places=3)
84 assert almost_equal(constraint["C"], 1, places=3)
85
86 def test_effective_size_undirected_borgatti(self):
87 effective_size = nx.effective_size(self.G)
88 assert almost_equal(effective_size["G"], 4.67, places=2)
89 assert almost_equal(effective_size["A"], 2.50, places=2)
90 assert almost_equal(effective_size["C"], 1, places=2)
91
92 def test_effective_size_undirected(self):
93 G = self.G.copy()
94 nx.set_edge_attributes(G, 1, "weight")
95 effective_size = nx.effective_size(G, weight="weight")
96 assert almost_equal(effective_size["G"], 4.67, places=2)
97 assert almost_equal(effective_size["A"], 2.50, places=2)
98 assert almost_equal(effective_size["C"], 1, places=2)
99
100 def test_constraint_weighted_undirected(self):
101 G = self.G.copy()
102 nx.set_edge_attributes(G, self.G_weights, "weight")
103 constraint = nx.constraint(G, weight="weight")
104 assert almost_equal(constraint["G"], 0.299, places=3)
105 assert almost_equal(constraint["A"], 0.795, places=3)
106 assert almost_equal(constraint["C"], 1, places=3)
107
108 def test_effective_size_weighted_undirected(self):
109 G = self.G.copy()
110 nx.set_edge_attributes(G, self.G_weights, "weight")
111 effective_size = nx.effective_size(G, weight="weight")
112 assert almost_equal(effective_size["G"], 5.47, places=2)
113 assert almost_equal(effective_size["A"], 2.47, places=2)
114 assert almost_equal(effective_size["C"], 1, places=2)
115
116 def test_constraint_isolated(self):
117 G = self.G.copy()
118 G.add_node(1)
119 constraint = nx.constraint(G)
120 assert math.isnan(constraint[1])
121
122 def test_effective_size_isolated(self):
123 G = self.G.copy()
124 G.add_node(1)
125 nx.set_edge_attributes(G, self.G_weights, "weight")
126 effective_size = nx.effective_size(G, weight="weight")
127 assert math.isnan(effective_size[1])
128
129 def test_effective_size_borgatti_isolated(self):
130 G = self.G.copy()
131 G.add_node(1)
132 effective_size = nx.effective_size(G)
133 assert math.isnan(effective_size[1])