-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader_representations.py
617 lines (515 loc) · 21.6 KB
/
loader_representations.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
"""
File name: approach_deepgru.py
Author: Pedro Ramoneda
Python Version: 3.7
"""
import csv
import os
import sys
import numpy as np
from utils import load_xmls, load_json, save_json
def get_path(alias):
if alias == "mikro1":
path = "mikrokosmos1"
if alias == "mikro2":
path = "pianoplayer"
if alias == "nak":
path = "nakamura"
return path
def rep_raw(alias):
path_alias = get_path(alias)
rep = {}
for grade, path, xml in load_xmls():
rep[path] = {
'grade': grade,
'right_velocity': [],
'left_velocity': [],
'right_fingers': [],
'left_fingers': []
}
r_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_rh.txt'])
l_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_lh.txt'])
for path_txt, hand in zip([r_h_cost, l_h_cost], ["right_", "left_"]):
with open(path_txt) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in read_tsv:
rep[path][hand + 'velocity'] = rep[path][hand + 'velocity'] + [float(l[8])]
rep[path][hand + 'fingers'] = rep[path][hand + 'fingers'] + [abs(int(l[7]))]
save_json(rep, os.path.join('representations', path_alias, 'rep_raw.json'))
def merge_chord_onsets(time_series):
new_time_series = [list(a) for a in time_series]
for ii in range(len(time_series)):
if ii + 1 < len(time_series) and time_series[ii][0] + 0.05 == time_series[ii + 1][0]:
if ii + 2 < len(time_series) and time_series[ii][0] + 0.1 == time_series[ii + 2][0]:
if ii + 3 < len(time_series) and time_series[ii][0] + 0.15 == time_series[ii + 3][0]:
if ii + 4 < len(time_series) and time_series[ii][0] + 0.2 == time_series[ii + 4][0]:
new_time_series[ii][0] = time_series[ii + 4][0]
else:
new_time_series[ii][0] = time_series[ii + 3][0]
else:
new_time_series[ii][0] = time_series[ii + 2][0]
else:
new_time_series[ii][0] = time_series[ii + 1][0]
else:
new_time_series[ii][0] = time_series[ii][0]
return [tuple(a) for a in new_time_series]
def finger2index(f):
if f > 0:
index = int(f) + 4
elif f < 0:
index = int(f) - 5
else: # == 0
index = -1000
return index
def velocity_piece(path, alias, xml):
path_alias = get_path(alias)
print(path)
r_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_rh.txt'])
l_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_lh.txt'])
intermediate_rep = []
for path_txt, hand in zip([r_h_cost, l_h_cost], ["right_", "left_"]):
time_series = []
with open(path_txt) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in read_tsv:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(float(l[8]))))
time_series = time_series[:-9]
intermediate_rep.extend(time_series)
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(intermediate_rep, key=(lambda a: a[0]))]
idx = 0
onsets = []
while idx < len(intermediate_rep):
onsets.append(intermediate_rep[idx][0])
t = [0] * 10
index = finger2index(intermediate_rep[idx][1])
t[index] = intermediate_rep[idx][2]
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
t[index] = intermediate_rep[j][2]
j += 1
idx = j
# print(t)
matrix.append(t)
return matrix, onsets
def rep_velocity(alias):
rep = {}
for grade, path, xml in load_xmls():
matrix, _ = velocity_piece(path, alias, xml)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', get_path(alias), 'rep_velocity.json'))
def prob_piece(path, alias, xml):
path_alias = get_path(alias)
print(path)
PIG_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '.txt'])
time_series = []
with open(PIG_cost) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in list(read_tsv)[1:]:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(abs(float(l[8])))))
time_series = time_series[:-3]
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(time_series, key=(lambda a: a[0]))]
onsets = []
idx = 0
while idx < len(intermediate_rep):
onsets.append(intermediate_rep[idx][0])
t = [0] * 10
index = finger2index(intermediate_rep[idx][1])
t[index] = intermediate_rep[idx][2]
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
t[index] = intermediate_rep[j][2]
j += 1
idx = j
# print(t)
matrix.append(t)
return matrix, onsets
def rep_prob(alias):
rep = {}
for grade, path, xml in load_xmls():
matrix, _ = prob_piece(path, alias, xml)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', get_path(alias), 'rep_nakamura.json'))
def rep_d_nakamura(alias):
path_alias = get_path(alias)
rep = {}
for grade, path, xml in load_xmls():
print(path, grade)
PIG_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '.txt'])
time_series = []
with open(PIG_cost) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in list(read_tsv)[1:]:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(abs(float(l[8]))), round(float(l[2]), 2)))
time_series = time_series[:-3]
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(time_series, key=(lambda a: a[0]))]
idx = 0
while idx < len(intermediate_rep):
t = [0] * 10
index = finger2index(intermediate_rep[idx][1])
t[index] = intermediate_rep[idx][2] / (intermediate_rep[idx][3] - intermediate_rep[idx][0])
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
t[index] = intermediate_rep[j][2] / (intermediate_rep[j][3] - (intermediate_rep[j][0]))
j += 1
idx = j
# print(t)
matrix.append(t)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', path_alias, 'rep_d_nakamura.json'))
def finger_piece(path, alias, xml):
path_alias = get_path(alias)
print(path)
r_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_rh.txt'])
l_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_lh.txt'])
intermediate_rep = []
for path_txt, hand in zip([r_h_cost, l_h_cost], ["right_", "left_"]):
time_series = []
with open(path_txt) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in read_tsv:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(float(l[8]))))
time_series = time_series[:-1]
intermediate_rep.extend(time_series)
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(intermediate_rep, key=(lambda a: a[0]))]
idx = 0
onsets = []
while idx < len(intermediate_rep):
onsets.append(intermediate_rep[idx][0])
t = [0] * 10
index = finger2index(intermediate_rep[idx][1])
t[index] = 1.0
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
t[index] = 1.0
j += 1
idx = j
# print(t)
matrix.append(t)
return matrix, onsets
def rep_finger(alias):
rep = {}
for grade, path, xml in load_xmls():
matrix, _ = finger_piece(path, alias, xml)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', get_path(alias), 'rep_finger.json'))
def finger_nakamura_piece(path, alias, xml):
path_alias = get_path(alias)
print(path)
PIG_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '.txt'])
time_series = []
with open(PIG_cost) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in list(read_tsv)[1:]:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(abs(float(l[8])))))
time_series = time_series[:-3]
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(time_series, key=(lambda a: a[0]))]
idx = 0
onsets = []
while idx < len(intermediate_rep):
onsets.append(intermediate_rep[idx][0])
t = [0] * 10
index = finger2index(intermediate_rep[idx][1])
t[index] = 1.0
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
t[index] = 1.0
j += 1
idx = j
# print(t)
matrix.append(t)
return matrix, onsets
def rep_finger_nakamura(alias):
rep = {}
for grade, path, xml in load_xmls():
matrix, _ = finger_nakamura_piece(path, alias, xml)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', get_path(alias), 'rep_finger_nakamura.json'))
def notes_piece(path, alias, xml):
path_alias = get_path(alias)
print(path)
r_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_rh.txt'])
l_h_cost = '/'.join(["Fingers", path_alias, os.path.basename(xml[:-4]) + '_lh.txt'])
intermediate_rep = []
for path_txt, hand in zip([r_h_cost, l_h_cost], ["right_", "left_"]):
time_series = []
with open(path_txt) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in read_tsv:
if int(l[7]) != 0:
# (onset, note)
time_series.append((round(float(l[1]), 2), int(l[3]) - 21))
intermediate_rep.extend(time_series)
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(intermediate_rep, key=(lambda a: a[0]))]
idx = 0
onsets = []
while idx < len(intermediate_rep):
onsets.append(intermediate_rep[idx][0])
t = [0.0] * 88
index = intermediate_rep[idx][1]
t[index] = 1.0
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = intermediate_rep[j][1]
t[index] = 1.0
j += 1
idx = j
# print(t)
matrix.append(t)
return matrix, onsets
def rep_notes(alias):
rep = {}
for grade, path, xml in load_xmls():
matrix, _ = notes_piece(path, alias, xml)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', get_path(alias), 'rep_note.json'))
def visualize_note_representation(alias, score='mikrokosmos/musicxml/69.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_note.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print(c, end="|")
print()
def visualize_finger_representation(alias, score='mikrokosmos/musicxml/69.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_finger.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print(c, end="|")
print()
def visualize_finger_representation_nakamura(alias="nak", score='mikrokosmos/musicxml/69.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_finger_nakamura.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print(c, end="|")
print()
def visualize_velocity_representation(alias, score='mikrokosmos/musicxml/69.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_velocity.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print(c, end="|")
print()
def visualize_prob_representation(alias="nak", score='mikrokosmos/musicxml/5.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_nakamura.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print('%02.1f' % c, end="|")
print()
def visualize_d_nakamura(alias="nak", score='mikrokosmos/musicxml/69.xml'):
data = load_json(os.path.join('representations', get_path(alias), 'rep_d_nakamura.json'))
matrix = data[score]['matrix']
for row in np.array(matrix).transpose():
for c in row:
print(c, end="|")
print()
def get_distance_type(last_semitone, current_semitone):
last_black = (last_semitone % 12) in [1, 3, 6, 8, 10]
current_black = (current_semitone % 12) in [1, 3, 6, 8, 10]
if not last_black and not current_black:
distance_type = 1
elif last_black and not current_black:
distance_type = 2
elif not last_black and current_black:
distance_type = 3
else: # bb
distance_type = 4
return distance_type
def rep_distances(alias):
path_alias = get_path(alias)
rep = {}
for grade, path, r_h, l_h in load_xmls():
print(path, grade)
r_h_cost = '/'.join(["Fingers", path_alias, r_h[:-11] + '_rh.txt'])
l_h_cost = '/'.join(["Fingers", path_alias, l_h[:-11] + '_lh.txt'])
intermediate_rep = []
for path_txt, hand in zip([r_h_cost, l_h_cost], ["right_", "left_"]):
time_series = []
with open(path_txt) as tsv_file:
read_tsv = csv.reader(tsv_file, delimiter="\t")
for l in read_tsv:
if int(l[7]) != 0:
time_series.append((round(float(l[1]), 2), int(l[7]), abs(float(l[8])), abs(float(l[3]))))
if alias == 'version_1.0':
time_series = merge_chord_onsets(time_series[:-10])
else:
time_series = time_series[:-10]
intermediate_rep.extend(time_series)
# order by onset and create matrix
matrix = []
intermediate_rep = [on for on in sorted(intermediate_rep, key=(lambda a: a[0]))]
# initial semitone: at the beginning the distance is 0
last_semitone_rh = next(x[3] for x in intermediate_rep if x[1] > 0)
last_semitone_lh = next(x[3] for x in intermediate_rep if x[1] < 0)
idx = 0
while idx < len(intermediate_rep):
d, dt, t = [0] * 10, [0] * 10, [0] * 10
index = finger2index(intermediate_rep[idx][1])
is_r_h = index >= 5
last_semitone = last_semitone_rh if is_r_h else last_semitone_lh
t[index] = intermediate_rep[idx][2]
d[index] = last_semitone - intermediate_rep[idx][3]
dt[index] = get_distance_type(last_semitone, intermediate_rep[idx][3])
if is_r_h:
last_semitone_rh = last_semitone
else:
last_semitone_lh = last_semitone
j = idx + 1
while j < len(intermediate_rep) and intermediate_rep[idx][0] == intermediate_rep[j][0]:
index = finger2index(intermediate_rep[j][1])
is_r_h = index >= 5
last_semitone = last_semitone_rh if is_r_h else last_semitone_lh
t[index] = intermediate_rep[j][2]
d[index] = last_semitone - intermediate_rep[idx][3]
dt[index] = get_distance_type(last_semitone, intermediate_rep[idx][3])
if is_r_h:
last_semitone_rh = last_semitone
else:
last_semitone_lh = last_semitone
j += 1
idx = j
# print(t)
# matrix.append([t, d , dt])
matrix.append(t + d + dt)
rep[path] = {
'grade': grade,
'matrix': matrix
}
save_json(rep, os.path.join('representations', path_alias, 'rep_distance.json'))
def rep_fing_vel_time(alias):
get_path(alias)
def rep_distances_time(alias):
get_path(alias)
def rep_merged_time(alias):
get_path(alias)
def load_rep(klass):
if klass == "rep_velocity":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_velocity.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_finger":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_finger.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_finger_nakamura":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_finger_nakamura.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_prob":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_nakamura.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_d_nakamura":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_d_nakamura.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_note":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_note.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
elif klass == "rep_distance":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_distance.json')
data = load_json(path)
ans = ([np.array(x['matrix']) for k, x in data.items()], np.array([x['grade'] for k, x in data.items()]))
return ans
def load_rep_info(klass):
if klass == "rep_velocity":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_velocity.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_finger":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_finger.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_finger_nakamura":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_finger_nakamura.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_prob":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_nakamura.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_d_nakamura":
path_alias = get_path("nak")
path = os.path.join('representations', path_alias, 'rep_d_nakamura.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_note":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_note.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
elif klass == "rep_distance":
path_alias = get_path("mikro2")
path = os.path.join('representations', path_alias, 'rep_distance.json')
data = load_json(path)
ans = np.array([k for k, x in data.items()])
return ans
if __name__ == '__main__':
# rep_raw("version_1.0")
rep_velocity("mikro2")
# rep_distances("version_1.0")
# load_rep("version_1.0", rep_velocity)
# load_rep("version_1.0", rep_velocity)
# visualize_note_representation("mikro1")
# visualize_note_representation("mikro1")
# rep_finger_nakamura("nak")
# rep_prob("nak")
rep_notes("mikro2")
rep_finger("mikro2")
# visualize_note_representation("mikro2")
# rep_d_nakamura("nak")
# visualize_prob_representation("nak")
# visualize_finger_representation_nakamura()