Skip to content

API Reference parrot.knowledge.graphindex.communities

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.knowledge.graphindex.communities

View source

Community detection for GraphIndex (FEAT-191, Leiden added FEAT-401).

Runs Leiden (default) or Louvain modularity-maximisation over the assembled rustworkx.PyDiGraph, computes per-community cohesion + global modularity, and writes a stable community_id onto every node's domain_tags so the assignment round-trips through :func:parrot.knowledge.graphindex.persist_arango._node_to_doc to ArangoDB with zero persist-layer changes.

Louvain runs via networkx (rustworkx 0.17 has no community detection). Leiden (FEAT-401) runs via the optional leidenalg + python-igraph dependencies (pip install ai-parrot[leiden]) and guarantees internally well-connected communities; when those packages are not importable, :func:detect_communities silently falls back to Louvain with a logged warning.

Optionally consumes :class:SignalRelevanceConfig (FEAT-190) to weight edges by signal_relevance(a, b).combined before detection runs, so community boundaries respect the signal model rather than raw edge counts. The FEAT-190 import is lazy — FEAT-191 ships standalone.

Class CommunitiesResult

Full partition + per-community metadata.

Fields:

Field Type Default Description
modularity float
resolution float
seed int
weighted bool
communities list[Community]
node_to_community dict[str, str]
algorithm str 'louvain'

Class Community

A single community in the partition.

Fields:

Field Type Default Description
community_id str
size int
member_node_ids list[str]
centroid_node_id str
cohesion float
modularity_contribution float
top_titles list[str]
label str ''

Class HierarchicalCommunitiesResult

Multi-resolution Leiden/Louvain partitions (FEAT-401).

Fields:

Field Type Default Description
resolutions list[float]
levels list[CommunitiesResult]

Alias SignalRelevanceConfig

Re-exported from parrot.knowledge.graphindex.signals.SignalRelevanceConfig.

Alias UniversalNode

Re-exported from parrot.knowledge.graphindex.schema.UniversalNode.

Function cohesion_for_community

internal_edges / (internal_edges + boundary_edges).

Returns 0.0 when the community has no incident edges (graph isolates yield singletons with zero cohesion by definition).

cohesion_for_community(nx_graph: nx.Graph, members: set[str]) -> float

Parameter Type Default Description
nx_graph nx.Graph
members set[str]

Returns: float

Function derive_community_label

Derive a deterministic, LLM-free label from member titles.

Counts salient keyword frequency across the supplied titles and joins the most common terms. Ties are broken alphabetically so the result is stable across runs. Returns an empty string when no salient keyword survives stopword filtering.

derive_community_label(titles: Iterable[str], max_terms: int = 3) -> str

Parameter Type Default Description
titles Iterable[str] Member node titles to summarise.
max_terms int 3 Maximum number of keywords to include in the label.

Returns: str — A capitalised label such as "Payment Gateway", or "".

Function detect_communities

Run community detection on the assembled graph (FEAT-401).

detect_communities(graph: rustworkx.PyDiGraph, nodes: list[UniversalNode], resolution: float = 1.0, seed: int = 42, signal_config: SignalRelevanceConfig | None = None, embedder: object | None = None, write_back_to_nodes: bool = True, algorithm: str = 'leiden') -> CommunitiesResult

Parameter Type Default Description
graph rustworkx.PyDiGraph The assembled PyDiGraph.
nodes list[UniversalNode] The UniversalNode list; mutated in-place when write_back_to_nodes=True.
resolution float 1.0 Resolution parameter (Louvain γ / Leiden resolution_parameter). >1.0 finds smaller (tighter) communities; <1.0 finds larger ones.
seed int 42 RNG seed for deterministic results across builds.
signal_config SignalRelevanceConfig | None None Optional FEAT-190 config; when set, edges are weighted by signal_relevance(a, b).combined before detection runs.
embedder object | None None Optional embedder forwarded to FEAT-190 when computing edge weights (ignored unless signal_config is set). Independently, the result's weighted flag is also True when the graph carries payload weights (edge domain_tags["weight"] via :meth:GraphAssembler.add_edge, FEAT-533 Module 0) even without a signal_config.
write_back_to_nodes bool True When True (default), writes domain_tags['community_id'] into every node and domain_tags['community_centroid']=True for each centroid.
algorithm str 'leiden' "leiden" (default) or "louvain". Leiden guarantees internally well-connected communities and requires the optional leidenalg + python-igraph dependencies (pip install ai-parrot[leiden]); when they are not importable, silently falls back to Louvain with a logged warning. "louvain" always uses the existing networkx code path (pre-FEAT-401 behaviour).

Returns: CommunitiesResult — class:CommunitiesResult with global modularity, the partition, the algorithm that actually ran, and a node_id → community_id lookup.

Function detect_hierarchical_communities

Run community detection at multiple resolutions (FEAT-401).

Uses Leiden's native recursive refinement (falling back to Louvain per-resolution when leidenalg is unavailable, same as :func:detect_communities) to expose hierarchical / multi-scale community structure.

detect_hierarchical_communities(graph: rustworkx.PyDiGraph, nodes: list[UniversalNode], resolutions: list[float] | None = None, seed: int = 42, signal_config: SignalRelevanceConfig | None = None, embedder: object | None = None) -> HierarchicalCommunitiesResult

Parameter Type Default Description
graph rustworkx.PyDiGraph The assembled PyDiGraph.
nodes list[UniversalNode] The UniversalNode list. Never mutated by this function — each resolution runs with write_back_to_nodes=False since a node cannot hold more than one community assignment per hierarchy level.
resolutions list[float] | None None Resolutions to sweep, ascending. Defaults to [0.25, 0.5, 1.0, 2.0, 4.0]; short-circuits to [1.0] when nodes has fewer than 20 entries. Explicit values are never short-circuited, even for tiny graphs.
seed int 42 RNG seed forwarded to every resolution's detection run.
signal_config SignalRelevanceConfig | None None Optional FEAT-190 config forwarded unchanged.
embedder object | None None Optional embedder forwarded unchanged.

Returns: HierarchicalCommunitiesResult — class:HierarchicalCommunitiesResult with one class:CommunitiesResult per resolution, sorted ascending.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally