-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_analysis_coop_TminusS.py
300 lines (172 loc) · 11.4 KB
/
data_analysis_coop_TminusS.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
#!/usr/bin/env python
'''
Code to read the pickle file with the raw data from the DAU experiments,
and make it into a csv file.
Created by Julia Poncela, on December 2014.
'''
import pickle
from unidecode import unidecode # to transform whatever unicode special characters into just plain ascii (otherwise networkx complains)
import histograma_bines_gral
import numpy
from scipy import stats
import random
def main():
pupulation_age="All" #"young" # or "adult" or "All"
if pupulation_age== "young":
min_age_threshold=0
max_age_threshold=15
elif pupulation_age== "adult":
min_age_threshold=16
max_age_threshold=100
elif pupulation_age== "All":
min_age_threshold=0
max_age_threshold=100
else:
print "wrong age range"
exit()
R=10
P=5
####### to select results only from given rounds (both ends included)
min_round=1
max_round=18
type_definition= "PD" #"higherHarmony" #"SD" #"lowerHarmony" # or "PD" "SH" "SD"
umbral_coop=0.75
######### input file
filename="../Data/userdata.pickle"
master_list=pickle.load(open(filename, 'rb')) # es una lista: un elemento por jugador (541)
filename2="../Results/list_good_guys_"+str(type_definition)+"_threshold_coop"+str(umbral_coop)+".pickle"
aux_list_good_guys=pickle.load(open(filename2, 'rb')) # list of players who cooperated in the lower half of the Harmony (obtained in code: data_analysis_coop_exploring_behavior2.py)
list_good_guys=[]
for element in aux_list_good_guys:
list_good_guys.append(int(element))
######### output files
output_filename1="../Results/Cooperation_vs_SminusT_"+str(pupulation_age)+"_ages_rounds"+str(min_round)+"_"+str(max_round)+str(type_definition)+".dat"
output1= open(output_filename1,'wt')
output_filename2="../Results/Cooperation_vs_SminusT_"+str(pupulation_age)+"_ages_rounds"+str(min_round)+"_"+str(max_round)+"_good_guys_"+str(type_definition)+".dat"
output2= open(output_filename2,'wt')
output_filename3="../Results/Cooperation_vs_SminusT_"+str(pupulation_age)+"_ages_rounds"+str(min_round)+"_"+str(max_round)+"_bad_guys_"+str(type_definition)+".dat"
output3= open(output_filename3,'wt')
output_filename4="../Results/Cooperation_vs_SminusT_RANDOM_"+str(pupulation_age)+"_ages_rounds"+str(min_round)+"_"+str(max_round)+"_bad_guys_"+str(type_definition)+".dat"
output4= open(output_filename4,'wt')
#########
### master_list tiene la forma: [{'guany_total': 110L, 'partida': 1L, 'genere': u'h', 'num_eleccions': 14, 'edat': 50L, 'rationality': 66.666666666666671, 'ambition': 100.0, 'rondes': [{'guany_oponent': 10L, 'ambition': None, 'seleccio': u'C', 'oponent': 7L, 'S': 6L, 'T': 5L, 'seleccio_oponent': u'C', 'numronda': 1L, 'guany': 10L, 'cuadrant': u'Harmony', 'rationality': 1.0}, {'guany_oponent': 6L, 'ambition': None, 'seleccio': u'D', 'oponent': 17L, 'S': 6L, 'T': 8L, 'seleccio_oponent': u'C', 'numronda': 2L, 'guany': 8L, 'cuadrant': u'Harmony', 'rationality': 0.0},...], 'nickname': u'Caesar', 'id': 2L}]
#la llave key tiene a su vez como valor una lista de diccionarios (uno por ronda)
# [{'guany_oponent': 10L, 'ambition': None, 'seleccio': u'C', 'oponent': 7L, 'S': 6L, 'T': 5L, 'seleccio_oponent': u'C', 'numronda': 1L, 'guany': 10L, 'cuadrant': u'Harmony', 'rationality': 1.0}, {'guany_oponent': 6L, 'ambition': None, 'seleccio': u'D', 'oponent': 17L, 'S': 6L, 'T': 8L, 'seleccio_oponent': u'C', 'numronda': 2L, 'guany': 8L, 'cuadrant': u'Harmony', 'rationality': 0.0}, ...]
dict_SminusT_list_actions={}
dict_SminusT_avg_coop={}
dict_SminusT_std_coop={}
dict_SminusT_sem_coop={} # error of the mean =std/ sqrt(num points)
dict_SminusT_list_actions_good_guys={}
dict_SminusT_avg_coop_good_guys={}
dict_SminusT_std_coop_good_guys={}
dict_SminusT_sem_coop_good_guys={} # error of the mean =std/ sqrt(num points)
dict_SminusT_list_actions_bad_guys={}
dict_SminusT_avg_coop_bad_guys={}
dict_SminusT_std_coop_bad_guys={}
dict_SminusT_sem_coop_bad_guys={} # error of the mean =std/ sqrt(num points)
dict_SminusT_list_random_actions={}
dict_SminusT_avg_coop_random_actions={}
dict_SminusT_std_coop_random_actions={}
dict_SminusT_sem_coop_random_actions={}
for dictionary in master_list: # cada elemento de la lista es a su vez un dict
partida=dictionary['partida']
num_elecciones=dictionary['num_eleccions']
age=int(dictionary['edat'])
num_rondas=len(dictionary['rondes'])
nickname=unidecode(dictionary['nickname']).replace(" ", "_")
user_id=int(dictionary['id'] )
list_dict_rondas=dictionary['rondes']
for dict_ronda in list_dict_rondas:
## cada diccionario de ronda tiene: {'guany_oponent': 10L, 'ambition': None, 'seleccio': u'C', 'oponent': 7L, 'S': 6L, 'T': 5L, 'seleccio_oponent': u'C', 'numronda': 1L, 'guany': 10L, 'cuadrant': u'Harmony', 'rationality': 1.0}
T=int(dict_ronda['T'])
S=int(dict_ronda['S'])
list_four_possible_values=[P,R,T,S]
SminusT=S-T
round_number=dict_ronda['numronda']
action=dict_ronda['seleccio']
if action =="C":
action=1.
elif action=="D":
action=0.
# si no ha elegido nada, es None
##### i generate a random action for comparison purposes
if action !=None: # to have exactly as many random actions as real ones
random_action= random.choice([0.,1.])
if SminusT not in dict_SminusT_list_random_actions:
dict_SminusT_list_random_actions[SminusT]=[]
dict_SminusT_list_random_actions[SminusT].append(random_action)
####### cooperation vs S-T for everybody
if SminusT in dict_SminusT_list_actions:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
dict_SminusT_list_actions[SminusT].append(action)
else:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
dict_SminusT_list_actions[SminusT]=[]
dict_SminusT_list_actions[SminusT].append(action)
####### cooperation vs S-T for good guys
if SminusT in dict_SminusT_list_actions_good_guys:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
if user_id in list_good_guys:
dict_SminusT_list_actions_good_guys[SminusT].append(action)
else:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
if user_id in list_good_guys:
dict_SminusT_list_actions_good_guys[SminusT]=[]
dict_SminusT_list_actions_good_guys[SminusT].append(action)
####### cooperation vs S-T for bad guys
if SminusT in dict_SminusT_list_actions_bad_guys:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
if user_id not in list_good_guys:
dict_SminusT_list_actions_bad_guys[SminusT].append(action)
else:
if action !=None:
if age >= min_age_threshold and age <= max_age_threshold:
if round_number <= max_round and round_number >= min_round :
if user_id not in list_good_guys:
dict_SminusT_list_actions_bad_guys[SminusT]=[]
dict_SminusT_list_actions_bad_guys[SminusT].append(action)
####### the the avg cooperation per (T-S) point
for SminusT in sorted(dict_SminusT_list_actions):
dict_SminusT_avg_coop[SminusT]=numpy.mean(dict_SminusT_list_actions[SminusT])
dict_SminusT_std_coop[SminusT]=numpy.std(dict_SminusT_list_actions[SminusT])
dict_SminusT_sem_coop[SminusT]=stats.sem(dict_SminusT_list_actions[SminusT])# error of the mean =std/ sqrt(num points)
dict_SminusT_avg_coop_good_guys[SminusT]=numpy.mean(dict_SminusT_list_actions_good_guys[SminusT])
dict_SminusT_std_coop_good_guys[SminusT]=numpy.std(dict_SminusT_list_actions_good_guys[SminusT])
dict_SminusT_sem_coop_good_guys[SminusT]=stats.sem(dict_SminusT_list_actions_good_guys[SminusT])# error of the mean =std/ sqrt(num points)
dict_SminusT_avg_coop_bad_guys[SminusT]=numpy.mean(dict_SminusT_list_actions_bad_guys[SminusT])
dict_SminusT_std_coop_bad_guys[SminusT]=numpy.std(dict_SminusT_list_actions_bad_guys[SminusT])
dict_SminusT_sem_coop_bad_guys[SminusT]=stats.sem(dict_SminusT_list_actions_bad_guys[SminusT])# error of the mean =std/ sqrt(num points)
dict_SminusT_avg_coop_random_actions[SminusT]=numpy.mean(dict_SminusT_list_random_actions[SminusT])
dict_SminusT_std_coop_random_actions[SminusT]=numpy.std(dict_SminusT_list_random_actions[SminusT])
dict_SminusT_sem_coop_random_actions[SminusT]=stats.sem(dict_SminusT_list_random_actions[SminusT])# error of the mean =std/ sqrt(num points)
print >> output1,SminusT,dict_SminusT_avg_coop[SminusT], dict_SminusT_std_coop[SminusT], dict_SminusT_sem_coop[SminusT]
print >> output2,SminusT,dict_SminusT_avg_coop_good_guys[SminusT], dict_SminusT_std_coop_good_guys[SminusT], dict_SminusT_sem_coop_good_guys[SminusT]
print >> output3,SminusT,dict_SminusT_avg_coop_bad_guys[SminusT], dict_SminusT_std_coop_bad_guys[SminusT], dict_SminusT_sem_coop_bad_guys[SminusT]
print >> output4,SminusT, dict_SminusT_avg_coop_random_actions[SminusT], dict_SminusT_std_coop_random_actions[SminusT], dict_SminusT_sem_coop_random_actions[SminusT]
output1.close()
print "written output datafile:", output_filename1
output2.close()
print "written output datafile:", output_filename2
output3.close()
print "written output datafile:", output_filename3
output4.close()
print "written output datafile:", output_filename4
######################################
######################################
######################################
if __name__ == '__main__':
# if len(sys.argv) > 1:
# graph_filename = sys.argv[1]
main()
#else:
# print "Usage: python script.py "