Export format for graphs loses type information?

2018-02-18 16:33:14

I have a situation where I am adding properties to vertices in my graph. In my case, I am adding a moduleId, which is an integer, and a kind, which is a string. I attach a weight to the edge as well.

correctGraph = Graph[ {

Property[Property["a", "moduleid" -> 121.4`], "kind" -> "class"],

Property[Property["b", "moduleid" -> 25.6`], "kind" -> "table"]},

{ Property["a" \[DirectedEdge] "b", EdgeWeight -> 5.34`]},

EdgeLabels -> "EdgeWeight", VertexLabels -> "Name"]

This generates the result, I am looking for:

When I look at the type of the moduleId property, I get the correct result

In[64]:= PropertyValue[{correctGraph, "a"}, "moduleid"]

Out[64]= 121.4

In[65]:= NumberQ[PropertyValue[{correctGraph, "a"}, "moduleid"]]

Out[65]= True

In[66]:= Normal@WeightedAdjacencyMatrix[correctGraph]

Out[66]= {{0, 5.34}, {0, 0}}

If I then export the graph and reimport it, the type of the moduleId changes to string. It seems that the type of the original property is not

  • GraphML export in Mathematica is a bit wonky, and the files that are produced are not standards-compliant. More importantly, many other systems will simply not read these GraphML files.

    For this reason, I included an alternative GraphML exporter in IGraph/M.

    For your graph, it produces this:

    IGExportString[correctGraph, "GraphML"]



    xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns \http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>



    attr.type='string' />



    attr.type='double' />



    attr.type='string' />



    2018-02-18 17:10:57