forked from mnskim/nlp_project_music_ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_midi_to_label.py
170 lines (140 loc) · 5.93 KB
/
map_midi_to_label.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
import csv
from collections import OrderedDict, defaultdict
import numpy as np
import json
from scipy.stats import gaussian_kde
from tqdm import tqdm
LABEL_LIST = ["Stable beat", "Mechanical Tempo", "Intensional", "Regular beat change", "Long", "Cushioned", "Saturated (wet)", "Clean", "Subtle change", "Even", "Rich", "Bright",
"Pure", "Soft", "Sophisticated(mellow)", "balanced", "Large range of dynamic", "Fast paced", "Flowing", "Swing(Flexible)", "Flat", "Harmonious", "Optimistic(pleasant)", "HIgh Energy",
"Dominant(forceful)", "Imaginative", "Ethereal", "Convincing"]
LABEL_MAP = {i: label for i, label in enumerate(LABEL_LIST)}
PIANIST_MAP = OrderedDict()
file = open('total.csv', encoding="utf-8")
#file = open('total.csv', encoding="utf-8")
def estimate_maxima(data):
if len(set(data))<=1: # all datas are equal
return data[0]
kde = gaussian_kde(data)
no_samples = 50
samples = np.linspace(min(data), max(data), no_samples)
probs = kde.evaluate(samples)
#maxima_index = probs.argmax()
# in case if more than 1 argmaxs
winner = np.argwhere(probs == np.amax(probs))
maxima = np.average(samples[winner.flatten()])
return maxima
def midi_label_map_apex():
csvreader = csv.reader(file)
header = []
header = next(csvreader)
rows = []
for row in csvreader:
rows.append(row)
# sort by each segments
music_label_map = defaultdict(list)
for row in rows:
user = row[0]
file_name = row[2].split(".")[0]
#label_row = row[3:-2]
label_row = row[6:-2] # skip 1-1 ~ 1-3
for idx, elem in enumerate(label_row):
if elem == "":
label_row[idx] = 0.0
else:
label_row[idx] = float(elem)
# skip 0
if 0.0 in label_row:
continue
else:
music_label_map[file_name].append(label_row)
music_label_map_apex = dict()
# kernel density estimation
for key, annot_list in tqdm(music_label_map.items()):
annot_list = np.array(annot_list).transpose()
maxima = np.array([estimate_maxima(row)/7 for row in annot_list])
maxima = maxima.transpose().tolist()
music_label_map_apex[key] = maxima
# add pianist info
for key, annot_list in tqdm(music_label_map_apex.items()):
if key.split("_")[-2] not in PIANIST_MAP:
PIANIST_MAP[key.split("_")[-2]] = len(PIANIST_MAP)
print(PIANIST_MAP)
for key, annot_list in tqdm(music_label_map_apex.items()):
music_label_map_apex[key].append(PIANIST_MAP[key.split("_")[-2]])
json.dump(music_label_map_apex, open("midi_label_map_apex_reg_cls.json", 'w'))
# def midi_label_map_apex_filtered() : # 0922
# filtered_label_list = ["Rich", "balanced", "Large range of dynamic", "Swing(Flexible)", "Flat", "Harmonious", "HIgh Energy",
# "Dominant(forceful)", "Imaginative", "Ethereal", "Convincing"]
# filtered_loc = [LABEL_LIST.index(name) for name in filtered_label_list]
# csvreader = csv.reader(file)
# header = []
# header = next(csvreader)
# rows = []
# for row in csvreader:
# rows.append(row)
# # sort by each segments
# music_label_map = defaultdict(list)
# for row in rows:
# user = row[0]
# file_name = row[2].split(".")[0]
# label_row = row[3:-2]
# label_row = [label_row[loc] for loc in filtered_loc]
# for idx, elem in enumerate(label_row):
# if elem == "":
# label_row[idx] = 0.0
# else:
# label_row[idx] = float(elem)
# # skip 0
# if 0.0 in label_row:
# continue
# else:
# music_label_map[file_name].append(label_row)
# music_label_map_apex = dict()
# # kernel density estimation
# for key, annot_list in tqdm(music_label_map.items()):
# annot_list = np.array(annot_list).transpose()
# maxima = np.array([estimate_maxima(row)/7 for row in annot_list])
# maxima = maxima.transpose().tolist()
# music_label_map_apex[key] = maxima
# json.dump(music_label_map_apex, open("data/xai/midi_label_map_apex_filtered.json", 'w'))
# def midi_label_map_apex_except_filtered() : # 0922 # 13 labels
# filtered_label_list = ["Rich", "balanced", "Large range of dynamic", "Swing(Flexible)", "Flat", "Harmonious", "HIgh Energy",
# "Dominant(forceful)", "Imaginative", "Ethereal", "Convincing"]
# filtered_loc = [LABEL_LIST.index(name) for name in filtered_label_list]
# filtered_loc = [loc for loc in list(range(0,28)) if loc not in filtered_loc]
# csvreader = csv.reader(file)
# header = []
# header = next(csvreader)
# rows = []
# for row in csvreader:
# rows.append(row)
# # sort by each segments
# music_label_map = defaultdict(list)
# for row in rows:
# user = row[0]
# file_name = row[2].split(".")[0]
# label_row = row[3:-2]
# label_row = [label_row[loc] for loc in filtered_loc]
# label_row = label_row[4:]
# for idx, elem in enumerate(label_row):
# if elem == "":
# label_row[idx] = 0.0
# else:
# label_row[idx] = float(elem)
# # skip 0
# if 0.0 in label_row:
# continue
# else:
# music_label_map[file_name].append(label_row)
# music_label_map_apex = dict()
# # kernel density estimation
# for key, annot_list in tqdm(music_label_map.items()):
# annot_list = np.array(annot_list).transpose()
# maxima = np.array([estimate_maxima(row)/7 for row in annot_list])
# maxima = maxima.transpose().tolist()
# music_label_map_apex[key] = maxima
# json.dump(music_label_map_apex, open("data/xai/midi_label_map_apex_except_filtered.json", 'w'))
if __name__ == "__main__":
#midi_label_map_apex_filtered()
#midi_label_map_apex_except_filtered()
midi_label_map_apex()