Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centrality update #313

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/source/api/algorithms/xgi.algorithms.centrality.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ xgi.algorithms.centrality

.. autofunction:: CEC_centrality
.. autofunction:: HEC_centrality
.. autofunction:: ZEC_centrality
.. autofunction:: node_edge_centrality
.. autofunction:: line_vector_centrality
3 changes: 1 addition & 2 deletions docs/source/api/stats/xgi.stats.nodestats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
.. autofunction:: clustering
.. autofunction:: degree
.. autofunction:: hec_centrality
.. autofunction:: node_edge_centrality
.. autofunction:: zec_centrality
.. autofunction:: node_edge_centrality
1 change: 0 additions & 1 deletion tests/algorithms/test_assortativity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


def test_dynamical_assortativity(edgelist1, edgelist6, edgelist9, edgelist10):

H = xgi.Hypergraph()
with pytest.raises(XGIError):
xgi.dynamical_assortativity(H)
Expand Down
69 changes: 59 additions & 10 deletions tests/algorithms/test_centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@


def test_cec_centrality():
# test empty hypergraph
H = xgi.Hypergraph()
assert xgi.CEC_centrality(H) == dict()
# Test no edges
H.add_nodes_from([0, 1, 2])
assert xgi.CEC_centrality(H) == {0: np.NaN, 1: np.NaN, 2: np.NaN}
# test disconnected
H.add_edge([0, 1])
assert xgi.CEC_centrality(H) == {0: np.NaN, 1: np.NaN, 2: np.NaN}

H = xgi.sunflower(3, 1, 3)
c = H.nodes.cec_centrality.asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
Expand All @@ -20,6 +30,19 @@ def test_cec_centrality():

@pytest.mark.slow
def test_hec_centrality():
# test empty hypergraph
H = xgi.Hypergraph()
c = xgi.HEC_centrality(H)
assert c == dict()
# Test no edges
H.add_nodes_from([0, 1, 2])
c = xgi.HEC_centrality(H)
assert c == {0: np.NaN, 1: np.NaN, 2: np.NaN}
# test disconnected
H.add_edge([0, 1])
c = xgi.HEC_centrality(H)
assert c == {0: np.NaN, 1: np.NaN, 2: np.NaN}

H = xgi.sunflower(3, 1, 5)
c = H.nodes.hec_centrality(max_iter=1000).asnumpy()
assert norm(c[1:] - c[1]) < 1e-4
Expand All @@ -36,6 +59,19 @@ def test_hec_centrality():


def test_node_edge_centrality():
# test empty hypergraph
H = xgi.Hypergraph()
assert xgi.node_edge_centrality(H) == (dict(), dict())
# Test no edges
H.add_nodes_from([0, 1, 2])
assert xgi.node_edge_centrality(H) == ({0: np.NaN, 1: np.NaN, 2: np.NaN}, dict())
# test disconnected
H.add_edge([0, 1])
assert xgi.node_edge_centrality(H) == (
{0: np.NaN, 1: np.NaN, 2: np.NaN},
{0: np.NaN},
)

H = xgi.Hypergraph([[0, 1, 2, 3, 4]])
c = H.nodes.node_edge_centrality.asnumpy()
assert norm(c - c[0]) < 1e-6
Expand All @@ -48,6 +84,28 @@ def test_node_edge_centrality():
assert abs(c[0] - c[1]) < 1e-6


def test_line_vector_centrality():
H = xgi.Hypergraph()
c = xgi.line_vector_centrality(H)
assert c == dict()

with pytest.raises(XGIError):
H = xgi.Hypergraph()
H.add_nodes_from([0, 1, 2])
H.add_edge([0, 1])
xgi.line_vector_centrality(H)

H = xgi.sunflower(3, 1, 3) << xgi.sunflower(3, 1, 5)
c = xgi.line_vector_centrality(H)
assert len(c[0]) == 4 # sizes 2 through 5
assert np.allclose(c[0], [0, 0.40824829, 0, 0.24494897])
assert set(c.keys()) == set(H.nodes)

with pytest.raises(Exception):
H = xgi.Hypergraph([["a", "b"], ["b", "c"], ["a", "c"]])
xgi.line_vector_centrality(H)


