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

Fix random hypergraph #597

Merged
merged 51 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
c833190
Add ability to generate ER hypergraphs without multiedges
nwlandry Sep 16, 2024
47bfd49
update
nwlandry Sep 16, 2024
eb70415
Remove bad characters
nwlandry Sep 16, 2024
28cbbf3
Fix #361
nwlandry Sep 16, 2024
407443c
Revert "Fix #361"
nwlandry Sep 16, 2024
3bbbb5b
Fix #361
nwlandry Sep 16, 2024
ff6f85e
Update test.yml
nwlandry Sep 20, 2024
ad081ee
fix non-multi-edge probability
nwlandry Sep 24, 2024
aecb125
formatting
nwlandry Sep 24, 2024
1c1e28b
Update random.py
nwlandry Oct 8, 2024
e6e2258
Update xgi/generators/uniform.py
nwlandry Oct 9, 2024
2532ace
Update using-xgi.rst
nwlandry Oct 9, 2024
4d57ab7
Response to review
nwlandry Oct 9, 2024
93c7e81
Update uniform.py
nwlandry Oct 9, 2024
6a314cf
Update xgi/generators/random.py
nwlandry Oct 10, 2024
40e7049
Update xgi/generators/random.py
nwlandry Oct 10, 2024
c9c4842
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
919834a
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
4e566d2
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
505f093
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
5a4bf69
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
e8e89b5
Update xgi/generators/uniform.py
nwlandry Oct 10, 2024
d2994fb
updates
nwlandry Oct 10, 2024
81bad23
Conditional API note (#595)
nwlandry Sep 16, 2024
70f954f
Remove bad characters
nwlandry Sep 16, 2024
b45b714
Update random.py
nwlandry Oct 8, 2024
670d59c
Update xgi/generators/random.py
nwlandry Oct 10, 2024
1ba031c
Update xgi/generators/random.py
nwlandry Oct 10, 2024
10d8a7c
response to review
nwlandry Oct 10, 2024
064b365
updates
nwlandry Oct 10, 2024
edb5ebd
Update random.py
nwlandry Oct 10, 2024
5e8bd4e
Merge branch 'fix-random-hypergraph' of https://github.com/xgi-org/xg…
nwlandry Oct 10, 2024
ae6b4aa
format
nwlandry Oct 10, 2024
bedf6dd
Update random.py
nwlandry Oct 10, 2024
859e156
Merge branch 'main' into fix-random-hypergraph
nwlandry Oct 10, 2024
5e56026
update docs
nwlandry Oct 10, 2024
ba740c2
Merge branch 'fix-random-hypergraph' of https://github.com/xgi-org/xg…
nwlandry Oct 11, 2024
25a93a6
add unit tests
nwlandry Oct 11, 2024
21a6139
Fix test
nwlandry Oct 11, 2024
eb429b1
test different version
nwlandry Oct 11, 2024
bb1c7d2
fix test
nwlandry Oct 11, 2024
e1d34b5
updates
nwlandry Oct 11, 2024
84ef8b6
last additions
nwlandry Oct 11, 2024
383ab1e
update
nwlandry Oct 11, 2024
4a37e1a
fix error
nwlandry Oct 11, 2024
4893bb6
unnecessary import
nwlandry Oct 11, 2024
de56e5f
add unit tests
nwlandry Oct 15, 2024
390e997
Update xgi/generators/random.py
nwlandry Oct 16, 2024
57c1a29
Update xgi/generators/random.py
nwlandry Oct 16, 2024
756277f
Update xgi/generators/random.py
nwlandry Oct 16, 2024
36ab30a
Update xgi/generators/random.py
nwlandry Oct 16, 2024
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
2 changes: 1 addition & 1 deletion xgi/core/dihypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def add_edges_from(self, ebunch_to_add, **attr):
i.e. you cannot mix different formats. The iterables containing edge
members cannot be strings.

attr : \*\*kwargs, optional
**attr : kwargs, optional
Additional attributes to be assigned to all edges. Attribues specified via
`ebunch_to_add` take precedence over `attr`.

Expand Down
2 changes: 1 addition & 1 deletion xgi/core/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def add_edges_from(self, ebunch_to_add, **attr):
the same length, i.e. you cannot mix different formats. The iterables
containing edge members cannot be strings.

attr : \*\*kwargs:, optional
**attr : kwargs, optional
Additional attributes to be assigned to all edges. Attribues specified via
`ebunch_to_add` take precedence over `attr`.

Expand Down
2 changes: 1 addition & 1 deletion xgi/core/simplicialcomplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def add_simplices_from(self, ebunch_to_add, max_order=None, **attr):
Maximal dimension of simplices to add. If None (default), adds all
simplices. If int, and `ebunch_to_add` contains simplices of order >
`max_order`, creates and adds all its subfaces up to `max_order`.
attr : \*\*kwargs, optional
**attr : kwargs, optional
Additional attributes to be assigned to all simplices. Attribues specified
via `ebunch_to_add` take precedence over `attr`.

Expand Down
95 changes: 81 additions & 14 deletions xgi/generators/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import warnings
from collections import defaultdict
from itertools import combinations
from warnings import warn

import numpy as np
from scipy.special import comb

from .classic import empty_hypergraph
from .lattice import ring_lattice
from .uniform import uniform_erdos_renyi_hypergraph

__all__ = [
"random_hypergraph",
Expand All @@ -20,15 +21,15 @@
]


def random_hypergraph(N, ps, order=None, seed=None):
def fast_random_hypergraph(n, ps, order=None, seed=None):
"""Generates a random hypergraph

Generate N nodes, and connect any d+1 nodes
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
by a hyperedge with probability ps[d-1].

Parameters
----------
N : int
n : int
Number of nodes
ps : list of float
List of probabilities (between 0 and 1) to create a
Expand All @@ -50,24 +51,29 @@ def random_hypergraph(N, ps, order=None, seed=None):
----------
Described as 'random hypergraph' by M. Dewar et al. in https://arxiv.org/abs/1703.07686

See Also
--------
random_hypergraph

Example
-------
>>> import xgi
>>> H = xgi.random_hypergraph(50, [0.1, 0.01])
>>> H = xgi.fast_random_hypergraph(50, [0.1, 0.01])

"""
if seed is not None:
np.random.seed(seed)
ps = np.array(ps)

if order is not None:
if len(ps) != 1:
raise ValueError("ps must contain a single element if order is an int")

if (np.any(np.array(ps) < 0)) or (np.any(np.array(ps) > 1)):
if (ps < 0).any() or (ps > 1).any():
raise ValueError("All elements of ps must be between 0 and 1 included.")

nodes = range(N)
hyperedges = []
nodes = range(n)

H = empty_hypergraph()
H.add_nodes_from(nodes)

for i, p in enumerate(ps):

Expand All @@ -76,13 +82,74 @@ def random_hypergraph(N, ps, order=None, seed=None):
else:
d = i + 1 # order, ps[0] is prob of edges (d=1)

potential_edges = combinations(nodes, d + 1)
n_comb = comb(N, d + 1, exact=True)
mask = np.random.random(size=n_comb) <= p # True if edge to keep
size = d + 1
if p > 0:
h_uniform = uniform_erdos_renyi_hypergraph(n, size, p, seed=seed)
H << h_uniform
nwlandry marked this conversation as resolved.
Show resolved Hide resolved

return H


def random_hypergraph(N, ps, seed=None):
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
"""Generates a random hypergraph

Generate N nodes, and connect any d+1 nodes
by a hyperedge with probability ps[d-1].

Parameters
----------
N : int
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
Number of nodes
ps : list of float
List of probabilities (between 0 and 1) to create a
hyperedge at each order d between any d+1 nodes. For example,
nwlandry marked this conversation as resolved.
Show resolved Hide resolved
ps[0] is the wiring probability of any edge (2 nodes), ps[1]
of any triangles (3 nodes).
seed : integer, random_state, or None (default)
Indicator of random number generation state.

Returns
-------
Hypergraph object
The generated hypergraph

References
----------
Described as 'random hypergraph' by M. Dewar et al. in https://arxiv.org/abs/1703.07686

Warns
-----
warnings.warn
Because `fast_random_hypergraph` is a much faster method for generating random hypergraphs.

See Also
--------
fast_random_hypergraph

Example
-------
>>> import xgi
>>> H = xgi.random_hypergraph(50, [0.1, 0.01])

"""
warn("This method is much slower than fast_random_hypergraph")
if seed is not None:
random.seed(seed)

if (np.any(np.array(ps) < 0)) or (np.any(np.array(ps) > 1)):
raise ValueError("All elements of ps must be between 0 and 1 included.")

nodes = range(N) # list(G.nodes())
hyperedges = [] # hyperedges = list(G.edges())

for i, p in enumerate(ps):
d = i + 1 # order, ps[0] is prob of edges (d=1)

edges_to_add = [e for e, val in zip(potential_edges, mask) if val]
for hyperedge in combinations(nodes, d + 1):
if random.random() <= p:
hyperedges.append(hyperedge)

hyperedges += edges_to_add
hyperedges += [[i] for i in nodes] # add singleton edges
nwlandry marked this conversation as resolved.
Show resolved Hide resolved

H = empty_hypergraph()
H.add_nodes_from(nodes)
Expand Down
Loading