Created
March 22, 2018 09:09
-
-
Save kantale/ff9eecf60bfea95b3653f1b66a29a07a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gexf(nodes, edges, output_fn, nodes_meta=[], edges_meta=[]): | |
''' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2"> | |
<meta lastmodifieddate="2009-03-20"> | |
<creator>Gexf.net</creator> | |
<description>A hello world! file</description> | |
</meta> | |
<graph mode="static" defaultedgetype="directed"> | |
<attributes class="node"> | |
$$NODEATTRIBUTES$$ | |
</attributes> | |
<attributes class="edge"> | |
$$EDGEATTRIBUTES$$ | |
</attributes> | |
<nodes> | |
<node id="0" label="Hello" /> | |
<node id="1" label="Word" /> | |
</nodes> | |
<edges> | |
<edge id="0" source="0" target="1" /> | |
</edges> | |
</graph> | |
</gexf> | |
''' | |
ret = '''<?xml version="1.0" encoding="UTF-8"?> | |
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2"> | |
<meta lastmodifieddate="2009-03-20"> | |
<creator>Gexf.net</creator> | |
<description>A hello world! file</description> | |
</meta> | |
<graph mode="static" defaultedgetype="directed"> | |
<attributes class="node"> | |
$$NODEATTRIBUTES$$ | |
</attributes> | |
<attributes class="edge"> | |
$$EDGEATTRIBUTES$$ | |
</attributes> | |
<nodes> | |
$$NODES$$ | |
</nodes> | |
<edges> | |
$$EDGES$$ | |
</edges> | |
</graph> | |
</gexf> | |
''' | |
attributes_l = lambda m : '\n'.join(['<attribute id="{}" title="{}" type="float"/>'.format(x_i, x) for x_i, x in enumerate(m)]) | |
node_attributes = attributes_l(nodes_meta) | |
edge_attributes = attributes_l(edges_meta) | |
#meta_writer = lambda metas, d: ' '.join(['{}="{}"'.format(x, d[x]) for x in metas]) | |
def meta_writer(meta_list, data): | |
ret = '<attvalues>\n' | |
ret += '\n'.join(['<attvalue for="{}" value="{}" />\n'.format(x_i, data[x]) for x_i, x in enumerate(meta_list)]) | |
ret += '</attvalues>\n' | |
return ret | |
def node_writer(node): | |
return '<node id="{}" label="{}">\n{}</node>\n'.format(node['id'], node['name'], meta_writer(nodes_meta, node) ) | |
nodes_ml = '\n'.join(['<node id="{}" label="{}">\n{}\n</node>'.format( | |
x['id'], | |
x['name'], | |
meta_writer(nodes_meta, x) | |
) for x in nodes]) | |
edges_ml = '\n'.join(['<edge id="{}" source="{}" target="{}">\n{}\n</edge>'.format( | |
x_i, | |
x['source'], | |
x['target'], | |
meta_writer(edges_meta, x)) for x_i, x in enumerate(edges)]) | |
text = ret.replace('$$NODES$$', nodes_ml) | |
text = text.replace('$$EDGES$$', edges_ml) | |
text = text.replace('$$NODEATTRIBUTES$$', node_attributes) | |
text = text.replace('$$EDGEATTRIBUTES$$', edge_attributes) | |
if output_fn: | |
with open(output_fn, 'w') as f: | |
f.write(text) | |
print ('Created file:', output_fn) | |
else: | |
print (text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment