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
update
  • Loading branch information
nwlandry committed Jun 14, 2022
commit 0cfd033381326dbe054ecd3cb31ea7e9560d89b0
65 changes: 36 additions & 29 deletions xgi/algorithms/assortativity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

__all__ = [
"dynamical_assortativity",
"top_2_assortativity",
"top_bottom_assortativity",
"uniform_assortativity",
"degree_assortativity"
]

def dynamical_assortativity(H):
Expand All @@ -32,35 +30,44 @@ def dynamical_assortativity(H):
if not xgi.is_uniform(H):
raise XGIError("Hypergraph must be uniform!")

k1 = np.mean([H.degree(n) for n in H.nodes])
k2 = np.mean([H.degree(n)**2 for n in H.nodes])

kk1 = np.mean([H.degree(n1)*H.degree(n2) for e in H.edges for n1, n2 in combinations(H.edges.members(e), 2)])
degs = H.degree()
k1 = np.mean([degs[n] for n in H.nodes])
k2 = np.mean([degs[n]**2 for n in H.nodes])
kk1 = np.mean([degs[n1]*degs[n2] for e in H.edges for n1, n2 in combinations(H.edges.members(e), 2)])

return kk1*k1**2/k2**2 - 1


def top_2_assortativity(H):
degrees = dict(H.degree())
k1 = [max((degrees[n] for n in H.edges.members(e))) for e in H.edges]
try:
k2 = [sorted((degrees[n] for n in H.edges.members(e)))[-2] for e in H.edges]
except IndexError:
raise XGIError("Hypergraph must not have singleton edges.")
return np.corrcoef(k1, k2)[0, 1]


def top_bottom_assortativity(H):
degrees = dict(H.degree())
k1 = [max((degrees[n] for n in H.edges.members(e))) for e in H.edges]
k2 = [min((degrees[n] for n in H.edges.members(e))) for e in H.edges]
return np.corrcoef(k1, k2)[0, 1]
def degree_assortativity(H, type="uniform", exact=True, num_samples=1000):
k = dict(H.degree())
if exact:
k1k2 = np.array([choose_nodes(e, k) for e in H.edges if len(H.edges.members(e)) > 1])
else:
# edges = list(H.edges)
# k1k2 = np.array([choose_nodes(e, k) for H.edges[random.randint(H.num_edges)] in range(num_samples) if len(H.edges.members(e)) > 1])
raise XGIError("not done yet")

return np.corrcoef(k1k2)[0, 1]

def uniform_assortativity(H):
degrees = dict(H.degree())
try:
k1k2 = [[degrees[n] for n in random.sample(H.edges.members(e), 2)] for e in H.edges]
except ValueError:
raise XGIError("Hypergraph must not have singleton edges.")

return np.corrcoef(list(zip(*k1k2)))[0, 1]
def choose_nodes(e, k, type="uniform"):
if type == "uniform":
i = np.random.randint(len(e))
j = i
while i == j:
j = np.random.randint(len(e))
return (np.array([k[e[i]], k[e[j]]]))

elif type == "":
degs = sorted([k[i] for i in e])[-2:]
random.shuffle(degs)
return (degs)

elif type == "":
degs = sorted([k[i] for i in e])[::len(e)-1]
random.shuffle(degs)
return (degs)

else:
raise XGIError("Invalid choice function!")