-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_anything_combined.py
207 lines (159 loc) · 6.83 KB
/
plot_anything_combined.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
#!/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
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(loadfiles, plot_var, isings_lists = None, autoLoad = True,
sim_labels = [r'$\beta_i = 0.1$', r'$\beta_i = 1$', r'$\beta_i = 10$']):
'''
Can either plot one or multiple simulations in a combined plot
:param loadfile: save names of simulations; list of strings
:param plot_var: isings attribute to ne plotted over generation
:param isings_lists: list of isings list (one isings list for each simulation to be plotted)
:param autoLoad: If previously plotted should npz file be loaded to speed up process?
:param sim_labels: Labels of simulation in plot in case multi_sim = True
'''
if type(loadfiles) == str:
loadfiles = [loadfiles]
if type(isings_lists) == str:
isings_lists == [isings_lists]
#loadfiles = [loadfile]#loadfiles = ['sim-20191114-000009_server']
#Boo shows whether there are multiple simulations in one plot
multiple_sim = len(loadfiles) > 1
#energy_model = settings['energy_model']
#autoLoad = True
saveFigBool = True
fixGen2000 = False
new_order = [2, 0, 1]
labels = sim_labels
cmap = plt.get_cmap('seismic')
norm = colors.Normalize(vmin=0, vmax=len(loadfiles)) # age/color mapping
a = 0.15 # alpha
###########################
FOODS = []
for loadfile, isings_list in zip(loadfiles, isings_lists):
iter_list = detect_all_isings(loadfile) # iter_list = np.arange(0, 2000, 1)
settings = load_settings(loadfile)
numAgents = settings['pop_size']
f = fitness(loadfile, iter_list, isings_list, numAgents, autoLoad, saveFigBool, plot_var)
# 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, :]
FOODS.append(f)
# 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'$Generation$', 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'$%s$' %plot_var.replace('_',' '), va='center', rotation='vertical', fontsize=20)
title = '' #'Food consumed per organism'
fig.suptitle(title)
for i, FOOD in enumerate(FOODS):
c = cmap(norm(i))
muF = np.mean(FOOD, axis=1)
ax.plot(iter_list, muF, color=c, label=labels[i])
# ax.scatter(iter_list, muF, color=c, label=labels[i], alpha=0.15)
ax.scatter(np.tile(iter_list, (50, 1)), FOOD.transpose(), s=0.1, alpha=0.3, color=c)
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(0)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(1)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(2)), markersize=15),]
#Custom legend for multiple runs in one plot removed:
if multiple_sim:
ax.legend(custom_legend, sim_labels, loc='upper left')
#ax.legend(custom_legend, [r'$\beta = 10$', r'$\beta = 1$', r'$\beta = 0.1$'], loc='upper left')
if multiple_sim:
savefolder = 'multi_sim_plots/'
for loadfile in loadfiles:
savefolder += loadfile[0:18] + '__'
savefolder += '/'
else:
folder = 'save/' + loadfile
savefolder = folder + '/figs/' + plot_var + '_line/'
savefilename = savefolder + plot_var + '_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)
plt.close()
# 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):
folder = 'save/' + loadfile
folder2 = folder + '/figs/' + plot_var + '_line/'
fname2 = folder2 + plot_var + \
str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
'.npz'
if path.isfile(fname2) and autoLoad:
#Loading previously saved files
txt = 'Loading: ' + fname2
print(txt)
data = np.load(fname2)
FOOD = data['FOOD']
elif not isings_list is None:
#Loading directly from isings_list in case it has been passed
FOOD = np.zeros((len(iter_list), numAgents))
for ii, isings in enumerate(isings_list):
food = []
for i, I in enumerate(isings):
exec('food.append(I.%s)' % plot_var)
FOOD[ii, :] = food
if not path.exists(folder2):
makedirs(folder2)
np.savez(fname2, FOOD=FOOD)
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)
FOOD[ii, :] = food
if not path.exists(folder2):
makedirs(folder2)
np.savez(fname2, FOOD=FOOD)
return FOOD
if __name__ == '__main__':
loadfile = sys.argv[1]
plot_var = sys.argv[2] #plot_var = 'v'
main(loadfile, plot_var)