nxviz
nxviz copied to clipboard
KeyError: Cannot set node orders, grouping or colors according to some node attributes.
- nxviz version: 0.3.2
- Python version: 3.6.3
- Operating System: Windows 7 Enterprise Service Pack 1
- Using the Jupyter Notebook in Anaconda
Description
nxviz gives me a KeyError when attempting to set node orders and colors according to some of my node attributes. It works for the bipartite and centrality measures, but not for other attributes I've stored (like 'ORGANIZATION' or 'COUNTRY'), although I've confirmed that all these attributes are stored in the same way.
The 'ORGANIZATION' or 'COUNTRY' dictionaries contain strings with the names of countries or organizations of people in the dataset. I wonder if the error is caused by nxviz not being able to read strings as compared to the integers and floats in the centrality dictionaries.
@ericmjl speculated that the reason could be the number of variables exceeding the number of available colors in the colormap. But setting color according to a centrality measure seems to correctly produce a color map with a scale corresponding to measures: nxviz visualization with node color set according to a centrality measure
What I Did
c2 = CircosPlot(G, node_order='degree_centrality', node_grouping='bipartite', node_color='COUNTRY')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-15-4f179e07c4c4> in <module>()
----> 1 c2 = CircosPlot(G, node_order='degree_centrality', node_grouping='bipartite', node_color='MEP')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\plots.py in __init__(self, graph, node_order, node_size, node_grouping, node_color, edge_width, edge_color, data_types, nodeprops, edgeprops)
245 node_color=node_color, edge_width=edge_width,
246 edge_color=edge_color, data_types=data_types,
--> 247 nodeprops=nodeprops, edgeprops=edgeprops)
248
249 def compute_node_positions(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\plots.py in __init__(self, graph, node_order, node_size, node_grouping, node_color, edge_width, edge_color, data_types, nodeprops, edgeprops)
88 if self.node_color:
89 self.node_colors = []
---> 90 self.compute_node_colors()
91 else:
92 self.node_colors = ['blue'] * len(self.nodes)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\plots.py in compute_node_colors(self)
158 def compute_node_colors(self):
159 """Compute the node colors. Also computes the colorbar."""
--> 160 data = [self.graph.node[n][self.node_color] for n in self.nodes]
161 data_reduced = sorted(list(set(data)))
162 dtype = infer_data_type(data)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\plots.py in <listcomp>(.0)
158 def compute_node_colors(self):
159 """Compute the node colors. Also computes the colorbar."""
--> 160 data = [self.graph.node[n][self.node_color] for n in self.nodes]
161 data_reduced = sorted(list(set(data)))
162 dtype = infer_data_type(data)
KeyError: 'MEP'
Could it have something to do with this being a bipartite network, and so some of the nodes do not figure in the 'ORGANIZATION' or 'COUNTRY' dictionaries?
Potentially. I'm looking at the error trace, and seeing that the key MEP is the source of the error. I think that should be a node.
Can I just check, does every node have an ORGANIZATION and COUNTRY associated with it?
No, only nodes in one bipartite set have an ORGANIZATION and COUNTRY association. I just now gave the nodes in the other bipartite set a 'null' value for these keys, and that seems to have fixed the problem.
Okay, got it. This does raise the question of whether nxviz should auto-fill a "null" value for nodes that don't have a key. I'm hesitant to do so because it very likely means modifying the underlying graph data, which might have unintended consequences for a user. At the moment, it probably makes most sense to document for the end-user that all keys must be present on each node. I think I will create a separate "todo" issue regarding this.
Btw, just wanted to comment, this kind of feedback is the best for improving the library - thanks for taking the time to raise these issues!