Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Marie Tahon
spectral-clustering-music
Commits
e8f43fae
Commit
e8f43fae
authored
Nov 26, 2018
by
Marie Tahon
Browse files
add distance between clusters
parent
35085898
Changes
2
Hide whitespace changes
Inline
Side-by-side
params.py
View file @
e8f43fae
...
...
@@ -28,4 +28,5 @@ cluster_nb_max = 5 #maximum nb of clusters in 1 sec.
#plots
plot_simi
=
True
plot_struct
=
True
plot_dist
=
True
timestamps
=
True
spectral_clustering_audio.py
View file @
e8f43fae
...
...
@@ -461,33 +461,48 @@ def extract_time_boundaries(cluster_ids, onsets, nb_frames, sr):
return
bound_times
,
bound_labels
def
compute_distance
(
M
,
type_dist
,
plot_opt
):
frame0
=
np
.
mean
(
M
,
axis
=
1
)
distance
=
[]
for
i
in
range
(
M
.
shape
[
1
]):
#distance_eucl0.append( np.sum( (Msync_normed[:,i] - frame0)**2) )
#if i > 0: distance_cosine.append( scipy.spatial.distance.cosine(Msync_normed[:,i], Msync_normed[:,i-1]) )
if
type_dist
==
'cos'
:
distance
.
append
(
scipy
.
spatial
.
distance
.
cosine
(
M
[:,
i
],
frame0
))
elif
type_dist
==
'eucl'
:
distance
.
append
(
np
.
sum
(
(
M
[:,
i
]
-
frame0
)
**
2
)
)
if
plot_opt
:
plt
.
plot
(
distance
)
plt
.
tight_layout
()
return
distance
##################################
def
extract_cosine_distance_clusters
(
center_clusters
,
distance_ref
):
def
extract_cosine_distance_clusters
(
center_clusters
,
distance_ref
,
type_dist
=
'cos'
):
distance
=
[]
for
center
in
center_clusters
:
distance
.
append
(
scipy
.
spatial
.
distance
.
cosine
(
center
,
distance_ref
)
)
if
type_dist
==
'cos'
:
distance
.
append
(
scipy
.
spatial
.
distance
.
cosine
(
center
,
distance_ref
)
)
elif
type_dist
==
'eucl'
:
distance
.
append
(
np
.
sqrt
(
np
.
sum
(
(
center
-
distance_ref
)
**
2
)
))
return
distance
def
extract_distance_between_clusters
(
center_clusters
,
type_dist
=
'cos'
):
distance
=
np
.
zeros
((
center_clusters
.
shape
))
for
i
,
center_i
in
enumerate
(
center_clusters
):
for
j
,
center_j
in
enumerate
(
center_clusters
):
if
type_dist
==
'cos'
:
distance
[
i
,
j
]
=
scipy
.
spatial
.
distance
.
cosine
(
center_i
,
center_j
)
elif
type_dist
==
'eucl'
:
distance
[
i
,
j
]
=
np
.
sqrt
(
np
.
sum
(
(
center_i
-
center_j
)
**
2
)
)
x
=
range
(
i
+
1
)
y
=
range
(
j
+
1
)
xloc
=
[
c
+
0.5
for
c
in
x
]
cx
=
[
str
(
c
)
for
c
in
x
]
print
(
cx
)
fig_d
,
ax_d
=
plt
.
subplots
(
figsize
=
(
5
,
4
))
p_d
=
ax_d
.
pcolor
(
distance
,
cmap
=
'inferno_r'
)
cb
=
fig_d
.
colorbar
(
p_d
)
ax_d
.
xaxis
.
set_ticks
(
xloc
)
ax_d
.
xaxis
.
set_ticklabels
(
cx
)
ax_d
.
yaxis
.
set_ticks
(
xloc
)
ax_d
.
yaxis
.
set_ticklabels
(
cx
)
ax_d
.
set_title
(
'Distance between clusters'
)
ax_d
.
set_xlabel
(
'clusters numbers'
)
plt
.
tight_layout
()
return
distance
def
main
():
...
...
@@ -573,6 +588,10 @@ def main():
distance_cosine_cluster
=
extract_cosine_distance_clusters
(
KM
.
cluster_centers_
,
np
.
mean
(
X
[:
10
*
NFFT
,:],
axis
=
0
))
else
:
distance_cosine_cluster
=
None
if
params
.
plot_dist
:
distance_between_clusters
=
extract_distance_between_clusters
(
KM
.
cluster_centers_
)
# and plot the resulting structure representation
if
params
.
plot_struct
:
plot_structure
(
Rf
,
X
,
seg_ids
,
k
,
onset_times
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment