From 29a9e0fd9789a40f850a95ee31519b8b2b54c1a5 Mon Sep 17 00:00:00 2001 From: Agade09 Date: Mon, 10 Aug 2026 08:25:41 +0200 Subject: [PATCH 1/3] Enable using weights with spinglass. Fix typo 'Daefault'->'Default' --- cdlib/algorithms/crisp_partition.py | 47 ++++++++++++----------- cdlib/algorithms/overlapping_partition.py | 2 +- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/cdlib/algorithms/crisp_partition.py b/cdlib/algorithms/crisp_partition.py index 7b1d42f..96690c7 100644 --- a/cdlib/algorithms/crisp_partition.py +++ b/cdlib/algorithms/crisp_partition.py @@ -364,7 +364,7 @@ def gdmp2(g_original: object, min_threshold: float = 0.75) -> NodeClustering: ) -def spinglass(g_original: object, spins: int = 25) -> NodeClustering: +def spinglass(g_original: object, spins: int = 25, weights: object = None) -> NodeClustering: """ Spinglass relies on an analogy between a very popular statistical mechanic model called Potts spin glass, and the community structure. It applies the simulated annealing optimization technique on this model to optimize the modularity. @@ -375,11 +375,12 @@ def spinglass(g_original: object, spins: int = 25) -> NodeClustering: ========== ======== ======== Undirected Directed Weighted ========== ======== ======== - Yes No No + Yes No Yes ========== ======== ======== :param g_original: a networkx/igraph object :param spins: the number of spins to use. This is the upper limit for the number of communities. It is not a problem to supply a (reasonably) big number here, in which case some spin states will be unpopulated. + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None :return: NodeClustering object :Example: @@ -399,14 +400,14 @@ def spinglass(g_original: object, spins: int = 25) -> NodeClustering: ) g = convert_graph_formats(g_original, ig.Graph) - coms = g.community_spinglass(spins=spins) + coms = g.community_spinglass(spins=spins, weights=weights) communities = [] for c in coms: communities.append([g.vs[x]["name"] for x in c]) return NodeClustering( - communities, g_original, "Spinglass", method_parameters={"": ""} + communities, g_original, "Spinglass", method_parameters={"spins": spins, "weights": weights} ) @@ -606,9 +607,9 @@ def leiden( ========== ======== ======== :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None - :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Deafault 1 + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None + :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Default 1 :return: NodeClustering object :Example: @@ -690,8 +691,8 @@ def rb_pots( :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Default 1 :return: NodeClustering object @@ -770,10 +771,10 @@ def rber_pots( :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None - :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Deafault None - :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Deafault 1 + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None + :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Default None + :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Default 1 :return: NodeClustering object :Example: @@ -865,10 +866,10 @@ def cpm( :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None - :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Deafault None - :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Deafault 1 + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None + :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Default None + :param resolution_parameter: double >0 A parameter value controlling the coarseness of the clustering. Higher resolutions lead to more communities, while lower resolutions lead to fewer communities. Default 1 :return: NodeClustering object :Example: @@ -948,8 +949,8 @@ def significance_communities( :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Deafault None + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Default None :return: NodeClustering object :Example: @@ -1029,9 +1030,9 @@ def surprise_communities( ========== ======== ======== :param g_original: a networkx/igraph object - :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Deafault None - :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None - :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Deafault None + :param initial_membership: list of int Initial membership for the partition. If :obj:`None` then defaults to a singleton partition. Default None + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None + :param node_sizes: list of int, or vertex attribute Sizes of nodes are necessary to know the size of communities in aggregate graphs. Usually this is set to 1 for all nodes, but in specific cases this could be changed. Default None :return: NodeClustering object :Example: @@ -1098,7 +1099,7 @@ def greedy_modularity(g_original: object, weight: list = None) -> NodeClustering ========== ======== ======== :param g_original: a networkx/igraph object - :param weight: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Deafault None + :param weight: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None :return: NodeClustering object :Example: diff --git a/cdlib/algorithms/overlapping_partition.py b/cdlib/algorithms/overlapping_partition.py index 3b98cf2..aaa7850 100644 --- a/cdlib/algorithms/overlapping_partition.py +++ b/cdlib/algorithms/overlapping_partition.py @@ -161,7 +161,7 @@ def ego_networks(g_original: object, level: int = 1) -> NodeClustering: ========== ======== ======== :param g_original: a networkx/igraph object - :param level: extrac communities with all neighbors of distance<=level from a node. Deafault 1 + :param level: extrac communities with all neighbors of distance<=level from a node. Default 1 :return: NodeClustering object From 6334c1e10afe3d416e1739bf4f6ab2f47a1f99b5 Mon Sep 17 00:00:00 2001 From: Agade09 Date: Mon, 10 Aug 2026 11:11:24 +0200 Subject: [PATCH 2/3] Enable using weights with eigenvector --- cdlib/algorithms/crisp_partition.py | 9 +++++---- cdlib/test/test_community_discovery_models.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cdlib/algorithms/crisp_partition.py b/cdlib/algorithms/crisp_partition.py index 96690c7..86ab152 100644 --- a/cdlib/algorithms/crisp_partition.py +++ b/cdlib/algorithms/crisp_partition.py @@ -411,7 +411,7 @@ def spinglass(g_original: object, spins: int = 25, weights: object = None) -> No ) -def eigenvector(g_original: object) -> NodeClustering: +def eigenvector(g_original: object, weights: object = None) -> NodeClustering: """ Newman's leading eigenvector method for detecting community structure based on modularity. This is the proper internal of the recursive, divisive algorithm: each split is done by maximizing the modularity regarding the original network. @@ -422,10 +422,11 @@ def eigenvector(g_original: object) -> NodeClustering: ========== ======== ======== Undirected Directed Weighted ========== ======== ======== - Yes No No + Yes No Yes ========== ======== ======== :param g_original: a networkx/igraph object + :param weights: list of double, or edge attribute Weights of edges. Can be either an iterable or an edge attribute. Default None :return: NodeClustering object :Example: @@ -446,12 +447,12 @@ def eigenvector(g_original: object) -> NodeClustering: ) g = convert_graph_formats(g_original, ig.Graph) - coms = g.community_leading_eigenvector() + coms = g.community_leading_eigenvector(weights=weights) communities = [g.vs[x]["name"] for x in coms] return NodeClustering( - communities, g_original, "Eigenvector", method_parameters={"": ""} + communities, g_original, "Eigenvector", method_parameters={"weights": weights} ) diff --git a/cdlib/test/test_community_discovery_models.py b/cdlib/test/test_community_discovery_models.py index 9c19cda..b59f198 100644 --- a/cdlib/test/test_community_discovery_models.py +++ b/cdlib/test/test_community_discovery_models.py @@ -454,6 +454,17 @@ def test_eigenvector(self): self.assertEqual(type(com.communities[0]), list) self.assertEqual(type(com.communities[0][0]), str) + nx.set_edge_attributes(g, values=2, name="weight") + weight_list = [g[u][v]["weight"] for u, v in g.edges()] + for com in ( + algorithms.eigenvector(g, weights="weight"), + algorithms.eigenvector(g, weights=weight_list), + ): + self.assertEqual(type(com.communities), list) + if len(com.communities) > 0: + self.assertEqual(type(com.communities[0]), list) + self.assertEqual(type(com.communities[0][0]), str) + def test_Congo(self): g = get_string_graph() coms = algorithms.congo(g, number_communities=3, height=2) From 41ed8abf887a9d4e89a8b5575ea40e67c3a79080 Mon Sep 17 00:00:00 2001 From: Agade09 Date: Mon, 10 Aug 2026 11:13:21 +0200 Subject: [PATCH 3/3] Added missing test for spinglass weighted variant --- cdlib/test/test_community_discovery_models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cdlib/test/test_community_discovery_models.py b/cdlib/test/test_community_discovery_models.py index b59f198..b02f632 100644 --- a/cdlib/test/test_community_discovery_models.py +++ b/cdlib/test/test_community_discovery_models.py @@ -436,6 +436,17 @@ def test_spinglass(self): self.assertEqual(type(com.communities[0]), list) self.assertEqual(type(com.communities[0][0]), str) + nx.set_edge_attributes(g, values=2, name="weight") + weight_list = [g[u][v]["weight"] for u, v in g.edges()] + for com in ( + algorithms.spinglass(g, weights="weight"), + algorithms.spinglass(g, weights=weight_list), + ): + self.assertEqual(type(com.communities), list) + if len(com.communities) > 0: + self.assertEqual(type(com.communities[0]), list) + self.assertEqual(type(com.communities[0][0]), str) + def test_walktrap(self): if ig is not None: g = get_string_graph()