-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments_reddit.py
416 lines (373 loc) · 20.5 KB
/
experiments_reddit.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import division
import pprint
import time
from collections import OrderedDict, defaultdict
import networkx as nx
from ged_lib import Tracker
from ged_lib import preprocessing
from metrics import evaluate
from muturank import Muturank_new
from ordered_set import OrderedSet
from tabulate import tabulate
from tensor import TensorFact
class Data(object):
def __init__(self, comms, graphs, timeFrames, number_of_dynamic_communities, dynamic_truth={}):
self.comms = comms
self.graphs = graphs
self.timeFrames = timeFrames
self.number_of_dynamic_communities = number_of_dynamic_communities
self.dynamic_truth = dynamic_truth
def object_decoder(obj, num):
if 'type' in obj[num] and obj[num]['type'] == 'reddit':
edges = {int(tf): [(edge[0], edge[1]) for edge in edges] for tf, edges in obj[num]['edges'].items()}
graphs = {}
for i, edges in edges.items():
graphs[i] = nx.Graph(edges)
comms = {int(tf): {int(id): com for id, com in coms.items()} for tf, coms in obj[num]['comms'].items()}
dynamic_coms = {int(id): [str(node) for node in com] for id, com in obj[num]['dynamic_truth'].items()}
return Data(comms, graphs, len(graphs), len(dynamic_coms), dynamic_coms)
return obj
def create_Data_object_from_graphs(Gs):
def to_dict(d):
if isinstance(d, defaultdict):
d = {k: to_dict(v) for k, v in d.items()}
elif isinstance(d, set) or isinstance(d, OrderedSet):
d = list(d)
return d
communities = defaultdict(lambda: defaultdict(set))
dynamic_communities = defaultdict(OrderedSet) # dynamic_truth
graphs = {}
for G in Gs:
tf = int(G.name[-1])
graphs[tf] = G
for edge in G.edges(data=True):
subreddit = edge[2]['subreddit']
communities[tf][subreddit].add(edge[0])
communities[tf][subreddit].add(edge[1])
dynamic_communities[subreddit].add(edge[0] + '-t' + str(tf))
dynamic_communities[subreddit].add(edge[1] + '-t' + str(tf))
return Data(to_dict(communities), graphs, len(Gs), len(dynamic_communities), to_dict(dynamic_communities))
def run_experiments(data, ground_truth, results_file, overlap=False):
times = []
all_res = []
ov = '-Ov' if overlap else ''
# Timerank with one connection - default q
start_time = time.time()
mutu4 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='one',
clusters=len(ground_truth), default_q=True, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu4.dynamic_coms, "Timerank-STC-Uni" + ov, mutu4.tfs,
eval="dynamic", duration=mutu4.duration))
all_res.append(evaluate.get_results(ground_truth, mutu4.dynamic_coms, "Timerank-STC-Uni" + ov, mutu4.tfs,
eval="sets", duration=mutu4.duration))
all_res.append(evaluate.get_results(ground_truth, mutu4.dynamic_coms, "Timerank-STC-Uni" + ov, mutu4.tfs,
eval="per_tf", duration=mutu4.duration))
duration = time.time() - start_time
print("Timerank with one connection - default q: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
# Timerank with all connections - default q
start_time = time.time()
mutu5 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='all',
clusters=len(ground_truth), default_q=True, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu5.dynamic_coms, "Timerank-AOC-Uni" + ov, mutu5.tfs,
eval="dynamic", duration=mutu5.duration))
all_res.append(evaluate.get_results(ground_truth, mutu5.dynamic_coms, "Timerank-AOC-Uni" + ov, mutu5.tfs,
eval="sets", duration=mutu5.duration))
all_res.append(evaluate.get_results(ground_truth, mutu5.dynamic_coms, "Timerank-AOC-Uni" + ov, mutu5.tfs,
eval="per_tf", duration=mutu5.duration))
duration = time.time() - start_time
print("Timerank with all connection - default q: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
# Timerank with next connection - default q
start_time = time.time()
mutu6 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='next',
clusters=len(ground_truth), default_q=True, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu6.dynamic_coms, "Timerank-NOC-Uni" + ov,
mutu6.tfs, eval="dynamic", duration=mutu6.duration))
all_res.append(evaluate.get_results(ground_truth, mutu6.dynamic_coms, "Timerank-NOC-Uni" + ov,
mutu6.tfs, eval="sets", duration=mutu6.duration))
all_res.append(evaluate.get_results(ground_truth, mutu6.dynamic_coms, "Timerank-NOC-Uni" + ov,
mutu6.tfs, eval="per_tf", duration=mutu6.duration))
duration = time.time() - start_time
print("Timerank with next connection - default q: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
#
# Run Timerank - One connection
start_time = time.time()
mutu1 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='one',
clusters=len(ground_truth), default_q=False, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu1.dynamic_coms, "Timerank-STC" + ov, mutu1.tfs,
eval="dynamic", duration=mutu1.duration))
all_res.append(evaluate.get_results(ground_truth, mutu1.dynamic_coms, "Timerank-STC" + ov, mutu1.tfs,
eval="sets", duration=mutu1.duration))
all_res.append(evaluate.get_results(ground_truth, mutu1.dynamic_coms, "Timerank-STC" + ov, mutu1.tfs,
eval="per_tf", duration=mutu1.duration))
duration = time.time() - start_time
print("Timerank with one connection: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
muturank_res = OrderedDict()
muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu1.tfs_list]
for i, node in enumerate(mutu1.node_ids):
muturank_res[node] = [mutu1.p_new[tf * len(mutu1.node_ids) + i] for tf in range(mutu1.tfs)]
f = open(results_file, 'a')
f.write("ONE CONNECTION\n")
f.write(tabulate(muturank_res, headers="keys", tablefmt="grid") + "\n")
f.write(tabulate(zip(['t' + str(tf) for tf in mutu1.tfs_list], mutu1.q_new), headers="keys",
tablefmt="grid") + "\n")
f.close()
# Timerank with all connections
start_time = time.time()
mutu2 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='all',
clusters=len(ground_truth), default_q=False, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu2.dynamic_coms, "Timerank-AOC" + ov, mutu2.tfs,
eval="dynamic", duration=mutu2.duration))
all_res.append(evaluate.get_results(ground_truth, mutu2.dynamic_coms, "Timerank-AOC" + ov, mutu2.tfs,
eval="sets", duration=mutu2.duration))
all_res.append(evaluate.get_results(ground_truth, mutu2.dynamic_coms, "Timerank-AOC" + ov, mutu2.tfs,
eval="per_tf", duration=mutu2.duration))
duration = time.time() - start_time
print("Timerank with all connection: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
muturank_res = OrderedDict()
muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu2.tfs_list]
for i, node in enumerate(mutu2.node_ids):
muturank_res[node] = [mutu2.p_new[tf * len(mutu2.node_ids) + i] for tf in range(mutu2.tfs)]
f = open(results_file, 'a')
f.write("ALL CONNECTIONS\n")
f.write(tabulate(muturank_res, headers="keys", tablefmt="grid") + "\n")
f.write(tabulate(zip(['t' + str(tf) for tf in mutu2.tfs_list], mutu2.q_new), headers="keys",
tablefmt="grid") + "\n")
f.close()
# Timerank with next connection
start_time = time.time()
mutu3 = Muturank_new(data.graphs, threshold=1e-6, alpha=0.85, beta=0.85, connection='next',
clusters=len(ground_truth), default_q=False, overlap=overlap)
all_res.append(evaluate.get_results(ground_truth, mutu3.dynamic_coms, "Timerank-NOC" + ov, mutu3.tfs,
eval="dynamic", duration=mutu3.duration))
all_res.append(evaluate.get_results(ground_truth, mutu3.dynamic_coms, "Timerank-NOC" + ov, mutu3.tfs,
eval="sets", duration=mutu3.duration))
all_res.append(evaluate.get_results(ground_truth, mutu3.dynamic_coms, "Timerank-NOC" + ov, mutu3.tfs,
eval="per_tf", duration=mutu3.duration))
duration = time.time() - start_time
print("Timerank with next connection: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
muturank_res = OrderedDict()
muturank_res["tf/node"] = ['t' + str(tf) for tf in mutu3.tfs_list]
for i, node in enumerate(mutu3.node_ids):
muturank_res[node] = [mutu3.p_new[tf * len(mutu3.node_ids) + i] for tf in range(mutu3.tfs)]
f = open(results_file, 'a')
f.write("NEXT CONNECTION\n")
f.write(tabulate(muturank_res, headers="keys", tablefmt="grid") + "\n")
f.write(tabulate(zip(['t' + str(tf) for tf in mutu3.tfs_list], mutu3.q_new), headers="keys",
tablefmt="grid") + "\n")
f.write("GROUND TRUTH\n")
pprint.pprint(ground_truth, stream=f, width=150)
f.write("ONE CONNECTION\n")
pprint.pprint(mutu1.dynamic_coms, stream=f, width=150)
f.write("ALL CONNECTIONS\n")
pprint.pprint(mutu2.dynamic_coms, stream=f, width=150)
f.write("NEXT CONNECTION\n")
pprint.pprint(mutu3.dynamic_coms, stream=f, width=150)
f.close()
# NNTF
start_time = time.time()
fact = TensorFact(data.graphs, num_of_coms=len(ground_truth), threshold=1e-4, seeds=1, overlap=False)
fact_dur = time.time() - start_time
fact_dur = "%d:%d" % (fact_dur // 60, fact_dur % 60)
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF", mutu6.tfs, eval="dynamic",
duration=fact_dur))
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF", mutu6.tfs, eval="sets",
duration=fact_dur))
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF", mutu6.tfs, eval="per_tf",
duration=fact_dur))
duration = time.time() - start_time
print("NNTF: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
with open(results_file, 'a') as f:
f.write("NNTF\n")
f.write("Error: " + str(fact.error) + "Seed: " + str(fact.best_seed) + "\n")
f.write("A\n")
pprint.pprint(fact.A, stream=f, width=150)
f.write("B\n")
pprint.pprint(fact.B, stream=f, width=150)
f.write("C\n")
pprint.pprint(fact.C, stream=f, width=150)
pprint.pprint(fact.dynamic_coms, stream=f, width=150)
# NNTF-Overlap
start_time = time.time()
fact = TensorFact(data.graphs, num_of_coms=len(ground_truth), threshold=1e-4, seeds=1, overlap=True)
fact_dur = time.time() - start_time
fact_dur = "%d:%d" % (fact_dur // 60, fact_dur % 60)
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF-Overlap", mutu6.tfs, eval="dynamic",
duration=fact_dur))
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF-Overlap", mutu6.tfs, eval="sets",
duration=fact_dur))
all_res.append(evaluate.get_results(ground_truth, fact.dynamic_coms, "NNTF-Overlap", mutu6.tfs, eval="per_tf",
duration=fact_dur))
duration = time.time() - start_time
print("NNTF-Overlap: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
with open(results_file, 'a') as f:
f.write("NNTF-Overlap\n")
f.write("Error: " + str(fact.error) + "Seed: " + str(fact.best_seed) + "\n")
f.write("A\n")
pprint.pprint(fact.A, stream=f, width=150)
f.write("B\n")
pprint.pprint(fact.B, stream=f, width=150)
f.write("C\n")
pprint.pprint(fact.C, stream=f, width=150)
pprint.pprint(fact.dynamic_coms, stream=f, width=150)
# # NNTF-Timerank tensor
# new_graphs = {}
# for i, A in mutu1.a.items():
# new_graphs[i] = nx.from_scipy_sparse_matrix(A)
# start_time = time.time()
# fact2 = TensorFact(new_graphs, num_of_coms=len(ground_truth), threshold=1e-4, seeds=1, overlap=False,
# original_graphs=data.graphs)
# fact_dur = time.time() - start_time
# fact_dur = "%d:%d" % (fact_dur // 60, fact_dur % 60)
# all_res.append(evaluate.get_results(ground_truth, fact2.dynamic_coms, "NNTF-Timerank tensor", mutu6.tfs,
# eval="dynamic", duration=fact_dur))
# all_res.append(evaluate.get_results(ground_truth, fact2.dynamic_coms, "NNTF-Timerank tensor", mutu6.tfs,
# eval="sets", duration=fact_dur))
# all_res.append(evaluate.get_results(ground_truth, fact2.dynamic_coms, "NNTF-Timerank tensor", mutu6.tfs,
# eval="per_tf", duration=fact_dur))
# duration = time.time() - start_time
# print("NNTF-Timerank tensor: TIME: %d min, %d sec" % (duration // 60, duration % 60))
# times.append(duration)
# with open(results_file, 'a') as f:
# f.write("NNTF\n")
# f.write("Error: " + str(fact2.error) + "Seed: " + str(fact2.best_seed) + "\n")
# f.write("A\n")
# pprint.pprint(fact2.A, stream=f, width=150)
# f.write("B\n")
# pprint.pprint(fact2.B, stream=f, width=150)
# f.write("C\n")
# pprint.pprint(fact2.C, stream=f, width=150)
# pprint.pprint(fact2.dynamic_coms, stream=f, width=150)
# GED
import sys
sys.path.insert(0, '../GED/')
start_time = time.time()
from ged import GedWrite, ReadGEDResults
ged_data = GedWrite(data)
graphs = preprocessing.getGraphs(ged_data.fileName)
tracker = Tracker.Tracker(graphs)
tracker.compare_communities()
outfile = os.path.join(os.path.split(results_file)[0], 'GED-events-reddit.csv')
with open(outfile, 'w+') as f:
for hypergraph in tracker.hypergraphs:
hypergraph.calculateEvents(f)
ged = ReadGEDResults.ReadGEDResults(file_coms=ged_data.fileName, file_output=outfile)
ged_dur = time.time() - start_time
ged_dur = "%d:%d" % (ged_dur // 60, ged_dur % 60)
with open(results_file, 'a') as f:
f.write("GED\n")
pprint.pprint(ged.dynamic_coms, stream=f, width=150)
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED-T", mutu6.tfs, eval="dynamic",
duration=ged_dur))
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED-T", mutu6.tfs, eval="sets",
duration=ged_dur))
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED-T", mutu6.tfs, eval="per_tf",
duration=ged_dur))
duration = time.time() - start_time
print("GED-T: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
# GED with timerank communities
# GED
import sys
sys.path.insert(0, '../GED/')
start_time = time.time()
from ged import GedWrite, ReadGEDResults
ged_data = GedWrite(Data(mutu1.comms, data.graphs, len(graphs), len(mutu1.dynamic_coms), mutu1.dynamic_coms))
graphs = preprocessing.getGraphs(ged_data.fileName)
tracker = Tracker.Tracker(graphs)
tracker.compare_communities()
outfile = os.path.join(os.path.split(results_file)[0], 'GED-wTimerank-events-reddit.csv')
with open(outfile, 'w') as f:
for hypergraph in tracker.hypergraphs:
hypergraph.calculateEvents(f)
ged = ReadGEDResults.ReadGEDResults(file_coms=ged_data.fileName, file_output=outfile)
ged_dur = time.time() - start_time
ged_dur = "%d:%d" % (ged_dur // 60, ged_dur % 60)
with open(results_file, 'a') as f:
f.write("GED\n")
pprint.pprint(ged.dynamic_coms, stream=f, width=150)
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED - with Timerank comms", mutu6.tfs,
eval="dynamic", duration=ged_dur))
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED - with Timerank comms", mutu6.tfs,
eval="sets", duration=ged_dur))
all_res.append(evaluate.get_results(ground_truth, ged.dynamic_coms, "GED - with Timerank comms", mutu6.tfs,
eval="per_tf", duration=ged_dur))
duration = time.time() - start_time
print("GED - with Timerank comms: TIME: %d min, %d sec" % (duration // 60, duration % 60))
times.append(duration)
print("TOTAL TIME: %d min, %d sec" % (sum(times) // 60, sum(times) % 60))
return all_res
def create_ground_truth(communities, number_of_dynamic_communities):
ground_truth = {i: [] for i in range(number_of_dynamic_communities)}
for tf, coms in communities.items():
for i, com in coms.items():
for node in com:
ground_truth[i].append(str(node) + "-t" + str(tf))
return ground_truth
if __name__ == "__main__":
from reddit.reddit_parser import *
path_full = os.path.dirname(os.path.abspath(__file__))
path_full = os.path.join(path_full, "reddit")
#####
import sys
arg_names = ['executable', 'experiment', 'overlap']
args = dict(zip(arg_names, sys.argv))
exp = int(args['experiment']) if 'experiment' in args else EXPERIMENT
overlap = True if 'overlap' in args and args['overlap'] == '--overlap' else False
choice, exp_string = choose_experiment(exp)
if exp == 5:
data_ = read_data_from_json((2010, 9), sampled=True, specific_users=choice)
data_.extend(read_data_from_json((2010, 10), days=31, sampled=True, specific_users=choice))
else:
data_ = read_data_from_json((2010, 9), sampled=True, specific_reddits=choice)
data_.extend(read_data_from_json((2010, 10), days=31, sampled=True, specific_reddits=choice))
print(len(data_))
Gs = create_graph_files(data_, year=2010, month=9, min_degree=1)
#####
Gs = read_graphs([(2010, 9)], tfs=4)
data = create_Data_object_from_graphs(Gs)
# from plot import PlotGraphs
# PlotGraphs(data.graphs, len(data.graphs), 'reddit'+str(i), 500)
results_file = os.path.join(path_full, 'results_reddit' + exp_string + '.txt')
f = open(results_file, 'w+')
f.write("\n" + "-" * 80 + "REDDIT NETWORK" + "-" * 80 + "\n")
reddit_overlap_percentage(data_, fwrite=f)
f.close()
all_res = run_experiments(data, data.dynamic_truth, results_file, overlap=overlap)
results = OrderedDict()
results["Method"] = []
results['Eval'] = []
results['NMI'] = []
results['Omega'] = []
results['Bcubed-Precision'] = []
results['Bcubed-Recall'] = []
results['Bcubed-F1'] = []
results['Duration'] = []
for res in all_res:
for k, v in res.items():
results[k].extend(v)
f = open(results_file, 'a')
f.write(tabulate(results, headers="keys", tablefmt="grid") + "\n")
import pandas as pd
df = pd.DataFrame.from_dict(results)
del df["Duration"]
f.write("\\begin{table}[h!] \n\centering \n\\begin{tabular}{ |p{4cm}||p{2cm}|p{3cm}|p{2cm}|p{2cm}|p{2cm}|} "
"\n"
"\hline \n\multicolumn{6}{|c|}{Evaluation comparison} \\\\\n\hline\n Method& NMI & Omega Index & "
"BCubed Precision & BCubed Recall & BCubed F\\\\\n\hline\n")
for index, row in df.iterrows():
if row["Eval"] == "dynamic":
del row["Eval"]
f.write(str(row[0]))
for item in row[1:]:
f.write(" & " + str(item))
f.write(str("\\\\") + "\n")
f.write("\hline\n\end{tabular}\n\caption{Comparison of different frameworks on Reddit Network \\" +
"\n\label{table:results-network}\n\end{table}\n")
f.close()