-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_many_simulations_parallel_one_pop_linspace_beta_settings_very_sub_critical.py
352 lines (294 loc) · 15.2 KB
/
plot_many_simulations_parallel_one_pop_linspace_beta_settings_very_sub_critical.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import matplotlib
matplotlib.use('Agg')
from automatic_plot_helper import all_folders_in_dir_with
from automatic_plot_helper import load_isings_attr
from automatic_plot_helper import load_isings_specific_path
from automatic_plot_helper import attribute_from_isings
from automatic_plot_helper import load_settings
from automatic_plot_helper import choose_copied_isings
from automatic_plot_helper import calc_normalized_fitness
from automatic_plot_helper import load_isings_from_list
from automatic_plot_helper import all_sim_names_in_parallel_folder
import numpy as np
import matplotlib.pyplot as plt
import os
import pickle
from scipy.signal import savgol_filter
from scipy.interpolate import interp1d
import seaborn as sns
from matplotlib.lines import Line2D
import matplotlib.colors as colors_package
from heat_capacity_parameter import calc_heat_cap_param_main
from matplotlib.patches import Patch
import matplotlib.cm as cm
from matplotlib.colors import LinearSegmentedColormap
def main_plot_parallel_sims(folder_name, plot_settings):
plt.rc('text', usetex=True)
font = {'family': 'serif', 'size': 18, 'serif': ['computer modern roman']}
plt.rc('font', **font)
if plot_settings['only_copied']:
plot_settings['only_copied_str'] = '_only_copied_orgs'
else:
plot_settings['only_copied_str'] = '_all_orgs'
if plot_settings['only_plot_certain_generations']:
plot_settings['plot_generations_str'] = 'gen_{}_to_{}' \
.format(plot_settings['lowest_and_highest_generations_to_be_plotted'][0],
plot_settings['lowest_and_highest_generations_to_be_plotted'][1])
else:
plot_settings['plot_generations_str'] = 'gen_all'
if not plot_settings['only_plot']:
attrs_lists = load_attrs(folder_name, plot_settings)
save_plot_data(folder_name, attrs_lists, plot_settings)
else:
attrs_lists = load_plot_data(folder_name, plot_settings)
plot(attrs_lists, plot_settings)
def save_plot_data(folder_name, attrs_lists, plot_settings):
save_dir = 'save/{}/one_pop_plot_data/'.format(folder_name)
save_name = 'plot_data_{}{}_min_ts{}_min_food{}_{}.pickle' \
.format(plot_settings['attr'], plot_settings['only_copied_str'], plot_settings['min_ts_for_plot'],
plot_settings['min_food_for_plot'], plot_settings['plot_generations_str'])
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pickle_out = open(save_dir + save_name, 'wb')
pickle.dump(attrs_lists, pickle_out)
pickle_out.close()
def load_plot_data(folder_name, plot_settings):
save_dir = 'save/{}/one_pop_plot_data/'.format(folder_name)
save_name = 'plot_data_{}{}_min_ts{}_min_food{}_{}.pickle'. \
format(plot_settings['attr'], plot_settings['only_copied_str'], plot_settings['min_ts_for_plot'],
plot_settings['min_food_for_plot'], plot_settings['plot_generations_str'])
print('Load plot data from: {}{}'.format(save_dir, save_name))
try:
file = open(save_dir+save_name, 'rb')
attrs_lists = pickle.load(file)
file.close()
except FileNotFoundError:
print('Did not find original plot file where all generations are plotted...looking for older version file')
if not plot_settings['only_plot_certain_generations']:
save_name = 'plot_data_{}{}_min_ts{}_min_food{}.pickle'. \
format(plot_settings['attr'], plot_settings['only_copied_str'], plot_settings['min_ts_for_plot'],
plot_settings['min_food_for_plot'])
file = open(save_dir+save_name, 'rb')
attrs_lists = pickle.load(file)
file.close()
return attrs_lists
def dynamic_regime_param_all_sims(plot_settings, generation):
all_deltas = []
for folder_name in plot_settings['all_folder_names']:
sim_names = all_sim_names_in_parallel_folder(folder_name)
for sim_name in sim_names:
mean_log_beta_distance_dict, log_beta_distance_dict, beta_distance_dict, beta_index_max, betas_max_gen_dict, \
heat_caps_max_dict, smoothed_heat_caps = calc_heat_cap_param_main(sim_name, {}, gen_list=[generation])
all_deltas.append(mean_log_beta_distance_dict[generation])
return all_deltas
def colormap_according_to_delta(generation, plot_settings):
colors = ['navy', plot_settings['colors']['b10'], plot_settings['colors']['b1'], plot_settings['colors']['b01']]
cmap_name = 'custom_cmap'
# cmap = plt.get_cmap('brg')
cmap = LinearSegmentedColormap.from_list(
cmap_name, colors)
cmap = shiftedColorMap(cmap=cmap, start=0, midpoint=0.38, stop=1, name=cmap_name)
return cmap
def plot(attrs_lists, plot_settings):
if plot_settings['first_plot']:
plt.figure(figsize=(10, 7))
# colors = sns.color_palette("dark", len(attrs_lists))
all_deltas = dynamic_regime_param_all_sims(plot_settings, plot_settings['color_according_to_delta_in_generation'])
# cmap = plt.get_cmap('brg')
cmap = colormap_according_to_delta(0, plot_settings)
norm = colors_package.Normalize(vmin=min(all_deltas), vmax=max(all_deltas))
# if plot_settings['last_plot']:
cbar = plt.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap))
cbar.set_label(r'$\langle \delta \rangle$ at Generation 0', rotation=270, labelpad=23)
sim_names = all_sim_names_in_parallel_folder(plot_settings['folder_name'])
deltas = []
for sim_name in sim_names:
mean_log_beta_distance_dict, log_beta_distance_dict, beta_distance_dict, beta_index_max, betas_max_gen_dict, \
heat_caps_max_dict, smoothed_heat_caps = calc_heat_cap_param_main(sim_name, {}, gen_list=[plot_settings['color_according_to_delta_in_generation']])
deltas.append(mean_log_beta_distance_dict[plot_settings['color_according_to_delta_in_generation']])
print('Deltas:')
print(deltas)
print('all Deltas:')
print(all_deltas)
for attrs_list, delta in zip(attrs_lists, deltas):
color = cmap(norm(delta))
generations = np.arange(len(attrs_list))
mean_attrs_list = [np.nanmean(gen_attrs) for gen_attrs in attrs_list]
plt.scatter(generations, mean_attrs_list, s=1, alpha=0.085, c=color)
if plot_settings['sliding_window']:
slided_mean_attrs_list, slided_x_axis = slide_window(mean_attrs_list, plot_settings['sliding_window_size'])
plt.plot(slided_x_axis, slided_mean_attrs_list, alpha=0.5, linewidth=2, c=color)
if plot_settings['smooth']:
'''
Trying to make some sort of regression, that smoothes and interpolates
Trying to find an alternative to moving average, where boundary values are cut off
'''
# smoothed_mean_attrs_list = gaussian_kernel_smoothing(mean_attrs_list)
# Savitzky-Golay filter:
smoothed_mean_attrs_list = savgol_filter(mean_attrs_list, 201, 3) # window size, polynomial order
# plt.plot(generations, smoothed_mean_attrs_list, c=color)
# Uncommand the following, if interpolation shall be applied to smoothed data
f_interpolate = interp1d(generations, smoothed_mean_attrs_list, kind='cubic')
x_interp = np.linspace(np.min(generations), np.max(generations), num=4000, endpoint=True)
y_interp = f_interpolate(x_interp)
plt.plot(x_interp, y_interp, c=color, alpha=0.5, linewidth=2)
# plt.scatter(generations, mean_attrs_list, s=20, alpha=1)
plt.xlabel('Generation')
# plt.ylabel(plot_settings['attr'])
plt.ylabel(r'$\langle E_\mathrm{org} \rangle$')
plt.ylim(plot_settings['ylim'])
# plt.title(plot_settings['title'], color=plot_settings['title_color'])
if plot_settings['legend'] and plot_settings['last_plot']:
create_legend()
if plot_settings['last_plot']:
save_dir = 'save/{}/figs/several_plots{}/'.format(folder_name, plot_settings['add_save_name'])
save_name = 'several_sims_criticial_{}{}_{}_min_ts{}_min_food{}_{}_all_in_one.png'. \
format(plot_settings['attr'], plot_settings['only_copied_str'], plot_settings['folder_name'],
plot_settings['min_ts_for_plot'], plot_settings['min_food_for_plot'],
plot_settings['plot_generations_str'])
if not os.path.exists(save_dir):
os.makedirs(save_dir)
plt.savefig(save_dir+save_name, bbox_inches='tight', dpi=300)
def create_legend():
legend_elements = [
# Line2D([0], [0], marker='o', color='w', markerfacecolor='w', markersize=15, alpha=0.0001, label=r'$10$ Simulations'),
Line2D([0], [0], marker='o', color='w', markerfacecolor='grey', markersize=15, alpha=0.75, label=r'One Generation'),
Line2D([0], [0], color='b', lw=4, c='grey', alpha=0.7, label=r'One Simulation Smoothed'),
]
plt.legend(handles=legend_elements, fontsize=18)
def load_attrs(folder_name, plot_settings):
folder_dir = 'save/{}'.format(folder_name)
dir_list = all_folders_in_dir_with(folder_dir, 'sim')
attrs_list_all_sims = []
settings_list = []
for dir in dir_list:
sim_name = dir[(dir.rfind('save/')+5):]
settings = load_settings(sim_name)
if plot_settings['only_plot_certain_generations']:
load_generations = np.arange(plot_settings['lowest_and_highest_generations_to_be_plotted'][0],
plot_settings['lowest_and_highest_generations_to_be_plotted'][1]+1)
isings_list = load_isings_from_list(sim_name, load_generations, decompress=plot_settings['decompress'])
else:
isings_list = load_isings_specific_path('{}/isings'.format(dir), decompress=plot_settings['decompress'])
if plot_settings['only_copied']:
isings_list = [choose_copied_isings(isings) for isings in isings_list]
if plot_settings['attr'] == 'norm_avg_energy' or plot_settings['attr'] == 'norm_food_and_ts_avg_energy':
calc_normalized_fitness(isings_list, plot_settings, settings)
isings_list = below_threshold_nan(isings_list, settings)
attrs_list = [attribute_from_isings(isings, plot_settings['attr']) if isings is not None else np.nan
for isings in isings_list]
attrs_list_all_sims.append(attrs_list)
del isings_list
# settings_list.append(load_settings(dir))
return attrs_list_all_sims
def below_threshold_nan(isings_list, sim_settings):
for i, isings in enumerate(isings_list):
if isings[0].time_steps < plot_settings['min_ts_for_plot']:
isings_list[i] = None
if sim_settings['random_food_seasons']:
if isings[0].food_in_env < plot_settings['min_food_for_plot']:
isings_list[i] = None
return isings_list
def slide_window(iterable, win_size):
slided = []
x_axis_gens = []
n = 0
while n+win_size < len(iterable)-1:
mean = np.nanmean(iterable[n:n+win_size])
slided.append(mean)
x_axis_gens.append(n+int(win_size/2))
n += 1
return slided, x_axis_gens
def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'):
'''
Function to offset the "center" of a colormap. Useful for
data with a negative min and positive max and you want the
middle of the colormap's dynamic range to be at zero.
Input
-----
cmap : The matplotlib colormap to be altered
start : Offset from lowest point in the colormap's range.
Defaults to 0.0 (no lower offset). Should be between
0.0 and `midpoint`.
midpoint : The new center of the colormap. Defaults to
0.5 (no shift). Should be between 0.0 and 1.0. In
general, this should be 1 - vmax / (vmax + abs(vmin))
For example if your data range from -15.0 to +5.0 and
you want the center of the colormap at 0.0, `midpoint`
should be set to 1 - 5/(5 + 15)) or 0.75
stop : Offset from highest point in the colormap's range.
Defaults to 1.0 (no upper offset). Should be between
`midpoint` and 1.0.
'''
cdict = {
'red': [],
'green': [],
'blue': [],
'alpha': []
}
# regular index to compute the colors
reg_index = np.linspace(start, stop, 257)
# shifted index to match the data
shift_index = np.hstack([
np.linspace(0.0, midpoint, 128, endpoint=False),
np.linspace(midpoint, 1.0, 129, endpoint=True)
])
for ri, si in zip(reg_index, shift_index):
r, g, b, a = cmap(ri)
cdict['red'].append((si, r, r))
cdict['green'].append((si, g, g))
cdict['blue'].append((si, b, b))
cdict['alpha'].append((si, a, a))
newcmap = matplotlib.colors.LinearSegmentedColormap(name, cdict)
plt.register_cmap(cmap=newcmap)
return newcmap
if __name__ == '__main__':
# folder_name = 'sim-20201020-181300_parallel_TEST'
plot_settings = {}
# Only plot loads previously saved plotting file instead of loading all simulations to save time
plot_settings['only_plot'] = True
plot_settings['decompress'] = True
plot_settings['add_save_name'] = ''
plot_settings['attr'] = 'avg_energy' #'norm_food_and_ts_avg_energy' #'norm_avg_energy'
# plot_settings['only_plot_fittest']
if plot_settings['attr'] == 'norm_food_and_ts_avg_energy':
plot_settings['ylim'] = (-0.0001, 0.00025)
else:
plot_settings['ylim'] = (-0.001, 0.015)
plot_settings['ylim'] = (-1, 10)
# plot_settings['ylim'] = (-0.000001, 0.00007)
# This only plots individuals that have not been mutated in previous generation (thus were fittest in previous generation)
plot_settings['only_copied'] = True
plot_settings['sliding_window'] = False
plot_settings['smooth'] = True
plot_settings['sliding_window_size'] = 100
# ONLY PLOT HAS TO BE FALSE FOR FOLLOWING SETTINGS to work:
plot_settings['min_ts_for_plot'] = 0
plot_settings['min_food_for_plot'] = 0
plot_settings['only_plot_certain_generations'] = False
plot_settings['lowest_and_highest_generations_to_be_plotted'] = [0, 1000]
plot_settings['title'] = ''
plot_settings['legend'] = True
plot_settings['colors'] = {'b1': 'olive', 'b01': 'maroon', 'b10': 'royalblue'}
# folder_names = ['sim-20201226-002401_parallel_beta_linspace_rec_c40_30_sims']
# folder_names = ['sim-20201226-002401_parallel_beta_linspace_rec_c40_30_sims_HEL_ONLY_PLOT']
folder_names = ['sim-20210118-014339_parallel_beta_linspace_break_eat_rec_c40_30_sims']
plot_settings['color_according_to_delta_in_generation'] = 0
init_betas = [1]
title_colors = ['olive', 'royalblue', 'maroon']
titles = [r'$\beta_\mathrm{init} = 1$', r'$\beta_\mathrm{init} = 10$', r'$\beta_\mathrm{init} = 0.1$']
for i, (folder_name, title, title_color, init_beta) in enumerate(zip(folder_names, titles, title_colors, init_betas)):
plot_settings['folder_name'] = folder_name
plot_settings['title'] = title
plot_settings['all_folder_names'] = folder_names
plot_settings['init_beta'] = init_beta
if i == 0:
plot_settings['first_plot'] = True
else:
plot_settings['first_plot'] = False
if i == len(folder_names) - 1:
plot_settings['last_plot'] = True
else:
plot_settings['last_plot'] = False
plot_settings['title_color'] = title_color
main_plot_parallel_sims(folder_name, plot_settings)