-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclust_plot.py
144 lines (127 loc) · 4.96 KB
/
clust_plot.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
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
from os.path import isfile, join
from matplotlib.patches import Ellipse
import matplotlib.pyplot as plt
def draw_clusters(data, clust, name):
if data.shape[1] != 2:
raise Exception('number of columns should be equal to 2. The number of columns was: {}'.format(data.shape[1]))
plt.figure(figsize=(15,15))
plt.title('draw_clusters')
plt.scatter(data[:, 0], data[:,1], c = clust, s=6)
plt.savefig("result/"+name[:-4]+"_clustered.png", dpi=300)
plt.close()
def draw_ground_truth(data, parts, name):
if data.shape[1] != 2:
raise Exception('number of columns should be equal to 2. The number of columns was: {}'.format(data.shape[1]))
size = math.ceil(math.sqrt(len(parts)))
labels_pred = np.zeros(len(parts)).astype(int)
patch_size = size//2
for i in range(0, size, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 0
for i in range(1, size, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 1
for i in range(size, size*2, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 2
for i in range(size+1, size*2, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 3
plt.figure(figsize=(15,15))
plt.title('ground_truth')
plt.scatter(data[:, 0], data[:,1], c = labels_pred, s=6)
plt.savefig("result/"+name[:-4]+"_ground_truth.png", dpi=300)
plt.close()
def describe(ads, spec, name, parts):
ads = pd.DataFrame(ads)
spec = pd.DataFrame(spec)
####
size = math.ceil(math.sqrt(len(parts)))
labels_pred = np.zeros(len(parts)).astype(int)
patch_size = size//2
for i in range(0, size, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 0
for i in range(1, size, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 1
for i in range(size, size*2, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 2
for i in range(size+1, size*2, 2):
labels_pred[i*patch_size:(i+1)*patch_size] = 3
first = np.argwhere(labels_pred == 0).reshape(1,-1)[0]
second = np.argwhere(labels_pred == 1).reshape(1,-1)[0]
third = np.argwhere(labels_pred == 2).reshape(1,-1)[0]
fourth = np.argwhere(labels_pred == 3).reshape(1,-1)[0]
####
bla = []
bla.append( [ads.iloc[first].mean(), ads.iloc[first].std()] )
bla.append( [ads.iloc[second].mean(), ads.iloc[second].std()] )
bla.append( [ads.iloc[third].mean(), ads.iloc[third].std()] )
bla.append( [ads.iloc[fourth].mean(), ads.iloc[fourth].std()] )
bla2 = []
bla2.append( [spec.iloc[first].mean(), spec.iloc[first].std()] )
bla2.append( [spec.iloc[second].mean(), spec.iloc[second].std()] )
bla2.append( [spec.iloc[third].mean(), spec.iloc[third].std()] )
bla2.append( [spec.iloc[fourth].mean(), spec.iloc[fourth].std()] )
labels = ['upper left','upper right','lower left','lower right']
colors = ['red', 'green', 'blue', 'yellow']
i=0
plt.figure(figsize=(10,10))
for elem in bla:
x = range(0, len(elem[0]))
plt.scatter(x, elem[0], label=labels[i], c = colors[i])
data = {
'x': x,
'y1': [y - 2*e for y, e in zip(elem[0], elem[1])],
'y2': [y + 2*e for y, e in zip(elem[0], elem[1])]
}
plt.fill_between(**data, alpha=.25, color=colors[i])
plt.legend()
i += 1
plt.savefig("result/" + name[:-4] + "_mfs" + ".png", dpi=300)
plt.close()
i=0
plt.figure(figsize=(10,10))
for elem in bla2:
#x = np.array([1,2,3,4,5,7,10,15,20,30,40])
x = np.array([-20,-15,-10,-5,-4,-3,-2,-1, 1, 2,4, 7, 10, 15, 20])
x = range(x[0], x[-1]+1)
plt.scatter(x, elem[0], label=labels[i], c=colors[i])
data = {
'x': x,
'y1': [y - e for y,e in zip(elem[0], elem[1])],
'y2': [y + e for y,e in zip(elem[0], elem[1])]
}
plt.fill_between(**data, alpha=.25, color=colors[i])
plt.legend()
i += 1
plt.savefig("result/" + name[:-4] + "_reni" + ".png", dpi=300)
plt.close()
#region draw_gmm
#https://jakevdp.github.io/PythonDataScienceHandbook/05.12-gaussian-mixtures.html
def draw_ellipse(position, covariance, ax=None, **kwargs):
"""Draw an ellipse with a given position and covariance"""
ax = ax or plt.gca()
# Convert covariance to principal axes
if covariance.shape == (2, 2):
U, s, Vt = np.linalg.svd(covariance)
angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
width, height = 2 * np.sqrt(s)
else:
angle = 0
width, height = 2 * np.sqrt(covariance)
# Draw the Ellipse
for nsig in range(1, 4):
ax.add_patch(Ellipse(position, nsig * width, nsig * height,
angle, **kwargs))
def plot_gmm(gmm, X, label=True, ax=None):
ax = ax or plt.gca()
labels = gmm.fit(X).predict(X)
if label:
ax.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis', zorder=2)
else:
ax.scatter(X[:, 0], X[:, 1], s=40, zorder=2)
ax.axis('equal')
w_factor = 0.2 / gmm.weights_.max()
for pos, covar, w in zip(gmm.means_, gmm.covariances_, gmm.weights_):
draw_ellipse(pos, covar, alpha=w * w_factor)
#endregion