NetworkX 3.0 GraphML Writer: Handling of 'edgedefault' and Node ID Conversion
0 reputation · 16 May 2024, 13:31 UTC
0 reputation · 16 May 2024, 13:31 UTC
NetworkX 3.0 now writes an explicit edgedefault attribute (directed or undirected) in GraphML files, replacing the prior implicit inference from the graph type. The reader no longer infers directedness when this attribute is missing; it defaults to undirected and issues a warning if directed edges are present.
Additionally, node identifiers of non‑string or non‑int types are silently converted to strings during read/write, which can alter the graph structure when reloaded. Edge attributes not defined in the GraphML schema are dropped, and the writer no longer accepts an encoding keyword argument, potentially causing errors with non‑ASCII data.
These changes create unresolved decisions regarding how to preserve node types, manage directedness inference, and support encoding when writing GraphML.
edgedefault is missing but directed edges exist?encoding parameter to handle non‑ASCII characters reliably?35275 reputation · 16 May 2024, 15:59 UTC
In NetworkX 3.0 the GraphML writer always writes an explicit edgedefault attribute (set to directed or undirected) and converts every node identifier to a string. The encoding keyword argument was removed, so output is UTF‑8 XML by default.
edgedefault to the GraphML header; the reader uses it when present to set graph directedness.str on write and returned as strings on read; original non‑string IDs are not preserved.write_graphml no longer accepts an encoding argument; the produced XML is UTF‑8.The change makes directedness explicit instead of being inferred from the graph type, and it enforces GraphML’s requirement that node IDs be strings. Removing the encoding argument simplifies the API because the underlying XML writer already uses UTF‑8.
enumerate or a dict).nx.relabel_nodes using that mapping.write_graphml.edgedefault (the writer does this automatically). When reading older files that lack the attribute, the reader defaults to undirected and warns; you can either add the attribute manually or suppress the warning after confirming the graph is indeed directed.encoding='UTF-8' (it does).Are you seeing warnings about a missing edgedefault attribute when reading existing GraphML files? Knowing whether the files were generated with a pre‑3.0 version will determine if you need to add the attribute or adjust the reader’s handling.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 16 May 2024, 18:48 UTC
GraphML requires node id attributes to be strings per the spec, so NetworkX 3.0’s silent conversion of non-string / non-int node identifiers to strings on read/write is spec-compliant, even if it changes graph identity on round-trip. The writer now emits an explicit edgedefault attribute, and the reader defaults to undirected when that attribute is missing and warns if directed edges are present.
For verification, assume NetworkX 3.0 behavior: write a graph with mixed node types, inspect the generated XML for edgedefault="directed" or undirected, then reload and compare G.is_directed() and node key types. The conversion is intentional to meet GraphML constraints, not a bug, which affects any downstream code relying on original identifier types.