-
Notifications
You must be signed in to change notification settings - Fork 0
/
genotypes-tsne-generational
304 lines (232 loc) · 8.84 KB
/
genotypes-tsne-generational
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
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import colors
from scipy.special import comb
import pickle
from sklearn import manifold
from os import path, makedirs
import os
import time
# beta-1/sim-20180511-163319
# beta-10/sim-20180512-105824
# beta-0-1/sim-20180512-105719
os.chdir('D:\Masterarbeit_ausgelagert')
loadfile = 'sim-20191114-000009_server'
#loadfile = 'sim-20191023-151526_server_2000new'
# iter_list = [0, 25, 50, 75, 200, 500, 1000]
#iter_list = np.arange(0, 482, 2)
iter_list=np.arange(0,2000, 10)
# iter_list = np.arange(0, 2000, 6)
numAgents = 150
numNeurons = 10
size_par = 'avg_energy' #Isings parameter that shall determine scatter size (usually 'fitness')
# ---- t-SNE settings ------
autoLoad = True
loadJ = False
loadFit = False
transposeJ = False
perplexity = 30
n_iter = 2000
# ---- plot settings ------
saveFigBool = True
scatter3d = False
# markers = ['o', '+', '.', 'D', 's', '^', 'v', '*', 'h', 0, 1, 2, 3, 4]
# alpha = np.linspace(0.3, 1, len(iter_list))
# alpha = np.logspace(np.log(0.3), np.log(1), len(iter_list))
cmap = plt.get_cmap('plasma')
norm = colors.Normalize(vmin=0, vmax=len(iter_list)) # age/color mapping
alpha = 0.4
# plt.rc('text', usetex=True)
# font = {'family': 'serif', 'size': 15, 'serif': ['computer modern roman']}
# plt.rc('font', **font)
# plt.rc('legend', **{'fontsize': 12})
# markersize = np.linspace(1, 10, len(iter_list))
# markersize = np.logspace(np.log(1), np.log(10), len(iter_list))
# selected = np.logspace(np.log10(1), np.log10(len(iter_list)), 4) - 1
selected = np.linspace(0, len(iter_list) - 1, 4)
selected = [int(i) for i in selected]
def load_saved_data(filename):
txt = 'Loading: ' + filename
print(txt)
data = np.load(filename)
return data
def upper_tri_masking(A):
m = A.shape[0]
r = np.arange(m)
mask = r[:, None] < r
return A[mask]
def load_J_from_isings(loadfile, iter_list, numAgents, numNeurons):
J = np.zeros((numAgents * len(iter_list), int(comb(numNeurons, 2))) )
iter_label = []
orgNum = 0
for iter in iter_list:
filename = 'save/' + loadfile + '/isings/gen[' + str(iter) + ']-isings.pickle'
startstr = 'Loading simulation:' + filename
print(startstr)
isings = pickle.load(open(filename, 'rb'))
iter_label.append('Gen: ' + str(iter))
for I in isings:
J[orgNum, :] = upper_tri_masking(I.J)
orgNum += 1 # this counter iterates for both loops!
return J
def load_J_fitness_from_isings(loadfile, iter_list, numAgents, numNeurons):
J = np.zeros((numAgents * len(iter_list), int(comb(numNeurons, 2))) )
fitness = np.zeros(numAgents * len(iter_list))
iter_label = []
orgNum = 0
start_time = time.time()
for iter in iter_list:
filename = 'save/' + loadfile + '/isings/gen[' + str(iter) + ']-isings.pickle'
startstr = 'Loading simulation:' + filename
print(startstr)
isings = pickle.load(open(filename, 'rb'))
iter_label.append('Gen: ' + str(iter))
for I in isings:
J[orgNum, :] = upper_tri_masking(I.J)
#fitness[orgNum] = I.fitness
exec('fitness[orgNum] = I.%s' % size_par)
orgNum += 1 # this counter iterates for both loops!
# if iter == 2000: # this iteration double counted fitness by accident!
# for iOrg, I in enumerate(isings):
# print('Correcting for generation 2000 double counting.')
# fitness[(orgNum - 50 + iOrg)] -= fitness[orgNum - 100 + iOrg]
end_time = time.time()
print('Time:', end_time - start_time)
return J, fitness
def load_fitness_from_isings(loadfile, iter_list, numAgents):
fitness = np.zeros(numAgents * len(iter_list))
iter_label = []
orgNum = 0
for iter in iter_list:
filename = 'save/' + loadfile + '/isings/gen[' + str(iter) + ']-isings.pickle'
startstr = 'Loading fitness from simulation:' + filename
print(startstr)
isings = pickle.load(open(filename, 'rb'))
iter_label.append('Gen: ' + str(iter))
for I in isings:
exec('fitness[orgNum] = I.%s' %size_par)
orgNum += 1 # this counter iterates for both loops!
# if iter == 2000: # this iteration double counted fitness by accident!
# for iOrg, I in enumerate(isings):
# print('Correcting for generation 2000 double counting.')
# fitness[(orgNum - 50 + iOrg)] -= fitness[orgNum - 100 + iOrg]
return fitness
folder = 'save/' + loadfile
# fname = folder + \
# '/figs/tsne_gen-' + \
# str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
# '.png'
folder2 = folder + '/figs/J_tsne_source/'
if transposeJ:
fname2 = folder2 + 'J_tsne-T_gen-' + \
str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
'_perp-' + str(perplexity) + '_n_iter-' + str(n_iter) + \
'.npz'
else:
fname2 = folder2 + 'J_tsne_gen-' + \
str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
'_perp-' + str(perplexity) + '_n_iter-' + str(n_iter) + \
'.npz'
# See if you've generated this tsne already (they take a long time)
if path.isfile(fname2) and autoLoad:
data = load_saved_data(fname2)
J_tsne = data['J_tsne']
fitness = data['fitness']
else:
# CALCULUATE T-SNE
# [ organism(gen), edges]
if (loadJ and loadFit) or ( not loadJ and not loadFit):
J, fitness = load_J_fitness_from_isings(loadfile, iter_list, numAgents, numNeurons)
elif loadJ:
J = load_J_from_isings(loadfile, iter_list, numAgents, numNeurons)
data = load_saved_data(fname2)
fitness = data['fitness']
elif loadFit:
fitness = load_fitness_from_isings(loadfile, iter_list, numAgents)
data = load_saved_data(fname2)
J_tsne = data['J_tsne']
if not path.exists(folder2):
makedirs(folder2)
# print('Overwriting save file...')
# np.savez(fname2, J_tsne=J_tsne, fitness=fitness)
if not loadFit:
# ----------------------------------------------------------------------
print("Computing t-SNE embedding")
tsne = manifold.TSNE(n_components=2, init='pca',
random_state=0, perplexity=perplexity, n_iter=n_iter)
if transposeJ: # model NN edges as 'agent' instead of organisms.
J_tsne = tsne.fit_transform(np.transpose(J))
else:
J_tsne = tsne.fit_transform(J)
if not path.exists(folder2):
makedirs(folder2)
np.savez(fname2, J_tsne=J_tsne, fitness=fitness)
# PLOT
# markerrank = rankdata(fitness, method='min')
# markersize = np.logspace(np.log(1), np.log(5), len(markerrank))
if scatter3d:
fig = plt.figure(figsize=(19,9))
ax = fig.add_subplot(111, projection='3d')
else:
fig, ax = plt.subplots(1, 1, figsize=(18, 9))
b = 0
c = [b, b, b]
if transposeJ:
x1 = J_tsne[:, 0]
y1 = J_tsne[:, 1]
ax.scatter(x1, y1)
else:
for ii, iter in enumerate(iter_list):
if ii in selected:
label = str(iter)
else:
label = None
i = ii * numAgents
j = i + numAgents
x1 = J_tsne[i:j, 0]
y1 = J_tsne[i:j, 1]
z1 = np.tile(iter, (len(x1), 1))
c = cmap(norm(ii))
if scatter3d:
ax.scatter(x1, y1, z1, label=label,
color=np.tile(c, (len(x1), 1)), alpha=alpha,
s=np.exp(10.5 * fitness[i:j] / np.max(fitness)) / 100)
else:
max_fit = np.max(fitness)
if max_fit == 0.0:
max_fit = 0.05
ax.scatter(x1, y1, label=label,
color=np.tile(c, (len(x1), 1)), alpha=alpha,
s=np.exp(10.5 * fitness[i:j] / max_fit) / 25)
# s=markersize[markerrank[i:j]-1])
# plt.rc('text', usetex=True)
# font = {'family': 'serif', 'size': 28, 'serif': ['computer modern roman']}
# plt.rc('font', **font)
# plt.rc('legend', **{'fontsize': 20})
titlestr = 'Connectivity t-SNE Projections'
plt.title(titlestr)
# xlblstr = 'Perplexity: ' + str(perplexity)
# plt.xlabel(xlblstr)
# leg = plt.legend(loc=2, title='Generation')
#
# for lh in leg.legendHandles:
# lh.set_alpha(1)
# # lh.set_sizes(30)
plt.tight_layout()
# ax = plt.gca()
ax.set_aspect(1./ax.get_data_ratio())
ax.set_xticklabels([])
ax.set_yticklabels([])
# manager = plt.get_current_fig_manager()
# manager.resize(*manager.window.maxsize())
savefilename = fname2[:-4] + '.png'
if not path.exists(folder2):
makedirs(folder2)
if saveFigBool:
plt.savefig(savefilename, bbox_inches='tight', dpi=2000)
# plt.close()
savemsg = 'Saving ' + savefilename
print(savemsg)
plt.show()