-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocessing.py
More file actions
339 lines (280 loc) · 14.4 KB
/
postprocessing.py
File metadata and controls
339 lines (280 loc) · 14.4 KB
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
from typing import List
import numpy as np
import pickle as pk
import time
import matplotlib.pyplot as plt
import os
from dissimilarities import *
def name(a):
return [key for key, value in globals().items() if value is a][0]
def diss_correlation(Dir_save_partition): # post-processing
os.chdir(Dir_save_partition)
with open("Result_all", 'rb') as fichier:
Result_all = pk.load(fichier)
fichier.close()
# MATRICES_DIS[euclide] = MatDistance(listB,euclide) #uncomment if you didn't compute these matrices before,else they are supposed to be stored in the global hashmap named M
# MATRICES_DIS[grassmann] = MatDistance(listB,grassmann)
# MATRICES_DIS[procrustes] = MatDistance(listB,procrustes)
Schubert_list = []
Euclide_list = []
Grassmann_list = []
for i in range(len(MATRICES_DIS[grassmann])):
for j in range(i):
Euclide_list.append(MATRICES_DIS[euclide][i, j])
Grassmann_list.append(MATRICES_DIS[grassmann][i, j])
plt.figure("Correlation between grassmann and euclidiean dissimilarities")
plt.xlabel("Grassmann dissimilaritites")
plt.ylabel("Euclidean dissimilaritites")
plt.plot(Grassmann_list, Euclide_list, '.')
plt.figure("Correlation between Schubert and Grassmann dissimilarities")
plt.xlabel("Schubert dissimilaritites")
plt.ylabel("Grassmann dissimilaritites")
plt.plot(Schubert_list, Grassmann_list, '.')
plt.figure("Schubert's dissimilarities distribution")
plt.xlabel("Schubert's dissimilarities")
plt.hist(Schubert_list, bins=400)
plt.figure("Euclidian's dissimilarities distribution")
plt.xlabel("Euclidian's dissimilarities")
plt.hist(Euclide_list, bins=400)
plt.figure("Grassmann's dissimilarities distribution")
plt.xlabel("Grassmann's dissimilarities")
plt.hist(Grassmann_list, bins=400)
plt.show()
def MDS_repr(Dir_save_partition):
os.chdir(Dir_save_partition)
with open("Result_all", 'rb') as fichier:
Result_all = pk.load(fichier)
fichier.close()
### MDS representations
with open("schubert_spectral_12_clusters.part", "rb") as fichier:
part_schubert_spectral = pk.load(fichier)
part_schubert_spectral.embedded_coord, part_schubert_spectral.MDS_stress = part_schubert_spectral.MDS()
fichier.close()
with open("schubert_k_medoids_12_clusters.part", "rb") as fichier:
part_schubert_kmed = pk.load(fichier)
part_schubert_kmed.embedded_coord, part_schubert_kmed.MDS_stress = part_schubert_spectral.embedded_coord, part_schubert_spectral.MDS_stress
fichier.close()
with open("biprojection_spectral_12_clusters.part", "rb") as fichier:
part_biprojections_spectral = pk.load(fichier)
part_biprojections_spectral.embedded_coord, part_biprojections_spectral.MDS_stress = part_biprojections_spectral.MDS()
fichier.close()
with open("biprojection_k_medoids_12_clusters.part", "rb") as fichier:
part_biprojections_kmed = pk.load(fichier)
part_biprojections_kmed.embedded_coord, part_biprojections_kmed.MDS_stress = part_biprojections_spectral.embedded_coord, part_biprojections_spectral.MDS_stress
fichier.close()
plt.figure("MDS plot of clustering results", figsize=(15, 8))
plt.subplot(221)
plt.title("MDS plot (from Schubert distances, spectral clustering)")
for key, cluster in part_schubert_spectral.clusters.items():
plt.scatter(part_schubert_spectral.embedded_coord[cluster.simulations, 0],
part_schubert_spectral.embedded_coord[cluster.simulations, 1], marker='.',
label="cluster " + str(key))
if len(part_schubert_spectral.trash) > 0:
plt.scatter(part_schubert_spectral.embedded_coord[part_schubert_spectral.trash, 0],
part_schubert_spectral.embedded_coord[part_schubert_spectral.trash, 1], marker='x', label="trash")
plt.legend(loc="best")
plt.subplot(222)
plt.title("MDS plot (from Schubert distances, k-medoids clustering)")
for key, cluster in part_schubert_kmed.clusters.items():
plt.scatter(part_schubert_kmed.embedded_coord[cluster.simulations, 0],
part_schubert_kmed.embedded_coord[cluster.simulations, 1], marker='.', label="cluster " + str(key))
if len(part_schubert_kmed.trash) > 0:
plt.scatter(part_schubert_kmed.embedded_coord[part_schubert_kmed.trash, 0],
part_schubert_kmed.embedded_coord[part_schubert_kmed.trash, 1], marker='x', label="trash")
plt.legend(loc="best")
plt.subplot(223)
plt.title("MDS plot (from biprojection distances, spectral clustering)")
for key, cluster in part_biprojections_spectral.clusters.items():
plt.scatter(part_biprojections_spectral.embedded_coord[cluster.simulations, 0],
part_biprojections_spectral.embedded_coord[cluster.simulations, 1], marker='.',
label="cluster " + str(key))
if len(part_biprojections_spectral.trash) > 0:
plt.scatter(part_biprojections_spectral.embedded_coord[part_biprojections_spectral.trash, 0],
part_biprojections_spectral.embedded_coord[part_biprojections_spectral.trash, 1], marker='x',
label="trash")
plt.legend(loc="best")
plt.subplot(224)
plt.title("MDS plot (from biprojection distances, k-medoids clustering)")
for key, cluster in part_biprojections_kmed.clusters.items():
plt.scatter(part_biprojections_kmed.embedded_coord[cluster.simulations, 0],
part_biprojections_kmed.embedded_coord[cluster.simulations, 1], marker='.',
label="cluster " + str(key))
if len(part_biprojections_kmed.trash) > 0:
plt.scatter(part_biprojections_kmed.embedded_coord[part_biprojections_kmed.trash, 0],
part_biprojections_kmed.embedded_coord[part_biprojections_kmed.trash, 1], marker='x', label="trash")
plt.legend(loc="best")
plt.show()
def comparison_avg(Dir_save_partition):
os.chdir(Dir_save_partition)
with open("Result_all", 'rb') as fichier:
Result_all = pk.load(fichier)
fichier.close()
### Comparition
F = plt.figure(
"Influence of dissimilarity measure, of clustering technique, of K, and reduced base construction technique (average)",
figsize=(15, 8))
F.clear()
# plt.suptitle("Partition pour "+str(len(self.data))+' simulations, distance '+name(self.distance)+" methode "+self.clustering)
# plt.suptitle("Gain depending on metrix, number of clusters, reduced-base constructions and clustering techniques")
ax = plt.subplot(2, 2, 1)
plt.title("K_medoids, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Average gain")
for key, value in Result_all["k_medoids"]["DTLS_CV"].items():
plt.plot(value['K'], np.mean(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 2)
plt.title("Spectral, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Average gain")
for key, value in Result_all["spectral"]["DTLS_CV"].items():
plt.plot(value['K'], np.mean(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 3)
plt.title("K_medoids, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Average gain")
for key, value in Result_all["k_medoids"]["DTLS"].items():
plt.plot(value['K'], np.mean(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 4)
plt.title("Spectral, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Average gain")
for key, value in Result_all["spectral"]["DTLS"].items():
plt.plot(value['K'], np.mean(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
plt.show()
# comparison max
def comparison_max(Dir_save_partition):
os.chdir(Dir_save_partition)
with open("Result_all", 'rb') as fichier:
Result_all = pk.load(fichier)
fichier.close()
F = plt.figure(
"Influence of dissimilarity measure, of clustering technique, of K, and reduced base construction technique (max)",
figsize=(15, 8))
F.clear()
# plt.suptitle("Partition pour "+str(len(self.data))+' simulations, distance '+name(self.distance)+" methode "+self.clustering)
# plt.suptitle("Gain depending on metrix, number of clusters, reduced-base constructions and clustering techniques")
ax = plt.subplot(2, 2, 1)
plt.title("K_medoids, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Maximal gain")
for key, value in Result_all["k_medoids"]["DTLS_CV"].items():
plt.plot(value['K'], np.max(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 2)
plt.title("Spectral, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Maximal gain")
for key, value in Result_all["spectral"]["DTLS_CV"].items():
plt.plot(value['K'], np.max(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 3)
plt.title("K_medoids, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Maximal gain")
for key, value in Result_all["k_medoids"]["DTLS"].items():
plt.plot(value['K'], np.max(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 4)
plt.title("Spectral, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Maximal gain")
for key, value in Result_all["spectral"]["DTLS"].items():
plt.plot(value['K'], np.max(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
plt.show()
# comparison std
def comparison_std(Dir_save_partition):
os.chdir(Dir_save_partition)
with open("Result_all", 'rb') as fichier:
Result_all = pk.load(fichier)
fichier.close()
F = plt.figure(
"Influence of dissimilarity measure, of clustering technique, of K, and reduced base construction technique (standard deviation)",
figsize=(15, 8))
F.clear()
# plt.suptitle("Partition pour "+str(len(self.data))+' simulations, distance '+name(self.distance)+" methode "+self.clustering)
# plt.suptitle("Gain depending on metrix, number of clusters, reduced-base constructions and clustering techniques")
ax = plt.subplot(2, 2, 1)
plt.title("K_medoids, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Standard deviation of Gain")
for key, value in Result_all["k_medoids"]["DTLS_CV"].items():
plt.plot(value['K'], np.std(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 2)
plt.title("Spectral, DTLS cross validated reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Standard deviation of Gain")
for key, value in Result_all["spectral"]["DTLS_CV"].items():
plt.plot(value['K'], np.std(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 3)
plt.title("K_medoids, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Standard deviation of Gain")
for key, value in Result_all["k_medoids"]["DTLS"].items():
plt.plot(value['K'], np.std(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
ax = plt.subplot(2, 2, 4)
plt.title("Spectral, DTLS reduced basis")
plt.xlabel("Number of clusters")
plt.ylabel("Standard deviation of Gain")
for key, value in Result_all["spectral"]["DTLS"].items():
plt.plot(value['K'], np.std(value['gain'], axis=1), '-x', label=name(key))
plt.legend(loc='best')
plt.show()
"""
### utility of clustering
partition_tested = Partition(gamma, biprojection, 10, technique = "spectral")
partition_tested_2 = Partition(gamma, schubert, 15)
os.chdir(Dir_save_partition+"\\..")
with open("test_set", 'rb') as fichier:
test_set=pk.load(fichier)
fichier.close()
for i in partition_tested.clusters.keys():
plt.figure()
basis = partition_tested.clusters[i].baseDTLS_cv
medoid_id = partition_tested.clusters[i].medoid_id
for key, clu in partition_tested.clusters.items():
err_proj = [np.linalg.norm(q - np.dot( basis[:,:Nb_modes], np.dot(basis[:,:Nb_modes].T, q)))/np.linalg.norm(q) for q in test_set]
dist = partition_tested.mat_dist[medoid_id][clu.simulations]
plt.plot( dist,err_proj, '.', label="cluster "+str(key))
plt.legend(loc="best")
plt.xlabel("Dissimilarity")
plt.ylabel("Projection error")
plt.show()
for i in partition_tested_2.clusters.keys():
plt.figure()
basis = partition_tested_2.clusters[i].baseDTLS_cv
medoid_id = partition_tested_2.clusters[i].medoid_id
for key, clu in partition_tested_2.clusters.items():
err_proj = [np.linalg.norm(q - np.dot( basis[:,:Nb_modes], np.dot(basis[:,:Nb_modes].T, q)))/np.linalg.norm(q) for q in [partition_tested_2.data[i] for i in clu.simulations]]
dist = partition_tested_2.mat_dist[medoid_id][clu.simulations]
plt.plot( dist,err_proj, '.', label="cluster "+str(key))
plt.legend(loc="best")
plt.xlabel("Dissimilarity")
plt.ylabel("Projection error")
plt.show()
#gain_distance = [np.linalg.norm(q[:,:Nb_modes])/np.linalg.norm(q - np.dot( basis[:,:Nb_modes], np.dot(basis[:,:Nb_modes].T, q))) for [partition_tested.data[i] for i in clu.simulations] ]
#diss_basis = partition_tested.mat_dist[medoid_id][test_set]
F=plt.figure("Utility of clustering", figsize=(15,8))
plt.subplot(131)
plt.plot(diss_train_set,err_train_set, '.')
plt.xlabel("Dissimilarity")
plt.ylabel("Projection error")
plt.title("Correlation between projection error and dissimilarity on the train set")
plt.subplot(132)
plt.plot(diss_test_set, err_test_set, '.')
plt.xlabel("Dissimilarity")
plt.ylabel("Projection error")
plt.title("Correlation between projection error and dissimilarity on the test set")
plt.subplot(133)
plt.plot(diss_basis, gain_distance, '.')
plt.xlabel("Dissimilarity")
plt.ylabel("Gain")
plt.title("Gain function of dissimilarity with the local reduced model")
plt.show()"""