/
rejection_sampling_all_graphs_with_IC.py
509 lines (409 loc) · 14.2 KB
/
rejection_sampling_all_graphs_with_IC.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import os
os.environ["PATH"] += os.pathsep + 'C:/Users/anura/anaconda3/Library/bin/graphviz'
import sys
import networkx as nx
import numpy as np
import random
import pickle
import sys
from numba import jit
import elfi
import sklearn as sk
import scipy
from scipy.spatial import distance
import pandas as pd
import sys
import warnings
import statistics
warnings.filterwarnings('ignore')
print(sys.version)
global final_activation
global ic_count
global ltp_count
global lta_count
global ic_ltp
global ic_lta
global ltp_lta
global count_samples
def genNet(n, k=4, pRewire=.1, type='grid'):
# create net
if type == 'grid': #wrap the grid
net = nx.grid_2d_graph(int(n**.5), int(n**.5), periodic=True)
#net = nx.grid_2d_graph(3, 3, periodic=True)
#net = nx.grid_2d_graph(2, 2, periodic=False)
#plotNet(net)
#print(len(net.edges()))
#assert(0)
# rewire
numRewired = 0
#while numRewired < (pRewire * nx.number_of_nodes(net)):
while numRewired < 1:
tries = 0
while tries < 100:
tries = tries + 1
#print([numRewired, pRewire * nx.number_of_nodes(net), tries])
v1 = random.choice(net.nodes())
v2 = random.choice(net.nodes())
if not( net.has_edge(v1,v2) or v1==v2 or len(net.neighbors(v1)) <= 1): #net.neighbors is sometimes (often?) a blank set, changed so v1 needs 2 nb
#print net.neighbors(v1)
break
v1Neighbors = net.neighbors(v1)
#print v1Neighbors
#print v1
#print v2
#print(len(net.edges()))
tobeDeleted = random.choice(v1Neighbors)
net.remove_edge(v1, tobeDeleted)
#print(len(net.edges()))
#print([v1, tobeDeleted, v2])
net.add_edge(v1, v2)
numRewired = numRewired + 1
#plotNet(net)
#assert(0)
return net, nx.to_numpy_matrix(net, dtype=np.float)
elif type == 'smallworld':
#net = nx.connected_watts_strogatz_graph(n, k, .15)
net = nx.connected_watts_strogatz_graph(n, k, pRewire)
return net, nx.to_numpy_matrix(net, dtype=np.float)
elif type == 'pref':
net = nx.barabasi_albert_graph(n, 2)
return net, nx.to_numpy_matrix(net, dtype=np.float)
elif type == 'ER':
#net = nx.erdos_renyi_graph(n, .006)
targetDegree = 4.
nEdgesPossible = ((n*n)-n)/2.
pEdge = (n * targetDegree) / (2. * nEdgesPossible)
assert(pEdge <= 1)
# spare networks will likely be disconnected, so try a bunch
tries = 100
while tries > 0:
net = nx.erdos_renyi_graph(n, pEdge)
if nx.number_connected_components(net) > 1:
tries = tries - 1
else:
break
return net, nx.to_numpy_matrix(net, dtype=np.float)
def testThresh(agents, agents_ini, mn, mx):
if np.mean(agents) == 0:
print("stopping because no nodes are activated in the initial state")
return agents, False
if np.mean(agents) >= mx:
print("Bad data discarding the dataset because activation reached ", np.mean(agents))
agents = agents_ini
return agents, False
if np.mean(agents) <= mn:
return agents, False
if np.mean(agents) > mn and np.mean(agents) < mx:
return agents, True
return False
def ltProp(agents, adjMatrix, nAgents, avgDegree=0, haltMin=.49, haltMax=.51, rs = None):
# init some stuff for numba
thresholds = np.zeros_like(nAgents)
inp = np.zeros_like(nAgents)
step = 0
numNeighbors = np.zeros_like(nAgents)
prevMean = 0.
liveEdges = np.zeros_like(adjMatrix)
pInfect = np.zeros_like(adjMatrix)
flips = np.zeros_like(adjMatrix)
if rs is None:
thresholds = globalThreshold * np.random.random((1, nAgents))
else:
thresholds = globalThreshold * rs.random((1, nAgents))
numNeighbors = np.sum(adjMatrix, axis=0)
prevMean = -1
step = 1
# print("stopping conditions: ", haltMin, haltMax)
agents_ini = agents
loop_manager = False
while not loop_manager and (np.mean(agents) > prevMean):
agents, loop_manager = testThresh(agents, agents_ini, haltMin, haltMax)
# print("state activation: ", prevMean)
#while np.mean(agents) > prevMean:
prevMean = np.mean(agents)
inp = np.true_divide(np.dot(agents, adjMatrix), numNeighbors)
agents = np.logical_or(agents, (inp >= thresholds)).astype(int)
step = step + 1
# print("state activation: ", prevMean)
final_activation.append(prevMean)
#print('LT-proportional stopped at step ' + str(step) + ' '+ str(np.mean(agents)))
return agents
def ltAbs(agents, adjMatrix, nAgents, avgDegree=0, haltMin=.49, haltMax=.51, rs = None):
# init some stuff for numba
thresholds = np.zeros_like(nAgents)
inp = np.zeros_like(nAgents)
step = 0
numNeighbors = np.zeros_like(nAgents)
prevMean = 0
liveEdges = np.zeros_like(adjMatrix)
pInfect = np.zeros_like(adjMatrix)
flips = np.zeros_like(adjMatrix)
# this controls the thresholds/pInfects for all contagion types
if rs is None:
thresholds = np.random.randint(low=1, high=round(avgDegree*globalThreshold), size=(1, nAgents))
else:
thresholds = rs.randint(low=1, high=round(avgDegree*globalThreshold), size=(1, nAgents))
numNeighbors = np.sum(adjMatrix, axis=0)
prevMean = -1
step = 1
agents_ini = agents
loop_manager = False
while not loop_manager and (np.mean(agents) > prevMean):
agents, loop_manager = testThresh(agents, agents_ini, haltMin, haltMax)
# print("state activation: ", prevMean)
prevMean = np.mean(agents)
inp = np.dot(agents, adjMatrix)
agents = np.logical_or(agents, (inp >= thresholds)).astype(int)
step = step + 1
# print("state activation: ", prevMean)
# print('LT-absolute stopped at step ' + str(step) + ' '+ str(np.mean(agents)))
final_activation.append(prevMean)
return agents
def IC(agents, adjMatrix, nAgents, avgDegree=0, haltMin=.49, haltMax=.51, rs = None):
# init some stuff for numba
thresholds = np.zeros_like(nAgents)
inp = np.zeros_like(nAgents)
step = 0
numNeighbors = np.zeros_like(nAgents)
prevMean = -1
liveEdges = np.zeros_like(adjMatrix)
pInfect = np.zeros_like(adjMatrix)
flips = np.zeros_like(adjMatrix)
numNeighbors = np.sum(adjMatrix, axis=0)
#prevMean = -1
step = 1
# calculate each edges probability of allowing infection
# determine 'live' and 'blocked' edges
if rs is None:
pInfect = np.multiply(adjMatrix, np.random.random(adjMatrix.shape))
flips = np.random.random(pInfect.shape)
else:
pInfect = np.multiply(adjMatrix, rs.random(adjMatrix.shape))
flips = rs.random(pInfect.shape)
pInfect = np.multiply(cascadeParameter, pInfect)
liveEdges = flips < pInfect
agents_ini = agents
loop_manager = False
count = 40
while not loop_manager and (np.mean(agents) > prevMean):
agents, loop_manager = testThresh(agents, agents_ini, haltMin, haltMax)
prevMean = np.mean(agents)
inp = np.dot(agents, liveEdges)
agents = np.logical_or(agents, inp).astype(int)
step = step + 1
if count >= 40:
loop_manager = True
count += 1
# print('LT-absolute stopped at step ' + str(step) + ' '+ str(np.mean(agents)))
final_activation.append(prevMean)
return agents
def generateGraph(net, adjMatrix, pp, batch_size = 1, random_state=None):
gg = []
while True:
nAgents = nx.number_of_nodes(net)
agents = np.zeros((1, nAgents))
nodes = nx.nodes(net)
seed_nodes = []
inx = []
count = 0
for n in nodes:
if n in seeds['ID'].tolist():
seed_nodes.append(n)
inx.append(count)
count += 1
agents[:,inx] = 1
es_nodes = []
es_inx = []
count_es = 0
num_es = 0
for n in nodes:
if n in es['ID'].tolist():
es_nodes.append(n)
es_inx.append(count_es)
num_es += 1
count_es += 1
p_activation = num_es/count_es
# print("final_activation: ", p_activation)
# print("Adjaecency Matrix: ", adjMatrix.shape)
# seed neighbors of seeds
# print("zero initialized agents: ", agents[agents == 1].shape)
# for i in range(1):
# agents = np.logical_or(agents, np.dot(agents, adjMatrix)).astype(float) #all neighbors
avgDegree = 2*net.number_of_edges() / float(net.number_of_nodes())
# print("initially activated agents: ", agents[agents == 1].shape)
t = 1/3
if pp <= t:
agents_type = IC(agents, adjMatrix, nAgents, avgDegree=avgDegree,
haltMin = max(0,p_activation-activation_ci),
haltMax = min(p_activation+activation_ci,1),
rs = random_state)
elif pp > t and pp <= 2*t:
agents_type = ltProp(agents, adjMatrix, nAgents, avgDegree=avgDegree,
haltMin = max(0,p_activation-activation_ci),
haltMax = min(p_activation+activation_ci,1),
rs = random_state)
else:
agents_type = ltAbs(agents, adjMatrix, nAgents, avgDegree=avgDegree,
haltMin = max(0,p_activation-activation_ci),
haltMax = min(p_activation+activation_ci,1),
rs = random_state)
#if not testThresh(agents_type, haltMin, haltMax):
# print('bad data, LTabs1:\t'+str(np.mean(agents_type)))
# continue
gparam = {}
gparam['init'] = net
gparam['graph'] = agents_type
gg.append(gparam['graph'])
if len(gg) >= batch_size:
break
# print("final graph: ", gg[0])
return gg[0]
def compileG(ll, batch_size = 1, random_state=None):
return generateGraph(net_s, adjMat_s, ll[0], batch_size, random_state)
def yMetric(x, i = 0):
return x[0,i]
def eucMultiArgs(X, Y):
dist = np.linalg.norm(X - Y)
return np.array([dist])
if __name__ == "__main__":
#for i in range(len(sglist)):
# print('# of nodes in {i}th component is - ', str(np.mean(gmat[i])))
seeds = pd.read_csv("../data/icpsr/DS0001/paluck-seed.csv")
seeds['ID'] = ((seeds['SCHIDW2'] * 1000) + pd.to_numeric(seeds['ID'], errors='coerce'))
es = pd.read_csv("../data/icpsr/DS0001/paluck-endstate.csv")
es['ID'] = ((es['SCHIDW2'] * 1000) + pd.to_numeric(es['ID'], errors='coerce'))
# seed = 20170530 # this will be separately given by ELFI
# np.random.seed(seed)
ng = int(sys.argv[1])
N = int(sys.argv[2]) # samples for rejection sampling
networktype = 'pref' #pref, smallworld, grid, ER, korea1, korea2, ckm
ic_count = 0
ltp_count = 0
lta_count = 0
ic_ltp = 0
ic_lta = 0
ltp_lta = 0
count_samples = 0
print(nx.__version__)
#parameters of the script
path = '../data/icpsr/DS0001/paluck-edgelist.csv'
globalThreshold = 1.5
activation_ci = 0.075
cascadeParameter = 0.5
edgelist = pd.read_csv(path)
G = nx.from_pandas_edgelist(edgelist, source='ID', target='PEERID')
print(nx.info(G))
print(nx.number_of_nodes(G))
print(f'connected?\t{nx.is_connected(G)}')
print(f'# of connected components:\t{nx.number_connected_components(G)}')
components = nx.connected_components(G)
sglist = [G.subgraph(c) for c in nx.connected_components(G)]
gmat = []
for g in sglist:
gmat.append(nx.to_numpy_matrix(g, dtype=np.float))
graph_size_list = [nx.number_of_nodes(g) for g in sglist]
graph_size_series = pd.Series(graph_size_list)
ordered_graph_series = graph_size_series.sort_values()
ordered_graph_list = ordered_graph_series.index.tolist()
reslist = []
# for ng in range(nx.number_connected_components(G)):
#n,a = getGraphFromEdgelist(path)
print("Rejection Sampling for graph: ", ng+1)
net_s = sglist[ng]
adjMat_s = gmat[ng]
nAgobs = nx.number_of_nodes(net_s)
agobs = np.zeros((1, nAgobs))
nodeobs = nx.nodes(net_s)
es_nodeobs = []
es_inxobs = []
count_esobs = 0
num_esobs = 0
seed_node_count = 0
final_activation = []
for s in nodeobs:
if s in seeds['ID'].tolist():
seed_node_count += 1
print("number of seed nodes: ", seed_node_count)
if seed_node_count > 0:
for n in nodeobs:
if n in es['ID'].tolist():
es_nodeobs.append(n)
es_inxobs.append(count_esobs)
num_esobs += 1
count_esobs += 1
agobs[:,es_inxobs] = 1
gr_obs = np.matrix(agobs).astype(int)
dcom = "d=elfi.Distance('euclidean'"
prop_prob = elfi.Prior('uniform', 0, 1)
Y = elfi.Simulator(compileG, prop_prob, observed = gr_obs)
ret = []
for i in range(gr_obs.shape[1]):
st = str(i)
var_s = ''.join(['s',st])
ret.append(var_s)
com = var_s + ' = ' + 'elfi.Summary(yMetric, Y, '+st+')'
exec(com)
if i == 0:
dcom = dcom + ',s' + str(i)
else:
dcom = dcom + ',s' + str(i)
dcom = dcom + ')'
exec(dcom)
# d = elfi.Distance('euclidean',s7)
# d = elfi.Distance('euclidean',s0, s1, s2, s3, s4, s5, s6)
# rej = elfi.Rejection(d, batch_size=1, seed=seed)
rej = elfi.Rejection(d, batch_size=1)
result = rej.sample(N, quantile=0.1)
print(result)
# print("final activations check: ", final_activation)
count_samp = pd.to_numeric(pd.Series(list(result.samples['prop_prob'])))
count_samples = len(count_samp)
ic_count = len(count_samp[count_samp <= 0.3333])
ltp_count = len(count_samp[(count_samp > 0.3333) & (count_samp <= 0.6667)])
lta_count = len(count_samp[count_samp > 0.6667])
ic_ltp = len(count_samp[count_samp < 0.6667])
ic_lta = len(count_samp[(count_samp < 0.3333) | (count_samp > 0.6667)])
ltp_lta = len(count_samp[count_samp > 0.3333])
reslist.append([ng+1,
result.samples['prop_prob'].mean(),
np.median(result.samples['prop_prob']),
statistics.stdev(result.samples['prop_prob']),
ic_count/count_samples,
ltp_count/count_samples,
lta_count/count_samples,
ic_ltp/count_samples,
ic_lta/count_samples,
ltp_lta/count_samples,
seed_node_count/count_esobs,
num_esobs/count_esobs, np.max(final_activation),
result.samples['prop_prob']])
resdf = pd.DataFrame(reslist,
columns = ["graph_index",
"probability_parameter_mean",
"probability_parameter_median",
"probability_parameter_stdev",
"ic_probability_inference",
"ltp_probability_inference",
"lta_probability_inference",
"ic_or_ltp_probability_inference",
"ic_or_lta_probability_inference",
"ltp_or_lta_probability_inference",
"seed_activation",
"actual_end_activation",
"observed_max_end_activation_10_samples",
"probability_parameter_samples"])
fname = "../results/rejection_sampling/LT_IC/N_" + str(N) + ".csv"
if ng == 0:
resdf.to_csv(fname, index = False)
else:
prevdf = pd.read_csv(fname)
df_all = pd.concat([prevdf, resdf], ignore_index=True)
df_all.to_csv(fname, index = False)
dirname = "N_" + str(N)
dirpath = "../results/rejection_sampling/LT_IC/result_objects/" + dirname
if not os.path.exists(dirpath):
os.makedirs(dirpath)
filename = os.path.join(dirpath, "graph_" + str(ng+1) +".pkl")
filehandler = open(filename, 'wb')
pickle.dump(result, filehandler)
filehandler.close()