Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odML to graphML converter. #175

Open
msuzen opened this issue Sep 29, 2017 · 0 comments
Open

odML to graphML converter. #175

msuzen opened this issue Sep 29, 2017 · 0 comments

Comments

@msuzen
Copy link
Contributor

msuzen commented Sep 29, 2017

graphML link is used in graphics community a lot. Here is a simple conversion code that can be used in odML. This would be helpful for importing odML to graph databases natively. Here is an example conversion that can be used as a basic starting point.

"""

 odml2graphml

 Basic odML to graphML converter

 (c) 2017 German Neuroinformatics Node (G-Node)

"""

import xml.etree.ElementTree as ET
from pygraphml import Graph
from pygraphml import GraphMLParser
import uuid

# Files
base_name     = 'rg_dummy'
odml_fname    = base_name + '.odml'
graphml_fname = base_name + '.graphml'

# Read odml
tree = ET.parse(odml_fname)
root = tree.getroot()

odml_graph            = Graph()
child_id              = 0
is_terminal_node_prev = False
prev_uuid_value       = 0
prev_node             = 0
is_odml_rel_node      = 0
prev_tag              = 'odML
for elem in tree.iter(): # iter over elements
    is_terminal_node = elem.getchildren() == []
    is_odml_tag      = elem.tag in ['section', 'odML', 'property']
    if(is_odml_tag):
        uuid_value          = str(uuid.uuid4()) # generate UUID for an element
        node                = odml_graph.add_node(uuid_value)
        node["odml_entity"] = elem.tag
        node["uuid"]        = uuid_value
        child_id            = 1
        is_odml_rel_node    = 0
        if(elem.tag == "odML"):
           odml_node = node
    else:
        node[elem.tag]        = elem.text
        node["labels"]        = ":reach2Grasp"
        is_terminal_node_prev = is_terminal_node
    if(is_odml_rel_node < 1 and child_id > 0 and is_odml_tag == True and prev_tag in ['section', 'property']):
       edge = odml_graph.add_edge(prev_node, node, directed=True)
       edge = odml_graph.add_edge(odml_node, node, directed=True)
       edge['relation'] = 'child'
    if(elem.tag in ['section', 'odML']):
       is_odml_rel_node   = 1
       prev_node          = node
       prev_uuid_value    = uuid_value
       prev_tag           = elem.tag
parser = GraphMLParser()
parser.write(odml_graph, graphml_fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant