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

Add assortativity module #122

Merged
merged 19 commits into from
Jun 14, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests: add unit tests.
  • Loading branch information
nwlandry committed Jun 14, 2022
commit a82dce20ccfd8b9db1655dd85d85f2b85614fd17
60 changes: 60 additions & 0 deletions tests/algorithms/test_assortativity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import numpy as np
import pytest

import xgi
from xgi.algorithms.assortativity import choose_degrees
from xgi.exception import XGIError


def test_dynamical_assortativity(edgelist1, edgelist6):

H = xgi.Hypergraph()
with pytest.raises(XGIError):
xgi.dynamical_assortativity(H)

H.add_nodes_from([0, 1, 2])

with pytest.raises(XGIError):
xgi.dynamical_assortativity(H)

with pytest.raises(XGIError):
H1 = xgi.Hypergraph(edgelist1)
xgi.dynamical_assortativity(H1)

H1 = xgi.Hypergraph(edgelist6)

assert abs(xgi.dynamical_assortativity(H1) - -0.0526) < 1e-3


def test_degree_assortativity(edgelist1, edgelist6):
H1 = xgi.Hypergraph(edgelist1)
assert -1 <= xgi.degree_assortativity(H1, type="uniform") <= 1
assert -1 <= xgi.degree_assortativity(H1, type="top-2") <= 1
assert -1 <= xgi.degree_assortativity(H1, type="top-bottom") <= 1

H2 = xgi.Hypergraph(edgelist6)
assert -1 <= xgi.degree_assortativity(H2, type="uniform") <= 1
assert -1 <= xgi.degree_assortativity(H2, type="top-2") <= 1
assert -1 <= xgi.degree_assortativity(H2, type="top-bottom") <= 1


def test_choose_degrees(edgelist1, edgelist6):
H1 = xgi.Hypergraph(edgelist1)
k = H1.degree()

with pytest.raises(XGIError):
e = H1.edges.members(1)
choose_degrees(e, k)

e = H1.edges.members(0)
assert np.all(choose_degrees(e, k) == 1)

e = H1.edges.members(3)
assert set(choose_degrees(e, k, type="top-2")) == {1, 2}
assert set(choose_degrees(e, k, type="top-bottom")) == {1, 2}

H2 = xgi.Hypergraph(edgelist6)
e = H2.edges.members(2)
k = H2.degree()
assert set(choose_degrees(e, k, type="top-2")) == {2, 3}
assert set(choose_degrees(e, k, type="top-bottom")) == {1, 3}