/
exWind.py
316 lines (246 loc) · 9.97 KB
/
exWind.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
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
# HMM with mfcc
# hmmlearn from scikit learn
from hmmlearn.hmm import GaussianHMM
from sklearn.preprocessing import scale
from hmm.continuous.GMHMM import GMHMM
from hmm.discrete.DiscreteHMM import DiscreteHMM
import numpy
means = []
vars = []
hiddens = []
count = 0
nbAnalysis = len(b.ids)
n = 3
m = 1
d = 12
for analysis in b.analysis.lowlevel.mfcc:
if analysis is not None:
try:
obs = numpy.array(analysis)
obs = obs.T
obs = obs[1:]
obs = obs.T
obs = scale(obs)
model = GaussianHMM(algorithm='viterbi', covariance_type='diag', covars_prior=0.01,
covars_weight=1, init_params='mc', means_prior=0, means_weight=0,
min_covar=0.001, n_components=3, n_iter=1000, params='mc',
random_state=None, startprob_prior=1.0, tol=0.01, transmat_prior=1.0,
verbose=False)
model.startprob_ = numpy.array([1., 0, 0])
model.startprob_prior = model.startprob_
model.transmat_ = numpy.array([[0.9, 0.1, 0], [0, 0.9, 0.1], [0, 0, 1]])
model.transmat_prior = model.transmat_
model.fit(obs)
pi = model.startprob_
A = model.transmat_
w = numpy.ones((n, m), dtype=numpy.double)
hmm_means = numpy.ones((n, m, d), dtype=numpy.double)
hmm_means[0][0] = model.means_[0]
hmm_means[1][0] = model.means_[1]
hmm_means[2][0] = model.means_[2]
hmm_covars = numpy.array([[ numpy.matrix(numpy.eye(d,d)) for j in xrange(m)] for i in xrange(n)])
hmm_covars[0][0] = model.covars_[0]
hmm_covars[1][0] = model.covars_[1]
hmm_covars[2][0] = model.covars_[2]
gmmhmm = GMHMM(n,m,d,A,hmm_means,hmm_covars,w,pi,init_type='user',verbose=False)
# hidden_state = model.predict(obs)
hidden_state = gmmhmm.decode(obs)
mean_sequence = [None] * len(obs)
var_sequence = [None] * len(obs)
for i in range(len(obs)):
mean_sequence[i] = model.means_[hidden_state[i]]
var_sequence[i] = numpy.diag(model.covars_[hidden_state[i]])
means.append(mean_sequence)
vars.append(var_sequence)
hiddens.append(hidden_state)
except:
means.append(None)
vars.append(None)
hiddens.append(None)
else:
means.append(None)
vars.append(None)
hiddens.append(None)
count += 1
print str(count) + '/' + str(nbAnalysis)
################################################################################################
import copy
import essentia
import freesound
import numpy as np
import matplotlib.pyplot as plt
c = freesound.FreesoundClient()
c.set_token("","token") #put your id here...
# Needed to remove non asci caracter in names
def strip_non_ascii(string):
''' Returns the string without non ASCII characters'''
stripped = (c for c in string if 0 < ord(c) < 127)
return ''.join(stripped)
##########################################################################################################################################################
# search for sounds with "wind" query and tag, duration 0 to 30sec
# ask for analysis_frames in order to be ablet to use get_analysis_frames method
results_pager = c.text_search(query="wind",filter="tag:wind duration:[0 TO 30.0]",sort="rating_desc",fields="id,name,previews,username,analysis_frames",page_size=150)
results_pager_last = copy.deepcopy(results_pager)
# recup all sounds in a list
nbSound = results_pager.count
numSound = 0
sounds = [None]*nbSound
# 1st iteration
for i in results_pager:
i.name = strip_non_ascii(i.name)
sounds[numSound] = copy.deepcopy(i)
numSound = numSound+1
print '\n' + str(numSound) + '/' + str(nbSound) + '\n' + str(i.name)
# next iteration
while (numSound<nbSound):
results_pager = copy.deepcopy(results_pager_last.next_page())
for i in results_pager:
i.name = strip_non_ascii(i.name)
sounds[numSound] = copy.deepcopy(i)
numSound = numSound+1
print '\n' + str(numSound) + '/' + str(nbSound) + '\n' + str(i.name)
results_pager_last = copy.deepcopy(results_pager)
print ' \n CHANGE PAGE \n '
# recup mfcc in a list of array
allMfcc = [None]*nbSound
numSound = 0
# again the limitation can stop the loop
while (numSound<nbSound):
try:
allMfcc[numSound] = essentia.array(sounds[numSound].get_analysis_frames().lowlevel.mfcc)
except ValueError:
print "Oops! JSON files not found !"
numSound = numSound+1
print '\n' + str(numSound) + '/' + str(nbSound) + '\n'
# recup all analysis frames
allAnalysisFrames = [None]*nbSound
numSound = 0
while (numSound<nbSound):
try:
allAnalysisFrames[numSound] = sounds[numSound].get_analysis_frames()
except ValueError:
print "Oops! JSON files not found !"
numSound = numSound+1
print '\n' + str(numSound) + '/' + str(nbSound) + '\n'
# save all analysis frames in json files
import os
if not os.path.exists('analysis'):
os.makedirs('analysis')
numSound = 0
while (numSound<nbSound):
nameFile = 'analysis/' + str(sounds[numSound].id) + '.json'
if allAnalysisFrames[numSound]:
with open(nameFile, 'w') as outfile:
json.dump(allAnalysisFrames[numSound].as_json(), outfile)
numSound = numSound+1
print '\n' + str(numSound) + '/' + str(nbSound) + '\n'
# load all analysis from json files
files = os.listdir('./analysis/')
nbSound = len(files)
allAnalysisFrames = [None]*nbSound
for numSound in range(nbSound):
with open('analysis/'+files[numSound]) as infile:
allAnalysisFrames[numSound] = json.load(infile)
print '\n' + str(numSound) + '/' + str(nbSound)
# remove None items
allMfcc = [x for x in allMfcc if x is not None]
nbSound = len(allMfcc)
# save variables
import pickle
with open('windSounds.pickle', 'w') as f:
pickle.dump(sounds,f)
with open('windSoundsMfcc.pickle', 'w') as f:
pickle.dump(allMfcc,f)
# load
with open('windSounds.pickle') as f:
sounds = pickle.load(f)
with open('windSoundsMfcc.pickle') as f:
allMfcc = pickle.load(f)
# some plots...
# compute mean
allMfccMean = [None]*nbSound
for i in range(nbSound):
allMfccMean[i] = allMfcc[i].mean(axis=0)
# kmeans from : http://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html
from sklearn import metrics
from sklearn.cluster import KMeans
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
from time import time
data = scale(allMfccMean)
n_samples, n_features = data.shape
n_digits = 8
labels = [0]*nbSound
sample_size = 300
def bench_k_means(estimator, name, data):
t0 = time()
estimator.fit(data)
print('% 9s %.2fs %i %.3f %.3f %.3f %.3f %.3f %.3f'
% (name, (time() - t0), estimator.inertia_,
metrics.homogeneity_score(labels, estimator.labels_),
metrics.completeness_score(labels, estimator.labels_),
metrics.v_measure_score(labels, estimator.labels_),
metrics.adjusted_rand_score(labels, estimator.labels_),
metrics.adjusted_mutual_info_score(labels, estimator.labels_),
metrics.silhouette_score(data, estimator.labels_,
metric='euclidean',
sample_size=sample_size)))
bench_k_means(KMeans(init='k-means++', n_clusters=n_digits, n_init=10),
name="k-means++", data=data)
bench_k_means(KMeans(init='random', n_clusters=n_digits, n_init=10),
name="random", data=data)
# in this case the seeding of the centers is deterministic, hence we run the
# kmeans algorithm only once with n_init=1
pca = PCA(n_components=n_digits).fit(data)
bench_k_means(KMeans(init=pca.components_, n_clusters=n_digits, n_init=1),
name="PCA-based",
data=data)
print(79 * '_')
###############################################################################
# Visualize the results on PCA-reduced data
reduced_data = PCA(n_components=2).fit_transform(data)
kmeans = KMeans(init='k-means++', n_clusters=n_digits, n_init=10)
kmeans.fit(reduced_data)
# Step size of the mesh. Decrease to increase the quality of the VQ.
h = .02 # point in the mesh [x_min, m_max]x[y_min, y_max].
# Plot the decision boundary. For that, we will assign a color to each
x_min, x_max = reduced_data[:, 0].min() - 1, reduced_data[:, 0].max() + 1
y_min, y_max = reduced_data[:, 1].min() - 1, reduced_data[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# Obtain labels for each point in mesh. Use last trained model.
Z = kmeans.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure(1)
plt.clf()
plt.imshow(Z, interpolation='nearest',
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
cmap=plt.cm.Paired,
aspect='auto', origin='lower')
plt.plot(reduced_data[:, 0], reduced_data[:, 1], 'k.', markersize=4)
# Plot the centroids as a white X
centroids = kmeans.cluster_centers_
plt.scatter(centroids[:, 0], centroids[:, 1],
marker='x', s=169, linewidths=3,
color='w', zorder=10)
plt.title('K-means clustering on the digits dataset (PCA-reduced data)\n'
'Centroids are marked with white cross')
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
################### WORK IN PROGRESS
# JSON DUMP
def get_child_nodes(node_id):
request = urllib2.Request(ROOT_URL + node_id)
response = json.loads(urllib2.urlopen(request).read())
nodes = []
for childnode in response['childNode']:
temp_obj = {}
temp_obj['id'] = childnode['id']
temp_obj['name'] = childnode['name']
temp_obj['children'] = get_child_nodes(temp_obj['id'])
nodes.append(temp_obj)
return nodes