Louvain fallback hangs on large sparse graphs — expose max_level/threshold params
Problem
_partition() in cluster.py calls nx.community.louvain_communities(G, seed=42) with no max_level or threshold arguments (line 19). On large sparse graphs, this causes the algorithm to hang indefinitely.
Reproduction: 49K-node knowledge graph with density 0.00006, 2,433 connected components, 269 isolates (~1.37 edges/node). louvain_communities() with defaults (max_level=None, threshold=1e-7) loops through micro-optimizations forever on this topology.
Workaround: Monkey-patching _partition() at runtime with max_level=5, threshold=1e-4 produces equivalent community quality in ~2 seconds instead of hanging indefinitely.
Proposed fix
Expose max_level and threshold as parameters in _partition(), propagated from cluster():
def _partition(G: nx.Graph, max_level: int | None = 5, threshold: float = 1e-4) -> dict[str, int]:
try:
from graspologic.partition import leiden
return leiden(G)
except ImportError:
pass
communities = nx.community.louvain_communities(G, seed=42, max_level=max_level, threshold=threshold)
return {node: cid for cid, nodes in enumerate(communities) for node in nodes}
The defaults (max_level=5, threshold=1e-4) are safe for graphs up to ~50K nodes. Users with smaller/denser graphs who want finer granularity can pass max_level=None to restore the original behavior.
Alternatively, a simpler fix would be to just hardcode sensible defaults (e.g., max_level=10, threshold=1e-4) without exposing them — this would prevent the hang without adding API surface.
Environment
- graphify 0.3.9
- networkx 3.4.2
- Python 3.14
- Windows 11
- graspologic not installed (Louvain fallback path)
Related: #46, #47
Fixed in v0.3.11. Added max_level=10, threshold=1e-4 to the Louvain fallback — hardcoded safe defaults rather than exposing them as API to keep it minimal. Produces equivalent community quality on typical corpora and completes in ~2s on the 49K-node topology you described.