GraphViz input node labels accessible?

253 views
Skip to first unread message

srk

unread,
Apr 15, 2009, 6:26:19 AM4/15/09
to networkx-discuss
Hello,

I am reading into NetworkX a Graphviz (.dot) format file using read_dot
(). The file contain node labels. I need to be able to access and
print the node labels on a subset of the input graph nodes. Is this
possible using the existing classes, or will I need an external 'grep
+bash' solution ? Can I cast the output of read_dot() to the new
LabelledGraph class and access the labels that way?

Many thanks!

Steve

Aric Hagberg

unread,
Apr 15, 2009, 10:02:14 AM4/15/09
to networkx...@googlegroups.com

The node attributes are stored in the dictionary G.node_attr.
So you should be able to write

>>> for n in G:
... print G.node_attr[n]['label']

etc.

We are developing classes to more smoothly handle attributes
https://networkx.lanl.gov/trac/ticket/226
but they are still prototypes and not integrated yet into
all of the input and output formats.

Aric

Steve Kirk

unread,
Apr 15, 2009, 5:13:34 PM4/15/09
to networkx-discuss


On Apr 15, 4:02 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:
> On Wed, Apr 15, 2009 at 4:26 AM, srk <stevenrk...@gmail.com> wrote:
>
> > Hello,
>
> > I am reading into NetworkX a Graphviz (.dot) format file using read_dot
> > (). The file contain node labels. I need to be able to access and
> > print the node labels on a subset of the input graph nodes. Is this
> > possible using the existing classes, or will I need an external 'grep
> > +bash' solution ? Can I cast the output of read_dot() to the new
> > LabelledGraph class and access the labels that way?
>
> The node attributes are stored in the dictionary G.node_attr.
> So you should be able to write
>
> >>> for n in G:
>
> ...    print G.node_attr[n]['label']

This doesn't seem to work - I get an error:

AttributeError: 'MultiGraph' object has no attribute 'node_attr'

Steve

Aric Hagberg

unread,
Apr 16, 2009, 9:29:35 AM4/16/09
to networkx...@googlegroups.com

Can you send a small example and also what NetworkX version you are using?

Aric

Steve Kirk

unread,
Apr 16, 2009, 10:24:46 AM4/16/09
to networkx-discuss
Hello Aric,

I'm using v0.99


#!/bin/env python
#

import time
import sys
import networkx as NX
from operator import itemgetter

fin = sys.argv[1]
print "Reading Graph data for file: ",fin
G=NX.drawing.read_dot(fin)
for n in G:
print G.node_attr[n]['label']
print "Largest component (GCC)"
print "======================="
H=NX.component.connected_component_subgraphs(G)[0]
print "Nodes ",H.number_of_nodes()
print "Edges ",H.number_of_edges()
print "======================="
for n in H:
print H.node_attr[n]['label']

This reads in the file OK, and prints the G node labels i.e. node_attr
works on G, but not on H, e.g.

OUTPUT:
...
<all of the G labels, as expected >
Largest component (GCC)
=======================
Nodes 5272
Edges 13942
=======================
Traceback (most recent call last):
File "./example", line 21, in <module>
print H.node_attr[n]['label']
AttributeError: 'MultiGraph' object has no attribute 'node_attr'

I can email you the input .dot file if that would also help

Regards,
Steve

Aric Hagberg

unread,
Apr 16, 2009, 11:46:18 AM4/16/09
to networkx...@googlegroups.com

connected_component_subgraphs is not getting the attributes copied
correctly. This is a wart in the current hack for handling node attributes
that we will fix with the new code I mentioned earlier.

In the meantime you could use a reference to the attributes in G with
H.node_attr=G.node_attr
Aric

Steve Kirk

unread,
Apr 17, 2009, 4:02:57 AM4/17/09
to networkx-discuss
This works - many thanks!

On Apr 16, 5:46 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages