Skip to content

Commit

Permalink
Restructure folder for generators and linalg (#321)
Browse files Browse the repository at this point in the history
* refact: new folder structure for linalg

* refact: new folder structure for generators step 1g

* fix: imports

* fix: add missing files

* updated codecov params to avoid annoying warnings

* refact: moved more generators around

* refact: merged (non)uniform random_hypergraph() and remove nonuniform file

* fix: tests

* PR review comments

* minor adjustments

* minor adjustments

* docs: added missing documentation

* added the uniform Erdos-Renyi back in

---------

Co-authored-by: Nicholas Landry <[email protected]>
  • Loading branch information
maximelucas and nwlandry authored Apr 5, 2023
1 parent 7ee8aa2 commit 3435409
Show file tree
Hide file tree
Showing 34 changed files with 1,523 additions and 1,297 deletions.
6 changes: 2 additions & 4 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ coverage:
# basic
target: auto
threshold: 0.1
patch:
default:
# basic
target: 0
removed_code_behavior: removals_only # off, removals_only, adjust_base
patch: off
8 changes: 6 additions & 2 deletions docs/source/api/generators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ generators package
:toctree: generators

~xgi.generators.classic
~xgi.generators.nonuniform
~xgi.generators.uniform
~xgi.generators.simple
~xgi.generators.lattice
~xgi.generators.random
~xgi.generators.uniform
~xgi.generators.simplicial_complexes

7 changes: 1 addition & 6 deletions docs/source/api/generators/xgi.generators.classic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@
.. rubric:: Functions

.. autofunction:: empty_hypergraph
.. autofunction:: empty_simplicial_complex
.. autofunction:: flag_complex
.. autofunction:: flag_complex_d2
.. autofunction:: star_clique
.. autofunction:: sunflower
.. autofunction:: ring_lattice
.. autofunction:: empty_simplicial_complex
10 changes: 10 additions & 0 deletions docs/source/api/generators/xgi.generators.lattice.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
xgi.generators.lattice
======================

.. currentmodule:: xgi.generators.lattice

.. automodule:: xgi.generators.lattice

.. rubric:: Functions

.. autofunction:: ring_lattice
15 changes: 0 additions & 15 deletions docs/source/api/generators/xgi.generators.nonuniform.rst

This file was deleted.

13 changes: 13 additions & 0 deletions docs/source/api/generators/xgi.generators.random.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
xgi.generators.random
=====================

.. currentmodule:: xgi.generators.random

.. automodule:: xgi.generators.random

.. rubric:: Functions

.. autofunction:: chung_lu_hypergraph
.. autofunction:: dcsbm_hypergraph
.. autofunction:: random_hypergraph
.. autofunction:: watts_strogatz_hypergraph
11 changes: 11 additions & 0 deletions docs/source/api/generators/xgi.generators.simple.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
xgi.generators.simple
=====================

.. currentmodule:: xgi.generators.simple

.. automodule:: xgi.generators.simple

.. rubric:: Functions

.. autofunction:: star_clique
.. autofunction:: sunflower
14 changes: 14 additions & 0 deletions docs/source/api/generators/xgi.generators.simplicial_complexes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
xgi.generators.simplicial_complex
=================================

.. currentmodule:: xgi.generators.simplicial_complex

.. automodule:: xgi.generators.simplicial_complex

.. rubric:: Functions

.. autofunction:: flag_complex
.. autofunction:: flag_complex_d2
.. autofunction:: random_flag_complex
.. autofunction:: random_flag_complex_d2
.. autofunction:: random_simplicial_complex
4 changes: 3 additions & 1 deletion docs/source/api/linalg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ linalg package
.. autosummary::
:toctree: linalg

~xgi.linalg.matrix
~xgi.linalg.hypergraph_matrix
~xgi.linalg.laplacian_matrix
~xgi.linalg.hodge_matrix
11 changes: 11 additions & 0 deletions docs/source/api/linalg/xgi.linalg.hodge_matrix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
xgi.linalg.hodge_matrix
=======================

.. currentmodule:: xgi.linalg.hodge_matrix

.. automodule:: xgi.linalg.hodge_matrix

.. rubric:: Functions

.. autofunction:: boundary_matrix
.. autofunction:: hodge_laplacian
14 changes: 14 additions & 0 deletions docs/source/api/linalg/xgi.linalg.hypergraph_matrix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
xgi.linalg.hypergraph_matrix
============================

.. currentmodule:: xgi.linalg.hypergraph_matrix

.. automodule:: xgi.linalg.hypergraph_matrix

.. rubric:: Functions

.. autofunction:: adjacency_matrix
.. autofunction:: clique_motif_matrix
.. autofunction:: degree_matrix
.. autofunction:: incidence_matrix
.. autofunction:: intersection_profile
11 changes: 11 additions & 0 deletions docs/source/api/linalg/xgi.linalg.laplacian_matrix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
xgi.linalg.laplacian_matrix
===========================

.. currentmodule:: xgi.linalg.laplacian_matrix

.. automodule:: xgi.linalg.laplacian_matrix

.. rubric:: Functions

.. autofunction:: laplacian
.. autofunction:: normalized_hypergraph_laplacian
19 changes: 0 additions & 19 deletions docs/source/api/linalg/xgi.linalg.matrix.rst

This file was deleted.

111 changes: 0 additions & 111 deletions tests/generators/test_classic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import networkx as nx
import pytest

import xgi
from xgi.exception import XGIError


def test_empty_hypergraph():
Expand All @@ -13,110 +9,3 @@ def test_empty_hypergraph():
def test_empty_hypergraph():
SC = xgi.empty_simplicial_complex()
assert (SC.num_nodes, SC.num_edges) == (0, 0)


def test_star_clique():
with pytest.raises(ValueError):
H = xgi.star_clique(-1, 7, 3)
with pytest.raises(ValueError):
H = xgi.star_clique(6, -1, 3)
with pytest.raises(ValueError):
H = xgi.star_clique(6, 7, -1)
with pytest.raises(ValueError):
H = xgi.star_clique(6, 7, 7)

H = xgi.star_clique(6, 7, 3)
assert H.num_nodes == 13
assert H.num_edges == 97
assert xgi.max_edge_order(H) == 3


def test_flag_complex():
edges = [[0, 1], [1, 2], [2, 0], [0, 3]]
G = nx.Graph(edges)

S = xgi.flag_complex(G)

simplices_2 = [
frozenset({0, 1}),
frozenset({0, 2}),
frozenset({0, 3}),
frozenset({1, 2}),
]

simplices_3 = simplices_2 + [frozenset({0, 1, 2})]

assert S.edges.members() == simplices_3

S1 = xgi.flag_complex(G, ps=[1], seed=42)
S2 = xgi.flag_complex(G, ps=[0.5], seed=42)
S3 = xgi.flag_complex(G, ps=[0], seed=42)

assert S1.edges.members() == simplices_3
assert S2.edges.members() == simplices_2
assert S3.edges.members() == simplices_2

G1 = nx.complete_graph(4)
S4 = xgi.flag_complex(G1)
S5 = xgi.flag_complex(G1, ps=[1])
assert S4.num_nodes == S5.num_nodes
assert S4.num_edges == S5.num_edges
assert set(S4.edges.members()) == set(S5.edges.members())


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

S = xgi.flag_complex(G, max_order=2)
S2 = xgi.flag_complex_d2(G)

assert set(S.edges.members()) == set(S2.edges.members())


def test_ring_lattice():
H = xgi.ring_lattice(5, 2, 2, 0)
assert H.num_nodes == 5
assert H.num_edges == 5
assert xgi.unique_edge_sizes(H) == [2]

H = xgi.ring_lattice(5, 3, 4, 1)
edges = H.edges.members()
for i in range(H.num_edges - 1):
assert len(set(edges[i]).intersection(set(edges[i + 1]))) == 2 # d-l
assert xgi.unique_edge_sizes(H) == [3]

# k < 2 test
with pytest.warns(Warning):
H = xgi.ring_lattice(5, 2, 1, 0)
assert H.num_nodes == 5
assert H.num_edges == 0

# k % 2 != 0 test
with pytest.warns(Warning):
xgi.ring_lattice(5, 2, 3, 0)

# k < 0 test
with pytest.raises(XGIError):
xgi.ring_lattice(5, 2, -1, 0)


def test_sunflower():
with pytest.raises(XGIError):
H = xgi.sunflower(3, 4, 2)

H = xgi.sunflower(3, 1, 5)

assert H.nodes.memberships(0) == {0, 1, 2}
assert set(H.nodes) == set(range(13))
assert H.num_edges == 3
for n in range(1, H.num_nodes):
assert len(H.nodes.memberships(n)) == 1

H = xgi.sunflower(4, 3, 6)
for i in range(3):
H.nodes.memberships(i) == {0, 1, 2, 3}

assert H.num_nodes == 15

for i in range(3, 15):
assert len(H.nodes.memberships(i)) == 1
31 changes: 31 additions & 0 deletions tests/generators/test_lattice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

import xgi
from xgi.exception import XGIError


def test_ring_lattice():
H = xgi.ring_lattice(5, 2, 2, 0)
assert H.num_nodes == 5
assert H.num_edges == 5
assert xgi.unique_edge_sizes(H) == [2]

H = xgi.ring_lattice(5, 3, 4, 1)
edges = H.edges.members()
for i in range(H.num_edges - 1):
assert len(set(edges[i]).intersection(set(edges[i + 1]))) == 2 # d-l
assert xgi.unique_edge_sizes(H) == [3]

# k < 2 test
with pytest.warns(Warning):
H = xgi.ring_lattice(5, 2, 1, 0)
assert H.num_nodes == 5
assert H.num_edges == 0

# k % 2 != 0 test
with pytest.warns(Warning):
xgi.ring_lattice(5, 2, 3, 0)

# k < 0 test
with pytest.raises(XGIError):
xgi.ring_lattice(5, 2, -1, 0)
14 changes: 11 additions & 3 deletions tests/generators/test_nonuniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,27 @@ 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)
H3 = xgi.random_hypergraph(10, [0.1, 0.001], seed=2)
H1 = xgi.random_hypergraph(10, [0.1, 0.01], seed=1)
H2 = xgi.random_hypergraph(10, [0.1, 0.01], seed=2)
H3 = xgi.random_hypergraph(10, [0.1, 0.01], seed=2)

assert H1._edge != H2._edge
assert H2._edge == H3._edge

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

# wrong input
with pytest.raises(ValueError):
H1 = xgi.random_hypergraph(10, [1, 1.1])
with pytest.raises(ValueError):
H1 = xgi.random_hypergraph(10, [1, -2])

# uniform
H4 = xgi.random_hypergraph(10, [0.1], order=2, seed=1)
assert H4.num_nodes == 10
assert xgi.unique_edge_sizes(H4) == [3]


def test_random_simplicial_complex():
# seed
Expand Down
Loading

0 comments on commit 3435409

Please sign in to comment.