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

Community
  • 1
  • 1
belvoir
  • 335
  • 4
  • 9

2 Answers2

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
Tony J.
  • 81
  • 1
  • 10
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!

Lucas Aimaretto
  • 1,399
  • 1
  • 22
  • 34
  • 2
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) 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.](//stackoverflow.com/help/deleted-answers) – Dharman Sep 04 '19 at 12:53
  • Hi! Thanks for the suggestions ... let me add some info to the answer ... thanks! – Lucas Aimaretto Sep 04 '19 at 13:08
  • 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. – OverInflatedWalrus Feb 13 '20 at 10:23
  • @OverInflatedWalrus, I agree that yEd cannot handle big graphs. But, in that case, you can try out Gephi which also supports graphml. – Lucas Aimaretto Feb 26 '20 at 18:03
  • 1
    @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. – OverInflatedWalrus Mar 13 '20 at 17:09