-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_utils.py
249 lines (201 loc) · 6.02 KB
/
dom_utils.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
import os
import platform
import configparser
import numpy as np
import pandas as pd
import networkx as nx
from scipy import stats
from graph_plot import plotHierarchy
def setDomWorldCfg(filename, params):
"""
Setting parameters in domWorld config file.
"""
new_cfg = []
with open(filename, 'r') as f:
for row in f.readlines():
for k in params.keys():
if k in row:
row = k + ' = {}'.format(params[k]) + '\t' + row[row.find('#'):]
break
new_cfg.append(row)
f.close()
with open(filename, 'w') as f:
for item in new_cfg:
f.write(item)
f.close()
return
def runDomWorldModel(cfg_file):
"""
Runs DomWorld_Legacy model from python code.
"""
if platform.system() == 'Windows':
os.system('DomWorld_Legacy.exe .\{}'.format(cfg_file))
elif platform.system() == 'Linux':
os.system('wine DomWorld_Legacy.exe ./{}'.format(cfg_file))
else:
print('System not supported')
return
def unifyRunsOutput(f_name):
"""
Unifies output files of different runs in f_name file.
"""
out_files = []
for f in os.listdir('.'):
if ('output' in f) and ('.csv' in f) and ('F' not in f):
out_files.append(f)
print(out_files)
l = []
for filename in out_files:
df = pd.read_csv(filename, sep='\t', index_col=None, header=0)
l.append(df)
frame = pd.concat(l, axis=0, ignore_index=True)
frame.to_csv(f_name, sep=';', na_rep='NA', decimal='.')
return
def individualsNumber(cfg_file):
"""
Reading the number of individuas from the config file.
"""
with open(cfg_file) as f:
file_content = '[default]\n' + f.read()
f.close()
cp = configparser.ConfigParser()
cp.read_string(file_content)
return int(cp['default']['NumFemales']) + int(cp['default']['NumMales'])
def davidsScore(contest_mat):
"""
Calculate the David's Score given the contest matrix. The David's score
for an individual i is given by:
- DS_i = w + w_2 - l - l_2
"""
# compute win proportion matrix
n_ind = len(contest_mat[0])
P_mat = np.zeros((n_ind,n_ind)) # win proportion matrix
w = []
for i in range(n_ind):
P_list = []
for j in range(n_ind):
if i == j:
P_list.append(0)
continue
else:
a_ij = contest_mat[i][j] # no. times i defeat j
n_ij = a_ij + contest_mat[j][i] # no. interactions between i and j
P_ij = (a_ij/n_ij if n_ij != 0 else 0) # proportion of wins by i over j
P_mat[i][j] = P_ij
P_list.append(P_ij)
w_i = sum(P_list) # i win rate
w.append(w_i)
# compute l term to calculate David's Score
l = []
for j in range(n_ind):
l_list = []
for i in range(n_ind):
l_list.append(P_mat[i][j])
l_i = sum(l_list)
l.append(l_i)
# compute term w_2 and l_2 to calculate David's Score
DS = [] # David's scores
w_2 = [] # w_2 values list
l_2 = [] # l_2 values list
for i in range(n_ind):
w_2_i = []
l_2_i = []
for j in range(n_ind):
if i == j:
w_2_i.append(0)
l_2_i.append(0)
else:
w_2_ij = P_mat[i][j]*w[j]
l_2_ij = P_mat[j][i]*l[j]
w_2_i.append(w_2_ij)
l_2_i.append(l_2_ij)
w_2.append(sum(w_2_i))
l_2.append(sum(l_2_i))
DS_i = w[i] + w_2[i] - l[i] - l_2[i]
DS.append(DS_i)
d_score = {}
d_score['w'] = w
d_score['w2'] = w_2
d_score['l'] = l
d_score['l2'] = l_2
d_score['DS'] = DS
return d_score
def hierarchySteepness(d_score):
"""
Computes hierarchy steepness as linear fit of the ranked David's scores.
"""
# normalize the DS to ensure that steepness varies between 0 and 1
NormDS = []
DS = d_score['DS']
n_ind = len(DS)
for i in range(n_ind):
NormDS_i = (DS[i] + (n_ind*(n_ind-1))/2)/n_ind
NormDS.append(NormDS_i)
tmp = NormDS.copy()
NormDS.sort(reverse=True)
ind_ids = []
for pos in range(n_ind):
for i in range(n_ind):
if NormDS[pos] == tmp[i]:
ind_ids.append('{}'.format(i+1))
ticks = [i for i in range(0,n_ind)]
x = np.array(ticks)
y = np.array(NormDS)
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
print('\nhierarchy steepness: %.4f' % slope)
print('intercept: %.4f' % intercept)
print('r_value: %.4f' % r_value)
#plotHierarchy(x,y,ind_ids,intercept,slope)
return abs(slope)
def mapTriadCodes(census, rand_census, triad_cfg):
"""
Maps nx.triadic_census() subgraph codes to explicit to triadic patterns names.
"""
real = {}
for k,v in sorted(census.items()):
if k in triad_cfg:
real[triad_cfg[k]] = v
random = {}
for rc in rand_census:
for k,v in sorted(rc.items()):
if k in triad_cfg:
if triad_cfg[k] not in random.keys():
random[triad_cfg[k]] = []
random[triad_cfg[k]].append(v)
return (real, random)
def triadSignificanceProfile(G, triad_cfg):
"""
Compute the significance profile of the patterns mapped in triad_cfg,
inside directed graph G.
- G : directed graph representing the network;
- triads_cfg : dict mapping interesting triadic patterns codes,
as in nx.triadic_census(), with explicit names.
(e.g. triad_cfg = {'003' : 'Null', '012' : 'Single-edge'})
"""
census = nx.triadic_census(G)
in_degree_sequence = [d for n, d in G.in_degree()] # in degree sequence
out_degree_sequence = [d for n, d in G.out_degree()] # out degree sequence
#print("In_degree sequence %s" % in_degree_sequence)
#print("Out_degree sequence %s" % out_degree_sequence)
random_nets_census = []
for i in range(100):
rand_G = nx.directed_configuration_model(in_degree_sequence, out_degree_sequence, create_using=nx.DiGraph, seed=i)
random_nets_census.append(nx.triadic_census(rand_G))
real_census, random_census = mapTriadCodes(census,random_nets_census,triad_cfg)
#print(real_census)
#print(random_census)
z_score = []
for p in real_census.keys():
print(p)
N_real_p = real_census[p]
N_rand_p = np.mean(random_census[p])
std = np.std(random_census[p])
z_p = ((N_real_p - N_rand_p)/std if std != 0 else 0)
z_score.append(z_p)
SP = []
for i in range(len(z_score)):
z_norm = np.linalg.norm(z_score)
norm_z_score = (z_score[i]/z_norm if z_norm != 0 else z_score[i])
SP.append(round(norm_z_score,4))
#print(SP)
return SP