Skip to content

Commit

Permalink
More tests for layouts (#296)
Browse files Browse the repository at this point in the history
* tests: more for layouts. fix: keep isolated nodes when converting maximal SC to HG

* feat: added conversion to max SC in random layout
  • Loading branch information
maximelucas authored Mar 15, 2023
1 parent e37c299 commit e36b9d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
40 changes: 38 additions & 2 deletions tests/drawing/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_random_layout():

assert len(pos1) == H.num_nodes

# simplicial complex
S = xgi.random_flag_complex_d2(10, 0.2)
pos = xgi.random_layout(S)
assert len(pos) == S.num_nodes


def test_pairwise_spring_layout():

Expand All @@ -37,8 +42,12 @@ def test_pairwise_spring_layout():

assert len(pos1) == H.num_nodes

# simplicial complex
S = xgi.random_flag_complex_d2(10, 0.2, seed=1)
pos = xgi.pairwise_spring_layout(S, seed=1)


def test_barycenter_spring_layout():
def test_barycenter_spring_layout(hypergraph1):

H = xgi.random_hypergraph(10, [0.2], seed=1)

Expand All @@ -59,8 +68,21 @@ def test_barycenter_spring_layout():
assert pos4.keys() == pos5.keys()
assert np.allclose(list(pos4.values()), list(pos5.values()))

# simplicial complex
S = xgi.random_flag_complex_d2(10, 0.2)
pos = xgi.barycenter_spring_layout(S)
assert len(pos) == S.num_nodes

# str nodes
pos = xgi.barycenter_spring_layout(hypergraph1)
assert len(pos) == hypergraph1.num_nodes

# larger hyperedges
H = xgi.random_hypergraph(10, [0.2, 0.1])
pos = xgi.barycenter_spring_layout(H)
assert len(pos) == H.num_nodes

def test_weighted_barycenter_spring_layout():
def test_weighted_barycenter_spring_layout(hypergraph1):

H = xgi.random_hypergraph(10, [0.2], seed=1)

Expand All @@ -82,3 +104,17 @@ def test_weighted_barycenter_spring_layout():
pos5 = xgi.weighted_barycenter_spring_layout(H, return_phantom_graph=False, seed=1)
assert pos4.keys() == pos5.keys()
assert np.allclose(list(pos4.values()), list(pos5.values()))

# simplicial complex
S = xgi.random_flag_complex_d2(10, 0.2)
pos = xgi.weighted_barycenter_spring_layout(S)
assert len(pos) == S.num_nodes

# str nodes
pos = xgi.weighted_barycenter_spring_layout(hypergraph1)
assert len(pos) == hypergraph1.num_nodes

# larger hyperedges
H = xgi.random_hypergraph(10, [0.2, 0.1])
pos = xgi.weighted_barycenter_spring_layout(H)
assert len(pos) == H.num_nodes
3 changes: 2 additions & 1 deletion xgi/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def from_incidence_matrix(d, create_using=None, nodelabels=None, edgelabels=None

def from_simplicial_complex_to_hypergraph(SC):
"""Returns a hypergraph constructed from the
maximal simpices of the provided simplicial complex.
maximal simplices of the provided simplicial complex.
Parameters
----------
Expand All @@ -460,6 +460,7 @@ def from_simplicial_complex_to_hypergraph(SC):

max_simplices = maximal_simplices(SC)
H = Hypergraph()
H.add_nodes_from(SC.nodes) # to keep node order and isolated nodes
H.add_edges_from([list(SC.edges.members(e)) for e in max_simplices])
return H

Expand Down
3 changes: 3 additions & 0 deletions xgi/drawing/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def random_layout(H, center=None, dim=2, seed=None):
"""
import numpy as np

if isinstance(H, SimplicialComplex):
H = convert.from_simplicial_complex_to_hypergraph(H)

if seed is not None:
np.random.seed(seed)

Expand Down

0 comments on commit e36b9d8

Please sign in to comment.