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 | def melodic_distance(
score1: Score,
score2: Score,
repr: str = "pcdist1",
metric: str = "taxi",
samples: int = 10,
rescale: bool = False,
miditoolbox_compatible: bool = False,
) -> float | None:
"""
Measurement of distance between specified feature data extracted from two
Score objects.
Parameters
----------
score1 : Score
The first Score object.
score2 : Score
The second Score object.
repr : str
Feature extraction function used for distance measuring.
metric : str
There are two options here:
(1) A norm metric as specified in norm.py
(2) "cosine" to specify the angle between the two vectors obtained
from repr.
samples : int
Number of samples for contour representation.
rescale : bool
Rescale distance to similarity value between 0 and 1.
miditoolbox_compatible : bool
Whether or not we want the result to be compatible with the original
matlab implementation of miditoolbox
Returns
-------
float | None
Value representing the distance between the two scores under the given
representation and metric.
None if cannot be calculated
Raises
------
ValueError
if argument combination is not supported
"""
if repr == "pcdist1":
dist1 = pitch_class_distribution_1(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = pitch_class_distribution_1(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "pcdist2":
dist1 = pitch_class_distribution_2(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = pitch_class_distribution_2(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "ivdist1":
dist1 = interval_distribution_1(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = interval_distribution_1(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "ivdist2":
dist1 = interval_distribution_2(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = interval_distribution_2(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "durdist1":
dist1 = duration_distribution_1(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = duration_distribution_1(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "durdist2":
dist1 = duration_distribution_2(
score1, miditoolbox_compatible=miditoolbox_compatible
)
dist2 = duration_distribution_2(
score2, miditoolbox_compatible=miditoolbox_compatible
)
data_vector_1 = np.array(dist1.data)
data_vector_2 = np.array(dist2.data)
elif repr == "melcontour":
if samples <= 0:
raise ValueError(
f"invalid sample number {samples} with {repr} option!"
)
note_list1 = melodySamplingContour(score1, samples, "rel")
note_list2 = melodySamplingContour(score2, samples, "rel")
if not note_list1 or not note_list2:
return None
mean1 = mean(note.midi_num for note in note_list1)
mean2 = mean(note.midi_num for note in note_list2)
data_vector_1 = np.fromiter(
(note.midi_num - mean1 for note in note_list1), int
)
data_vector_2 = np.fromiter(
(note.midi_num - mean2 for note in note_list2), int
)
elif repr == "combcontour":
score1_len = nnotes(score1)
score2_len = nnotes(score2)
mtx_len = min(score1_len, score2_len)
if not mtx_len:
return None
comb_mtx_1 = get_pitch_comparison_contour_matrix(
islice(score1.find_all(Note), mtx_len)
)
comb_mtx_2 = get_pitch_comparison_contour_matrix(
islice(score2.find_all(Note), mtx_len)
)
abs_diff_mtx = abs(comb_mtx_1.astype(int) - comb_mtx_2.astype(int))
distance = np.sum(abs_diff_mtx) / (mtx_len * mtx_len - mtx_len)
if rescale:
rescale_val = 0.5
if distance < rescale_val:
distance = 2 * (rescale_val - distance) + distance
else:
# there is a bug in the matlab miditoolbox version,
# namely the formula is functionally equivalent to the previous
# branch (when distance < rescale_val)
# Anyways, that 'potential' bug is reproduced here...
distance = 2 * (rescale_val - distance) + distance
return float(distance)
else:
raise ValueError("Unsupported representation type.")
rescale_val = 0
if metric == "taxi":
distance = pnorm_distance(data_vector_1, data_vector_2, 1)
if repr in distribution_repr:
rescale_val = 1
elif repr == melcontour_repr:
rescale_val = 8 * samples
elif metric == "euc":
distance = pnorm_distance(data_vector_1, data_vector_2, 2)
if repr in distribution_repr:
rescale_val = 1
elif repr == melcontour_repr:
rescale_val = 2 * samples
elif metric == "cosine":
distance = np.sum(data_vector_1.conj() * data_vector_2, axis=0) / (
np.linalg.norm(data_vector_1) * np.linalg.norm(data_vector_2)
)
distance = np.sum(distance)
if repr in distribution_repr:
rescale_val = 0.15
elif repr == melcontour_repr:
rescale_val = 15 * samples
distance += 11 * samples
else:
raise ValueError(f"Unsupported metric type {metric}!")
if rescale:
if repr in distribution_repr:
if distance < rescale_val:
distance = (2 * (rescale_val - distance) + distance) / 2
else:
# there is a bug in the matlab miditoolbox version,
# namely the formula is functionally equivalent to the previous
# branch (when distance < rescale_val)
# Anyways, that 'potential' bug is reproduced here...
distance = (2 * (rescale_val - distance) + distance) / 2
elif repr == melcontour_repr:
if distance < rescale_val:
distance = (2 * rescale_val - distance) / (2 * rescale_val)
else:
distance = (distance - 2 * (distance - rescale_val)) / (
2 * rescale_val
)
else:
raise ValueError(f"unimplemented option {repr}!")
return float(distance)
|