Processing yEd graphml file in python
Asked Answered
C

2

11

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

Calle answered 15/12, 2014 at 15:48 Comment(0)
T
3

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
Tannic answered 22/11, 2015 at 11:22 Comment(2)
Thanks for sharing your experience. I can't figure out how to read a graphml file using pyyed, or get label text from pygraphml.Pancake
pip3 install bs4, pip3 install lxmlPancake
S
3

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!

Sinter answered 4/9, 2019 at 12:44 Comment(5)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Mono
Hi! Thanks for the suggestions ... let me add some info to the answer ... thanks!Sinter
I am using this library, it is brilliant! Does not work too well on large graphs though (more than a couple of 1000 nodes), neither does the visualiser yEd.Dissever
@OverInflatedWalrus, I agree that yEd cannot handle big graphs. But, in that case, you can try out Gephi which also supports graphml.Sinter
@LucasAimaretto yes, I am using Gephi on larger graphs - works well, calculates stats, give you general idea of the graph structure. yEd is more for the cases where visual representation is important. Visual representation does not make sense on very large graphs, but there is a niche where it is still meaningful but not working with yEd anymore.Dissever

© 2022 - 2024 — McMap. All rights reserved.