Skip to content

Fix write_gexf timeformat for dynamic Graphs - #7914

Merged
MridulS merged 2 commits into
networkx:mainfrom
lmeNaN:patch-1
Mar 28, 2025
Merged

MridulS merged 2 commits into
networkx:mainfrom
lmeNaN:patch-1

Conversation

@lmeNaN

@lmeNaN lmeNaN commented Mar 18, 2025

Copy link
Copy Markdown

When using write_gexf on a Graph containing start/end attributes, the Graph timeformat parameter was not set, breaking compatibility with the gexf draft. This ensures that the timeformat parameter is set in the .gexf tag.

Before this fix, the .gexf file obtained when using write_gexf on a dynamic graph would not contain a timeformat attribute in the graph tag. This made read_gexf unable to parse the Graph.

After this fix, the timeformat argument for the graph tag is saved on line 6 of the gexf file, ensuring that a dynamic Graph exported through write_gexf can then be loaded again through read_gexf.

When using write_gexf on a Graph containing start/end attributes, the Graph timeformat parameter was not set, breaking compatibility with the gexf standard.
This ensures that the timeformat parameter is set in the .gexf <graph> tag.

@rossbar rossbar left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is a little hairy, but AFAICT the proposal is basically just to delete the top-level if that checks whether the mode is "static" - this seems reasonable to me, especially since AIUI the start/end/timeformat attrs are only relevant when mode="dynamic".

We'd have to take a deeper look at other parts of the code to see where/whether the mode is set. If there's a minimal example of the buggy behavior, it'd be great to add that as a test!

@lmeNaN

lmeNaN commented Mar 19, 2025

Copy link
Copy Markdown
Author

I wasn't in the right environment to publish a minimal example but here it is:

import networkx

if __name__ == '__main__':
    G = networkx.MultiGraph(mode='dynamic')
    G.add_node('U')
    G.nodes['U']['start'] = 1
    networkx.write_gexf(G,'/tmp/example.gexf')

    G = networkx.read_gexf('/tmp/example.gexf')

Specifying the Graph mode as 'dynamic' is what causes the buggy behavior.

diff /tmp/example.gexf /tmp/example_dynamic_graph_attr.gexf 
6c6
<   <graph defaultedgetype="undirected" mode="dynamic" name="" timeformat="long">
---
>   <graph defaultedgetype="undirected" mode="dynamic" name="">

Removing the check fixes that and infers whether the Graph is static or dynamic if a start or end attribute is present.

@rossbar

rossbar commented Mar 19, 2025

Copy link
Copy Markdown
Contributor

Thanks @lmeNaN for the example - that makes sense then. Let's go ahead and turn that into a test and I think this is good to go!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty of converting your example to an explicit test case and pushed it up. This LGTM - thanks @lmeNaN !

@MridulS MridulS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks @lmeNaN !

@MridulS
MridulS merged commit 0dd5cf2 into networkx:main Mar 28, 2025
@rossbar rossbar mentioned this pull request Apr 14, 2025
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants