File size: 3,965 Bytes
57db94b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import pytest
import networkx as nx
def test_richclub():
G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)])
rc = nx.richclub.rich_club_coefficient(G, normalized=False)
assert rc == {0: 12.0 / 30, 1: 8.0 / 12}
# test single value
rc0 = nx.richclub.rich_club_coefficient(G, normalized=False)[0]
assert rc0 == 12.0 / 30.0
def test_richclub_seed():
G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)])
rcNorm = nx.richclub.rich_club_coefficient(G, Q=2, seed=1)
assert rcNorm == {0: 1.0, 1: 1.0}
def test_richclub_normalized():
G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3), (1, 4), (4, 5)])
rcNorm = nx.richclub.rich_club_coefficient(G, Q=2, seed=42)
assert rcNorm == {0: 1.0, 1: 1.0}
def test_richclub2():
T = nx.balanced_tree(2, 10)
rc = nx.richclub.rich_club_coefficient(T, normalized=False)
assert rc == {
0: 4092 / (2047 * 2046.0),
1: (2044.0 / (1023 * 1022)),
2: (2040.0 / (1022 * 1021)),
}
def test_richclub3():
# tests edgecase
G = nx.karate_club_graph()
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {
0: 156.0 / 1122,
1: 154.0 / 1056,
2: 110.0 / 462,
3: 78.0 / 240,
4: 44.0 / 90,
5: 22.0 / 42,
6: 10.0 / 20,
7: 10.0 / 20,
8: 10.0 / 20,
9: 6.0 / 12,
10: 2.0 / 6,
11: 2.0 / 6,
12: 0.0,
13: 0.0,
14: 0.0,
15: 0.0,
}
def test_richclub4():
G = nx.Graph()
G.add_edges_from(
[(0, 1), (0, 2), (0, 3), (0, 4), (4, 5), (5, 9), (6, 9), (7, 9), (8, 9)]
)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {0: 18 / 90.0, 1: 6 / 12.0, 2: 0.0, 3: 0.0}
def test_richclub_exception():
with pytest.raises(nx.NetworkXNotImplemented):
G = nx.DiGraph()
nx.rich_club_coefficient(G)
def test_rich_club_exception2():
with pytest.raises(nx.NetworkXNotImplemented):
G = nx.MultiGraph()
nx.rich_club_coefficient(G)
def test_rich_club_selfloop():
G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
G.add_edge(1, 1) # self loop
G.add_edge(1, 2)
with pytest.raises(
Exception,
match="rich_club_coefficient is not implemented for " "graphs with self loops.",
):
nx.rich_club_coefficient(G)
def test_rich_club_leq_3_nodes_unnormalized():
# edgeless graphs upto 3 nodes
G = nx.Graph()
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {}
for i in range(3):
G.add_node(i)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {}
# 2 nodes, single edge
G = nx.Graph()
G.add_edge(0, 1)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {0: 1}
# 3 nodes, single edge
G = nx.Graph()
G.add_nodes_from([0, 1, 2])
G.add_edge(0, 1)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {0: 1}
# 3 nodes, 2 edges
G.add_edge(1, 2)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {0: 2 / 3}
# 3 nodes, 3 edges
G.add_edge(0, 2)
rc = nx.rich_club_coefficient(G, normalized=False)
assert rc == {0: 1, 1: 1}
def test_rich_club_leq_3_nodes_normalized():
G = nx.Graph()
with pytest.raises(
nx.exception.NetworkXError,
match="Graph has fewer than four nodes",
):
rc = nx.rich_club_coefficient(G, normalized=True)
for i in range(3):
G.add_node(i)
with pytest.raises(
nx.exception.NetworkXError,
match="Graph has fewer than four nodes",
):
rc = nx.rich_club_coefficient(G, normalized=True)
# def test_richclub2_normalized():
# T = nx.balanced_tree(2,10)
# rcNorm = nx.richclub.rich_club_coefficient(T,Q=2)
# assert_true(rcNorm[0] ==1.0 and rcNorm[1] < 0.9 and rcNorm[2] < 0.9)
|