-
Notifications
You must be signed in to change notification settings - Fork 1
/
score.py
292 lines (204 loc) · 6.85 KB
/
score.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
import networkx as nx
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.nn.inits import reset
import random
import numpy as np
from torch_geometric import utils
import scipy
import ndlib
import ndlib.models.epidemics as ep
import ndlib.models.ModelConfig as mc
import statistics as s
import time
from util import combinations, substract, subcombs
def scoreIC(g, config, node, seedset):
backup = []
for item in seedset:
backup.append(item)
backup.remove(node)
total = []
for i in range(1000):
g_mid = g.__class__()
g_mid.add_nodes_from(g)
g_mid.add_edges_from(g.edges)
model_mid = ep.IndependentCascadesModel(g_mid)
config_mid = mc.Configuration()
config_mid.add_model_initial_configuration('Infected', backup)
for a, b in g_mid.edges():
weight = config.config["edges"]['threshold'][(a, b)]
g_mid[a][b]['weight'] = weight
config_mid.add_edge_configuration('threshold', (a, b), weight)
model_mid.set_initial_status(config_mid)
iterations = model_mid.iteration_bunch(5)
trends = model_mid.build_trends(iterations)
total_no = 0
for i in range(5):
a = iterations[i]['node_count'][1]
total_no += a
total.append(total_no)
final = s.mean(total)
return final
def Y(df):
EY = s.mean(df['result'])
VY = s.pvariance(df['result'])
return EY, VY
def SobolT(df, result):
sobolt = {}
for node in result:
backup = []
for item in result:
backup.append(item)
backup.remove(node)
var = []
for sub in combinations(backup):
means = []
for case in combinations([node]):
total = []
seeds = sub + case
subdf = df
for item in result:
if item in seeds:
a = (subdf[item] == 1)
else:
a = (subdf[item] == 0)
subdf = subdf[a]
means.append(s.mean(subdf['result']))
var.append(s.pvariance(means))
sobolt[node] = s.mean(var)
return sobolt
def sobols(df, result):
allsobol = {}
for blist in combinations(result):
if blist == []:
continue
rest = substract(result, blist)
exp = []
for comb in combinations(blist):
means = []
for sub in combinations(rest):
totals = []
seeds = comb + sub
conditions =[]
#subdf = df
for item in result:
if item in seeds:
a = (df[item] == 1)
else:
a = (df[item] == 0)
#subdf = df[a]
conditions.append(a)
subdf = df[conditions[0] & conditions[1] & conditions[2] & conditions[3] & conditions[4]]
means.append(s.mean(subdf['result']))
exp.append(s.mean(means))
if len(blist) == 1:
score = s.pvariance(exp)
else:
sumsobol = 0
for item in subcombs(blist):
string = ''
for thing in item:
string += str(thing)
string += '.'
sumsobol += allsobol[string]
score = s.pvariance(exp) - sumsobol
string = ''
for item in blist:
string += str(item)
string += '.'
allsobol[string] = score
sorted_sobol = {}
for k in sorted(allsobol, key=len):
sorted_sobol[k] = allsobol[k]
return sorted_sobol
def IE(df, result):
full = df[(df[result[0]] == 1) & (df[result[1]] == 1) & (df[result[2]] == 1) & (df[result[3]] == 1) & (df[result[4]] == 1)]
E = s.mean(full['result'])
std = s.stdev(full['result'])
return E, std
'''
# sobol total of a set sized-k
def STS(df, result):
k = int(0.5*len(result))
sobolt = {}
for set in combinations(result):
if len(set) < k:
continue
if len(set) > k:
break
name = ''
for item in set:
name += str(item)
name += '.'
backup = []
for item in result:
backup.append(item)
backup = substract(backup, set)
var = []
for sub in combinations(backup):
means = []
for case in combinations(set):
total = []
seeds = sub + case
subdf = df
for item in result:
if item in seeds:
a = (subdf[item] == 1)
else:
a = (subdf[item] == 0)
subdf = subdf[a]
means.append(s.mean(subdf['result']))
var.append(s.pvariance(means))
sobolt[name] = s.mean(var)
return sobolt
'''
def effectIC(g, config, result):
input = []
for i in range(1000):
g_mid = g.__class__()
g_mid.add_nodes_from(g)
g_mid.add_edges_from(g.edges)
model_mid = ep.IndependentCascadesModel(g_mid)
config_mid = mc.Configuration()
config_mid.add_model_initial_configuration('Infected', result)
for a, b in g_mid.edges():
weight = config.config["edges"]['threshold'][(a, b)]
g_mid[a][b]['weight'] = weight
config_mid.add_edge_configuration('threshold', (a, b), weight)
model_mid.set_initial_status(config_mid)
iterations = model_mid.iteration_bunch(5)
trends = model_mid.build_trends(iterations)
total_no = 0
for j in range(5):
a = iterations[j]['node_count'][1]
total_no += a
input.append(total_no)
e = s.mean(input)
v = s.stdev(input)
return e,v
def effectLT(g, config, result):
input = []
for i in range(1000):
g_mid = g.__class__()
g_mid.add_nodes_from(g)
g_mid.add_edges_from(g.edges)
model_mid = ep.ThresholdModel(g_mid)
config_mid = mc.Configuration()
config_mid.add_model_initial_configuration('Infected', result)
for a, b in g_mid.edges():
weight = config.config["edges"]['threshold'][(a, b)]
g_mid[a][b]['weight'] = weight
config_mid.add_edge_configuration('threshold', (a, b), weight)
for i in g.nodes():
threshold = random.randrange(1, 20)
threshold = round(threshold / 100, 2)
config_mid.add_node_configuration("threshold", i, threshold)
model_mid.set_initial_status(config_mid)
iterations = model_mid.iteration_bunch(5)
trends = model_mid.build_trends(iterations)
total_no = iterations[4]['node_count'][1]
input.append(total_no)
e = s.mean(input)
v = s.stdev((input))
return e,v