fix: handle leiden clustering with zero edges gracefully - #168
fix: handle leiden clustering with zero edges gracefully#168majiayu000 wants to merge 2 commits into
Conversation
| ) | ||
| node_communities: dict[str, list[dict[str, str]]] = {} | ||
| for i, node in enumerate(graph.nodes()): | ||
| node_communities[node] = [{"level": 0, "cluster": i}] |
There was a problem hiding this comment.
typing mismatch between str and "i" in field cluster
| @@ -231,6 +231,19 @@ async def _leiden_clustering(self): | |||
| from graspologic.partition import hierarchical_leiden | |||
|
|
|||
| graph = NetworkXStorage.stable_largest_connected_component(self._graph) | |||
There was a problem hiding this comment.
if each node is independent, will a node be randomly selected here? This might prevent you from achieving your goal of treating all independent nodes as separate clusters.
|
Thanks for your PR 🤗 . I have one question regarding this fix: will it correctly treat multiple independent nodes as a cluster as expected? I'm concerned that the LCC extraction logic might prune it down to just a single node. Since I’ve forgotten some of the implementation details, it would be great if you could take some time to verify this |
When the graph has nodes but no edges, the leiden clustering algorithm from graspologic would fail. This commit adds a check for this edge case and treats each node as its own community when there are no edges. Crucially, this check is performed BEFORE extracting the largest connected component, ensuring that isolated nodes are not discarded. Fixes gusye1234#167 Signed-off-by: majiayu000 <1835304752@qq.com>
0b3bd62 to
c026478
Compare
The 'transformers' library is not listed in requirements.txt but was imported at the top level in _utils.py, causing ImportError in environments where it is not installed (e.g., CI). This commit wraps the import in a try-except block to make it optional, mirroring the logic in TokenizerWrapper. Signed-off-by: majiayu000 <1835304752@qq.com>
CI Failure AnalysisThe CI failure is not caused by this PR. Here's what's happening: Root Cause
EvidenceLooking at the main branch CI run from the same day, it also fails - but with the ConclusionThis PR's changes (handling leiden clustering with zero edges + optional transformers import) are correct. The Options:
|
|
Actually, from my personal perspective, if no valid edges are established between nodes, the graph structure becomes meaningless at that point. In such cases, we should consider whether there were issues with the earlier node or relationship extraction. Otherwise, letting this pass would be no better than just using naive RAG directly. Considering this appears to be a purely AI-generated PR, I'm closing it for now to reduce noise. |
Summary
When the graph has nodes but no edges after entity extraction, the leiden clustering algorithm fails with an error. This PR adds a check for this edge case and handles it gracefully by treating each node as its own community.
Changes:
hierarchical_leidenFixes #167