3

Ideally I would like to have networkx produce a graphML like the follwoing i.e. with attribute inside the data tag. This is because the value in the dta tag and the attribute goldKey has a one to one correspondence:

    <node id="1">
      <data key="d0">-10</data>
      <data key="d5" goldKey=5394 >viSram</data>
    </node>
    <node id="2">
      <data key="d5" globKey=2036 >BAvay</data>
      <data key="d0" >-23</data>
    </node>

I am aware that this is valid in graphML. But in networkx how do we achieve this?

Problem details

We have a networkx graph object, where the nodes need to be stored with multiple attributes. But two of the attributes have a one to one correspondence, i.e. the mapping looks like this

1151:'yogin'
2036:'BAvay'
5934:'viSram'

When using networkx I can store them as separate attributes, without the correspondence being reflected in the graphML file, as shown below:

k.add_node(1, {5392:'viSram'}, cng=108)

The correspomding graphML as provided by the networkx default write_graphml method looks like this

   <key attr.name="lemma" attr.type="string" for="node" id="d2" />
    <key attr.name="globKey" attr.type="int" for="node" id="d1" />
    <key attr.name="cng" attr.type="int" for="node" id="d0" />
    <node id="1">
          <data key="d0">108</data>
          <data key="d1">5394</data>
          <data key="d2">viSram</data>
    </node>

If I store the same as a dictionary in networkx, the correspondence gets reflected. But goes to unnecessary mapping as shown below:

  <key attr.name="1151" attr.type="string" for="node" id="d3" />
  <key attr.name="2036" attr.type="string" for="node" id="d2" />
  <key attr.name="5934" attr.type="string" for="node" id="d1" />
  <key attr.name="cng" attr.type="string" for="node" id="d0" />

    <node id="1">
      <data key="d0">-10</data>
      <data key="d1">viSram</data>
    </node>
    <node id="2">
      <data key="d2">BAvay</data>
      <data key="d0" >-23</data>
    </node>
    <node id="3">
      <data key="d0">29</data>
      <data key="d3" >yogin</data>
    </node>
    <node id="4">
      <data key="d2">as</data>
      <data key="d0">-23</data>
    </node>
Amrith Krishna
  • 2,768
  • 3
  • 31
  • 65

0 Answers0