-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_anythingXY_scatter_food_velocity_optimized.py
282 lines (230 loc) · 9.92 KB
/
plot_anythingXY_scatter_food_velocity_optimized.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
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg') #For server use
from matplotlib import colors
from matplotlib.lines import Line2D
from matplotlib.patches import Circle
import pickle
from os import makedirs, path
from automatic_plot_helper import detect_all_isings
from automatic_plot_helper import load_settings
from automatic_plot_helper import load_isings
import os
import sys
'''
loadfiles = ['beta_experiment/beta-0-1/sim-20180512-105719',
'beta_experiment/beta-1/sim-20180511-163319',
'beta_experiment/beta-10/sim-20180512-105824']
'''
def main(loadfile, settings, isings_list, plot_var_x, plot_var_y, s=0.8, alpha=0.13, autoLoad=True, x_lim=None,
y_lim=None, log=True, y_noise=True):
loadfiles = [loadfile]#loadfiles = ['sim-20191114-000009_server']
iter_list = detect_all_isings(loadfile) # iter_list = np.arange(0, 2000, 1)
#
energy_model = settings['energy_model']
numAgents = settings['pop_size']
#autoLoad = True
saveFigBool = True
fixGen2000 = False
new_order = [2, 0, 1]
labels = [r'$\beta_i = 0.1$', r'$\beta_i = 1$', r'$\_i = 10$']
cmap = plt.get_cmap('seismic')
norm = colors.Normalize(vmin=0, vmax=len(loadfiles)) # age/color mapping
# norm = [[194, 48, 32, 255],
# [146, 49, 182, 255],
# [44, 112, 147, 255]
# ]
# norm = np.divide(norm, 255)
a = 0.15 # alpha
x_pars_list, y_pars_list = fitness(loadfile, iter_list, isings_list, numAgents, autoLoad, saveFigBool, plot_var_x,
plot_var_y)
#fig, ax = plt.subplots()
cmap = plt.get_cmap('plasma')
norm = colors.Normalize(vmin=0, vmax=len(iter_list))
font = {'family': 'normal',
'weight': 'bold',
'size': 10}
plt.rc('font', **font)
plt.figure()
for gen, (x_pars, y_pars) in enumerate(zip(x_pars_list, y_pars_list)):
c = cmap(norm(gen))
if y_noise:
y_pars = y_pars.astype(float)
y_pars = y_pars + np.random.rand(np.shape(y_pars)[0]) - 0.5
ax = plt.scatter(x_pars, y_pars, s = s, alpha = alpha, c=c)
if y_noise:
plt.ylim(1,1000)
if log:
plt.xscale('log')
plt.yscale('log')
#TODO:colour acc to generation!!
plt.xlim(x_lim)
plt.ylim(y_lim)
plt.xlabel('{}'.format(plot_var_x.replace('_', ' ')))
plt.ylabel('{}'.format(plot_var_y.replace('_', ' ')))
folder = 'save/' + loadfile
savefolder = folder + '/figs/' + plot_var_x + '_vs_' + plot_var_y + '_line/'
savefilename = savefolder + plot_var_x + '_vs_' + plot_var_y + '_gen' + str(iter_list[0]) + '-' + str(
iter_list[-1]) + '.png'
if not path.exists(savefolder):
makedirs(savefolder)
if saveFigBool:
plt.savefig(savefilename, bbox_inches='tight', dpi=500)
plt.show()
# Trying to fix memory leak with this:
plt.cla()
plt.clf()
plt.close('all')
'''
###########################
FOODS = []
for loadfile in loadfiles:
x_pars_list, y_pars_list = fitness(loadfile, iter_list, isings_list, numAgents, autoLoad, saveFigBool, plot_var_x, plot_var_y)
FOODS.append((x_pars_list, y_pars_list))
# FIX THE DOUBLE COUNTING PROBLEM
# if f.shape[0] > 2000 and fixGen2000:
# print('Fixing Double Counting at Gen 2000')
# f[2000, :] = f[2000, :] - f[1999, :]
# FIX THE DOUBLE COUNTING OF THE FITNESS
plt.rc('text', usetex=True)
font = {'family': 'serif', 'size': 28, 'serif': ['computer modern roman']}
plt.rc('font', **font)
plt.rc('legend', **{'fontsize': 20})
fig, ax = plt.subplots(1, 1, figsize=(19, 10))
fig.text(0.51, 0.035, r'${}$'.format(plot_var_x.replace('_',' ')), ha='center', fontsize=20)
# fig.text(0.07, 0.5, r'$Avg. Food Consumed$', va='center', rotation='vertical', fontsize=20)
fig.text(0.07, 0.5, r'${}$'.format(plot_var_y.replace('_',' ')), va='center', rotation='vertical', fontsize=20)
title = 'Food consumed per organism'
fig.suptitle(title)
for i, FOOD in enumerate(FOODS):
# for i in range(0, numAgents):
# ax.scatter(iter_list, FOOD[:, i], color=[0, 0, 0], alpha=0.2, s=30)
c = cmap(norm(new_order[i]))
# c = norm[i]
# c = norm[IC[i]]
muF = np.mean(FOOD, axis=1)
ax.plot(iter_list, muF, color=c, label=labels[new_order[i]])
# for numOrg in range(FOOD.shape[1]):
# ax.scatter(iter_list, FOOD[:, numOrg],
# alpha=0.01, s=8, color=c, label=labels[new_order[i]])
# maxF = np.max(FOOD, axis=1)
# minF = np.min(FOOD, axis=1)
# ax.fill_between(iter_list, maxF, minF,
# color=np.divide(c, 2), alpha=a)
sigmaF = FOOD.std(axis=1)
ax.fill_between(iter_list, muF + sigmaF, muF - sigmaF,
color=c, alpha=a
)
custom_legend = [Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(1)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(0)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(2)), markersize=15),]
# custom_legend = [Circle((0, 0), 0.001,
# facecolor=cmap(norm(1))),
# Circle((0, 0), 1,
# facecolor=cmap(norm(0))),
# Circle((0, 0), 1,
# facecolor=cmap(norm(2)))]
ax.legend(custom_legend, [r'$\beta = 10$', r'$\beta = 1$', r'$\beta = 0.1$'], loc='upper left')
# plt.legend(loc=2)
# yticks = np.arange(0, 150, 20)
# ax.set_yticks(yticks)
# xticks = [0.1, 0.5, 1, 2, 4, 10, 50, 100, 200, 500, 1000, 2000]
# ax.set_xscale("log", nonposx='clip')
# ax.set_xticks(xticks)
# ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
folder = 'save/' + loadfile
savefolder = folder + '/figs/' + plot_var_x + '_vs_'+ plot_var_y + '_line/'
savefilename = savefolder + plot_var_x + '_vs_'+ plot_var_y + '_gen' + str(iter_list[0]) + '-' + str(iter_list[-1]) + '.png'
if not path.exists(savefolder):
makedirs(savefolder)
if saveFigBool:
plt.savefig(savefilename, bbox_inches='tight', dpi=300)
# plt.close()
savemsg = 'Saving ' + savefilename
print(savemsg)
# if saveFigBool:
# savefolder = folder + '/figs/fitness/'
# savefilename = savefolder + 'fitness_gen_' + str(iter_list[0]) + '-' + str(iter_list[-1]) + '.png'
# plt.savefig(bbox_inches = 'tight', dpi = 300)
plt.show()
'''
def upper_tri_masking(A):
m = A.shape[0]
r = np.arange(m)
mask = r[:, None] < r
return A[mask]
def fitness(loadfile, iter_list, isings_list, numAgents, autoLoad, saveFigBool, plot_var_x, plot_var_y):
folder = 'save/' + loadfile
folder2 = folder + '/figs/' + plot_var_x + '_vs_'+ plot_var_y + '/'
fname2 = folder2 + plot_var_x + '_vs_'+ plot_var_y + \
str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
'.npz'
# if path.isfile(fname2) and autoLoad:
# txt = 'Loading: ' + fname2
# print(txt)
# data = np.load(fname2)
# FOOD = data['FOOD']
if path.isfile(fname2) and autoLoad:
#Loading previously saved files
txt = 'Loading: ' + fname2
print(txt)
data = np.load(fname2)
x_pars_list = data['x_pars_list']
y_pars_list = data['y_pars_list']
else:
#Loading directly from isings_list in case it has been passed
x_pars_list = np.zeros((len(iter_list), numAgents))
y_pars_list = np.zeros((len(iter_list), numAgents))
for ii, isings in enumerate(isings_list):
x_pars = []
y_pars = []
for i, I in enumerate(isings):
exec('x_pars.append(I.%s)' % plot_var_x)
exec('y_pars.append(I.%s)' % plot_var_y)
x_pars_list[ii, :] = x_pars
y_pars_list[ii, :] = y_pars
if not path.exists(folder2):
makedirs(folder2)
np.savez(fname2, x_pars_list=x_pars_list)
return x_pars_list, y_pars_list
# else:
# #Otherwise load file directly
# FOOD = np.zeros((len(iter_list), numAgents))
# for ii, iter in enumerate(iter_list):
# filename = 'save/' + loadfile + '/isings/gen[' + str(iter) + ']-isings.pickle'
# startstr = 'Loading simulation:' + filename
# print(startstr)
#
# try:
# isings = pickle.load(open(filename, 'rb'))
# except Exception:
# print("Error while loading %s. Skipped file" % filename)
# #Leads to the previous datapoint being drawn twice!!
#
#
# food = []
# for i, I in enumerate(isings):
# exec('food.append(I.%s)' % plot_var)
#
# # food = np.divide(food, 6)
# x_pars_list[ii, :] = x_pars
#
# if not path.exists(folder2):
# makedirs(folder2)
#np.savez(fname2, FOOD=x_pars_list)
if __name__ == '__main__':
#loadfile = sys.argv[1]
#plot_var = sys.argv[2] #plot_var = 'v'
loadfile = 'sim-20200103-170556-ser_-s_-b_1_-ie_2_-a_0_500_1000_1500_1999'
plot_var_x = 'avg_energy'
plot_var_y = 'avg_velocity'#'food'
isings_list = load_isings(loadfile)
settings = load_settings(loadfile)
#TODO: add something that detetcts .npz file and skips loading isings in that case
main(loadfile, settings, isings_list, plot_var_x, plot_var_y, autoLoad=False, x_lim=(-1,20), y_lim=(-0.1, 0.8), alpha = 0.05)
#TODO: Evt. PCA oder decision trees um herauszufinden welche eigenschaften wichtig sind fuer hohe avg energy?