From a78cd6a6c64acd6bcf13c7885ee37471d76abfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Ballester=20Bautista?= Date: Mon, 8 Jul 2024 10:16:36 +0200 Subject: [PATCH] Clearer parameters for `graph_to_clique_complex` Co-authored-by: Florian Frantzen <2105496+ffl096@users.noreply.github.com> --- .../test_graph_to_simplicial_complex.py | 4 +-- .../transform/graph_to_simplicial_complex.py | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/test/transform/test_graph_to_simplicial_complex.py b/test/transform/test_graph_to_simplicial_complex.py index 79ea938e..68be9ff4 100644 --- a/test/transform/test_graph_to_simplicial_complex.py +++ b/test/transform/test_graph_to_simplicial_complex.py @@ -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 @@ -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 diff --git a/toponetx/transform/graph_to_simplicial_complex.py b/toponetx/transform/graph_to_simplicial_complex.py index 90826ae2..2726413a 100644 --- a/toponetx/transform/graph_to_simplicial_complex.py +++ b/toponetx/transform/graph_to_simplicial_complex.py @@ -37,7 +37,7 @@ 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. @@ -45,20 +45,20 @@ def graph_to_clique_complex( ---------- 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) @@ -66,8 +66,10 @@ def graph_to_clique_complex( # 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 @@ -101,7 +103,7 @@ 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. @@ -109,15 +111,15 @@ def graph_2_clique_complex( ---------- 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(