KeyError

363 views
Skip to first unread message

Ranjit

unread,
Apr 18, 2011, 10:30:59 PM4/18/11
to networkx-discuss
Hi everyone,

I'm having trouble just reading in a GML graph from the sample graphs
linked to from the documentation page. I tried loading the Zachary
Karate Club graph here:

http://www-personal.umich.edu/~mejn/netdata/

like this:
G=nx.read_gml("karate.gml")

And I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/EPD64.framework/Versions/6.3/lib/python2.6/
site-packages/networkx/readwrite/gml.py", line 81, in read_gml
G=parse_gml(lines)
File "/Library/Frameworks/EPD64.framework/Versions/6.3/lib/python2.6/
site-packages/networkx/readwrite/gml.py", line 159, in parse_gml
label=vdict['label']
KeyError: 'label'

Any idea why this isn't working?

Thanks,

-Ranjit

Aric Hagberg

unread,
Apr 18, 2011, 10:49:02 PM4/18/11
to networkx...@googlegroups.com

Maybe you have an old version of NetworkX? It works with networkx-1.4.

Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import networkx as nx
>>> G=nx.read_gml('karate.gml')
>>> len(G)
34

Aric

Ranjit

unread,
Apr 19, 2011, 7:35:08 AM4/19/11
to networkx-discuss
Thanks. The problem was some combination of an old version and
something going wrong when I tried to update it. I have the Enthought
distribution installed on my computer, and it includes version 1.3 of
networkx. I tried easy_install and compiling from source at first, but
I had to delete the old package first. Now everything works.

Thanks,

-Ranjit


On Apr 18, 10:49 pm, Aric Hagberg <ahagb...@gmail.com> wrote:

satyam mukherjee

unread,
Apr 19, 2011, 9:20:49 AM4/19/11
to networkx...@googlegroups.com
You can use a node relabel :

import networkx as nx

def transform_labels_to_nx(G):
    H = nx.Graph()
    label_mapping={}
        #add the nodes by label from G to H
    for node in G.nodes(data=True):
        H.add_node(node[1]['label'])
        label_mapping[node[0]] = node[1]['label']

        if len(node[1]) > 2:
            for key in node[1]:
                H.node[node[1]['label']][key] = node[1][key]
    #add the appropriate edges
    for edge in G.edges(data=True):
        H.add_edge(label_mapping[edge[0]], label_mapping[edge[1]])
        if len(edge[2]) > 0:
            for key in edge[2]:
                H[label_mapping[edge[0]]][label_mapping[edge[1]]][key] = edge[2][key]
    return H

 

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.




--
-------------------------------------------
WHEN LIFE GIVES U HUNDRED REASONS TO CRY,SHOW LIFE THAT U HAVE THOUSAND REASONS TO SMILE :-)

satyam mukherjee
224-436-3672 (Mob)
847-491-7238 (Off)

Donald Ephraim Curtis

unread,
May 16, 2011, 7:36:35 PM5/16/11
to networkx-discuss
Bump!

I am having this problem with SVN HEAD at the moment. Little
different error:

>>> import networkx as nx; nx.read_gml("karate.gml")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/dcurtis/Development/networkx/networkx/readwrite/
gml.py", line 85, in read_gml
G=parse_gml(lines,labels=labels)
File "/Users/dcurtis/Development/networkx/networkx/readwrite/
gml.py", line 184, in parse_gml
mapping=dict((n,d['label']) for n,d in G.node.items())
File "/Users/dcurtis/Development/networkx/networkx/readwrite/
gml.py", line 184, in <genexpr>
mapping=dict((n,d['label']) for n,d in G.node.items())
KeyError: 'label'

The problem is that the argument "labels" defaults to True. It seems
like this should be something that is detected or at least caught with
a try,except?

Aric Hagberg

unread,
May 16, 2011, 8:05:25 PM5/16/11
to networkx...@googlegroups.com
On Mon, May 16, 2011 at 5:36 PM, Donald Ephraim Curtis
<dcu...@gmail.com> wrote:
> Bump!
>
> I am having this problem with SVN HEAD at the moment.  Little
> different error:
>

The latest NetworkX source is in the mercurial repository at
http://networkx.lanl.gov/hg/networkx

If you are using the SVN repository you have old code that might not
read the karate.gml file correctly.

Aric

Reply all
Reply to author
Forward
0 new messages