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 respected. If I try to roundtrip the graph into graphml and back again:
newgraph = ImportString[ExportString[correctGraph, "graphml"], "graphml"];
I do not get the equality that might be expected:
newgraph == correctGraph
It turns out that the types of the moduleId properties has become strings instead of numbers:
In[34]:= { NumberQ[PropertyValue[{newgraph, "a"}, "moduleid"]],
NumberQ[PropertyValue[{correctGraph, "a"}, "moduleid"]] }
Out[34]= {False, True}
I have edited the exported string slightly for readability, hoping I did not lose anythng important:
<?xml version='1.0' encoding='UTF-8'?>
<graphml>
<key id='nodeKey1'
for='node'
attr.name='kind'
attr.type='String' />
<key id='nodeKey3'
for='node'
attr.name='moduleid'
attr.type='String' />
<key id='edgeKey2'
for='edge'
attr.name='EdgeWeight'
attr.type='String' />
<graph id='Graph1'
edgedefault='directed'>
<node id='a'>
<data key='nodeKey1'>class</data>
<data key='nodeKey3'>121.4`</data>
</node>
<node id='b'>
<data key='nodeKey1'>table</data>
<data key='nodeKey3'>25.6`</data>
<data key='nodeKey5'>Name</data>
</node>
<edge id='e1'
source='a'
target='b'>
<data key='nodeKey2'>EdgeWeight</data>
<data key='edgeKey2'>5.34`</data>
</edge>
</graph>
</graphml>
As you can see, the exported file stipulates that the type of the moduleId property should be string, not a number:
<node id='a'>
...
<data key='nodeKey3'>121.4`</data>
</node>
I would have expected the attr.type attribute to read "double" instead of string. In fact, the type 'String' as specified may not even be legal, since the standard for the graphml format specifies:
The type of the GraphML-Attribute can be either boolean, int, long, float, double, or string. These types are defined like the corresponding types in the Java(TM)-Programming language.
Unfortunately, changing the type to double and importing it does not provide the expected relief.
It would seem that the code for doing export is sloppy. A preliminary investigation shows that the gxl format is no better.
Since I am new at mathematica, I know that a little humility is in order (I may be doing something wrong), but I would like to know what the process is for posting bugs to Wolfram?
BR PV
==
/===
are reliable for comparing graphs ... I'd avoid them ... I'm saying this becauseGraph
objects are atomic and rather complex. $\endgroup$ – Szabolcs Jan 4 '15 at 23:50