Dynamic Graphs saved as gexf for use with Gephi

3,369 views
Skip to first unread message

jockel

unread,
Apr 13, 2011, 9:46:53 AM4/13/11
to networkx-discuss
Hi All,

I am trying to build a dynamic graph (nodes and edges have timed
appearance) using networkx. No matter which attributes I write on
nodes and edges (start, end, Time Interval): gephi does not recognize
the time data correctly. I save the network as gexf. Is there a
working example of using networkx to build a dynamic graph, saved as
gexf and opened with Gephi? In which format do I have to pass the
attributes?

All the best

Jörn

Aric Hagberg

unread,
Apr 13, 2011, 9:56:12 AM4/13/11
to networkx...@googlegroups.com

I'm glad you asked this question. I tried to support reading and
writing GEXF format files with dynamics. Try reading the file from
http://gexf.net/data/dynamics.gexf

That shows how NetworkX stores the data internally as attributes.
e.g.
In [1]: import networkx as nx

In [2]: G=nx.read_gexf('dynamics.gexf')

In [3]: G.nodes(data=True)
Out[3]:
[('1',
{'indegree': [(1.0, None, '2009-03-01'),
(2.0, '2009-03-01', '2009-03-10'),
(1.0, '2009-03-10', None)],
'label': 'Network'}),
('0',
{'indegree': [(1.0, None, None)],
'label': 'Gephi',
'start': '2009-03-01',
'url': u'http://gephi.org'}),
('3',
{'frog': False,
'indegree': [(0.0, None, '2009-03-01'), (1.0, '2009-03-01', None)],
'label': 'Graph'}),
('2',
{'indegree': [(0.0, None, '2009-03-01'), (1.0, '2009-03-01', None)],
'label': 'Visualization',
'spells': [(None, '2009-03-01'), ('2009-03-05', '2009-03-10')]})]

In [4]: G.edges(data=True)
Out[4]:
[('1', '0', {'id': '2', 'start': '2009-03-01'}),
('0', '1', {'id': '0', 'start': '2009-03-01'}),
('0', '3', {'id': '4', 'start': '2009-03-01'}),
('0', '2', {'end': '2009-03-10', 'id': '1', 'start': '2009-03-01'}),
('2', '1', {'end': '2009-03-10', 'id': '3'})]

You can then write this graph

In [5]: nx.write_gexf(G,'test.gexf')

and (hopefully) Gephi can read it.

Is there a simpler or more natural way to store the time attributes
with NetworkX?
Aric

jockel

unread,
Apr 13, 2011, 2:16:33 PM4/13/11
to networkx-discuss
Dear Aric,

thank you for the reply. I tried your example and receive the
following error:

File "generateGraph.py", line 140, in main
g = nx.read_gexf("dynamics.gexf")
File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
\readwrite\gexf.py", line 153, in read_gexf
G=reader(fh)
File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
\readwrite\gexf.py", line 492, in __call__
raise nx.NetworkXError("No <graph> element in GEXF file")
networkx.exception.NetworkXError: No <graph> element in GEXF file

I am on python 2.6 using networkx 1.4. The gexf is readable as is in
Gephy, but I cannot load it in python.
The gexf file is directly downloaded from http://gexf.net/format/dynamics.html
(should be fine) and it contains a graph element...

Cheers
Jörn


On 13 Apr., 15:56, Aric Hagberg <ahagb...@gmail.com> wrote:
> On Wed, Apr 13, 2011 at 7:46 AM, jockel <anjoerngr...@googlemail.com> wrote:
> > Hi All,
>
> > I am trying to build a dynamic graph (nodes and edges have timed
> > appearance) using networkx. No matter which attributes I write on
> > nodes and edges (start, end, Time Interval): gephi does not recognize
> > the time data correctly. I save the network as gexf. Is there a
> > working example of using networkx to build a dynamic graph, saved as
> > gexf and opened with Gephi? In which format do I have to pass the
> > attributes?
>
> I'm glad you asked this question.  I tried to support reading and
> writing GEXF format files with dynamics.  Try reading the file fromhttp://gexf.net/data/dynamics.gexf

Ben Edwards

unread,
Apr 13, 2011, 7:57:02 PM4/13/11
to networkx-discuss
I am not sure about your file format, perhaps you could post it and we
could take a look?

Aside from that, I think there was a bug in the write_gexf. 'start'
and 'end' seem to be special keywords that need to go in the node
definition, ie

<node id="0" label="0" start="0" end="1">

as apposed to edge attributes as they are now.

I may be the case that Gephi is supposed to read them either way, but
the gexf format seems to indicate that we are wrong. I've fixed it and
will push the code up.

Ben

