Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing yEd graphml file in python

Tags:

python

graphml

I want to get a list of all nodes and some attributes (e.g. label name) in a yEd created graphml file regardless of where they are located in the graph. This has been partially dealt with already (Processing XML file with networkx in python and How to iterate over GraphML file with lxml) but not when you 'group' nodes within yEd - and I have lots of groupings within groupings.

Have tried networkx and lxml but not getting complete set of results using simple approaches suggested - any suggestions on elegant way to resolve and which library to use short of recursively iterating through tree and identifying group nodes and drilling down again.

Example:

Sample output for very simple graph using networkx when you have groupings:

('n0', {})
('n1', {'y': '0.0', 'x': '26.007967509920633', 'label': 'A'})
('n0::n0', {})
('n0::n1', {})

Simple representation of the graph

like image 366
belvoir Avatar asked Dec 15 '14 15:12

belvoir


People also ask

What is a GraphML file?

What is GraphML? GraphML is a comprehensive and easy-to-use file format for graphs. It consists of a language core to describe the structural properties of a graph and a flexible extension mechanism to add application-specific data.


2 Answers

After trying out networkx, lxml and pygraphml, I decided they won't do the job at all. I'm using BeautifulSoup and writing everything from the ground up:

from bs4 import BeautifulSoup

fp = "files/tes.graphml"

with open(fp) as file:
    soup = BeautifulSoup(file, "lxml")

    nodes = soup.findAll("node", {"yfiles.foldertype":""})
    groups = soup.find_all("node", {"yfiles.foldertype":"group"})
    edges = soup.findAll("edge")

Then you get your results like this:

print " --- Groups --- "
for group in groups:
    print group['id']
    print group.find("y:nodelabel").text.strip()

print " --- Nodes --- "
for node in nodes:
    print node['id']
    print node.find("y:nodelabel").text.strip()

This should get you going. You can make Group, Node & Edge objects and use them for some processing.

I may open source the library I am working on as it would be used for a bigger purpose than just parsing graphs.

enter image description here

And the output:

 --- Groups --- 
n0 / SimpleApp
 --- Nodes --- 
n0::n0 / main
n0::n1 / say hello
n1 / Exit
 --- Edges --- 
n0::e0 / n0::n0 / n0::n1 / str:username, int:age
e0 / n0::n1 / n1 / None
like image 139
Tony J. Avatar answered Oct 15 '22 15:10

Tony J.


I think you can try this out.

It is a Python library that, as per the author...

provides an easy interface that lets you specify how a graph should look, and generates corresponding graphML that can be opened in yEd.

https://github.com/jamesscottbrown/pyyed

Hope this helps!

Cheers!

like image 21
Lucas Aimaretto Avatar answered Oct 15 '22 15:10

Lucas Aimaretto