-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_analysis.py
146 lines (117 loc) · 4.41 KB
/
net_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
import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx
from graph_plot import plotNetwork
from dom_utils import setDomWorldCfg, runDomWorldModel, davidsScore, \
unifyRunsOutput, hierarchySteepness, triadSignificanceProfile
if len(sys.argv) > 1:
from json import loads
CONFIG_FILE = sys.argv[1]
OUTPUT_FILE = sys.argv[2]
params = loads(sys.argv[3])
else:
CONFIG_FILE = 'Config_domHierarchies.ini'
OUTPUT_FILE = 'FILENAME.csv'
params = {
"Periods" : 260,
"firstDataPeriod" : 200,
"InitialDensity" : 1.7,
"NumFemales" : 4, # 4, 6, 9, 12, 15, 18, 21, 24
"NumMales" : 4, # 4, 6, 9, 12, 15, 18, 21, 24
"Rating.Dom.female.Intensity" : 0.1, # eg: 0.1 desp: 0.8
"Rating.Dom.male.Intensity" : 0.2, # eg: 0.2 desp: 1.0
"female.PersSpace" : 2.0,
"female.FleeDist" : 2.0,
"male.PersSpace" : 2.0,
"male.FleeDist" : 2.0
}
setDomWorldCfg(CONFIG_FILE,params)
runDomWorldModel(CONFIG_FILE)
# Reading data from DomWorld output
unifyRunsOutput(OUTPUT_FILE) # unify different runs output files
data = pd.read_csv(OUTPUT_FILE, usecols=['run','period','actor.id','actor.sex','actor.behavior','actor.score',
'receiver.id','receiver.sex','receiver.behavior','receiver.score'], sep=';')
# selecting the rows representing dominance interactions
df_attacks = data.query('`actor.behavior` == "Fight" | `actor.behavior` == "Flee"')
#print(df_attacks)
N_IND = int(params['NumFemales']) + int(params['NumMales'])
dom_mat = np.zeros((N_IND,N_IND))
# Create contest matrix from raw interaction data
# counting the number of wins in each dyad:
# dom_mat[r][c] <- n. of times r wins over c
for idx in df_attacks.index:
act_idx = int(df_attacks['actor.id'][idx]) - 1 # domMatrix attacker index (row)
recv_idx = int(df_attacks['receiver.id'][idx]) - 1 # domMatrix receiver index (col)
if df_attacks['actor.behavior'][idx] == "Fight": # attacker wins
dom_mat[act_idx][recv_idx] += 1
elif df_attacks['actor.behavior'][idx] == "Flee": # receiver wins
dom_mat[recv_idx][act_idx] += 1
print('\nContest matrix:')
print(dom_mat)
'''
N_IND = 7
dom_mat = [
[0, 0, 1, 2, 10, 63, 8],
[0, 0, 2, 3, 0, 88, 4],
[0, 0, 0, 4, 65, 84, 3],
[0, 0, 0, 0, 0, 80, 10],
[0, 0, 0, 0, 0, 4, 1],
[0, 1, 5, 0, 10, 0, 6],
[0, 0, 0, 0, 0, 2, 0]
]'''
# Compute hierarchy ranking with the David's score measure
ds = davidsScore(dom_mat)
steep = hierarchySteepness(ds)
# create dominance matrix, dom_mat[r][c] is equal to:
# - 1 -> r dominates c
# - 0 -> c dominates r
# - 0.5 -> equal number of wins
for r in range(N_IND):
for c in range(r, N_IND):
if r == c:
dom_mat[r][c] = 0 # no fight against itself
continue
if dom_mat[r][c] > dom_mat[c][r]: # r wins over c
dom_mat[r][c] = 1
dom_mat[c][r] = 0
elif dom_mat[r][c] < dom_mat[c][r]: # c wins over r
dom_mat[r][c] = 0
dom_mat[c][r] = 1
else: # deuce
tmp = (0.5 if dom_mat[r][c] != 0 else 0)
dom_mat[r][c] = tmp
dom_mat[c][r] = tmp
print('\nDominance matrix:')
print(dom_mat)
# triadic census of the dominance network represented as a digraph,
# individuals are the nodes, and edges their dominance relationship
triad_cfg = {
'003' : 'Null',
'012' : 'Single-edge',
'021C': 'Pass-along',
'021D': 'Double-dominant',
'021U': 'Double-subordinate',
'030C': 'Cycle',
'030T': 'Transitive'
}
net_G = nx.from_numpy_matrix(dom_mat, create_using=nx.DiGraph)
census = nx.triadic_census(net_G)
sp = triadSignificanceProfile(net_G, triad_cfg)
#with open('requirements.txt', 'a') as f:
# f.write('%s\n' % sp)
f_census = {}
f_census['group-size'] = [N_IND]
f_census['flee-dist'] = [params['female.FleeDist']]
f_census['aggr-intensity'] = [('mild' if params['Rating.Dom.female.Intensity'] == 0.1 else 'fierce')]
f_census['steepness'] = round(steep,4)
print('\nNetwork Triadic Census:')
for k,v in sorted(census.items()):
if k in triad_cfg:
f_census[triad_cfg[k]] = [v]
print(' ' + triad_cfg[k] + ': ' + str(v))
res = pd.DataFrame.from_dict(f_census, orient='columns')
res.to_csv('results.csv', mode='a', sep=';', header=False)
#plotNetwork(net_G)