On Apr 13, 12:16 pm, jockel <anjoerngr...@googlemail.com> wrote:
> Dear Aric,
>
> thank you for the reply. I tried your example and receive the
> following error:
>
>  File "generateGraph.py", line 140, in main
>     g = nx.read_gexf("dynamics.gexf")
>   File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> \readwrite\gexf.py", line 153, in read_gexf
>     G=reader(fh)
>   File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> \readwrite\gexf.py", line 492, in __call__
>     raise nx.NetworkXError("No <graph> element in GEXF file")
> networkx.exception.NetworkXError: No <graph> element in GEXF file
>
> I am on python 2.6 using networkx 1.4. The gexf is readable as is in
> Gephy, but I cannot load it in python.
> The gexf file is directly downloaded fromhttp://gexf.net/format/dynamics.html

Aric Hagberg

unread,
Apr 13, 2011, 8:59:20 PM4/13/11
to networkx...@googlegroups.com
On Wed, Apr 13, 2011 at 5:57 PM, Ben Edwards <bjed...@gmail.com> wrote:
> I am not sure about your file format, perhaps you could post it and we
> could take a look?
>
> Aside from that, I think there was a bug in the write_gexf. 'start'
> and 'end' seem to be special keywords that need to go in the node
> definition, ie
>
> <node id="0" label="0" start="0" end="1">
>
> as apposed to edge attributes as they are now.
>
> I may be the case that Gephi is supposed to read them either way, but
> the gexf format seems to indicate that we are wrong. I've fixed it and
> will push the code up.
>

If you want to do it correctly (and it likely isn't completely correct
the way it is), read the spec page 15ff at
http://gexf.net/1.2draft/gexf-12draft-primer.pdf

Note that the NetworkX code tries to handle both the 1.1 spec and the
1.2draft spec which I think differ in the way they handle dynamic
data.

Aric

Aric Hagberg

unread,
Apr 13, 2011, 9:01:24 PM4/13/11
to networkx...@googlegroups.com
On Wed, Apr 13, 2011 at 12:16 PM, jockel <anjoer...@googlemail.com> wrote:
> Dear Aric,
>
> thank you for the reply. I tried your example and receive the
> following error:
>
>  File "generateGraph.py", line 140, in main
>    g = nx.read_gexf("dynamics.gexf")
>  File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> \readwrite\gexf.py", line 153, in read_gexf
>    G=reader(fh)
>  File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> \readwrite\gexf.py", line 492, in __call__
>    raise nx.NetworkXError("No <graph> element in GEXF file")
> networkx.exception.NetworkXError: No <graph> element in GEXF file
>
> I am on python 2.6 using networkx 1.4. The gexf is readable as is in
> Gephy, but I cannot load it in python.
> The gexf file is directly downloaded from http://gexf.net/format/dynamics.html
> (should be fine) and it contains a graph element...
>

I think this is a bug that we fixed fairly recently to handle 1.2draft
spec files.
https://networkx.lanl.gov/trac/ticket/497
That fix was after the networkx-1.4 release so you will need to
install the development version.
You can get that from
http://networkx.lanl.gov/download/networkx/dev/

Aric

Aric Hagberg

unread,
Apr 13, 2011, 9:07:37 PM4/13/11
to networkx...@googlegroups.com

Ticket at
https://networkx.lanl.gov/trac/ticket/540
for attaching example data and code fixes.

Aric
>

jockel

unread,
Apr 14, 2011, 4:25:17 AM4/14/11
to networkx-discuss
Hi,

I am not an expert in python so I do not know where to put the gexf.py
to replace the 'old' one in the networkx module. I will try to update
to the current developer version though.

Joern

On 14 Apr., 03:07, Aric Hagberg <ahagb...@gmail.com> wrote:
> On Wed, Apr 13, 2011 at 7:01 PM, Aric Hagberg <ahagb...@gmail.com> wrote:
> > On Wed, Apr 13, 2011 at 12:16 PM, jockel <anjoerngr...@googlemail.com> wrote:
> >> Dear Aric,
>
> >> thank you for the reply. I tried your example and receive the
> >> following error:
>
> >>  File "generateGraph.py", line 140, in main
> >>    g = nx.read_gexf("dynamics.gexf")
> >>  File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> >> \readwrite\gexf.py", line 153, in read_gexf
> >>    G=reader(fh)
> >>  File "C:\Python26\lib\site-packages\networkx-1.4-py2.6.egg\networkx
> >> \readwrite\gexf.py", line 492, in __call__
> >>    raise nx.NetworkXError("No <graph> element in GEXF file")
> >> networkx.exception.NetworkXError: No <graph> element in GEXF file
>
> >> I am on python 2.6 using networkx 1.4. The gexf is readable as is in
> >> Gephy, but I cannot load it in python.
> >> The gexf file is directly downloaded fromhttp://gexf.net/format/dynamics.html
> >> (should be fine) and it contains a graph element...
>
> > I think this is a bug that we fixed fairly recently to handle 1.2draft
> > spec files.
> >https://networkx.lanl.gov/trac/ticket/497
> > That fix was after the networkx-1.4 release so you will need to
> > install the development version.
> > You can get that from
> >  http://networkx.lanl.gov/download/networkx/dev/
>
> Ticket athttps://networkx.lanl.gov/trac/ticket/540
Reply all
Reply to author
Forward
0 new messages