2

I don’t have experience with Python/NetworkX. Everything I try is a dead-end. I have Python 2.7 installed on Windows 8. (Took forever to install NetworkX.)

A. I cannot get NetworkX to read my weighted network

I have a text file with the edges and weights, like this:

1 2 2

2 3 1

2 4 1

4 5 4    (…etc.)

I name the file test.edgelist (exactly as I’ve seen in many examples) and then used this code to read it:

import networkx as nx
fh=open("test.edgelist", 'rb')
G=nx.read_weighted_edgelist(fh, nodetype=int)
fh.close()

I get the following error message:

'module' object has no attribute 'read_weighted_edgelist'

(note: for the unweighted version with just the first two columns, using the same code, only with read_edgelist instead of read_weighted_edgelist, it’s working just fine)

And by using this alternative code:

G = nx.read_edgelist("test.edgelist", nodetype=int, data=(("weight",float),))

I get the following error message:

read_edgelist() got an unexpected keyword argument 'data'

B. Can't find a way to read some node attributes from a file.

The text file will be something like:

Label Sex Country Colour

1 F GB green

2 M DE red

3 M IT blue (…etc.)

I found this, which I think is the only remotely relevant to what I’m looking for:

Reading nodes with pos attribute from file in networkx

Although csv format is not the problem in my case, I took a shot and installed pandas, but all I get is errors:

from pandas.io.api import *

from pandas.io.gbq import read_gbq

import pkg_resources
ImportError: No module named pkg_resources
TylerH
  • 20,799
  • 66
  • 75
  • 101
user3656340
  • 23
  • 1
  • 3
  • You probably need a newer version of networkx. What version do you have? The latest is 1.8.1 https://pypi.python.org/pypi/networkx/ – Aric May 21 '14 at 23:45

1 Answers1

2

A.

if your data is in a text file, then you need to open it as text rather than binary.

import networkx as nx
fh=open("test.edgelist", 'r')
# ------------------------|----- note 'r' not 'rb'
G=nx.read_weighted_edgelist(fh, nodetype=int)
fh.close()

With the sample data that you provided, both methods work fine for me. It is particularly surprising that the second command does not work, and makes me wonder whether you have overwritten a built-in (see e.g. How to stop myself overwriting Python functions when coding?).

I am using networkx version 1.6. (You can test this by typing nx.__version__ in an interactive shell)

B.

Pandas is quite flexible in reading data - it doesn't have to be comma separated (even with the read_csv function). For instance, assuming your the second labelled dataset is in a file "data.txt",

import pandas as pd
df = pd.read_csv('data.txt', sep='\s')

In [41]: print df
   Label   Sex Country Colour
0      1     F      GB  green
1      2     M      DE    red
2      3     M      IT   blue
3    NaN  None    None   None

With this data, you can construct a graph whose nodes take on the properties:

# a new empty graph object
G2 = nx.DiGraph()
# create nodes with properties from dataframe (two examples shown, but any number
# of properties can be entered into the attributes dictionary of each node)
for idx, row in df.iterrows():
    G2.add_node(row['Label'], colour=row['Colour'], sex=row['Sex'])

# construct a list of colors from the nodes so we can render with that property
colours = [ d['colour'] for n, d in G2.nodes(data=True)]

nx.draw_networkx(G2, node_color=colours)

I'm not entirely sure why you need pkg_resources (it doesn't seem to be used in the answer that you linked), but see No module named pkg_resources for how to solve the error.

Community
  • 1
  • 1
Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • About A: I don't think the thing about overwriting is an issue as I use the code provided in examples. I think you're on to something with your question about the version. I just couldn't for the life of me install netwokrx using egg or .py file! I spend two days trying everything I found on the internet but it was impossible. So, I used a 0.34.win32.exe file and it worked. Your answer made me wonder whether this command comes with the newer versions. I found a 0.99 exe file but I got the same error. (I haven't tried what you said about B yet.) (Thank you very much for your time) – user3656340 May 21 '14 at 16:08
  • Using "r" instead of "rb" doesn't really change anything, but thanks, I was wondering what this is. – user3656340 May 21 '14 at 18:12
  • Have you tried to [install nx with pip](http://networkx.github.io/documentation/latest/install.html)? `pip` is a package manager for python, and for many open-source python projects this is a very easy way to get up and running. As Aric commented above, you might well be using an old version that is causing your problems. – Bonlenfum May 22 '14 at 09:59
  • The python [IO tutorial](https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files) gives some information on reading files, and the difference between the r/a/rb/w opening modes. While this might not be the only problem you have, it is crucial that you open a file in the mode for which the data is written. In brief, if you can read it in a text editor, it is ascii (or similar) - read with open('filename', 'r'); if it looks like machine code (lots of 0-9,a-f characters, if your editor even opens it) then you need to read in 'b' mode. – Bonlenfum May 22 '14 at 10:04
  • A. Bonlefum and Aric you were both right. It was the version. I HAD tried pip and everything else I could find on the internet, but nothing worked. Maybe it's because I have Windows 8, I don't know. But, I remembered Enthought Canopy (which I didn't want to use for various reasons btw, but now I had no other option) and with that I could use networkx 1.8.1-2 and pandas 0.13.1-1 and all above commands worked fine. – user3656340 May 22 '14 at 16:43
  • B. Bonlefum, the code you suggested for the node attributes worked like a charm and an extra thank you for the code to colour the nodes accordingly. Used practically the same code to add the edges (with their weights) from a file, with pandas and everything and was amazed to see that worked beautifully, too. You saved me. Thanks. – user3656340 May 22 '14 at 16:46