AllenDowney/ThinkComplexity2

NetworkX 2.0 not supported

dosyoyas opened this issue · 9 comments

Chapter 02 notebook uses G.nodes_iter, which has been removed in v2.0:
https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html

Maybe a note on the modules versions needed or a requirements.txt file would be useful.

NetworkX 2.0 was released on Sep 20, 2017.

@AllenDowney do you have any thoughts on how you'd like to proceed with respect to NetworkX 2.0 support? Over the term I don't mind working on making changes but it would be important before forging ahead with this to know if you would like to switch to 2.0 or if you'd like the notebooks to maintain compatibility with 1.X.

The migration guide @dosyoyas linked to above gives good advice for writing code that works with both versions but it would likely be a lot cleaner to just expect everyone to move to 2.0.

@dosyoyas thanks for raising this issue, and @gwtaylor thanks for the additional information.

I am using a limited set of NetworkX features, so I suspect the code can be made compatible with both. I will get to it as soon as a I can, but any help would also be appreciated!

My environment.yml includes networkx as a requirement, but doesn't specify a version. What's the right way to require 1.X without requiring a specific X?

The pull request above makes a small change to the environment.yml file to force 1.X series installation.

Myself and my students may be able to help with updating the code such that it's compatible with 2.0 and 1.X. We'll keep you posted so that we don't duplicate efforts.

Merged, and thank you. That resolves this issue for now, but I'll leave it open to remind us to work on 2.0 compatibility.

for rookies like me :

  1. use pip to uninstall networkx
  2. install the specific 1.* networkx

code(python3):
pip3 uninstall networkx
pip3 install -Iv networkx==1.10

code(python2):
pip uninstall networkx
pip install -Iv networkx==1.10

With this commit f5e94e0, I believe we are compliant with NetworkX 2.0. If anyone has a chance to test, please let me know if you find any problems.

I tested this out in an environment with NetworkX 2.1.
chap02, chap02soln, chap03, chap03soln, chap04, chap04soln all run fine.

There was a problem in social2 with this code:

def sample_fof_degree(G):
    nodes = G.nodes()
    node = np.random.choice(nodes)
    friends = G.neighbors(node)
    friend = np.random.choice(friends)
    fofs = G.neighbors(friend)
    fof = np.random.choice(fofs)
    return G.degree(fof)

In nx2, G.neighbours() returns an iterator, not a list. np.random.choice does not like this.

I submitted pr #39 which just explicitly casts these to lists before passing to np.random.choice.

I just wanted to note that these changes do break compatibility with NetworkX 1.x series, for example, this code from chap03:

def path_lengths(G):
    length_iter = nx.shortest_path_length(G)
    for source, dist_map in length_iter:
        for dest, dist in dist_map.items():
            yield dist

will break on 1.x because it expects that shortest_path_length returns an iterator.

I don't think this is a huge deal because you have modified the environment file to explicitly request NetworkX 2.x.

NetworkX 1.x also uses a bunch of deprecated plotting commands and generates a lot of warnings on my version of matplotlib. So it may be a good move to force upgrade at this point.

Right. I tried to keep it compatible with 1.x and 2.x, but did not find a way without obfuscating the code. So I'm requiring 2.x. Thanks for your help!