def ratio(r, m, kind="CEC"):
"""Generate the ratio between largest and second largest centralities
for the sunflower hypergraph with one core node.
Expand All @@ -59,18 +117,13 @@ def ratio(r, m, kind="CEC"):
m : int
Size of edges
kind : str, default: "CEC"
"CEC", "HEC", or "ZEC"
"CEC" or "HEC"

Returns
-------
float
Ratio

Raises
------
XGIError
If edge size is too small for ZEC

References
----------
Three Hypergraph Eigenvector Centralities,
Expand All @@ -79,9 +132,5 @@ def ratio(r, m, kind="CEC"):
"""
if kind == "CEC":
return 2 * r * (m - 1) / (np.sqrt(m**2 + 4 * (m - 1) * (r - 1)) + m - 2)
elif kind == "ZEC":
if m > 4:
raise XGIError("Choose a larger m value.")
return r**0.5
elif kind == "HEC":
return r ** (1.0 / m)
2 changes: 0 additions & 2 deletions tests/classes/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


def test_num_edges_order(edgelist2):

H = xgi.Hypergraph(edgelist2)

assert xgi.num_edges_order(H, 0) == 0
Expand Down Expand Up @@ -723,7 +722,6 @@ def test_incidence_density(edgelist1):


def test_subfaces(edgelist5):

assert xgi.subfaces(edgelist5) == [
(0,),
(1,),
Expand Down
4 changes: 0 additions & 4 deletions tests/drawing/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


def test_random_layout():

H = xgi.random_hypergraph(10, [0.2], seed=1)

# seed
Expand All @@ -27,7 +26,6 @@ def test_random_layout():


def test_pairwise_spring_layout():

H = xgi.random_hypergraph(10, [0.2], seed=1)

# seed
Expand All @@ -47,7 +45,6 @@ def test_pairwise_spring_layout():


def test_barycenter_spring_layout(hypergraph1):

H = xgi.random_hypergraph(10, [0.2], seed=1)

# seed
Expand Down Expand Up @@ -83,7 +80,6 @@ def test_barycenter_spring_layout(hypergraph1):


def test_weighted_barycenter_spring_layout(hypergraph1):

H = xgi.random_hypergraph(10, [0.2], seed=1)

# seed
Expand Down
1 change: 0 additions & 1 deletion tests/dynamics/test_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def test_simulate_kuramoto():


def test_compute_kuramoto_order_parameter():

N = 5
n_steps = 10
dt = 0.002
Expand Down
1 change: 0 additions & 1 deletion tests/generators/test_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def test_flag_complex():


def test_flag_complex_d2():

G = nx.erdos_renyi_graph(15, 0.3, seed=3)

S = xgi.flag_complex(G, max_order=2)
Expand Down
6 changes: 0 additions & 6 deletions tests/generators/test_nonuniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


def test_chung_lu_hypergraph():

k1 = {1: 1, 2: 2, 3: 3, 4: 4}
k2 = {1: 2, 2: 2, 3: 3, 4: 3}
H = xgi.chung_lu_hypergraph(k1, k2)
Expand All @@ -27,7 +26,6 @@ def test_chung_lu_hypergraph():


def test_dcsbm_hypergraph():

n = 50
k1 = {i: random.randint(1, n) for i in range(n)}
k2 = {i: sorted(k1.values())[i] for i in range(n)}
Expand All @@ -48,7 +46,6 @@ def test_dcsbm_hypergraph():


def test_random_hypergraph():

# seed
H1 = xgi.random_hypergraph(10, [0.1, 0.001], seed=1)
H2 = xgi.random_hypergraph(10, [0.1, 0.001], seed=2)
Expand All @@ -65,7 +62,6 @@ def test_random_hypergraph():


def test_random_simplicial_complex():

# seed
S1 = xgi.random_simplicial_complex(10, [0.1, 0.001], seed=1)
S2 = xgi.random_simplicial_complex(10, [0.1, 0.001], seed=2)
Expand All @@ -82,7 +78,6 @@ def test_random_simplicial_complex():


def test_random_flag_complex():

# seed
S1 = xgi.random_flag_complex(10, 0.1, seed=1)
S2 = xgi.random_flag_complex(10, 0.1, seed=2)
Expand All @@ -99,7 +94,6 @@ def test_random_flag_complex():


def test_random_flag_complex_d2():

# seed
S1 = xgi.random_flag_complex_d2(10, 0.1, seed=1)
S2 = xgi.random_flag_complex_d2(10, 0.1, seed=2)
Expand Down
1 change: 0 additions & 1 deletion tests/generators/test_uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_uniform_configuration_model_hypergraph():


def test_uniform_HSBM():

# sum of sizes != n
with pytest.raises(XGIError):
m = 2
Expand Down
1 change: 0 additions & 1 deletion tests/readwrite/test_bipartite_edgelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
),
)
def test_read_bipartite_edgelist(file_string, extra_kwargs):

_, filename = tempfile.mkstemp()

