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 ability to generate ER hypergraphs without multiedges #596

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down
22 changes: 19 additions & 3 deletions tests/generators/test_uniform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest
from scipy.special import comb

import xgi
from xgi.exception import XGIError
Expand Down Expand Up @@ -150,14 +151,29 @@ def test_uniform_HPPM():
def test_uniform_erdos_renyi_hypergraph():
m = 2
n = 10
k = 2
H1 = xgi.uniform_erdos_renyi_hypergraph(n, m, k, seed=0)
p = 1
H1 = xgi.uniform_erdos_renyi_hypergraph(n, m, p, seed=0)
ne1 = H1.num_edges
H1.merge_duplicate_edges(rename="tuple")
ne2 = H1.num_edges
assert ne1 == ne2
assert ne1 == comb(n, m)

assert H1.num_nodes == 10
assert xgi.unique_edge_sizes(H1) == [2]

H2 = xgi.uniform_erdos_renyi_hypergraph(n, m, p, seed=0, multiedges=True)
print(H2.edges)
ne1 = H2.num_edges
H2.merge_duplicate_edges()
ne2 = H2.num_edges
assert ne1 != ne2
assert ne1 == n**m - n # remove loopy edges

# test that the seed works
H2 = xgi.uniform_erdos_renyi_hypergraph(n, m, k, seed=0)
p = 0.1
H1 = xgi.uniform_erdos_renyi_hypergraph(n, m, p, seed=0)
H2 = xgi.uniform_erdos_renyi_hypergraph(n, m, p, seed=0)

assert H1.edges.members(dtype=dict) == H2.edges.members(dtype=dict)

Expand Down
3 changes: 1 addition & 2 deletions xgi/core/dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,7 @@ def cleanup(self, isolates=False, relabel=True, in_place=True):

convert_labels_to_integers(_DH, in_place=True)

if not in_place:
return _DH
return _DH

def freeze(self):
"""Method for freezing a dihypergraph which prevents it from being modified
Expand Down
3 changes: 1 addition & 2 deletions xgi/core/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,7 @@ def cleanup(

convert_labels_to_integers(_H, in_place=True)

if not in_place:
return _H
return _H

def freeze(self):
"""Method for freezing a hypergraph which prevents it from being modified
Expand Down
3 changes: 1 addition & 2 deletions xgi/core/simplicialcomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,7 @@ def cleanup(self, isolates=False, connected=True, relabel=True, in_place=True):

convert_labels_to_integers(_S, in_place=True)

if not in_place:
return _S
return _S

def freeze(self):
"""Method for freezing a simplicial complex
Expand Down
86 changes: 75 additions & 11 deletions xgi/generators/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from functools import reduce

import numpy as np
from scipy.special import comb

from ..exception import XGIError
from .classic import empty_hypergraph
Expand Down Expand Up @@ -156,7 +157,7 @@ def uniform_HSBM(n, m, p, sizes, seed=None):
if len(set(np.shape(p))) != 1:
raise XGIError("'p' must be a square tensor.")
if np.max(p) > 1 or np.min(p) < 0:
raise XGIError("Entries of 'p' not in [0,1].")
raise XGIError("Entries of 'p' not in [0, 1].")
if np.sum(sizes) != n:
raise XGIError("Sum of sizes does not match n")

Expand Down Expand Up @@ -260,7 +261,7 @@ def uniform_HPPM(n, m, k, epsilon, rho=0.5, seed=None):
return uniform_HSBM(n, m, p, sizes, seed=seed)


def uniform_erdos_renyi_hypergraph(n, m, p, p_type="degree", seed=None):
def uniform_erdos_renyi_hypergraph(n, m, p, p_type="prob", multiedges=False, seed=None):
"""Generate an m-uniform Erdős–Rényi hypergraph

This creates a hypergraph with `n` nodes where
Expand All @@ -277,7 +278,16 @@ def uniform_erdos_renyi_hypergraph(n, m, p, p_type="degree", seed=None):
Mean expected degree if p_type="degree" and
probability of an m-hyperedge if p_type="prob"
p_type : str
"degree" or "prob", by default "degree"
"degree" or "prob", by default "prob"
multiedges : bool, optional
Whether or not to allow multiedges. If True, there
can be significant speedups but at the cost of creating
(potentially unwanted artifacts). When multiedges=True,
it treats each edge permutation as distinct, which can
lead to multiedges, especially for dense hypergraphs.
For sparse hypergraphs, however, this is unlikely to
be the case.
By default, False.
seed : integer or None (default)
The seed for the random number generator

Expand All @@ -286,7 +296,6 @@ def uniform_erdos_renyi_hypergraph(n, m, p, p_type="degree", seed=None):
Hypergraph
The Erdos Renyi hypergraph


See Also
--------
~xgi.generators.random.random_hypergraph
Expand All @@ -299,28 +308,42 @@ def uniform_erdos_renyi_hypergraph(n, m, p, p_type="degree", seed=None):
H.add_nodes_from(node_labels)

if p_type == "degree":
q = p / (m * n ** (m - 1)) # wiring probability
if multiedges:
q = p / (m * n ** (m - 1)) # wiring probability
else:
q = p * n / (m * comb(n, m))
elif p_type == "prob":
q = p
else:
raise XGIError("Invalid p_type!")

if q > 1 or q < 0:
raise XGIError("Probability not in [0,1].")
raise XGIError("Probability not in [0, 1].")

if multiedges:
max_index = n**m
index = np.random.geometric(q) - 1 # -1 b/c zero indexing
f = _index_to_edge_prod
else:
max_index = comb(n, m, exact=True)
index = np.random.geometric(q)
f = _index_to_edge_comb

index = np.random.geometric(q) - 1 # -1 b/c zero indexing
max_index = n**m
while index < max_index:
e = set(_index_to_edge(index, n, m))
while index <= max_index:
e = set(f(index, n, m))
if len(e) == m:
H.add_edge(e)
index += np.random.geometric(q)
return H


def _index_to_edge(index, n, m):
def _index_to_edge_prod(index, n, m):
"""Generate a hyperedge given an index in the list of possible edges.

In this method, it treats each edge permutation as distinct, which can
lead to multiedges, especially for dense hypergraphs.


Parameters
----------
index : int > 0
Expand All @@ -346,6 +369,47 @@ def _index_to_edge(index, n, m):
return [(index // (n**r) % n) for r in range(m - 1, -1, -1)]


def _index_to_edge_comb(index, n, m):
"""Generate a hyperedge given an index in the list of possible edges.

In this function, we prohibit multiedges, so each index corresponds to a
unique edge.

Parameters
----------
index : int > 0
The index of the hyperedge in the list of all possible hyperedges.
n : int > 0
The number of nodes
m : int > 0
The hyperedge size.

Returns
-------
list
The reconstructed hyperedge

See Also
--------
_index_to_edge_partition

References
----------
https://math.stackexchange.com/questions/1227409/indexing-all-combinations-without-making-list
"""
c = []
r = index
j = -1
for s in range(1, m + 1):
cs = j + 1
while r - comb(n - 1 - cs, m - s, exact=True) > 0:
r -= comb(n - 1 - cs, m - s, exact=True)
cs += 1
c.append(cs)
j = cs
return c


def _index_to_edge_partition(index, partition_sizes, m):
"""Generate a hyperedge given an index in the list of possible edges
and a partition of community labels.
Expand Down
Loading