forked from mdepak/fake-news-propagation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
temporal_analysis.py
328 lines (221 loc) · 11.2 KB
/
temporal_analysis.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
import queue
import numpy as np
from analysis_util import sort_tweet_node_object_by_created_time, get_numpy_array, BaseFeatureHelper
from structure_temp_analysis import get_first_post_time, get_post_tweet_deepest_cascade, get_max_out_degree_node
from util.constants import RETWEET_NODE, REPLY_NODE, RETWEET_EDGE, REPLY_EDGE
from util.util import tweet_node
def get_avg_retweet_time_deepest_cascade(news_graph: tweet_node):
deep_cascade, max_height = get_post_tweet_deepest_cascade(news_graph)
return get_avg_time_between_retweets(deep_cascade)
def get_time_diff_post_time_last_retweet_time_deepest_cascade(news_graph: tweet_node):
deep_cascade, max_height = get_post_tweet_deepest_cascade(news_graph)
first_post_time = deep_cascade.created_time
last_retweet_time = get_last_retweet_by_time(deep_cascade)
return last_retweet_time - first_post_time
def get_avg_time_between_replies(prop_graph: tweet_node):
q = queue.Queue()
q.put(prop_graph)
reply_diff_values = list()
while q.qsize() != 0:
node = q.get()
for child in node.children:
q.put(child)
if node.node_type == REPLY_NODE and child.node_type == REPLY_NODE:
reply_diff_values.append(child.created_time - node.created_time)
if len(reply_diff_values) == 0:
return 0
else:
return np.mean(np.array(reply_diff_values))
def get_avg_time_between_retweets(prop_graph: tweet_node):
q = queue.Queue()
q.put(prop_graph)
retweet_diff_values = list()
while q.qsize() != 0:
node = q.get()
for child in node.retweet_children:
q.put(child)
if node.node_type == RETWEET_NODE and child.node_type == RETWEET_NODE:
retweet_diff_values.append(child.created_time - node.created_time)
if len(retweet_diff_values) == 0:
return 0
else:
return np.mean(np.array(retweet_diff_values))
def get_last_retweet_by_time(news_graph: tweet_node):
max_time = 0
if news_graph:
for node in news_graph.retweet_children:
max_time = max(max_time, get_last_retweet_by_time(node))
if news_graph and news_graph.created_time is not None:
max_time = max(max_time, news_graph.created_time)
return max_time
def get_last_reply_by_time(news_graph: tweet_node):
max_time = 0
if news_graph:
for node in news_graph.retweet_children:
max_time = max(max_time, get_last_retweet_by_time(node))
if news_graph and news_graph.created_time is not None and news_graph.node_type == REPLY_NODE:
max_time = max(max_time, news_graph.created_time)
return max_time
def get_avg_time_between_replies_deepest_cascade(news_graph: tweet_node):
deep_cascade, max_height = get_post_tweet_deepest_cascade(news_graph, edge_type=REPLY_EDGE)
return get_avg_time_between_replies(deep_cascade)
def get_time_diff_post_time_last_reply_time_deepest_cascade(news_graph: tweet_node):
deep_cascade, max_height = get_post_tweet_deepest_cascade(news_graph, edge_type=REPLY_EDGE)
first_post_time = deep_cascade.created_time
last_reply_time = get_last_reply_by_time(deep_cascade)
if last_reply_time == 0:
return 0
else:
return last_reply_time - first_post_time
def get_time_diff_first_post_last_reply(news_graph: tweet_node):
first_post_time = get_first_post_time(news_graph)
last_reply_time = get_last_reply_by_time(news_graph)
if last_reply_time == 0:
return 0
else:
return last_reply_time - first_post_time
def get_first_reply_by_time(news_graph: tweet_node):
min_time = float("inf")
if news_graph:
for node in news_graph.retweet_children:
min_time = min(min_time, get_first_retweet_by_time(node))
if news_graph and news_graph.created_time is not None and news_graph.node_type == REPLY_NODE:
min_time = min(min_time, news_graph.created_time)
return min_time
def get_first_retweet_by_time(news_graph: tweet_node):
min_time = float("inf")
if news_graph:
for node in news_graph.retweet_children:
min_time = min(min_time, get_first_retweet_by_time(node))
if news_graph and news_graph.created_time is not None and news_graph.node_type == RETWEET_NODE:
min_time = min(min_time, news_graph.created_time)
return min_time
def get_time_diff_first_post_last_retweet(news_graph: tweet_node):
first_post_time = get_first_post_time(news_graph)
last_retweet_time = get_last_retweet_by_time(news_graph)
return last_retweet_time - first_post_time
def get_time_diff_first_post_first_retweet(news_graph: tweet_node):
first_post_time = get_first_post_time(news_graph)
first_retweet_time = get_first_retweet_by_time(news_graph)
if first_retweet_time == float("inf"):
return 0
return first_retweet_time - first_post_time
def get_time_diff_first_last_post_tweet(news_graph: tweet_node):
post_tweets = list(news_graph.children)
if len(post_tweets) <= 1:
# print("only one tweet")
return 0
post_tweets = sort_tweet_node_object_by_created_time(post_tweets)
return post_tweets[len(post_tweets) - 1].created_time - post_tweets[0].created_time
def get_average_time_between_post_tweets(news_graph: tweet_node):
post_tweets = list(news_graph.children)
if len(post_tweets) <= 1:
# print("only one tweet")
return 0
post_tweets = sort_tweet_node_object_by_created_time(post_tweets)
time_diff = []
for i in range(1, len(post_tweets)):
time_diff.append(post_tweets[i].created_time - post_tweets[i - 1].created_time)
return np.mean(time_diff)
def get_stats_for_features(news_graps: list, get_feature_fun_ref, print=False, feature_name=None):
result = []
for graph in news_graps:
result.append(get_feature_fun_ref(graph))
if print:
print_stat_values(feature_name, result)
return result
def print_stat_values(feature_name, values):
print("=========================================")
print("Feature : {}".format(feature_name))
print("Min value : {}".format(min(values)))
print("Max value : {}".format(max(values)))
print("Mean value : {}".format(np.mean(np.array(values))))
print("=========================================")
def graph_has_retweet(news_graph: tweet_node):
post_tweets = news_graph.children
for post in post_tweets:
if len(post.retweet_children) > 0:
return True
return False
def count_graph_with_no_retweets(news_graphs: list):
count = 0
for prop_graph in news_graphs:
if not graph_has_retweet(prop_graph):
count += 1
print("Graph with no retweets : {}".format(count))
def get_all_temporal_features(prop_graphs, micro_features, macro_features):
macro_features_functions = [get_average_time_between_post_tweets,
get_time_diff_first_last_post_tweet,
get_time_diff_first_post_last_retweet,
get_time_diff_first_post_first_retweet,
get_avg_time_between_retweets,
get_avg_retweet_time_deepest_cascade,
get_time_diff_post_time_last_retweet_time_deepest_cascade]
micro_features_functions = [get_avg_time_between_replies,
get_time_diff_first_post_last_reply,
get_time_diff_post_time_last_reply_time_deepest_cascade]
function_refs = []
if macro_features:
function_refs.extend(macro_features_functions)
if micro_features:
function_refs.extend(micro_features_functions)
all_features = []
for function_reference in function_refs:
features_set = get_stats_for_features(prop_graphs, function_reference, print=False, feature_name=None)
all_features.append(features_set)
return np.transpose(get_numpy_array(all_features))
def time_difference_between_first_post_node_with_max_out_degree_macro(prop_graph):
max_out_degree_node, max_out_degree = get_max_out_degree_node(prop_graph, RETWEET_EDGE)
first_post_time = get_first_post_time(prop_graph)
if max_out_degree_node is None:
return 0
return max_out_degree_node.created_time - first_post_time
def get_time_diff_first_post_first_reply(news_graph):
first_reply_time = get_first_reply_by_time(news_graph)
first_post_time = get_first_post_time(news_graph)
if first_reply_time == float("inf"):
return 0
return first_reply_time - first_post_time
class TemporalFeatureHelper(BaseFeatureHelper):
def get_feature_group_name(self):
return "temp"
def get_micro_feature_method_references(self):
method_refs = [get_avg_time_between_replies,
get_time_diff_first_post_first_reply,
get_time_diff_first_post_last_reply,
get_avg_time_between_replies_deepest_cascade,
get_time_diff_post_time_last_reply_time_deepest_cascade]
return method_refs
def get_micro_feature_method_names(self):
feature_names = ["Average time diff between adjacent replies",
"Time diff between first tweet posting node and first reply node",
"Time diff between first tweet posting news and last reply node",
"Average time between adjacent reply nodes in the deepest cascade",
"Time diff between first tweet posting news and last reply node in the deepest cascade"]
return feature_names
def get_micro_feature_short_names(self):
feature_names = ["T9", "T10", "T11", "T12", "T13"]
return feature_names
def get_macro_feature_method_references(self):
method_refs = [get_avg_time_between_retweets,
get_time_diff_first_post_last_retweet,
time_difference_between_first_post_node_with_max_out_degree_macro, # not implemented
get_time_diff_first_last_post_tweet,
get_time_diff_post_time_last_retweet_time_deepest_cascade,
get_avg_retweet_time_deepest_cascade,
get_average_time_between_post_tweets,
get_time_diff_first_post_first_retweet]
return method_refs
def get_macro_feature_method_names(self):
feature_names = ["Average time diff between the adjacent retweet nodes in macro network",
"Time diff between first tweet and most recent node in macro network",
"Time diff between first tweet posting news and max out degree node",
"Time difference between the first and last tweet posting news",
"Time diff between tweet posting news and latest retweet node in the deepest cascade",
"Average time diff between the adjacent retweet nodes in deepest cascade",
"Average time between the tweets posted related to news",
"Avg time diff between the tweet post time and the first retweet time"]
return feature_names
def get_macro_feature_short_names(self):
feature_names = ["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8"]
return feature_names