-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSongStructureGUI.py
More file actions
193 lines (179 loc) · 6.01 KB
/
SongStructureGUI.py
File metadata and controls
193 lines (179 loc) · 6.01 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
#Programmer: Chris Tralie
#Purpose: To extract similarity alignments for use in the GUI
import numpy as np
import os
import skimage
import skimage.io
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import json
import base64
from SimilarityFusion import *
from DiffusionMaps import *
from Laplacian import *
import time
def imresize(D, dims, kind='cubic'):
"""
Resize a floating point image
Parameters
----------
D : ndarray(M1, N1)
Original image
dims : tuple(M2, N2)
The dimensions to which to resize
kind : string
The kind of interpolation to use
Returns
-------
D2 : ndarray(M2, N2)
A resized array
"""
M, N = dims
x1 = np.array(0.5 + np.arange(D.shape[1]), dtype=np.float32)/D.shape[1]
y1 = np.array(0.5 + np.arange(D.shape[0]), dtype=np.float32)/D.shape[0]
x2 = np.array(0.5 + np.arange(N), dtype=np.float32)/N
y2 = np.array(0.5 + np.arange(M), dtype=np.float32)/M
f = scipy.interpolate.interp2d(x1, y1, D, kind=kind)
return f(x2, y2)
def getBase64File(filename):
fin = open(filename, "rb")
b = fin.read()
b = base64.b64encode(b)
fin.close()
return b.decode("ASCII")
def getBase64PNGImage(pD, cmapstr, logfloor_quantile = 0):
"""
Get an image as a base64 string
"""
D = np.array(pD)
if logfloor_quantile > 0:
floor = np.quantile(pD.flatten(), logfloor_quantile)
D = np.log(D + floor)
c = plt.get_cmap(cmapstr)
D = D-np.min(D)
D = np.round(255.0*D/np.max(D))
C = c(np.array(D, dtype=np.int32))
C = np.array(np.round(C*255), dtype=np.uint8)
skimage.io.imsave("temp.png", C)
b = getBase64File("temp.png")
os.remove("temp.png")
return "data:image/png;base64, " + b
#http://stackoverflow.com/questions/1447287/format-floats-with-standard-json-module
class PrettyFloat(float):
def __repr__(self):
return '%.4g' % self
def pretty_floats(obj):
if isinstance(obj, float):
return PrettyFloat(obj)
elif isinstance(obj, dict):
return dict((k, pretty_floats(v)) for k, v in obj.items())
elif isinstance(obj, (list, tuple)):
return map(pretty_floats, obj)
return obj
def get_graph_obj(W, K=10, res = 400):
"""
Return an object corresponding to a nearest neighbor graph
Parameters
----------
W: ndarray(N, N)
The N x N time-ordered similarity matrix
K: int
Number of nearest neighbors to use in graph representation
res: int
Target resolution of resized image
"""
fac = 1
if res > -1:
fac = int(np.round(W.shape[0]/float(res)))
res = int(W.shape[0]/fac)
WRes = imresize(W, (res, res))
else:
res = W.shape[0]
WRes = np.array(W)
np.fill_diagonal(WRes, 0)
pix = np.arange(res)
I, J = np.meshgrid(pix, pix)
WRes[np.abs(I - J) == 1] = np.max(WRes)
c = plt.get_cmap('Spectral')
C = c(np.array(np.round(np.linspace(0, 255,res)), dtype=np.int32))
C = np.array(np.round(C[:, 0:3]*255), dtype=int)
colors = C.tolist()
K = min(int(np.round(K*2.0/fac)), res) # Use slightly more edges
print("res = %i, K = %i"%(res, K))
S = getS(WRes, K).tocoo()
I, J, V = S.row, S.col, S.data
V *= 10
ret = {}
ret["nodes"] = [{"id":"%i"%i, "color":colors[i]} for i in range(res)]
ret["links"] = [{"source":"%i"%I[i], "target":"%i"%J[i], "value":"%.3g"%V[i]} for i in range(I.shape[0])]
ret["fac"] = fac
return ret
def saveResultsJSON(filename, times, Ws, neigs, jsonfilename, diffusion_znormalize):
"""
Save a JSON file holding the audio and structure information, which can
be parsed by SongStructureGUI.html. Audio and images are stored as
base64 for simplicity
Parameters
----------
filename: string
Path to audio
times: ndarray(N)
A list of times corresponding to each row in Ws
Ws: Dictionary of (str, ndarray(N, N))
A dictionary of N x N similarity matrices for different feature types
neigs: int
Number of eigenvectors to compute in graph Laplacian
jsonfilename: string
File to which to save the .json file
diffusion_znormalize: boolean
Whether to Z-normalize diffusion maps to spread things out more evenly
"""
Results = {'songname':filename, 'times':times.tolist()}
print("Saving results...")
#Add music as base64 files
_, ext = os.path.splitext(filename)
Results['audio'] = "data:audio/%s;base64, "%ext[1::] + getBase64File(filename)
W = Ws['Fused']
WOut = np.array(W)
np.fill_diagonal(WOut, 0)
Results['W'] = getBase64PNGImage(WOut, 'magma_r', logfloor_quantile=0.01)
Results['dim'] = W.shape[0]
# Compute Laplacian eigenvectors
tic = time.time()
v = getRandomWalkLaplacianEigsDense(W)
v = v[:, 1:neigs+1]
print("Elapsed Time Laplacian: %.3g"%(time.time()-tic))
# Resize the eigenvectors so they're easier to see
fac = 10
vout = np.zeros((v.shape[1]*fac, v.shape[0]))
for i in range(fac):
vout[i::fac, :] = v.T
Results['v'] = getBase64PNGImage(vout, 'magma_r')
Results['v_height'] = vout.shape[0]
# Setup the graph
Results['graph'] = json.dumps(get_graph_obj(WOut))
# Setup diffusion maps
c = plt.get_cmap('Spectral')
C = c(np.array(np.round(np.linspace(0, 255,W.shape[0])), dtype=np.int32))
C = C.flatten()
WDiff = np.array(W)
floor = np.quantile(WDiff, 0.01)
WDiff = np.log(WDiff+floor)
WDiff -= np.min(WDiff)
np.fill_diagonal(WDiff, 0)
X = getDiffusionMap(WDiff, neigs=4, thresh=0)
X = X[:, 0:-1]
if diffusion_znormalize:
X = X - np.mean(X, 0)[None, :]
X = X/np.sqrt(np.sum(X**2, 1))[:, None]
X = X.flatten()
Results['colors'] = C.tolist()
Results['X'] = X.tolist()
fout = open(jsonfilename, "w")
fout.write(json.dumps(Results))
fout.close()
if __name__ == '__main__':
filename = "MJ.mp3"
path, ext = os.path.splitext(filename)
res = "data:audio/%s;base64, "%ext[1::] + getBase64File(filename)
print(res)