ericmjl / nxviz

Visualization Package for NetworkX
https://ericmjl.github.io/nxviz
MIT License
450 stars 86 forks source link

AttributeError: 'NodeView' object has no attribute 'index' #160

Closed basselhak closed 6 years ago

basselhak commented 6 years ago

Description

When attempting to make any CircosPlot from a NetworkX 2.0 Graph object, I get the following error: "AttributeError: 'NodeView' object has no attribute 'index'". Does this have something to do with NetworkX 2.0 compatibility? I've updated nxviz to the latest github version which lists NetworkX 2.0 in the requirements, so I'm confused as to what's causing this.

What I Did

Here's an example with a random graph.

G3 = nx.Graph()
G3.add_nodes_from(range(10))
G3.add_edges_from([(0,1), (0,3), (2,4), (1,2), (2,7), (0,9), (5,6), (3,8), (1,9), (4,8)])
c3 = CircosPlot(G3)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-97dd4ee1b584> in <module>()
----> 1 c3 = CircosPlot(G3)

~\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)
    122 
    123         # Compute each node's positions.
--> 124         self.compute_node_positions()
    125 
    126     def check_data_types(self, data_types):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\plots.py in compute_node_positions(self)
    259         self.nodeprops['linewidth'] = radius * 0.01
    260         for node in self.nodes:
--> 261             x, y = get_cartesian(r=radius, theta=node_theta(self.nodes, node))
    262             xs.append(x)
    263             ys.append(y)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\nxviz\geometry.py in node_theta(nodelist, node)
     19     assert node in nodelist, 'node must be inside nodelist.'
     20 
---> 21     i = nodelist.index(node)
     22     theta = -np.pi + i*2*np.pi/len(nodelist)
     23 

AttributeError: 'NodeView' object has no attribute 'index'
ericmjl commented 6 years ago

Hi @basselhak, thanks for reporting the bug. Yes, this is a bug related to the new internals of networkx; I’m surprised myself that my automated tests didn't catch this.

The fix should be a simple one, so give me an evening to fix this and I’ll get it up on PyPI and conda forge.

Alternatively, if it’s something you’re comfortable handling, and would love to do an open source contribution (I’d love to see ore open source contributors too!), the only changes that are needed are with respect to graph nodes and edges, by casting them as lists. Happy to chat through here (after I finish my lunch).

ericmjl commented 6 years ago

@basselhak Please try the latest commit 969f464. On my own machine tests pass, but I'd like to get extra confirmation too. If it works on your system too, I'll check the CIs and then make a release.

basselhak commented 6 years ago

Hi Eric, Just checked it here and all good now. Thanks for the quick fix! I'd love to contribute in the future, but I'm still fairly new to Python (came across this package on your DataCamp course, which is really great). So give me a while to get up to speed. Happy to share the Jupyter Notebook I'm working on in case you're interested in seeing nxviz applied to network analysis in political science. Best, Jacob

ericmjl commented 6 years ago

Thanks for helping with the test, Jacob! Even this little bug report is useful. I will be sure to record it in the changelog (which I should start building too). Thanks for taking the DC course too, I'm sure @hugobowne and @yashasroy will be thrilled to hear about this too! :smile:

ericmjl commented 6 years ago

By the way, I would definitely love to see the Jupyter notebook that you're creating! If you'd like it, I'd love to tweet it around, or even link to it as a contributed example to nxviz.

basselhak commented 6 years ago

For some reason my account has just been flagged by Github. No idea why. I just sent in a support request. There's a nxviz tree available on my braindrain repository where the notebook is stored: https://github.com/basselhak/braindrain/tree/nxviz

Not sure if you can see it as long as I'm flagged. You are of course more than welcome to tweet it, but it's still a work-in-progress, and actually, there are a few more things I can't get working through nxviz.

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. Any idea what's causing this? Not sure if it warrants reporting another issue.

ericmjl commented 6 years ago

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. Any idea what's causing this? Not sure if it warrants reporting another issue.

Yeah, talk about this one on a separate issue. It'll help with "grouping"/"tagging" the issues down the road. Feel free to go ahead on that. As for the KeyError, my first hunch is that it's related to number of countries exceeding the number of available colours in the colour mapping. Anyways, let's bring this to a new thread.

ericmjl commented 6 years ago

For some reason my account has just been flagged by Github. No idea why. I just sent in a support request. There's a nxviz tree available on my braindrain repository where the notebook is stored: https://github.com/basselhak/braindrain/tree/nxviz

Not sure if you can see it as long as I'm flagged. You are of course more than welcome to tweet it, but it's still a work-in-progress, and actually, there are a few more things I can't get working through nxviz.

I can see the notebook, and yes, it's shaping up to be very nice! I'll respect your timeline, and when you feel it's ready for prime time, I'd love to be able to link it as an example of how to use the nxviz API!

ericmjl commented 6 years ago

Closing this discussion as it looks like it's concluded. @basselhak feel free to reopen if you think otherwise.

basselhak commented 6 years ago

Hi @ericmjl ,

Here's the latest version of the network analysis: https://nbviewer.jupyter.org/github/basselhak/braindrain/blob/organizations/brain_drain_politics_organizations.ipynb

I only do a quick visualization with nxviz before moving on to doing the rest in matplotlib, because I need labels for now. Once we get labels working, I think I could update the notebook to do a more extended visualization in nxviz and then you could tweet that as an example of how to use your package, if you like.

If you have any comments or suggestions on the code as it stands now, using matplotlib, feel free to let me know. And thanks for being responsive on the nxviz issues. I really like the package and hope to see it expanded in the future.

Best, Jacob

musaahmed commented 5 years ago

please the same error is occured ,after running the following code segments in network graph analysis:full package is on github import DBCP as dp import Evaluation as eva

the function of "Find_Controller_Placement" has three parameters:

1. graph_url is the file name of graph;

2. avg_weight is the weight to select the placement according to the objective function of average controller-to-switch latency;

3. max_weight is the weight to select the placement according to the objective function of worst-case controller-to-switch latency;

4. inter_weight is the weight to select the placement according to the objective function of average inter-controller latency;

In this demo, we only consider average latency and worst-case latency.

the eva class has three function can be used. They are:

1. Avg_latency_of_end_to_end(graph_name,controller_list), it can measure the average latency of end-to-end communications;

2. Avg_latency_of_inter_controller(graph_name,controller_list), it can measure the average latency of inter-controller communications;

3. Worst_latency_of_controller_to_switch(graph_name,controller_list), it can measure the worst-case controller-to-switch communciations.

graph_name = 'Biznet.graphml' ctl_list dp.Find_Controller_Placement(graph_name,avg_weight=1.0,max_weight=1.0,inter_weight=1.0)

avgL = eva.Avg_latency_of_end_to_end(graph_name,ctl_list) print ('the average latency of end-to-end communications is',avgL)