-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsndlib2xml.py
48 lines (31 loc) · 1.49 KB
/
sndlib2xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import graph_tool.all as gt
import numpy as np
def scale(rawpoints, low=0.0, high=100.0):
mins = np.min(rawpoints, axis=0)
maxs = np.max(rawpoints, axis=0)
rng = maxs - mins
return high - (((high - low) * (maxs - rawpoints)) / rng)
def sndlib(f):
from xml.etree import ElementTree as ET
root = ET.parse(f)
ns = {'s': 'http://sndlib.zib.de/network'}
g = gt.Graph(directed=False)
g.add_vertex(len(root.findall('*/*/s:node', ns)))
index = {e.get('id'):i for i,e in enumerate(root.findall('*/*/s:node', ns))}
for e in root.findall('*/*/s:link', ns):
g.add_edge(index[e.find('s:source', ns).text], index[e.find('s:target', ns).text])
pos = g.new_vertex_property('vector<float>', scale(np.array([(float(e.find('*/s:x', ns).text), -float(e.find('*/s:y', ns).text)) for e in root.findall('*/*/s:node', ns)])))
gt.remove_parallel_edges(g)
gt.remove_self_loops(g)
return g, pos
if __name__ == '__main__':
from sys import argv
from re import findall
from statistics import mean
for f in argv[1:]:
g, pos = sndlib(f)
name = findall('[^/.]+', f)[-2]
deg = g.degree_property_map('total').a
print(name, g.num_vertices(), g.num_edges(), '{:.2f}'.format(mean(deg)), min(deg), max(deg), sep=' & ', end=' \\\\\n')
g.save('output/{}.xml'.format(name))
gt.graph_draw(g, pos, vertex_size=2, vertex_color='black', vertex_fill_color='black', edge_pen_width=0.2, edge_color='black', output='output/{}.pdf'.format(name), output_size=(75,75))