-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
255 lines (178 loc) · 6.52 KB
/
graph.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from collections import Counter
import inspect
import os
import networkx as nx
# import stellargraph as sg
import argparse
import logging
#my imports
import vars_paths as vp
import utils
import saves
# def create_graph(nodes_list):
# graph = nx.Graph()
# # vertices
# for nodes in nodes_list:
# graph.add_nodes_from(nodes[0], color_nodes=nodes[2])
# # label dos vertices
# dict_nodes = {}
# for nodes in nodes_list:
# for n in nodes[0]:
# dict_nodes[n] = nodes[1]
# nx.set_node_attributes(graph, dict_nodes, 'label')
# # cria as restas
# edges = list(zip(nodes_list[0][0], nodes_list[1][0]))
# # conta os pesos das arestas
# weights = dict(Counter(edges))
# # arestas com peso
# weighted_edges = []
# for e in edges:
# weighted_edges.append((e[0], e[1], weights[(e[0], e[1])]))
# graph.add_weighted_edges_from(weighted_edges)
# return graph
# def create_graph_peer_weights2(nodes_list, peer_lists):
# graph = nx.Graph()
# # vertices
# for nodes in nodes_list:
# graph.add_nodes_from(nodes[0], color_nodes=nodes[2])
# # label dos vertices
# dict_nodes = {}
# for nodes in nodes_list:
# for n in nodes[0]:
# dict_nodes[n] = nodes[1]
# nx.set_node_attributes(graph, dict_nodes, 'label')
# # cria as restas
# edges = list(zip(nodes_list[0][0], nodes_list[1][0]))
# # print(edges, len(edges))
# # conta pesos das arestas
# weights = []
# for peer in peer_lists:
# weights.append(len(peer))
# # print(weights, len(weights))
# ew = zip(edges, weights)
# c = Counter()
# for k, v in ew:
# c[k] += v
# weighted_edges = []
# for e in edges:
# weighted_edges.append((e[0], e[1], c[(e[0], e[1])]))
# graph.add_weighted_edges_from(weighted_edges)
# return graph
# def create_graph_peer_weights3(monitors, trackers, peer_lists):
# graph = nx.Graph()
# # vertices
# graph.add_nodes_from(monitors[0], color_nodes=monitors[2])
# graph.add_nodes_from(trackers[0], color_nodes=trackers[2])
# if SHOWPEERS:
# for nodes in peer_lists[0]:
# graph.add_nodes_from(nodes, color_nodes=peer_lists[2])
# # USADO NO STELLARGRAPH
# # label dos vertices
# # dict_nodes = {}
# # for nodes in nodes_list[0:-1]:
# # for n in nodes[0]:
# # dict_nodes[n] = nodes[1]
# # nx.set_node_attributes(graph, dict_nodes, 'label')
# # arestas trackers peers
# if SHOWPEERS:
# edges_tp = []
# for i in range(len(peer_lists[0])):
# for peer in peer_lists[0][i]:
# edges_tp.append((trackers[0][i], peer))
# # print(edges_tp)
# graph.add_edges_from(edges_tp)
# # arestas monitors trackers
# edges_mt = list(zip(monitors[0], trackers[0]))
# # print(edges_mt, len(edges_mt))
# # conta pesos das arestas monitors trackers
# weights_mt = []
# for peer_list in peer_lists[0]:
# weights_mt.append(len(peer_list))
# # print(weights_mt, len(weights_mt))
# c = Counter()
# for k, v in zip(edges_mt, weights_mt):
# c[k] += v
# weighted_edges = []
# for e in edges_mt:
# weighted_edges.append((e[0], e[1], c[(e[0], e[1])]))
# graph.add_weighted_edges_from(weighted_edges)
# return graph
def main():
parser = argparse.ArgumentParser(description='Traces')
parser.add_argument('--file', '-f', help='input file', required=True, type=str)
parser.add_argument('--showpeers', '-p', help='peers in graph', action='store_true')
parser.add_argument('--showmaster', '-g', help='master in graph', action='store_true')
parser.add_argument('--showtrackers', '-t', help='trackers in graph', action='store_true')
parser.add_argument('--showmonitors', '-m', help='monitors in graph', action='store_true')
# parser.add_argument('--numberwindows', '-w', help='number windows', default=1, type=int)
# parser.add_argument('--numberedges', '-e', help='number edges', default=0, type=int)
help_msg = "Logging level (INFO=%d DEBUG=%d)" % (logging.INFO, logging.DEBUG)
parser.add_argument("--log", "-l", help=help_msg, default=vp.DEFAULT_LOG_LEVEL, type=int)
args = parser.parse_args()
if args.log == logging.DEBUG:
logging.basicConfig(format='%(asctime)s.%(msecs)03d: %(levelname)s {%(module)s} [%(funcName)s] %(message)s', datefmt=TIME_FORMAT, level=args.log)
else:
logging.basicConfig(format='%(asctime)s.%(msecs)03d: %(message)s', datefmt=vp.TIME_FORMAT, level=args.log)
logging.info('init ...')
utils.init(showmaster = args.showmaster, showpeers = args.showpeers, showtrackers = args.showtrackers, showmonitors = args.showmonitors)
logging.info('reading file ...')
epochs, trackers, monitors, peer_lists, leechers, seeders, ns = utils.read_file(args.file)
ls = []
for a, b in zip(leechers, seeders):
ls.append(a+b)
logging.info('calculating windows ...')
time_min, windows, windows_index_range = utils.cal_windows(epochs)
print('epochs:', len(epochs))
print('trackers:', len(trackers))
print('monitors:', len(monitors))
print('peer_lists:', len(peer_lists))
print('ls:', len(ls))
print('n:', len(ns))
print('windows:', len(windows))
for i in range(len(windows_index_range)):
print(i, windows_index_range[i])
logging.info('renaming entities ...')
# Label pra os vertices
monitor_labels = []
for m in monitors:
monitor_labels.append('M%d' % utils.my_hash_monitor(m))
# monitor_labels.append(str(utils.my_hash_monitor(m)))
# monitor_labels.append(m)
tracker_labels = []
for t in trackers:
tracker_labels.append('T%d' % utils.my_hash_tracker(t))
# tracker_labels.append(str(utils.my_hash_tracker(t)))
# tracker_labels.append(t)
peer_lists_labels = []
for l in peer_lists:
pl_labels = []
for p in l:
pl_labels.append('P%d' % utils.my_hash_peer(p))
# pl_labels.append(str(utils.my_hash_peer(p)))
# pl_labels.append(p)
peer_lists_labels.append(pl_labels)
ms = 'MS'
graphs = []
logging.info('creating graphs ...')
for wir in windows_index_range:
m = monitor_labels[wir[0]:wir[1]]
t = tracker_labels[wir[0]:wir[1]]
pl = peer_lists_labels[wir[0]:wir[1]]
wt = ls[wir[0]:wir[1]]
graph = utils.create_graph_master_tracker(ms, m, t, pl) #tcc
# graph = utils.create_graph_peer_weights(ms, m, t, pl)
graphs.append(graph)
# saves.show_graph(graph)
# for g in graphs:
# for e in g.edges.data():
# print(e)
# print('-------------')
saves.save_graphs_txt(graphs)
logging.info(str(len(graphs)) + ' graphs in directory:' + vp.PATH_GRAPHS + '/')
saves.save_graphs_fig(graphs)
logging.info(str(len(graphs)) + ' images graphs in directory:' + vp.PATH_FIGS + '/')
saves.save_graph_adj_csv(graphs)
saves.save_graph_weigths_csv(graphs)
logging.info('adjacency and weight matrices are directory:' + vp.PATH_MATRICES + '/')
if __name__ == '__main__':
main()