Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen15 committed Sep 21, 2024
1 parent 3cb0a47 commit 0a41df3
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions graphiti_core/utils/maintenance/community_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ async def build_community_projection(driver: AsyncDriver) -> str:
return records[0]['graph']


async def destroy_projection(driver: AsyncDriver, projection_name: str):
await driver.execute_query(
"""
CALL gds.graph.drop($projection_name)
""",
projection_name=projection_name,
)


async def get_community_clusters(driver: AsyncDriver) -> list[list[EntityNode]]:
community_clusters: list[list[EntityNode]] = []

Expand Down Expand Up @@ -76,16 +67,24 @@ async def get_community_clusters(driver: AsyncDriver) -> list[list[EntityNode]]:

cluster_uuids = label_propagation(projection)

community_clusters = list(
await asyncio.gather(
*[EntityNode.get_by_uuids(driver, cluster) for cluster in cluster_uuids]
community_clusters.extend(
list(
await asyncio.gather(
*[EntityNode.get_by_uuids(driver, cluster) for cluster in cluster_uuids]
)
)
)

return community_clusters


def label_propagation(projection: dict[str, list[Neighbor]]) -> list[list[str]]:
# Implement the label propagation community detection algorithm.
# 1. Start with each node being assigned its own community
# 2. Each node will take on the community of the plurality of its neighbors
# 3. Ties are broken by going to the largest community
# 4. Continue until no communities change during propagation

community_map = {uuid: i for i, uuid in enumerate(projection.keys())}

while True:
Expand All @@ -95,7 +94,7 @@ def label_propagation(projection: dict[str, list[Neighbor]]) -> list[list[str]]:
for uuid, neighbors in projection.items():
curr_community = community_map[uuid]

community_candidates: dict[str, int] = defaultdict(int)
community_candidates: dict[int, int] = defaultdict(int)
for neighbor in neighbors:
community_candidates[community_map[neighbor.node_uuid]] += neighbor.edge_count

Expand Down

0 comments on commit 0a41df3

Please sign in to comment.