stestagg / CommunityLayout

GNU General Public License v3.0
0 stars 0 forks source link

Issue when node isn't in a community #1

Open stestagg opened 7 months ago

stestagg commented 7 months ago

Sometimes you get this error:


KeyError Traceback (most recent call last) File .../networkx/drawing/nx_pylab.py:429, in draw_networkx_nodes(G, pos, nodelist, node_size, node_color, node_shape, alpha, cmap, vmin, vmax, ax, linewidths, edgecolors, label, margins) 428 try: --> 429 xy = np.asarray([pos[v] for v in nodelist]) 430 except KeyError as err:

KeyError: 'code_summary-100'

The above exception was the direct cause of the following exception:

NetworkXError Traceback (most recent call last) Cell In[24], line 1 ----> 1 layout.display()

File .../community_layout/layout_class.py:153, in CommunityLayout.display(self, colors, bundle, complex_alphas, ax) 151 else: 152 save = False --> 153 nx.draw_networkx_nodes(self.G, pos=self.full_positions, node_size=2, ax = ax, node_color=colors) 154 nx.draw_networkx_edges(self.G, pos=self.full_positions, node_size=2, width=alphas, alpha=alphas, ax = ax) 156 if save:

File .../networkx/drawing/nx_pylab.py:431, in draw_networkx_nodes(G, pos, nodelist, node_size, node_color, node_shape, alpha, cmap, vmin, vmax, ax, linewidths, edgecolors, label, margins) 429 xy = np.asarray([pos[v] for v in nodelist]) 430 except KeyError as err: --> 431 raise nx.NetworkXError(f"Node {err} has no position.") from err 433 if isinstance(alpha, Iterable): 434 node_color = apply_alpha(node_color, alpha, nodelist, cmap, vmin, vmax)

NetworkXError: Node 'code_summary-100' has no position.

Which I assume is due to some nodes not being in a community.

Nodes that aren't in communities aren't important for this display so can be hidden, but we need to update the code to do that rather than raise

autoprbot commented 7 months ago

🤖 I'm going to attempt this change. The issue described is a keyerror that occurs when the draw_networkx_nodes function from the networkx library is called with a node that does not have a position in the pos dictionary. this can happen if the node is not part of any community and thus was not assigned a position.

to resolve this issue, we can modify the code to filter out nodes that do not have a position before calling draw_networkx_nodes. this can be done by checking if each node in the nodelist is present in the pos dictionary and only including those that are.

the steps to implement this would be:

  1. identify the part of the code where draw_networkx_nodes is called.
  2. before calling draw_networkx_nodes, create a filtered list of nodes that only includes nodes that have an entry in the pos dictionary.
  3. pass this filtered list to the draw_networkx_nodes function instead of the original nodelist.

this approach would prevent the keyerror from occurring and would hide nodes that are not in any community from the display, as requested in the issue. since the change is relatively straightforward and localized to the part of the code where the display function is implemented, it is likely that i can implement it accurately without introducing any bugs.