Skip to content

Commit

Permalink
⬆️ Louvain initial partition
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRossetti committed May 13, 2024
1 parent d46a542 commit 04d3f0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cdlib/algorithms/crisp_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def agdl(g_original: object, number_communities: int, kc: int) -> NodeClustering

def louvain(
g_original: object,
partition: None,
partition: NodeClustering = None,
weight: str = "weight",
resolution: float = 1.0,
randomize: int = None,
Expand Down Expand Up @@ -541,8 +541,19 @@ def louvain(

g = convert_graph_formats(g_original, nx.Graph)

if partition is not None:
communities = {}
for idc, com in enumerate(partition.communities):
for n in com:
communities[n] = idc
partition = communities

coms = community_louvain.best_partition(
g, partition=partition, weight=weight, resolution=resolution, randomize=randomize
g,
partition=partition,
weight=weight,
resolution=resolution,
randomize=randomize,
)

# Reshaping the results
Expand Down
6 changes: 6 additions & 0 deletions cdlib/test/test_community_discovery_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def test_louvain(self):
self.assertEqual(type(coms.communities[0]), list)
self.assertEqual(type(coms.communities[0][0]), str)

coms2 = algorithms.louvain(g, partition=coms)
self.assertEqual(type(coms2.communities), list)
if len(coms2.communities) > 0:
self.assertEqual(type(coms2.communities[0]), list)
self.assertEqual(type(coms2.communities[0][0]), str)

def test_leiden(self):
if leidenalg is not None:
g = get_string_graph()
Expand Down

0 comments on commit 04d3f0d

Please sign in to comment.