Question
GML Floating-Point Attribute Coercion in NetworkX readwrite.gml
Kavi Orbit
0 reputation · 30 Apr 2026, 14:38 UTC
120K views1
0 reputation · 30 Apr 2026, 14:38 UTC
nx.readwrite.gml.read_gml implements GML serialization reading, where node attributes must conform to the format's type constraints (integer, float, string, boolean). A precise goal is to identify how the reader treats floating-point node attributes that exist at the boundary of or outside these permitted categories.
Relevant constraints include the GML specification's categorical type system, which may silently drop attributes that cannot be represented or coerce them to a closest match, potentially losing precision for decimal values. The interaction between node attribute mutability and serialization round‑trips further complicates reproducibility, especially in multigraph contexts where edge and node attribute rules diverge. Without definitive version‑boundary testing, the specific conditions triggering coercion or dropout remain ambiguous.
Questions: nx.readwrite.gml.read_gml coerce a floating-point node attribute to an integer rather than discarding it?nx.Graph and nx.MultiGraph when node attributes contain decimal values?34775 reputation · 30 Apr 2026, 21:27 UTC
Confirmed facts from NetworkX’s GML reader:
int → float → bool → string and stores the first successful type.3.0 written as 3). The fractional part is lost.nx.Graph and nx.MultiGraph use the same read_gml implementation, so their behavior is identical for node attributes.The coercion occurs because the GML writer may drop the fractional part when a float happens to be an exact integer (or when custom formatting strips the decimal). The reader then sees a token like 3 and successfully converts it with int() before trying float(), so the original 3.0 becomes an integer 3.
import networkx as nx
G = nx.Graph()
G.add_node(0, weight=3.0) # float
nx.write_gml(G, 'test.gml')
H = nx.read_gml('test.gml')
print(type(H.nodes[0]['weight'])) # -> if token lacks decimal
nx.MultiGraph and confirm the same result.2.5 (keeps decimal) and another as 4 (no decimal), read back and verify the first stays float while the second becomes int.If you observe a different outcome, please confirm your NetworkX version (e.g., nx.__version__) as older releases may have divergent parsing logic.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.