-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
419 lines (349 loc) · 15.6 KB
/
helpers.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
417
418
419
import networkx as nx
import numpy as np
import pandas as pd
import random as rand
from collections import Counter
#Spreading Animation Related
import os
from pathlib import Path
import shutil
import imageio as iio
#Graphics
import plotly
import plotly.graph_objects as go
import matplotlib
import matplotlib.pyplot as plt
from IPython.display import display, HTML
def generate_minimal_graph(n_nodes):
"""
Generates a graph following the minimal model with the desired number of nodes (n_nodes)
"""
g = nx.Graph()
g.add_edges_from([(1,2), (2,3), (3,1)])
for i in range(4, n_nodes+1):
edges = list(g.edges)
chosen_edge = rand.choice(edges)
g.add_edge(chosen_edge[0], i)
g.add_edge(chosen_edge[1], i)
return g
def maki_thompson_rumour_model(graph: nx.Graph, gamma: float, alpha: float, time_limit: int, number_infected=1, all_time_values=False, starting_nodes=[], outputDictQ=True, animation=False, gif_name="spreading", gif_duration=10):
"""
Simulates the spreading of a rumor, using the maki-thompson model with the desired parameters.
graph: the network where we wish to spread the rumor
gamma: the probability of a spreader to spread the rumor to an ignorant node
alpha: the probability of a spreader becoming a stiffler when in contact with a spreader/stiffler
time_limit: The desired time limit for the simulation
number_infected: Number of initial spreaders desired
all_time_values: If true, even when the simulation stop eraly, the return always return a list with length equal to the time_limit
starting_nodes: option for selecting the starting spreader nodes
outputDictQ: True if we want the population output as dict type, array c.c.
animation: True if we wish to see an animation of the spreading (This will make the simulation much slower)
gif_name: name of the generated gif file
gif_duration: duration of the generated gif
"""
#Clear status of the nodes of the given graph
#"status" tells the type of node (I - Ignorant, S - Spreader, R - Stifler)
#"visit" tells if the node should be visited in a cycle or not
node_status={key: "I" for key in graph.nodes}
node_visit={key: False for key in graph.nodes}
nx.set_node_attributes(graph, node_status, "status")
nx.set_node_attributes(graph, node_visit, "visit")
#Obtain a random sample of size "number_infected" out of the nodes of the given graph
if not(starting_nodes):
starting_nodes = rand.sample(list(graph.nodes), number_infected)
for node in starting_nodes:
graph.nodes[node]["status"]="S"
graph.nodes[node]["visit"]=True
#All neighbours of spreaders should be visited in the cycle for possible changes
for neighbor in graph.neighbors(node):
graph.nodes[neighbor]["visit"]=True
t=0
#Counter for every status type, for each time value "t"
counter=Counter(nx.get_node_attributes(graph, "status").values())
if outputDictQ:
status_count = [{"I": counter["I"], "S": counter["S"], "R": counter["R"]}]
else:
status_count= [[counter["I"], counter["S"], counter["R"]]]
#Creates directory for the images used in the animation gif
if animation:
centrality = nx.degree_centrality(graph)
centrality = np.fromiter(centrality.values(), float)
dir=os.getcwd()
folder_name="gif_images"
folder_path=os.path.join(dir, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
color_array = ["grey" if x == "I" else "red" if x == "S" else "green" for x in list(nx.get_node_attributes(graph, "status").values())]
fig = plt.figure()
nx.draw(graph, node_color=color_array, ax=fig.add_subplot(), pos=nx.random_layout(graph, seed=16), node_size=centrality*1e3)
matplotlib.use("Agg")
fig.savefig(os.path.join(folder_path, str(t)))
load=0
#Checks if there is still nodes worth visiting in the next iteration
continue_visitingQ=True
while t < time_limit-1 and continue_visitingQ:
#Only apply node_changes at the end of the cycle, the changes have to be done all at the same time.
node_changes=[]
for node in graph.nodes(data=True):
if node[1]["visit"]==True:
#Counting the amount of each type of nodes in the neighbourhood of the visited node
neighbours_status_count={"I":0, "S":0, "R":0}
for neighbor in graph.neighbors(node[0]):
neighbours_status_count[graph.nodes[neighbor]["status"]]+=1
#If this value is less than the probability of transforming then the status of the node changes
transform_value = rand.uniform(0,1)
#Transformation of the node status
if node[1]["status"]=="I":
#(1-gamma)**neighbours_status_count["S"] gives the probability of the event that no spreader transmits the rumour. We only need one of the spreaders to pass the rumour to transform the ignorant into spreader
transform_threshold = 1-(1-gamma)**neighbours_status_count["S"]
if transform_value <= transform_threshold:
node_changes.append((node[0], "S", True))
for neighbor in graph.neighbors(node[0]):
if graph.nodes[neighbor]["status"]!="R":
node_changes.append((neighbor, graph.nodes[neighbor]["status"], True))
load+=neighbours_status_count["S"]
elif node[1]["status"]=="S":
#Whenever we are transforming a spreader into a stiffler the stifflers and the spreaders contribute the same to the probability of transforming the spreader
transform_threshold = 1-(1-alpha)**(neighbours_status_count["S"] + neighbours_status_count["R"])
if transform_value <= transform_threshold:
node_changes.append((node[0], "R", False))
load+=neighbours_status_count["S"]+neighbours_status_count["R"]
#Applying all changes to the graph
for node_change in node_changes:
graph.nodes[node_change[0]]["status"]=node_change[1]
graph.nodes[node_change[0]]["visit"]=node_change[2]
#Changes colors in color array for the nodes that were changed
if animation:
if node_change[1]=="I":
color_array[node_change[0]]="grey"
elif node_change[1]=="S":
color_array[node_change[0]]="red"
else:
color_array[node_change[0]]="green"
counter=Counter(nx.get_node_attributes(graph, "status").values())
if outputDictQ:
status_count.append({"I": counter["I"], "S": counter["S"], "R": counter["R"]})
else:
status_count.append([counter["I"], counter["S"], counter["R"]])
#Stops the simulation when there are no more spreaders
if counter["S"]==0:
if not(all_time_values):
continue_visitingQ=False
else:
for i in range(time_limit-t-2):
if outputDictQ:
status_count.append({"I": counter["I"], "S": counter["S"], "R": counter["R"]})
else:
status_count.append([counter["I"], counter["S"], counter["R"]])
continue_visitingQ=False
t+=1
#Creates the plot of the graph for the current timestamp
if animation:
fig = plt.figure()
nx.draw(graph, node_color=color_array, ax=fig.add_subplot(), pos=nx.random_layout(graph, seed=16), node_size=centrality*1e3)
matplotlib.use("Agg")
fig.savefig(os.path.join(folder_path, str(t)))
load=load/len(graph)
#Joins the created images into a gif and deletes the directory
if animation:
images = []
img_names_order = sorted(list(Path(folder_path).iterdir()),
key=lambda path: int(path.stem))
for img in img_names_order:
images.append(iio.v3.imread(img))
iio.v2.mimsave(gif_name+".gif", images, duration = gif_duration)
shutil.rmtree(folder_path)
return status_count, load, [i[1] for i in list(graph.degree())], list(nx.get_node_attributes(graph, "status").values())
def simulation(name, n_nodes, gamma_name, gamma=1, ws_prob=0.01, n_graph_per_iteration=10):
"""
Simulates multiple instances of rumor spreading, with the desired parameters, for multiple alphas, and saves the results to multiple numpy arrays
"""
population_results=[]
degrees=[]
mean_loads=[]
status=[]
#It shouldn't have started at 0
for alpha in np.linspace(0,1,20):
alpha_population_results=[]
alpha_degrees=[]
alpha_mean_loads=[]
alpha_status=[]
for j in range(n_graph_per_iteration):
if name=="BA":
graph=nx.barabasi_albert_graph(n=n_nodes,m=2)
elif name=="MM":
graph=generate_minimal_graph(n_nodes=n_nodes)
else:
graph=nx.watts_strogatz_graph(n=n_nodes, k=4, p=ws_prob)
sim,load,degree,final_status=maki_thompson_rumour_model(graph, gamma=gamma, alpha=alpha, time_limit=1000, number_infected=1, all_time_values=True, outputDictQ=False)
alpha_population_results.append(sim)
alpha_degrees.append(degree)
alpha_mean_loads.append(load)
alpha_status.append(final_status)
population_results.append(alpha_population_results)
degrees.append(alpha_degrees)
mean_loads.append(alpha_mean_loads)
status.append(alpha_status)
dir=os.getcwd()
folder_name="gamma"+gamma_name
folder_path = os.path.join(dir, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
np.save(os.path.join(folder_path,name+"_population_"+gamma_name),np.array(population_results))
np.save(os.path.join(folder_path,name+"_degrees_"+gamma_name), np.array(degrees))
np.save(os.path.join(folder_path,name+"_loads_"+gamma_name), np.array(mean_loads))
np.save(os.path.join(folder_path,name+"_status_"+gamma_name), np.array(status))
def simulation_graph_size(name, alpha, gamma, ws_prob=0.1, n_graph_per_iteration=10):
"""Similar to simulation function, but instead changes the network size instead of the alpha parameter"""
population_results=[]
mean_loads=[]
for n_nodes in list(map(int,np.linspace(0,10000,11)[1:])):
alpha_population_results=[]
alpha_mean_loads=[]
for j in range(n_graph_per_iteration):
if name=="BA":
graph=nx.barabasi_albert_graph(n=n_nodes,m=2)
elif name=="MM":
graph=generate_minimal_graph(n_nodes=n_nodes)
else:
graph=nx.watts_strogatz_graph(n=n_nodes, k=4, p=ws_prob)
sim,load,degree,final_status=maki_thompson_rumour_model(graph, gamma=gamma, alpha=alpha, time_limit=1000, number_infected=1, all_time_values=True, outputDictQ=False)
alpha_population_results.append(sim)
alpha_mean_loads.append(load)
population_results.append(alpha_population_results)
mean_loads.append(alpha_mean_loads)
dir=os.getcwd()
folder_name="variable_graph_size"
folder_path = os.path.join(dir, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
np.save(os.path.join(folder_path,name+"_population"),np.array(population_results))
np.save(os.path.join(folder_path,name+"_loads"), np.array(mean_loads))
def plot_line_chart(x, y1, y2, y3, y4=np.array([]), title="", x_title="", y_title=""):
plotly.offline.init_notebook_mode()
display(HTML(
'<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_SVG"></script>'
))
fig = go.Figure()
# Add the first line
fig.add_trace(go.Scatter(x=x, y=y1,mode='lines+markers', name='BA'))
# Add the second line
fig.add_trace(go.Scatter(x=x, y=y2,mode='lines+markers', name='WS'))
#Add the third line
fig.add_trace(go.Scatter(x=x, y=y3,mode='lines+markers', name='MM'))
# Add the forth line
if y4.size>0:
fig.add_trace(go.Scatter(x=x, y=y4, mode='lines+markers', name='WS1'))
fig.update_layout(
title=title,
xaxis_title=x_title,
yaxis_title=y_title,
template='plotly',
plot_bgcolor='white',
barmode='stack',
width=600,
height=500
)
fig.update_xaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.update_yaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
return fig
def plot_3_line_chart(x, y1, y2, y3, title="", x_title="", y_title=""):
fig = go.Figure()
# Add the first line
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', name='Ignorant'))
# Add the second line
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', name='Spreader'))
# Add the second line
fig.add_trace(go.Scatter(x=x, y=y3, mode='lines', name='Stifler'))
fig.update_xaxes(title=x_title)
fig.update_yaxes(title=y_title)
fig.update_layout(
title=title,
template='plotly',
plot_bgcolor='white',
width=800,
height=400
)
fig.update_xaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.update_yaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
return fig
def plot_stacked_area_chart(x, y1, y2, y3, title="", x_title="", y_title=""):
fig = go.Figure()
# Add the first line
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', fill='tozeroy', name='Ignorant', stackgroup='one',
groupnorm='percent'))
# Add the second line
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', fill='tonexty', name='Spreader', stackgroup='one'))
# Add the second line
fig.add_trace(go.Scatter(x=x, y=y3, mode='lines', fill='tonexty', name='Stifler', stackgroup='one'))
fig.update_xaxes(title=x_title)
fig.update_yaxes(title=y_title)
fig.update_layout(
title=title,
template='plotly',
plot_bgcolor='white',
barmode='stack',
width=800,
height=400
)
fig.update_xaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
fig.update_yaxes(
mirror=False,
ticks='outside',
showline=True,
linecolor='black',
gridcolor='lightgrey'
)
return fig
def get_rk(status_matrix, degree_matrix):
res = {}
for a in range(np.shape(status_matrix)[0]):
r_k = {}
total_k = {}
for sim in range(np.shape(status_matrix)[1]):
statuses = status_matrix[a,sim,:]
degs = degree_matrix[a,sim,:]
for sts, degree in zip(statuses, degs):
if degree in total_k.keys():
total_k[degree] += 1
else:
total_k[degree] = 1
if sts == 'R':
if degree in r_k.keys():
r_k[degree] += 1
else:
r_k[degree] = 1
res[a] = [(deg, 1 - (count / total_k[deg])) for deg, count in r_k.items()]
for k in res.keys():
res[k].sort(key=lambda t: t[0])
return res