Visualizes information flow connections in humans
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

136 lines
4.5 KiB

#!/usr/bin/python
import csv
import xml.etree.ElementTree
import sys
#if len(sys.argv) != 2:
# print("Usage: ./convert.py <graph.graphml>")
# sys.exit()
#fname = sys.argv[1]
#raw = open(fname, "r").read()
BLACKLIST = ['Unknown', 'Corpuscallosum']
URI = "http://openconnecto.me/data/public/small_graphs/mrn/MRN114_M87136332_small_graph_w_inv.graphml"
tree = xml.etree.ElementTree.parse("MRN114_M87136332_small_graph_w_inv.graphml")
root = tree.getroot()
for child in root:
if child.tag.endswith('graph'):
graph = child
break
rawnodes = []
rawedges = []
edges_weighted = {}
edges = []
nodes = []
nodenames = []
nodeid_to_name = {}
def translate_name_to_token(name):
translation_table = {
'Bankssts': 'cns.temp.sts.banks',
'Caudalmiddlefrontal': 'cns.frontal.middle.caudal',
'Caudalanteriorcingulate': 'cns.basal.cingulate.anterior.dorsal',
'Cuneus': 'cns.vis.cuneus',
'Entorhinal': 'cns.temp.medial.entorhinal',
'Fusiform': 'cns.temp.fusi',
'Inferiorparietal': 'cns.pari.IPL',
'Inferiortemporal': 'cns.temp.ITC',
'Isthmuscingulate': 'cns.basal.cingulate.isthmus',
'Lateraloccipital': 'cns.vis.lateral',
'Lateralorbitofrontal': 'cns.frontal.ofc.lateral',
'Lingual': 'cns.vis.lingual',
'Medialorbitofrontal': 'cns.frontal.ofc.medial',
'Middletemporal': 'cns.temp.middle',
'Parahippocampal': 'cns.temp.parahippocampal',
'Paracentral': 'cns.frontal.paracentral',
'Parsopercularis': 'cns.frontal.parsopercularis',
'Parsorbitalis': 'cns.frontal.parsorbitalis',
'Parstriangularis': 'cns.frontal.parstriangularis',
'Pericalcarine': 'cns.vis.pericalcarine',
'Postcentral': 'cns.pari.S1',
'Posteriorcingulate': 'cns.basal.cingulate.posterior',
'Precentral': 'cns.frontal.motor.primary',
'Precuneus': 'cns.pari.precuneus',
'Rostralanteriorcingulate': 'cns.basal.cingulate.anterior.rostral',
'Rostralmiddlefrontal': 'cns.frontal.middle.rostral',
'Superiorfrontal': 'cns.frontal.superior',
'Superiorparietal': 'cns.pari.SPL',
'Superiortemporal': 'cns.temp.STC',
'Supramarginal': 'cns.pari.supramarginal',
'Frontalpole': 'cns.frontal.pole',
'Temporalpole': 'cns.temp.pole',
'Transversetemporal': 'cns.temp.A1.transverse',
}
return translation_table.get(name, name)
def normalize_region_name(name):
name = name[3:].capitalize()
return name
for item in graph:
if item.tag.endswith('edge'):
rawedges.append(item)
elif item.tag.endswith('node'):
rawnodes.append(item)
for node in rawnodes:
nodeid = node.attrib['id']
for item in node:
key = item.attrib['key']
if key == 'v_region_name':
nodename = item.text
name = normalize_region_name(nodename)
wiki = "http://www.wikipedia.org/search-redirect.php?family=wikipedia&search=%s&language=en&go=Go" % name
nodeid_to_name[nodeid] = name
if name not in BLACKLIST and name not in nodenames:
nodes.append([translate_name_to_token(name), name, "", "", wiki])
nodenames.append(name)
for edge in rawedges:
weight = 0
for item in edge:
key = item.attrib['key']
if key == 'e_weight':
weight = int(item.text)
source = translate_name_to_token(nodeid_to_name[edge.attrib['source']])
target = translate_name_to_token(nodeid_to_name[edge.attrib['target']])
if source != target and source not in BLACKLIST and target not in BLACKLIST:
try:
edges_weighted[(source, target)] += weight
except KeyError:
edges_weighted[(source, target)] = weight
for connection, weight in edges_weighted.items():
attr = "nodir"
if weight < 5000:
continue
attr += '|none'
elif weight < 7000:
attr += "|weak"
elif weight > 45000:
attr += "|vvstrong"
elif weight > 35000:
attr += "|vstrong"
elif weight > 25000:
attr += "|strong"
source, target = connection
edges.append([source, target, attr, URI])
edgewriter = csv.writer(open("edges.csv", "w"), dialect='unix')
edgewriter.writerow(["Source Name", "Destination Name", "Attributes", "URL"])
edgewriter.writerows(edges)
if False:
nodewriter = csv.writer(open("nodes.csv", "w"), dialect='unix')
nodewriter.writerow(["Token", "Displayed Name", "Full Name", "Attributes", "URL"])
nodewriter.writerows(nodes)
else:
nodes = []
print("wrote %d nodes, %d edges" % (len(nodes), len(edges)))