chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import math
|
||||
import unittest
|
||||
|
||||
from itertools import combinations
|
||||
|
||||
import easygraph as eg
|
||||
import numpy as np
|
||||
|
||||
from easygraph.utils.exception import EasyGraphError
|
||||
|
||||
|
||||
class test_assortativity(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.g = eg.get_graph_karateclub()
|
||||
self.edges = [(8, 9), (1, 2), (8, 4), (3, 6), (1, 3), (6, 4)]
|
||||
self.hg = [
|
||||
eg.Hypergraph(num_v=10, e_list=self.edges, e_property=None),
|
||||
eg.Hypergraph(num_v=2, e_list=[(0, 1)]),
|
||||
]
|
||||
# Valid uniform hypergraph
|
||||
self.hg_uniform = eg.Hypergraph(
|
||||
num_v=5,
|
||||
e_list=[
|
||||
(0, 1, 2),
|
||||
(1, 2, 3),
|
||||
(2, 3, 4),
|
||||
],
|
||||
)
|
||||
|
||||
# Non-uniform hypergraph
|
||||
self.hg_non_uniform = eg.Hypergraph(
|
||||
num_v=4,
|
||||
e_list=[
|
||||
(0, 1),
|
||||
(2, 3, 0),
|
||||
],
|
||||
)
|
||||
|
||||
# Singleton edge hypergraph (still needs num_v > 0)
|
||||
self.hg_singleton = eg.Hypergraph(
|
||||
num_v=3,
|
||||
e_list=[
|
||||
(0,),
|
||||
(1, 2),
|
||||
],
|
||||
)
|
||||
|
||||
# "Empty" hypergraph (has 1 node but no edges)
|
||||
self.hg_empty = eg.Hypergraph(
|
||||
num_v=1,
|
||||
e_list=[],
|
||||
)
|
||||
|
||||
def test_dynamical_assortativity(self):
|
||||
for i in self.hg:
|
||||
degs = i.deg_v
|
||||
print(degs)
|
||||
k1 = sum(degs) / len(degs)
|
||||
print(k1)
|
||||
k2 = np.mean(np.array(degs) ** 2)
|
||||
print(k2)
|
||||
kk1 = np.mean(
|
||||
[degs[n1] * degs[n2] for e in i.e[0] for n1, n2 in combinations(e, 2)]
|
||||
)
|
||||
print(kk1)
|
||||
print(eg.dynamical_assortativity(i))
|
||||
print()
|
||||
|
||||
def test_degree_assortativity(self):
|
||||
for i in self.hg:
|
||||
print(eg.degree_assortativity(i))
|
||||
|
||||
def test_dynamical_assortativity_valid(self):
|
||||
result = eg.dynamical_assortativity(self.hg_uniform)
|
||||
self.assertIsInstance(result, float)
|
||||
|
||||
def test_dynamical_assortativity_raises_on_empty(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.dynamical_assortativity(self.hg_empty)
|
||||
|
||||
def test_dynamical_assortativity_raises_on_singleton(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.dynamical_assortativity(self.hg_singleton)
|
||||
|
||||
def test_dynamical_assortativity_raises_on_nonuniform(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.dynamical_assortativity(self.hg_non_uniform)
|
||||
|
||||
def test_degree_assortativity_raises_on_invalid_kind(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.degree_assortativity(self.hg_uniform, kind="invalid")
|
||||
|
||||
def test_degree_assortativity_raises_on_singleton(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.degree_assortativity(self.hg_singleton)
|
||||
|
||||
def test_degree_assortativity_raises_on_empty(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.degree_assortativity(self.hg_empty)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,24 @@
|
||||
import easygraph as eg
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def g1():
|
||||
e_list = [(0, 1, 2, 5), (0, 1), (2, 3, 4), (1, 2, 4)]
|
||||
g = eg.Hypergraph(6, e_list=e_list)
|
||||
return g
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def g2():
|
||||
e_list = [(1, 2, 3), (0, 1, 3), (0, 1), (2, 4, 3), (2, 3)]
|
||||
e_weight = [0.5, 1, 0.5, 1, 0.5]
|
||||
g = eg.Hypergraph(5, e_list=e_list, e_weight=e_weight)
|
||||
return g
|
||||
|
||||
|
||||
def test_degree_centrality(g1, g2):
|
||||
print(eg.hyepergraph_degree_centrality(g1))
|
||||
print(eg.hyepergraph_degree_centrality(g2))
|
||||
assert eg.hyepergraph_degree_centrality(g1) == {0: 2, 1: 3, 2: 3, 3: 1, 4: 2, 5: 1}
|
||||
assert eg.hyepergraph_degree_centrality(g2) == {0: 2, 1: 3, 2: 3, 3: 4, 4: 1}
|
||||
@@ -0,0 +1,89 @@
|
||||
import unittest
|
||||
|
||||
import easygraph as eg
|
||||
|
||||
from easygraph.utils.exception import EasyGraphError
|
||||
|
||||
|
||||
class test_hypergraph_operation(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.g = eg.get_graph_karateclub()
|
||||
self.edges = [(1, 2), (8, 4)]
|
||||
self.hg = [
|
||||
eg.Hypergraph(num_v=10, e_list=self.edges, e_property=None),
|
||||
eg.Hypergraph(num_v=2, e_list=[(0, 1)]),
|
||||
]
|
||||
|
||||
def test_hypergraph_clustering_coefficient(self):
|
||||
for i in self.hg:
|
||||
print(eg.hypergraph_clustering_coefficient(i))
|
||||
|
||||
def test_hypergraph_local_clustering_coefficient(self):
|
||||
for i in self.hg:
|
||||
print(eg.hypergraph_local_clustering_coefficient(i))
|
||||
|
||||
def test_hypergraph_two_node_clustering_coefficient(self):
|
||||
for i in self.hg:
|
||||
print(eg.hypergraph_two_node_clustering_coefficient(i))
|
||||
|
||||
|
||||
class TestHypergraphClustering(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
|
||||
self.hg = eg.Hypergraph(num_v=4, e_list=self.edges)
|
||||
|
||||
def test_hypergraph_clustering_coefficient_basic(self):
|
||||
cc = eg.hypergraph_clustering_coefficient(self.hg)
|
||||
self.assertIsInstance(cc, dict)
|
||||
for k, v in cc.items():
|
||||
self.assertIn(k, self.hg.v)
|
||||
self.assertGreaterEqual(v, 0)
|
||||
|
||||
def test_hypergraph_local_clustering_coefficient_basic(self):
|
||||
cc = eg.hypergraph_local_clustering_coefficient(self.hg)
|
||||
self.assertIsInstance(cc, dict)
|
||||
for k, v in cc.items():
|
||||
self.assertIn(k, self.hg.v)
|
||||
self.assertGreaterEqual(v, 0)
|
||||
|
||||
def test_hypergraph_two_node_clustering_union(self):
|
||||
cc = eg.hypergraph_two_node_clustering_coefficient(self.hg, kind="union")
|
||||
self.assertIsInstance(cc, dict)
|
||||
|
||||
def test_hypergraph_two_node_clustering_min(self):
|
||||
cc = eg.hypergraph_two_node_clustering_coefficient(self.hg, kind="min")
|
||||
self.assertIsInstance(cc, dict)
|
||||
|
||||
def test_hypergraph_two_node_clustering_max(self):
|
||||
cc = eg.hypergraph_two_node_clustering_coefficient(self.hg, kind="max")
|
||||
self.assertIsInstance(cc, dict)
|
||||
|
||||
def test_hypergraph_two_node_clustering_invalid_kind(self):
|
||||
with self.assertRaises(EasyGraphError):
|
||||
eg.hypergraph_two_node_clustering_coefficient(self.hg, kind="invalid")
|
||||
|
||||
def test_single_edge(self):
|
||||
hg = eg.Hypergraph(num_v=2, e_list=[(0, 1)])
|
||||
cc = eg.hypergraph_clustering_coefficient(hg)
|
||||
self.assertTrue(all(k in cc for k in hg.v))
|
||||
|
||||
def test_disconnected_nodes(self):
|
||||
hg = eg.Hypergraph(num_v=4, e_list=[(0, 1)])
|
||||
cc = eg.hypergraph_clustering_coefficient(hg)
|
||||
for v in [2, 3]:
|
||||
self.assertEqual(cc[v], 0)
|
||||
|
||||
def test_fully_connected_hyperedge(self):
|
||||
hg = eg.Hypergraph(num_v=3, e_list=[(0, 1, 2)])
|
||||
cc = eg.hypergraph_clustering_coefficient(hg)
|
||||
for v in cc.values():
|
||||
self.assertEqual(v, 1.0)
|
||||
|
||||
def test_nan_safety_in_two_node_coefficient(self):
|
||||
hg = eg.Hypergraph(num_v=1, e_list=[(0,)])
|
||||
result = eg.hypergraph_two_node_clustering_coefficient(hg)
|
||||
self.assertEqual(result[0], 0.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,72 @@
|
||||
import math
|
||||
import unittest
|
||||
|
||||
import easygraph as eg
|
||||
|
||||
from easygraph.utils.exception import EasyGraphError
|
||||
|
||||
|
||||
class test_hypergraph_operation(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.g = eg.get_graph_karateclub()
|
||||
self.edges = [(1, 2), (8, 4)]
|
||||
self.hg = [
|
||||
eg.Hypergraph(num_v=10, e_list=self.edges, e_property=None),
|
||||
eg.Hypergraph(num_v=2, e_list=[(0, 1)]),
|
||||
]
|
||||
# checked -- num_v cannot be set to negative number
|
||||
|
||||
def test_hypergraph_operation(self):
|
||||
for i in self.hg:
|
||||
print(eg.hypergraph_density(i))
|
||||
i.draw(v_color="#e6928f", e_color="#4e9595")
|
||||
|
||||
def test_basic_density(self):
|
||||
hg = eg.Hypergraph(num_v=3, e_list=[(0, 1), (1, 2)])
|
||||
expected = 2 / (2**3 - 1)
|
||||
self.assertAlmostEqual(eg.hypergraph_density(hg), expected)
|
||||
|
||||
def test_density_ignore_singletons(self):
|
||||
hg = eg.Hypergraph(num_v=3, e_list=[(0,), (1, 2)])
|
||||
expected = 2 / ((2**3 - 1) - 3)
|
||||
self.assertAlmostEqual(
|
||||
eg.hypergraph_density(hg, ignore_singletons=True), expected
|
||||
)
|
||||
|
||||
def test_density_all_singletons(self):
|
||||
hg = eg.Hypergraph(num_v=3, e_list=[(0,), (1,), (2,)])
|
||||
expected = 3 / (2**3 - 1)
|
||||
self.assertAlmostEqual(eg.hypergraph_density(hg), expected)
|
||||
expected_ignoring = 3 / ((2**3 - 1) - 3)
|
||||
self.assertAlmostEqual(
|
||||
eg.hypergraph_density(hg, ignore_singletons=True), expected_ignoring
|
||||
)
|
||||
|
||||
def test_no_edges_returns_zero(self):
|
||||
hg = eg.Hypergraph(num_v=5, e_list=[])
|
||||
self.assertEqual(eg.hypergraph_density(hg), 0.0)
|
||||
|
||||
def test_single_node_single_edge(self):
|
||||
hg = eg.Hypergraph(num_v=1, e_list=[(0,)])
|
||||
self.assertEqual(eg.hypergraph_density(hg), 1.0)
|
||||
|
||||
def test_density_max_possible_edges(self):
|
||||
n = 4
|
||||
from itertools import chain
|
||||
from itertools import combinations
|
||||
|
||||
powerset = list(
|
||||
chain.from_iterable(combinations(range(n), r) for r in range(1, n + 1))
|
||||
)
|
||||
hg = eg.Hypergraph(num_v=n, e_list=powerset)
|
||||
self.assertAlmostEqual(eg.hypergraph_density(hg), 1.0)
|
||||
|
||||
def test_density_zero_division_guard(self):
|
||||
# Singleton ignored in n=1 graph should not divide by zero
|
||||
hg = eg.Hypergraph(num_v=1, e_list=[(0,)])
|
||||
result = eg.hypergraph_density(hg, ignore_singletons=True)
|
||||
self.assertEqual(result, 0.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user