1+ '''
2+ GETGRAPHEDGES Generate a graphml file from the weights matrix simulation output
3+
4+ This function reads the Graphitti weight matrix output from the
5+ growth simulation and reformats it to graphml for input into the
6+ STDP simulation.
7+
8+ Input:
9+ weights_file - Weight matrix output from Graphitti. The entire path can be used; for example
10+ '/CSSDIV/research/biocomputing/data/2025/tR_1.0--fE_0.90_10000/weights-epoch-1.xml'
11+ graphml_file - graphml file to add the edges. This is typically the same file
12+ used as input to the Graphitti simulation.
13+
14+ Output:
15+ - weight_graph.graphml
16+
17+ Author: Vanessa Arndorfer ([email protected] ) 18+ Last updated: 07/01/2025
19+ '''
20+
21+ import numpy as np
22+ import os
23+ import pandas as pd
24+ import sys
25+ import xml .etree .ElementTree as ET
26+
27+
28+ def xmlToNumpy (node , rows , cols ):
29+ print ("Converting xml to matrix for: " + node .tag )
30+
31+ m = np .zeros (shape = (rows , cols ))
32+ r = 0
33+ c = 0
34+ for child in node :
35+ m [r ][c ] = float (child .text )
36+ c += 1
37+
38+ # new row
39+ if c == cols :
40+ r += 1
41+ c = 0
42+
43+ return m
44+
45+
46+ def getWeightMatrix (file_name , src_root , weights_root ):
47+ print ("Building weights matrix for: " + file_name )
48+
49+ tree = ET .parse (file_name )
50+ root = tree .getroot ()
51+
52+ idNum = 0
53+ srcIdx = root .find (src_root )
54+ weights = root .find (weights_root )
55+
56+ rows = 10000
57+ cols = 200
58+
59+ srcIdx_np = xmlToNumpy (srcIdx , rows , cols )
60+ weights_np = xmlToNumpy (weights , rows , cols )
61+
62+ print ("Converting matrices into square format..." )
63+ edge_weights = np .zeros (shape = (rows , rows ))
64+
65+ weight_count = 0
66+
67+ for r in range (weights_np .shape [0 ]):
68+ for c in range (weights_np .shape [1 ]):
69+ if weights_np [r ][c ] != 0 :
70+ weight_count += 1
71+ src = int (srcIdx_np [r ][c ])
72+ edge_weights [src ][r ] = weights_np [r ][c ]
73+
74+ print ("Total weighted edges: " + str (weight_count ))
75+
76+ return edge_weights
77+
78+
79+ def getEdgeGraphML (edge_weights , graphml_file , output_dir ):
80+ print ("Adding edges to the graph file: " + graphml_file )
81+
82+ # register xml namespaces
83+ ET .register_namespace ('' , "http://graphml.graphdrawing.org/xmlns" )
84+ ET .register_namespace ('xsi' , "http://www.w3.org/2001/XMLSchema-instance" )
85+ ET .register_namespace ('xsi:schemaLocation' , "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd" )
86+
87+ tree = ET .parse (graphml_file )
88+ root = tree .getroot ()
89+
90+ # define weight attribute for edges
91+ weight_key = ET .Element ("key" , attrib = {"id" :"weight" , "for" :"edge" , "attr.name" :"weight" , "attr.type" :"double" })
92+ root .append (weight_key )
93+
94+ idNum = 0
95+ graph = root .find ('./{http://graphml.graphdrawing.org/xmlns}graph' )
96+
97+ for r in range (edge_weights .shape [0 ]):
98+ for c in range (edge_weights .shape [1 ]):
99+ if edge_weights [r ][c ] != 0 :
100+ edgeId = "e" + str (idNum )
101+ edge = ET .Element ("edge" , attrib = {"id" :edgeId , "source" :str (r ), "target" :str (c )})
102+ data = ET .SubElement (edge , "data" , attrib = {"key" : "weight" })
103+ data .text = str (edge_weights [r ][c ])
104+ graph .append (edge )
105+ idNum += 1
106+
107+ print (str (idNum ) + " edges created" )
108+ ET .indent (tree , space = "\t " , level = 0 )
109+ tree .write (output_dir + "/weight_graph.graphml" , encoding = "utf-8" , xml_declaration = True )
110+
111+
112+ if __name__ == "__main__" :
113+ # execution format: python3.9 ./getGraphEdges.py weights_file graphml_file
114+ # example: python3.9 ./getGraphEdges.py /CSSDIV/research/biocomputing/data/2025/tR_1.0--fE_0.90_10000/weights-epoch-25.xml configfiles/graphs/fE_0.90_10000.graphml
115+ weights_file = sys .argv [1 ]
116+ graphml_file = sys .argv [2 ]
117+
118+ ht = os .path .split (weights_file )
119+ output_dir = ht [0 ]
120+ print ("Output dir: " + output_dir )
121+
122+ df = getWeightMatrix (weights_file , "SourceVertexIndex" , "WeightMatrix" )
123+ getEdgeGraphML (df , graphml_file , output_dir )
124+
0 commit comments