-
Notifications
You must be signed in to change notification settings - Fork 1
/
topologies_graphicator.py
77 lines (62 loc) · 2.17 KB
/
topologies_graphicator.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
'''
This script reads the topologies stored in the topologies directory
and generates the images that represent them.
'''
import os
import networkx as nx
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
__author__ = "Marcelo Bianchetti"
__credits__ = ["Marcelo Bianchetti"]
__version__ = "1.0.0"
__maintainer__ = "Marcelo Bianchetti"
__email__ = "mbianchetti at dc.uba.ar"
__status__ = "Production"
def readTopology(tops_dir, top_fname):
G = nx.DiGraph()
''' Returns the graph '''
with open(os.path.join(tops_dir, top_fname)) as f:
for l in f:
if l.startswith('#'):
continue
ls = l.split()
n, m = int(ls[0]), int(ls[1])
G.add_nodes_from([i for i in range(n)])
for i in range(m):
l = f.readline().split()
e1, e2 = int(l[0]), int(l[1])
G.add_edge(e1, e2)
G.add_edge(e2, e1)
return G
def draw(G, iname, layout='net', **args):
if layout == 'net':
nx.draw_networkx(G, **args)
if layout == 'circ':
nx.draw_circular(G, **args)
if layout == 'kam':
nx.draw_kamada_kawai(G, **args)
if layout == 'spec':
nx.draw_spectral(G, **args)
if layout == 'rnd':
nx.draw_random(G, **args)
plt.savefig(iname, dpi=400)
plt.close()
return
if __name__ == "__main__":
main_dir = os.path.dirname(os.path.abspath(__file__))
topologies_dir = os.path.join(main_dir, 'topologies')
images_dir = os.path.join(main_dir, 'images')
image_fname = 'image_{}_{}.png'
if not os.path.exists(images_dir):
os.makedirs(images_dir)
for top_fname in os.listdir(topologies_dir):
top_name = os.path.splitext(top_fname)[0]
G = readTopology(topologies_dir, top_fname)
image_dir = os.path.join(images_dir, image_fname)
layouts = ['net', 'circ', 'kam', 'spec', 'rnd']
for layout in layouts:
iname = image_dir.format(top_name, layout)
draw(G, iname, layout, with_labels=True, node_color='#abcfff',
arrowsize=5, node_size=250)