forked from adrienycart/PEAMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_features.py
95 lines (71 loc) · 4.33 KB
/
compute_features.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
import os
import numpy as np
import pretty_midi as pm
import mir_eval
import peamt.features.utils as utils
from peamt.features.benchmark import framewise
from peamt.features.high_low_voice import framewise_highest, framewise_lowest,notewise_highest, notewise_lowest
from peamt.features.loudness import false_negative_loudness
from peamt.features.out_key import out_key_errors, out_key_errors_binary_mask
from peamt.features.repeat_merge import repeated_notes, merged_notes
MIDI_path = 'app/static/data/all_midi_cut'
systems = ['kelz','lisu','google','cheng']
for example in os.listdir(MIDI_path)[0:10]:
example_path = os.path.join(MIDI_path,example)
print('________________')
print(example)
target_data = pm.PrettyMIDI(os.path.join(example_path,'target.mid'))
for system in systems:
# if example=="MAPS_MUS-scn15_11_ENSTDkAm_5" and system=='google':
if system=='google' or system == 'kelz':
print(system)
system_data = pm.PrettyMIDI(os.path.join(example_path,system+'.mid'))
target_pr = (target_data.get_piano_roll()>0).astype(int)
system_pr = (system_data.get_piano_roll()>0).astype(int)
target_pr,system_pr= utils.even_up_rolls([target_pr,system_pr])
P_f,R_f,F_f = framewise(system_pr,target_pr)
print("Frame P,R,F:", P_f,R_f,F_f)
notes_target, intervals_target, vel_target = utils.get_notes_intervals(target_data,with_vel=True)
notes_system, intervals_system = utils.get_notes_intervals(system_data)
match_on = mir_eval.transcription.match_notes(intervals_target, notes_target, intervals_system, notes_system,offset_ratio=None, pitch_tolerance=0.25)
match_onoff = mir_eval.transcription.match_notes(intervals_target, notes_target, intervals_system, notes_system,offset_ratio=0.2, pitch_tolerance=0.25)
P_n_on = float(len(match_on))/len(notes_system)
R_n_on = float(len(match_on))/len(notes_target)
F_n_on = 2*P_n_on*R_n_on/(P_n_on+R_n_on+np.finfo(float).eps)
print("Note-On P,R,F:", P_n_on,R_n_on,F_n_on)
P_n_onoff = float(len(match_onoff))/len(notes_system)
R_n_onoff = float(len(match_onoff))/len(notes_target)
F_n_onoff = 2*P_n_onoff*R_n_onoff/(P_n_onoff+R_n_onoff+np.finfo(float).eps)
print("Note-OnOff P,R,F:", P_n_onoff,R_n_onoff,F_n_onoff)
# notewise_highest(notes_system,intervals_system,notes_targ mask = make_key_mask(target_pr)et,intervals_target,match_onoff)
_,_,repeated = repeated_notes(intervals_target,notes_system,intervals_system,match_on)
if len(repeated)>0 or True:
import matplotlib.pyplot as plt
fig,((ax0,ax1),(ax2,ax3)) = plt.subplots(2,2)
for i in range(len(notes_target)):
target_pr[notes_target[i],int(intervals_target[i][0]*100)] += 1
ax0.imshow(target_pr,aspect='auto',origin='lower')
for i in range(len(notes_system)):
if notes_system[i] == 62:
print intervals_system[i]
system_pr[notes_system[i],int(intervals_system[i][0]*100)] += 1
ax1.imshow(system_pr,aspect='auto',origin='lower')
display1 = np.zeros_like(system_pr)
display2 = np.zeros_like(system_pr)
matched_targets,matched_outputs = zip(*match_on)
for i in matched_targets:
display1[notes_target[i],int(intervals_target[i][0]*100):int(intervals_target[i,1]*100)] = 1
display1[notes_target[i],int(intervals_target[i][0]*100)] += 1
for i in matched_outputs:
display2[notes_system[i],int(intervals_system[i][0]*100):int(intervals_system[i,1]*100)] = 1
display2[notes_system[i],int(intervals_system[i][0]*100)] += 1
for i in repeated:
display2[notes_system[i],int(intervals_system[i][0]*100):int(intervals_system[i,1]*100)] = 3
display2[notes_system[i],int(intervals_system[i][0]*100)] += 1
ax2.imshow(display1,aspect='auto',origin='lower')
ax3.imshow(display2,aspect='auto',origin='lower')
plt.show()
# print match
############
##### TESTS
############