Skip to content

Commit

Permalink
Clearer parameters for graph_to_clique_complex
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Frantzen <[email protected]>
  • Loading branch information
rballeba and ffl096 committed Jul 8, 2024
1 parent 2a8572f commit a78cd6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions test/transform/test_graph_to_simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_graph_to_clique_complex(self):
assert sc[(0,)]["label"] == 5
assert sc[(0, 1)]["weight"] == 10

sc = graph_to_clique_complex(G, max_dim=2)
sc = graph_to_clique_complex(G, max_rank=1)

assert sc.dim == 1
assert (0, 2, 3) not in sc
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_graph_2_clique_complex(self):
assert (0, 1, 2) in sc

with pytest.deprecated_call():
sc = graph_2_clique_complex(G, max_dim=2)
sc = graph_2_clique_complex(G, max_rank=1)

assert sc.dim == 1
assert (0, 2, 3) not in sc
Expand Down
26 changes: 14 additions & 12 deletions toponetx/transform/graph_to_simplicial_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,39 @@ def graph_to_neighbor_complex(G: nx.Graph) -> SimplicialComplex:


def graph_to_clique_complex(
G: nx.Graph, max_dim: int | None = None
G: nx.Graph, max_rank: int | None = None
) -> SimplicialComplex:
"""Get the clique complex of a graph.
Parameters
----------
G : networks.Graph
Input graph.
max_dim : int, optional
The max dimension of the cliques in the output clique complex.
max_rank : int, optional
The maximum rank of the simplices in the output clique complex.
Returns
-------
SimplicialComplex
The clique simplicial complex of dimension dim of the graph G.
The clique simplicial complex of rank `max_rank` of the graph G.
"""
cliques = nx.enumerate_all_cliques(G)

# `nx.enumerate_all_cliques` returns cliques in ascending order of size. Abort calling the generator once we reach
# cliques larger than the requested max dimension.
cliques = takewhile(
lambda clique: max_dim is None or len(clique) <= max_dim, cliques
lambda clique: max_rank is None or len(clique) <= max_rank + 1, cliques
)

SC = SimplicialComplex(cliques)

# copy attributes of the input graph
for node in G.nodes:
SC[[node]].update(G.nodes[node])
for edge in G.edges:
SC[edge].update(G.edges[edge])
# if the resulting complex has dimension 1 or higher, copy the attributes of the edges
if SC.dim >= 1:
for edge in G.edges:
SC[edge].update(G.edges[edge])
SC.complex.update(G.graph)

return SC
Expand Down Expand Up @@ -101,23 +103,23 @@ def graph_2_neighbor_complex(G) -> SimplicialComplex:
"`graph_2_clique_complex` is deprecated and will be removed in a future version, use `graph_to_clique_complex` instead."
)
def graph_2_clique_complex(
G: nx.Graph, max_dim: int | None = None
G: nx.Graph, max_rank: int | None = None
) -> SimplicialComplex:
"""Get the clique complex of a graph.
Parameters
----------
G : networks.Graph
Input graph.
max_dim : int, optional
The max dimension of the cliques in the output clique complex.
max_rank : int, optional
The maximum rank of the simplices in the output clique complex.
Returns
-------
SimplicialComplex
The clique simplicial complex of dimension dim of the graph G.
The clique simplicial complex of rank `max_rank` of the graph `G`.
"""
return graph_to_clique_complex(G, max_dim)
return graph_to_clique_complex(G, max_rank)


def weighted_graph_to_vietoris_rips_complex(
Expand Down

0 comments on commit a78cd6a

Please sign in to comment.