-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot-realizations.py
128 lines (101 loc) · 3.97 KB
/
plot-realizations.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# %% initial imports
import pandas as pd
#import numpy as np
import matplotlib as mpl
from os.path import exists
#import matplotlib.colors as colors
mpl.rc('text', usetex=True)
mpl.rc('font', family='serif')
mpl.rc('font', size=10)
# %% data loading
# file with data from the experiment
# Note: header=6 is for NetLogo data
exp_desc = 'cooperators-realizations-64-long'
sxs = { 'vN' : "von Neumann", 'rvN' : 'random von Neumann', 'M': 'Moore', 'rM': 'random Moore'} #, 'rvNM': 'random von Neumann or random Moore' }
#sxs = { 'vN' : "von Neumann"}
markers = ['o', 'x', 's', '^', '2']
colors = ['k--', 'r-.', 'b:', 'g-', 'm']
#%% read data
data = dict()
v = ['synergy-factor', '[step]', 'cooperators-fraction-mean', 'cooperators-fraction-std']
df = dict() # dcit of pandas dfs
# %% save/load data
if not exists('data_' + exp_desc + '.h5'):
for sx in sxs :
data[sx] = pd.read_csv(exp_desc + '-' + sx + '.csv', header=0)
df[sx] = pd.DataFrame(columns=v)
# select variables for the analysis
# this depends on the experiment
#%% values of the synergy factor
# sfs = data["synergy-factor"].unique()[::2] # read from file
sfs = [3.5, 3.6, 3.7, 3.8,
4.0, 4.1, 4.2, 4.4,
4.5, 4.7, 4.8, 4.9,
5.0, 5.1, 5.3, 5.5 ] # some preselected values
sfs = [
4.5, 4.7, 4.8, 4.9,
5.0, 5.1, 5.3, 5.5 ] # some preselected values
# skip some steps further on
skip = 512
# handle the step upper limit (based on experiments.xml)
max_steps = 32768
# %% save/load data
if not exists('data_' + exp_desc + '.h5'):
steps = data['vN']["[step]"].unique() # read from file
#%% data calculation
for sx in sxs :
for sf in sfs:
for st in steps[::skip]:
df[sx].loc[len(df[sx].index)] = [
sf,
st,
data[sx][(data[sx]["synergy-factor"] == sf) & (data[sx]["[step]"] == st)]["cooperators-fraction"].mean(),
data[sx][(data[sx]["synergy-factor"] == sf) & (data[sx]["[step]"] == st)]["cooperators-fraction"].std()
]
# save all dfs
for sx in sxs :
df[sx].to_hdf('data_' + exp_desc + '.h5', key=sx)
else:
for sx in sxs :
df[sx] = pd.read_hdf('data_' + exp_desc + '.h5', key=sx)
#%% plotting
plot_data_mean = dict()
plot_data_std = dict()
fig = mpl.figure.Figure(figsize=(6.5, 8.0))
for i, sf in enumerate(sfs):
axs = fig.add_subplot(4,4,i+1)
# axs.set_xscale("log", base=10)
# axs.set_yscale("log", base=10)
axs.set_ylim([0.0, 1.05])
# axs.set_xlim([0.0, 100])
axs.set_xlim([1, max_steps])
axs.set_xticks([0,10000,20000,30000])
axs.set_yticks([0,0.25,0.5,0.75,1])
axs.grid(True, linestyle=':', linewidth=0.5, c='k')
if i % 4 != 0:
axs.set_yticklabels([])
if i < len(sfs)-4:
axs.set_xticklabels([])
else:
axs.set_xticklabels(["0","1", "2", "3"])
axs.set_xlabel(r"step $[\times 10^3]$")
for i, sx in enumerate( sxs ) :
plot_data_mean[sx] = df[sx][df[sx]['synergy-factor'] == sf][["[step]","cooperators-fraction-mean"]].to_numpy()
plot_data_std[sx] = df[sx][df[sx]['synergy-factor'] == sf][["[step]","cooperators-fraction-std"]].to_numpy()
axs.set_title(r"$r={}$".format(sf))
axs.fill_between(plot_data_mean[sx].T[0],
plot_data_mean[sx].T[1]+plot_data_std[sx].T[1],
plot_data_mean[sx].T[1]-plot_data_std[sx].T[1], color=colors[i][0], alpha=.25, linewidth=.35)
axs.plot(plot_data_mean[sx].T[0], plot_data_mean[sx].T[1], colors[i], label = sxs[sx], linewidth=1)
#axs.set_yticks([0.1, .25, .50, .75, 1.00])
#axs.set_xticks(steps[::10*skip])
handles, labels = axs.get_legend_handles_labels()
lgd = fig.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.525,1.03), ncol=5)
fig.tight_layout()
display(fig)
# %% saving
fName = "plot_" + exp_desc + ".pdf"
print("INFO] Saving " + fName)
fig.savefig(fName, format="pdf", bbox_inches='tight')