-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultivariate_birdsounds_test.py
349 lines (287 loc) · 10.2 KB
/
multivariate_birdsounds_test.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import os
import matplotlib as mpl
from leitmotifs.lama import read_audio_from_dataframe
mpl.rcParams['figure.dpi'] = 150
from audio.lyrics import *
from leitmotifs.competitors import *
# path outside the git
path_to_wav = "../../motiflets_use_cases/birds/"
path = "../datasets/audio/"
write_audio = False
datasets = {
"Common-Starling": {
"ks": [4],
"motif_length": 50,
"n_dims": 2,
"slack": 1.0,
"length_range": np.arange(25, 100, 5),
"audio_file_url": path_to_wav + "xc27154---common-starling---sturnus-vulgaris.mp3",
"pandas_file_url": path + "common-starling-sturnus-vulgaris.csv"
},
# "House-Sparrow": {
# "ks": [4],
# "n_dims": 10,
# "motif_length": 50,
# "slack": 1.0,
# "length_range": np.arange(25, 100, 5),
# "audio_file_url": path_to_wav + "house-sparrow-passer-domesticus-audio.mp3",
# "pandas_file_url": path + "house-sparrow-passer-domesticus.csv"
# }
}
# dataset = datasets["Common-Starling"]
# dataset = datasets["House-Sparrow"]
channels = [
'MFCC 0',
'MFCC 1', 'MFCC 2', 'MFCC 3', 'MFCC 4', 'MFCC 5',
'MFCC 6', 'MFCC 7', 'MFCC 8', 'MFCC 9', 'MFCC 10',
'MFCC 11', 'MFCC 12', 'MFCC 13'
]
def get_ds_parameters(name):
global ds_name, k_max, n_dims, length_range, motif_length
global audio_file_url, pandas_file_url, ks, slack
ds_name = name
dataset = datasets[name]
ks = dataset["ks"]
k_max = np.max(ks) + 2
n_dims = dataset["n_dims"]
length_range = dataset["length_range"]
slack = dataset["slack"]
audio_file_url = dataset["audio_file_url"]
pandas_file_url = dataset["pandas_file_url"]
motif_length = dataset["motif_length"]
# def test_read_write():
# audio_length_seconds, df, index_range = read_from_wav(audio_file_url)
# df.to_csv(pandas_file_url, compression='gzip')
# audio_length_seconds2, df2, index_range2 = read_from_dataframe()
def test_ground_truth():
get_ds_parameters("Common-Starling")
audio_length_seconds, df, index_range, ground_truth \
= read_audio_from_dataframe(pandas_file_url, channels)
ml = LAMA(ds_name, df,
dimension_labels=df.index,
n_dims=n_dims,
ground_truth=ground_truth
)
# print("Positions:", index_range[ground_truth.loc[0][0]])
positions = []
pos = np.array([[509, 559], [566, 616], [2229, 2279], [2137, 2187]])
pos[:, 0] -= 15
positions.append(pos)
print(repr(positions))
if os.path.isfile(audio_file_url):
# extract motif sets
for a, motif in enumerate(positions):
motif_length = motif[a][1] - motif[a][0]
length_in_seconds = motif_length * audio_length_seconds / df.shape[1]
extract_audio_segment(
df, ds_name, audio_file_url, "snippets",
length_in_seconds, index_range, motif_length,
np.array(motif)[:, 0], id=(a + 1))
ml.plot_dataset()
def test_lama(
dataset_name="Common-Starling",
minimize_pairwise_dist=False,
use_PCA=False,
motifset_name="LAMA",
distance="znormed_ed",
exclusion_range=None,
plot=True):
get_ds_parameters(dataset_name)
audio_length_seconds, df, index_range, ground_truth \
= read_audio_from_dataframe(pandas_file_url, channels)
# make the signal uni-variate by applying PCA
if use_PCA:
from sklearn.decomposition import PCA
pca = PCA(n_components=1)
df_transform = pca.fit_transform(df.T).T
else:
df_transform = df
ml = LAMA(
ds_name, df_transform,
dimension_labels=df.index,
distance=distance,
n_dims=n_dims,
minimize_pairwise_dist=minimize_pairwise_dist,
ground_truth=ground_truth,
slack=exclusion_range if exclusion_range else slack
)
# learn parameters
# motif_length, all_minima = ml.fit_motif_length(
# k_max, length_range,
# plot_motifsets=False
# )
# print("Best length", motif_length, length_in_seconds, "s")
dists, motif_sets, elbow_points = ml.fit_k_elbow(
k_max,
motif_length=motif_length,
plot_elbows=False,
plot_motifsets=False)
print("Positions (Frame):", repr(np.sort(motif_sets[ks])))
# print("Time:", repr(np.sort(index_range[np.int32(motif_sets[ks])])))
if plot:
ml.plot_motifset(motifset_name=motifset_name)
if use_PCA:
dims = [np.argsort(pca.components_[:])[:, :n_dims][0] for _ in ks]
else:
dims = ml.leitmotifs_dims[ks]
for a, eb in enumerate(ml.elbow_points):
motiflet = np.sort(ml.leitmotifs[eb])
print("Positions:")
print("\tpos\t:", repr(motiflet))
print("\tdims\t:", repr(dims))
# if write_audio:
length_in_seconds = index_range[motif_length]
if os.path.isfile(audio_file_url):
extract_audio_segment(
df, ds_name, audio_file_url, "bird_songs",
length_in_seconds, index_range, motif_length,
ml.leitmotifs[ml.elbow_points[-1]])
return motif_sets[ks], dims
def plot_spectrogram(audio_file_urls):
fig, ax = plt.subplots(len(audio_file_urls), 1,
figsize=(10, 5),
sharex=True, sharey=True)
offset = [3000, 3000, 10000, 10000]
for i, audio_file_url in enumerate(audio_file_urls):
if os.path.isfile(audio_file_url):
samplingFrequency, data = read_wave(audio_file_url)
left, right = data[offset[i]:, 0], data[offset[i]:, 1]
ax[i].specgram(left,
Fs=samplingFrequency,
cmap='Grays',
# scale='dB',
vmin=-30, vmax=30
)
ax[i].set_ylabel("Freq.")
ax[i].set_ylim([0, 10000])
ax[i].set_xlim([0, 0.92])
else:
raise ("No audio file found.")
ax[-1].set_xlabel('Time')
plt.tight_layout()
plt.savefig("images_paper/bird_songs/spectrogram.pdf")
def test_plot_spectrogram():
audio_file_urls = \
["images_paper/bird_songs/Common-Starling_Dims_10_Length_50_Motif_0.wav",
"images_paper/bird_songs/Common-Starling_Dims_10_Length_50_Motif_1.wav",
"images_paper/bird_songs/Common-Starling_Dims_10_Length_50_Motif_2.wav",
"images_paper/bird_songs/Common-Starling_Dims_10_Length_50_Motif_3.wav"]
plot_spectrogram(audio_file_urls)
plt.show()
def test_emd_pca(dataset_name="Common-Starling", plot=True):
return test_lama(dataset_name, use_PCA=True, motifset_name="PCA", plot=plot)
def test_mstamp(dataset_name="Common-Starling", plot=True, use_mdl=True):
get_ds_parameters(dataset_name)
audio_length_seconds, df, index_range, ground_truth \
= read_audio_from_dataframe(pandas_file_url, channels)
return run_mstamp(df, ds_name, motif_length=motif_length,
ground_truth=ground_truth, plot=plot,
use_mdl=use_mdl, use_dims=n_dims)
def test_kmotifs(dataset_name="Common-Starling", first_dims=True, plot=True):
get_ds_parameters(dataset_name)
audio_length_seconds, df, index_range, ground_truth \
= read_audio_from_dataframe(pandas_file_url, channels[:n_dims])
motif_sets = []
used_dims = []
for target_k in ks:
motif, dims = run_kmotifs(
df,
ds_name,
motif_length=motif_length,
slack=slack,
r_ranges=np.arange(1, 100, 1),
use_dims=n_dims if first_dims else df.shape[0], # first dims or all dims
target_k=target_k,
ground_truth=ground_truth,
plot=plot
)
used_dims.append(np.arange(dims))
motif_sets.append(motif)
return motif_sets, used_dims
def test_publication(plot=False, method_names=None):
dataset_names = [
"Common-Starling"
]
if method_names is None:
method_names = [
"LAMA",
"LAMA (naive)",
"mSTAMP+MDL",
"mSTAMP",
"EMD*",
"K-Motifs (TOP-f)",
"K-Motifs (all)",
"LAMA (cid)",
"LAMA (ed)",
"LAMA (cosine)"
]
file_prefix = "results_birdsounds"
for dataset_name in dataset_names:
get_ds_parameters(dataset_name)
run_tests(
dataset_name,
ks=ks,
method_names=method_names,
test_lama=test_lama,
test_mstamp=test_mstamp,
test_emd_pca=test_emd_pca,
test_kmotifs=test_kmotifs,
file_prefix=file_prefix,
plot=plot
)
def test_plot_results(
plot=True, method_names=None, all_plot_names=None):
dataset_names = [
"Common-Starling"
]
if method_names is None:
method_names = [
"LAMA",
"LAMA (naive)",
"mSTAMP+MDL",
"mSTAMP",
"EMD*",
"K-Motifs (TOP-f)",
"K-Motifs (all)",
"LAMA (cid)",
"LAMA (ed)",
"LAMA (cosine)"
]
results = []
if all_plot_names is None:
all_plot_names = {
"_new": [
"mSTAMP+MDL",
"mSTAMP",
"EMD*",
"K-Motifs (all)",
"LAMA",
], "_distances": [
"LAMA",
"LAMA (cid)",
"LAMA (ed)",
"LAMA (cosine)"
]
}
file_prefix = "results_birdsounds"
output_file = "birdsounds_precision"
for dataset_name in dataset_names:
get_ds_parameters(dataset_name)
audio_length_seconds, df, index_range, ground_truth \
= read_audio_from_dataframe(pandas_file_url, channels)
eval_tests(
dataset_name,
ds_name,
df,
method_names,
motif_length,
ground_truth,
all_plot_names,
file_prefix,
results,
plot=plot
)
pd.DataFrame(
data=np.array(results),
columns=["Dataset", "Method", "Precision", "Recall"]).to_csv(
"results/" + output_file + ".csv")