-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreproducing_plots.py
194 lines (165 loc) · 5.75 KB
/
reproducing_plots.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
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 25 15:27:38 2022
@author: mahi
"""
import glob
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
"""
code for reproducing plots from Luthra, Todd 2022 ALIFE paper (currently under review)
"""
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
# experiment 1; figure 4
files = glob.glob("final_data/experiment1/*.csv")
columns = pd.read_csv(files[0]).columns
data = pd.DataFrame(columns = columns)
for f in files:
d = pd.read_csv(f)
data = data.append(d, ignore_index = True)
patches = np.unique(np.array(data.patch))
var = "social"
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3.5))
c=1
filt_dat = data[data["combination"]==c]
filt_dat = filt_dat[filt_dat["steps"]>2800]
plot_data = []
for p in patches:
new = np.array(filt_dat[var][filt_dat.patch==p])
plot_data.append(new)
plot_data = plot_data[::-1]
parts = ax1.violinplot(plot_data, np.arange(len(patches)), showmeans=True)
for pc in parts['bodies']:
pc.set_facecolor(colors[0])
pc.set_alpha(0.5)
for partname in ('cbars','cmins','cmaxes','cmeans'):
vp = parts[partname]
vp.set_edgecolor("black")
vp.set_linewidth(1)
ax1.set_xlabel("Clustering Level", size = 12)
ax1.set_ylabel("Social Search", size = 12)
ax1.set_ylim(-1, 1)
ax1.set_title("A. Normal Perception")
ax1.set_xticks(np.arange(0, 6))
ax1.set_xticklabels(np.arange(1, 7))
ax1.tick_params(right=True, labelright=False)
c=3
filt_dat = data[data["combination"]==c]
filt_dat = filt_dat[filt_dat["steps"]>2800]
plot_data = []
for p in patches:
new = np.array(filt_dat[var][filt_dat.patch==p])
plot_data.append(new)
plot_data = plot_data[::-1]
parts = ax2.violinplot(plot_data, np.arange(len(patches)), showmeans=True)
for pc in parts['bodies']:
pc.set_facecolor(colors[1])
pc.set_alpha(0.5)
for partname in ('cbars','cmins','cmaxes','cmeans'):
vp = parts[partname]
vp.set_edgecolor("black")
vp.set_linewidth(1)
ax2.set_xlabel("Clustering Level", size = 12)
ax2.set_ylim(-1, 1)
ax2.set_title("B. Enhanced Perception")
ax2.set_xticks(np.arange(0, 6))
ax2.set_xticklabels(np.arange(1, 7))
ax2.tick_params(right=True, labelright=False)
c=2
filt_dat = data[data["combination"]==c]
filt_dat = filt_dat[filt_dat["steps"]>2800]
plot_data = []
for p in patches:
new = np.array(filt_dat[var][filt_dat.patch==p])
plot_data.append(new)
plot_data = plot_data[::-1]
parts = ax3.violinplot(plot_data, np.arange(len(patches)), showmeans=True)
for pc in parts['bodies']:
pc.set_facecolor(colors[2])
pc.set_alpha(0.5)
for partname in ('cbars','cmins','cmaxes','cmeans'):
vp = parts[partname]
vp.set_edgecolor("black")
vp.set_linewidth(1)
ax3.set_xlabel("Clustering Level", size = 12)
ax3.set_ylim(-1, 1)
ax3.set_title("C. Normal Perception for Resources;\nEnhanced Perception for Consumers")
ax3.set_xticks(np.arange(0, 6))
ax3.tick_params(right=True, labelright=False)
ax3.set_xticklabels(np.arange(1, 7))
# experiment 2; figure 5
files = glob.glob("final_data/experiment2/*.csv")
columns = pd.read_csv(files[0]).columns
data = pd.DataFrame(columns = columns)
for f in files:
d = pd.read_csv(f)
data = data.append(d, ignore_index = True)
social = np.unique(np.array(data.social))
var = "neigh10"
fig, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(5, 4))
filt_dat = data[data["steps"]>2800]
plot_data = []
for s in social:
new = np.array(filt_dat[var][filt_dat.social==s])
plot_data.append(1-new)
parts = ax1.violinplot(plot_data, np.arange(len(social)), showmeans=True)
for pc in parts['bodies']:
pc.set_facecolor(colors[3])
pc.set_alpha(0.4)
for partname in ('cbars','cmins','cmaxes','cmeans'):
vp = parts[partname]
vp.set_edgecolor("black")
vp.set_linewidth(1)
ax1.set_xlabel("Social Search", size = 12)
ax1.set_ylabel("Resource Clustering", size = 12)
ax1.set_ylim(0, 0.7)
ax1.set_xticks(np.arange(0, 9))
ax1.set_xticklabels(np.arange(-1, 1.1, 0.25))
ax1.tick_params(right=True, labelright=False)
# experiment 3; figure 8
files = glob.glob("final_data/experiment3/*.csv")
columns = pd.read_csv(files[0]).columns
data = pd.DataFrame(columns = columns)
for f in files:
d = pd.read_csv(f)
data = data.append(d, ignore_index = True)
def moving_average(var_data, window_size):
i = 0
moving_averages = []
while i < len(var_data) - window_size + 1:
window_average = np.mean(var_data[i : i + window_size])
moving_averages.append(window_average)
i += 1
return moving_averages
var1 = "social"
var2 = "neigh10"
start_cogs = np.unique(data.start_cog)
social_data =np.zeros((20, 2991))
patch_data =np.zeros((20, 2991))
for r in range(20):
filt_dat = data[(data["round"]==(r+1))]
social = moving_average(filt_dat[var1], 10)
patch = moving_average(1-filt_dat[var2], 10)
social_data[r, :]+=social
patch_data[r, :]+=patch
social_mean = np.mean(social_data, 0)
social_error = np.std(social_data, 0)/(20**0.5)
patch_mean = np.mean(patch_data, 0)
patch_error = np.std(patch_data, 0)/(20**0.5)
social_mean = social_mean[100:]
social_error = social_error[100:]
patch_mean = patch_mean[100:]
patch_error = patch_error[100:]
plt.figure(figsize = (5,4))
plt.plot(np.arange(105, 2996), social_mean, color = '#1f77b4')
plt.fill_between(np.arange(105, 2996), social_mean-social_error, social_mean+social_error, alpha = 0.4)
plt.plot(np.arange(105, 2996), patch_mean, color = 'crimson')
plt.fill_between(np.arange(105, 2996), patch_mean-patch_error, patch_mean+patch_error, alpha = 0.4, color = "crimson")
plt.ylim(-0.1, 0.55)
plt.xlim(0, 3000)
plt.xlabel("Timesteps", size = 12)
plt.ylabel("Parameter Values", size = 12)
plt.legend(["Social Search", "Resource Clustering"])
plt.tick_params(right=True, labelright=False)