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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
import warnings
import unittest
from igraph import Graph
class AtlasTestBase:
def testPageRank(self):
for idx, g in enumerate(self.__class__.graphs):
try:
pr = g.pagerank()
except Exception as ex:
self.assertTrue(
False,
msg="PageRank calculation threw exception for graph #%d: %s"
% (idx, ex),
)
raise
if g.vcount() == 0:
self.assertEqual([], pr)
continue
self.assertAlmostEqual(
1.0,
sum(pr),
places=5,
msg="PageRank sum is not 1.0 for graph #%d (%r)" % (idx, pr),
)
self.assertTrue(
min(pr) >= 0,
msg="Minimum PageRank is less than 0 for graph #%d (%r)" % (idx, pr),
)
def testEigenvectorCentrality(self):
# Temporarily turn off the warning handler because g.evcent() will print
# a warning for DAGs
warnings.simplefilter("ignore")
try:
for idx, g in enumerate(self.__class__.graphs):
try:
ec, eval = g.evcent(return_eigenvalue=True)
except Exception as ex:
self.assertTrue(
False,
msg="Eigenvector centrality threw exception for graph #%d: %s"
% (idx, ex),
)
raise
if g.vcount() == 0:
self.assertEqual([], ec)
continue
if not g.is_connected():
# Skip disconnected graphs; this will be fixed in igraph 0.7
continue
n = g.vcount()
if abs(eval) < 1e-4:
self.assertTrue(
min(ec) >= -1e-10,
msg="Minimum eigenvector centrality is smaller than 0 for graph #%d"
% idx,
)
self.assertTrue(
max(ec) <= 1,
msg="Maximum eigenvector centrality is greater than 1 for graph #%d"
% idx,
)
continue
self.assertAlmostEqual(
max(ec),
1,
places=7,
msg="Maximum eigenvector centrality is %r (not 1) for graph #%d (%r)"
% (max(ec), idx, ec),
)
self.assertTrue(
min(ec) >= 0,
msg="Minimum eigenvector centrality is less than 0 for graph #%d"
% idx,
)
ec2 = [sum(ec[u.index] for u in v.predecessors()) for v in g.vs]
for i in range(n):
self.assertAlmostEqual(
ec[i] * eval,
ec2[i],
places=7,
msg="Eigenvector centrality in graph #%d seems to be invalid "
"for vertex %d" % (idx, i),
)
finally:
# Reset the warning handler
warnings.resetwarnings()
def testHubScore(self):
for idx, g in enumerate(self.__class__.graphs):
try:
sc = g.hub_score()
except Exception as ex:
self.assertTrue(
False,
msg="Hub score calculation threw exception for graph #%d: %s"
% (idx, ex),
)
raise
if g.vcount() == 0:
self.assertEqual([], sc)
continue
self.assertAlmostEqual(
max(sc),
1,
places=7,
msg="Maximum authority score is not 1 for graph #%d" % idx,
)
self.assertTrue(
min(sc) >= 0, msg="Minimum hub score is less than 0 for graph #%d" % idx
)
def testAuthorityScore(self):
for idx, g in enumerate(self.__class__.graphs):
try:
sc = g.authority_score()
except Exception as ex:
self.assertTrue(
False,
msg="Authority score calculation threw exception for graph #%d: %s"
% (idx, ex),
)
raise
if g.vcount() == 0:
self.assertEqual([], sc)
continue
self.assertAlmostEqual(
max(sc),
1,
places=7,
msg="Maximum authority score is not 1 for graph #%d" % idx,
)
self.assertTrue(
min(sc) >= 0,
msg="Minimum authority score is less than 0 for graph #%d" % idx,
)
class GraphAtlasTests(unittest.TestCase, AtlasTestBase):
graphs = [Graph.Atlas(i) for i in range(1253)]
# Skip some problematic graphs
GraphAtlasTests.graphs = [
g for idx, g in enumerate(GraphAtlasTests.graphs) if idx not in {70, 180}
]
class IsoclassTests(unittest.TestCase, AtlasTestBase):
graphs = [Graph.Isoclass(3, i, directed=True) for i in range(16)] + [
Graph.Isoclass(4, i, directed=True) for i in range(218)
]
# Skip some problematic graphs
IsoclassTests.graphs = [
g for idx, g in enumerate(IsoclassTests.graphs) if idx not in {136}
]
def suite():
atlas_suite = unittest.defaultTestLoader.loadTestsFromTestCase(GraphAtlasTests)
isoclass_suite = unittest.defaultTestLoader.loadTestsFromTestCase(IsoclassTests)
return unittest.TestSuite([atlas_suite, isoclass_suite])
def test():
runner = unittest.TextTestRunner()
runner.run(suite())
if __name__ == "__main__":
test()
|