with open(filename, "w") as file:
Expand Down
1 change: 0 additions & 1 deletion tests/readwrite/test_incidence_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
),
)
def test_read_incidence_matrix(file_string, extra_kwargs):

_, filename = tempfile.mkstemp()

with open(filename, "w") as file:
Expand Down
42 changes: 33 additions & 9 deletions tests/readwrite/test_xgi_data.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import tempfile
import time
from os.path import join

import pytest

from xgi import load_xgi_data
from xgi import download_xgi_data, load_xgi_data, read_json
from xgi.exception import XGIError


@pytest.mark.webtest
@pytest.mark.slow
def test_load_xgi_data():
H = load_xgi_data("email-enron", cache=False)
assert H.num_nodes == 148
assert H.num_edges == 10885
assert H["name"] == "email-Enron"
assert H.nodes["4"]["name"] == "[email protected]"
assert H.edges["0"]["timestamp"] == "2000-01-11T10:29:00"
# test loading the online data
H1 = load_xgi_data("email-enron", cache=False)
assert H1.num_nodes == 148
assert H1.num_edges == 10885
assert H1["name"] == "email-Enron"
assert H1.nodes["4"]["name"] == "[email protected]"
assert H1.edges["0"]["timestamp"] == "2000-01-11T10:29:00"

start = time.time()
H2 = load_xgi_data("email-enron", max_order=2, cache=True)
elapsed_time = time.time() - start

assert len(H2.edges.filterby("order", 2, mode="gt")) == 0
assert len(H.edges.filterby("order", 2, mode="gt")) == 1283
assert len(H1.edges.filterby("order", 2, mode="gt")) == 1283

start = time.time()
H2 = load_xgi_data("email-enron", max_order=2)
H3 = load_xgi_data("email-enron", max_order=2)
cached_elapsed_time = time.time() - start

assert cached_elapsed_time < elapsed_time
assert H2.edges.members() == H3.edges.members()

with pytest.raises(XGIError):
load_xgi_data("test")

# test the empty argument
assert load_xgi_data() is None

with pytest.warns(Warning):
load_xgi_data("email-enron", read=True)

dir = tempfile.mkdtemp()
download_xgi_data("email-enron", dir)
H4 = load_xgi_data("email-enron", read=True, path=dir)
assert H1.edges.members() == H4.edges.members()


def test_download_xgi_data():
dir = tempfile.mkdtemp()
download_xgi_data("email-enron", dir)
H = read_json(join(dir, "email-enron.json"))
H_online = load_xgi_data("email-enron")
assert H.edges.members() == H_online.edges.members()
1 change: 0 additions & 1 deletion tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def test_to_hyperedge_dict(edgelist1):


def test_from_bipartite_pandas_dataframe(dataframe5):

H1 = xgi.from_bipartite_pandas_dataframe(
dataframe5, node_column="col2", edge_column="col1"
)
Expand Down
2 changes: 0 additions & 2 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def test_dual_dict(dict5):


def test_powerset():

edge = [1, 2, 3, 4]

PS = xgi.powerset(edge)
Expand Down Expand Up @@ -47,7 +46,6 @@ def test_powerset():


def test_find_triangles():

G = nx.erdos_renyi_graph(20, 0.2, seed=0)
triangles = xgi.find_triangles(G)

Expand Down
3 changes: 0 additions & 3 deletions tutorials/case_study_zhang2022.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
"\n",
"# compute Lyapunov exponents for all alpha values\n",
"for j, HG in enumerate(HGs): # for all hypergraphs\n",
"\n",
" for i, alpha in enumerate(alphas):\n",
" lyap_1 = compute_eigenvalues(HG, order=1, weight=1 - alpha)\n",
" lyap_2 = compute_eigenvalues(HG, order=2, weight=alpha)\n",
Expand All @@ -191,7 +190,6 @@
"\n",
"# compute Lyapunov exponents for all alpha values\n",
"for j, MSC in enumerate(MSCs): # for all simplicial complexes\n",
"\n",
" for i, alpha in enumerate(alphas):\n",
" lyap_1 = compute_eigenvalues(MSC, order=1, weight=1 - alpha)\n",
" lyap_2 = compute_eigenvalues(MSC, order=2, weight=alpha)\n",
Expand Down Expand Up @@ -318,7 +316,6 @@
"lyaps_HG_2 = np.zeros((len(p_1s), len(alphas), N))\n",
"\n",
"for j, p_1 in enumerate(p_1s):\n",
"\n",
" ps = [\n",
" p_1,\n",
" p_2,\n",
Expand Down
Loading