-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
154 lines (115 loc) · 3.73 KB
/
plotting.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
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import matplotlib.patheffects as path_effects
from sklearn.preprocessing import StandardScaler
from umap import UMAP
from mpl_toolkits import mplot3d
import numpy as np
from pitches_problem import PitchesProblem
def plot_pca(X, y, labelnames):
X = StandardScaler().fit_transform(X)
X = PCA(2).fit_transform(X, y)
plt.figure()
for i in set(y):
name = labelnames[i]
x = X[y == i]
plt.scatter(*x.T, label=name)
plt.legend()
plt.show()
def plot_umap(X, y, labelnames):
X = StandardScaler().fit_transform(X)
X = UMAP(n_components=2).fit_transform(X, y)
plt.figure()
for i in set(y):
artist_name = labelnames[i]
x = X[y == i]
plt.scatter(*x.T, label=artist_name)
plt.legend()
plt.show()
def plot_pca_3d(X, y, labelnames):
X = StandardScaler().fit_transform(X)
pca = PCA(3)
X = pca.fit_transform(X)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
for i in set(y):
name = labelnames[i]
x = X[y == i]
plt.scatter(*x.T, label=name)
plt.legend()
plt.show()
def plot_umap_3d(X, y, labelnames):
X = StandardScaler().fit_transform(X)
X = UMAP(n_components=3).fit_transform(X, y)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
for i in set(y):
artist_name = labelnames[i]
x = X[y == i]
plt.scatter(*x.T, label=artist_name)
plt.legend()
plt.show()
def imshow_chroma(chroma, title="", fname=None):
plt.figure(figsize=(30, 2))
plt.imshow(chroma.T, aspect="auto", interpolation="nearest")
# plt.colorbar()
plt.title(title)
plt.gca().invert_yaxis()
if fname:
plt.savefig(fname)
plt.show()
def imshow_confusion_matrix(mat: np.ndarray, classnames, title=None, out_fname=None):
l = len(mat)
plt.figure(figsize=(.55 * len(mat), .5*len(mat)), dpi=700)
plt.imshow(mat / np.sum(mat, 1))
if title:
plt.title(title)
plt.xticks(range(l), [s[:4] for s in classnames])
plt.yticks(range(l), classnames)
plt.xlabel("Predicted")
plt.ylabel("Real")
# plt.colorbar()
for i in range(l):
for j in range(l):
plt.text(
j,
i,
mat[i, j],
color="white",
horizontalalignment="center",
verticalalignment="center",
path_effects=[
path_effects.Stroke(linewidth=2, foreground="black"),
path_effects.Normal(),
],
)
plt.xlim(-0.5, l - 0.5)
plt.ylim(l - 0.5, -0.5)
plt.tight_layout()
if out_fname is None:
plt.show()
else:
plt.savefig(out_fname, transparent=True, bbox_inches='tight', dpi='figure')
shifter = np.roll(np.arange(0, 7*12, 7) % 12, 6)
def imshow_transition_matrix(vector_144: np.ndarray, shift: bool=True):
M = vector_144.reshape(12, 12)
chromae = np.array(['I', 'I#', 'II', 'II#', 'III', 'IV', 'IV#', 'V', 'V#', 'VI', 'VI#', 'VII'])
chromae = np.array(["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"])
if shift:
M = M[shifter][:, shifter]
chromae = chromae[shifter]
plt.figure()
plt.imshow(M)
plt.xticks(np.arange(12), chromae)
plt.yticks(np.arange(12), chromae)
plt.ylim(-.5, 11.5)
plt.xlabel('From')
plt.ylabel('To')
plt.show()
#%%
if __name__ == "__main__":
if False:
import numpy as np
mat = np.array([[20, 6, 1, 11], [5, 18, 8, 7], [6, 5, 20, 6], [16, 4, 5, 12]])
classnames = ["the beatles", "coldplay", "radiohead", "paul mccartney"]
imshow_confusion_matrix(mat, classnames)