3

I have a GML(Graph (not Graffiti) Modeling Language) file from which I want to create a mapping of IDs to labels. I am at a loss as to how I can do this since my list manipulations do not seem to work. I tried using the example which specifies how to use two delimiters, but it would not work on my file.

Can someone please guide me?

The GML file is arranged as follows:

graph [
  node [
    id 0
    label "24"
  ]
  node [
    id 1
    label "25"
  ]
  node [
    id 2
    label "26"
  ]
  node [
    id 3
    label "27"
  ]
  node [
    id 4
    label "20"
  ]
  node [
    id 5
    label "21"
  ]
(... some more nodes)
  edge [
    source 0
    target 75
    weight 4
  ]
  edge [
    source 0
    target 68
    weight 2
  ]
(many more edges)
]

I want to extract the data in each node block and create a mapping of the node ID to the node label. For example, the first node's mapping would be 0->24 and so on. I am not worried about the edge information.

Thanks in advance for any help.

John Powell
  • 12,253
  • 6
  • 59
  • 67
adwaraki
  • 342
  • 1
  • 5
  • 14
  • [`pygml`](http://code.google.com/p/pygml/source/browse/trunk/pygml.py)? – Burhan Khalid Feb 08 '13 at 19:17
  • Can you please point me to any documentation, usage or anything? I tried finding it, but the only thing I could find was some code on Google Code. I am new to Python, so please excuse my naivete. :) – adwaraki Feb 08 '13 at 19:18
  • I should have clarified in the post itself, my mistake. GML meaning Graph Modeling Language, not Graffiti Modeling Language. – adwaraki Feb 08 '13 at 19:24

1 Answers1

10

If you can use NetworkX (Python) you can read the file (t.gml is your file) like this and get the mapping

In [1]: import networkx as nx

In [2]: G = nx.read_gml('t.gml')

In [3]: G.node
Out[3]: 
{0: {'id': 0, 'label': u'24'},
 1: {'id': 1, 'label': u'25'},
 2: {'id': 2, 'label': u'26'},
 3: {'id': 3, 'label': u'27'},
 4: {'id': 4, 'label': u'20'},
 5: {'id': 5, 'label': u'21'}}

e.g.

In [4]: G.node[0]['label']
Out[4]: u'24'
Aric
  • 24,511
  • 5
  • 78
  • 77
  • How do you change the file path using nx.read_gml? I'm uncertain where it is searching for gml file, but it cannot seem to find it. – mlg4080 Feb 01 '15 at 05:28